Skip to content

Commit d3d4313

Browse files
committed
Added non-blocking radio receive
1 parent 7d648ad commit d3d4313

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

nrf-hal-common/src/ieee802154.rs

+54
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,47 @@ use crate::{
1515
timer::{self, Timer},
1616
};
1717

18+
pub enum RecvStatus {
19+
NotDone,
20+
Success(u16),
21+
CrcFailure(u16),
22+
}
23+
24+
/// Non-blocking receive
25+
pub struct Recv<'a, 'c> {
26+
radio: &'a mut Radio<'c>,
27+
}
28+
29+
impl<'a, 'c> Recv<'a, 'c> {
30+
fn new(radio: &'a mut Radio<'c>) -> Self {
31+
Self { radio }
32+
}
33+
34+
pub fn is_done(&mut self) -> RecvStatus {
35+
if self.radio.radio.events_end.read().events_end().bit_is_set() {
36+
self.radio.radio.events_end.reset();
37+
38+
dma_end_fence();
39+
40+
let crc = self.radio.radio.rxcrc.read().rxcrc().bits() as u16;
41+
42+
if self.radio.radio.crcstatus.read().crcstatus().bit_is_set() {
43+
RecvStatus::Success(crc)
44+
} else {
45+
RecvStatus::CrcFailure(crc)
46+
}
47+
} else {
48+
RecvStatus::NotDone
49+
}
50+
}
51+
}
52+
53+
impl<'a, 'c> Drop for Recv<'a, 'c> {
54+
fn drop(&mut self) {
55+
self.radio.cancel_recv();
56+
}
57+
}
58+
1859
/// IEEE 802.15.4 radio
1960
pub struct Radio<'c> {
2061
radio: RADIO,
@@ -323,6 +364,19 @@ impl<'c> Radio<'c> {
323364
}
324365
}
325366

367+
/// Receives one radio packet and copies its contents into the given `packet` buffer
368+
///
369+
/// This method is non-blocking
370+
pub fn recv_non_blocking(&mut self, packet: &mut Packet) -> Recv<'_, 'c> {
371+
// Start the read
372+
// NOTE(unsafe) We block until reception completes or errors
373+
unsafe {
374+
self.start_recv(packet);
375+
}
376+
377+
Recv::new(self)
378+
}
379+
326380
/// Listens for a packet for no longer than the specified amount of microseconds
327381
/// and copies its contents into the given `packet` buffer
328382
///

0 commit comments

Comments
 (0)