1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#![no_std]
#![feature(const_transmute)]
#![feature(trace_macros)]
#![feature(proc_macro_hygiene)]
extern crate macros as mynewt_macros;
#[allow(non_camel_case_types)]
#[allow(non_upper_case_globals)]
pub mod kernel;
#[allow(dead_code)]
#[allow(non_camel_case_types)]
#[allow(non_upper_case_globals)]
pub mod hw;
#[allow(dead_code)]
pub mod sys;
#[macro_use]
#[allow(non_camel_case_types)]
#[allow(non_upper_case_globals)]
pub mod encoding;
#[macro_use]
pub mod util;
#[allow(non_camel_case_types)]
#[allow(non_upper_case_globals)]
pub mod libs;
mod hal;
pub use hal::{ Delay, GPIO, SPI };
pub mod spi;
pub fn sysinit() {
unsafe { rust_sysinit(); }
sys::console::flush();
}
pub mod result {
use crate::kernel::os;
pub type MynewtResult<T> = ::core::result::Result<T, MynewtError>;
#[repr(i32)]
#[derive(PartialEq)]
#[allow(non_camel_case_types)]
pub enum MynewtError {
SYS_EOK = os::SYS_EOK as i32,
SYS_ENOMEM = os::SYS_ENOMEM,
SYS_EINVAL = os::SYS_EINVAL,
SYS_ETIMEOUT = os::SYS_ETIMEOUT,
SYS_ENOENT = os::SYS_ENOENT,
SYS_EIO = os::SYS_EIO,
SYS_EAGAIN = os::SYS_EAGAIN,
SYS_EACCES = os::SYS_EACCES,
SYS_EBUSY = os::SYS_EBUSY,
SYS_ENODEV = os::SYS_ENODEV,
SYS_ERANGE = os::SYS_ERANGE,
SYS_EALREADY = os::SYS_EALREADY,
SYS_ENOTSUP = os::SYS_ENOTSUP,
SYS_EUNKNOWN = os::SYS_EUNKNOWN,
SYS_EREMOTEIO = os::SYS_EREMOTEIO,
SYS_EDONE = os::SYS_EDONE,
SYS_EPERUSER = os::SYS_EPERUSER,
}
impl From<MynewtError> for i32 {
fn from(err: MynewtError) -> Self {
err as i32
}
}
impl From<i32> for MynewtError {
fn from(num: i32) -> Self {
unsafe {
::core::mem::transmute::
<i32, MynewtError>
(num)
}
}
}
impl From<()> for MynewtError {
fn from(_: ()) -> Self {
MynewtError::SYS_EUNKNOWN
}
}
impl core::fmt::Debug for MynewtError {
fn fmt(&self, _fmt: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
Ok(())
}
}
}
#[derive(Clone, Copy)]
pub struct Strn {
pub rep: StrnRep
}
#[derive(Clone, Copy)]
#[repr(u8)]
pub enum StrnRep {
ByteStr(&'static [u8]),
CStr(*const u8),
}
impl Strn {
pub fn new(bs: &'static [u8]) -> Strn {
assert_eq!(bs.last(), Some(&0u8), "no null");
Strn {
rep: StrnRep::ByteStr(bs)
}
}
pub fn from_cstr(cstr: *const u8) -> Strn {
Strn {
rep: StrnRep::CStr(cstr)
}
}
pub fn as_ptr(&self) -> *const u8 {
match self.rep {
StrnRep::ByteStr(bs) => { bs.as_ptr() }
StrnRep::CStr(cstr) => { cstr }
}
}
pub fn len(&self) -> usize {
match self.rep {
StrnRep::ByteStr(bs) => {
assert_eq!(bs.last(), Some(&0u8), "no null");
bs.len() - 1
}
StrnRep::CStr(cstr) => {
if cstr.is_null() { return 0; }
for len in 0..127 {
let ptr: *const u8 = ((cstr as u32) + len) as *const u8;
if unsafe { *ptr } == 0 { return len as usize; }
}
assert!(false, "big strn");
return 128 as usize;
}
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn as_cstr(&self) -> *const u8 {
match self.rep {
StrnRep::ByteStr(bs) => {
assert_eq!(bs.last(), Some(&0u8), "no null");
bs.as_ptr() as *const u8
}
StrnRep::CStr(cstr) => { cstr }
}
}
pub fn as_bytestr(&self) -> &'static [u8] {
match self.rep {
StrnRep::ByteStr(bs) => {
assert_eq!(bs.last(), Some(&0u8), "no null");
&bs
}
StrnRep::CStr(_cstr) => {
assert!(false, "strn cstr");
b"\0"
}
}
}
pub fn validate(&self) {
match self.rep {
StrnRep::ByteStr(bs) => {
assert_eq!(bs.last(), Some(&0u8), "no null");
}
StrnRep::CStr(_cstr) => {}
}
}
pub fn validate_bytestr(bs: &'static [u8]) {
assert_eq!(bs.last(), Some(&0u8), "no null");
}
}
unsafe impl Send for Strn {}
unsafe impl Sync for Strn {}
pub type Out<T> = &'static mut T;
pub type Ptr = *mut ::cty::c_void;
pub const NULL: Ptr = core::ptr::null_mut();
#[link(name = "libs_mynewt_rust")]
extern {
fn rust_sysinit();
}