Skip to content

Commit

Permalink
Added non-blocking radio receive
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindbergem committed Aug 18, 2024
1 parent 04cd0c8 commit 9ac3659
Showing 1 changed file with 74 additions and 44 deletions.
118 changes: 74 additions & 44 deletions nrf-hal-common/src/ieee802154.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use core::{
sync::atomic::{self, Ordering},
};

use nb::block;

use crate::{
clocks::{Clocks, ExternalOscillator},
pac::{
Expand All @@ -15,6 +17,51 @@ use crate::{
timer::{self, Timer},
};

/// Non-blocking receive
pub struct Recv<'a, 'c> {
radio: &'a mut Radio<'c>,
_packet: &'a mut Packet,
}

impl<'a, 'c> Recv<'a, 'c> {
fn new(radio: &'a mut Radio<'c>, packet: &'a mut Packet) -> Self {
Self {
radio,
_packet: packet,
}
}

/// Check if receive is done
///
/// This methods returns the `Ok` variant if the CRC included the
/// packet was successfully validated by the hardware. It returns
/// `Err(nb::Error::WouldBlock)` if a packet hasn't been received
/// yet, and `Err(nb::Error::Other)` if the CRC check failed.
pub fn is_done(&mut self) -> nb::Result<u16, u16> {
if self.radio.radio.events_end.read().events_end().bit_is_set() {
self.radio.radio.events_end.reset();

dma_end_fence();

let crc = self.radio.radio.rxcrc.read().rxcrc().bits() as u16;

if self.radio.radio.crcstatus.read().crcstatus().bit_is_set() {
Ok(crc)
} else {
Err(nb::Error::Other(crc))
}
} else {
Err(nb::Error::WouldBlock)
}
}
}

impl<'a, 'c> Drop for Recv<'a, 'c> {
fn drop(&mut self) {
self.radio.cancel_recv();
}
}

/// IEEE 802.15.4 radio
pub struct Radio<'c> {
radio: RADIO,
Expand Down Expand Up @@ -305,22 +352,26 @@ impl<'c> Radio<'c> {
/// validated by the hardware; otherwise it returns the `Err` variant. In either case, `packet`
/// will be updated with the received packet's data
pub fn recv(&mut self, packet: &mut Packet) -> Result<u16, u16> {
// Start non-blocking receive
let mut recv = self.recv_non_blocking(packet);

// Block untill receive is done
block!(recv.is_done())
}

/// Receives one radio packet and copies its contents into the given `packet` buffer
///
/// This method is non-blocking
pub fn recv_non_blocking<'a>(&'a mut self, packet: &'a mut Packet) -> Recv<'a, 'c> {
// Start the read
// NOTE(unsafe) We block until reception completes or errors
// NOTE(unsafe)
// The packet must live until the transfer is done. Recv takes
// a mutable reference to the packet to enforce this.
unsafe {
self.start_recv(packet);
}

// wait until we have received something
self.wait_for_event(Event::End);
dma_end_fence();

let crc = self.radio.rxcrc.read().rxcrc().bits() as u16;
if self.radio.crcstatus.read().crcstatus().bit_is_set() {
Ok(crc)
} else {
Err(crc)
}
Recv::new(self, packet)
}

/// Listens for a packet for no longer than the specified amount of microseconds
Expand All @@ -347,39 +398,23 @@ impl<'c> Radio<'c> {
// Start the timeout timer
timer.start(microseconds);

// Start the read
// NOTE(unsafe) We block until reception completes or errors
unsafe {
self.start_recv(packet);
}

// Wait for transmission to end
let mut recv_completed = false;
// Start non-blocking receive
let mut recv = self.recv_non_blocking(packet);

// Check if either receive is done or timeout occured
loop {
if self.radio.events_end.read().bits() != 0 {
// transfer complete
dma_end_fence();
recv_completed = true;
break;
match recv.is_done() {
Ok(crc) => break Ok(crc),
Err(err) => match err {
nb::Error::Other(crc) => break Err(Error::Crc(crc)),
nb::Error::WouldBlock => (),
},
}

if timer.reset_if_finished() {
// timeout
break;
}
}

if !recv_completed {
// Cancel the reception if it did not complete until now
self.cancel_recv();
Err(Error::Timeout)
} else {
let crc = self.radio.rxcrc.read().rxcrc().bits() as u16;
if self.radio.crcstatus.read().crcstatus().bit_is_set() {
Ok(crc)
} else {
Err(Error::Crc(crc))
// Break loop in case of timeout. Receive is
// cancelled when `recv` is dropped.
break Err(Error::Timeout);
}
}
}
Expand Down Expand Up @@ -674,10 +709,6 @@ impl<'c> Radio<'c> {

fn wait_for_event(&self, event: Event) {
match event {
Event::End => {
while self.radio.events_end.read().events_end().bit_is_clear() {}
self.radio.events_end.reset();
}
Event::PhyEnd => {
while self
.radio
Expand Down Expand Up @@ -728,7 +759,6 @@ fn dma_end_fence() {
}

enum Event {
End,
PhyEnd,
}

Expand Down

0 comments on commit 9ac3659

Please sign in to comment.