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
use table::table;
#[repr(C)]
pub struct Config {
pub channel: u8,
pub txmac: [u8; 5],
pub mac0: [u8; 5],
pub mac1: [u8; 5],
pub mac2345: [u8; 4],
pub nrmacs: u8,
pub maclen: [u8; 5],
}
pub fn set_config(config: &Config) {
(table().nrf_config_set)(config as *const Config as *const ());
}
const R_CONFIG_EN_CRC: u8 = 0x08;
pub fn rx(crc: bool) -> Rx {
let config = if crc {
R_CONFIG_EN_CRC
} else {
0
};
(table().nrf_rcv_pkt_start)(config);
Rx
}
pub struct Rx;
impl Drop for Rx {
fn drop(&mut self) {
(table().nrf_rcv_pkt_end)();
}
}
pub enum RxError {
WouldBlock,
NoPacket,
Invalid,
}
impl Rx {
pub fn poll(&self) -> Result<Payload, RxError> {
let mut buf = [0u8; 32];
let maxsize = buf.len();
let ptr = buf.as_mut_ptr();
let len = (table().nrf_rcv_pkt_poll)(maxsize as isize, ptr);
match len {
_ if len > 0 && len <= maxsize as isize =>
Ok(Payload::new(len as u8, buf)),
0 => Err(RxError::WouldBlock),
-1 => Err(RxError::Invalid),
-2 => Err(RxError::NoPacket),
_ => Err(RxError::Invalid),
}
}
}
pub struct Payload {
buf: [u8; 32],
len: u8,
}
impl Payload {
pub fn new(len: u8, buf: [u8; 32]) -> Self {
Payload { buf, len }
}
pub fn len(&self) -> usize {
self.len.into()
}
}
impl AsRef<[u8]> for Payload {
fn as_ref(&self) -> &[u8] {
&self.buf[0..self.len()]
}
}