Skip to content

Commit 35a0099

Browse files
committed
take &mut Packet from the send methods
to enforce that the buffer is RAM allocated
1 parent 50588d9 commit 35a0099

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

nrf52840-hal/src/ieee802154.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! IEEE 802.15.4 radio
22
33
use core::{
4+
marker::PhantomData,
45
ops::{self, RangeFrom},
56
sync::atomic::{self, Ordering},
6-
marker::PhantomData,
77
};
88

99
use embedded_hal::timer::CountDown as _;
@@ -380,7 +380,8 @@ impl<'c> Radio<'c> {
380380
}
381381

382382
fn start_recv(&mut self, packet: &mut Packet) {
383-
// NOTE we do NOT check the address of `packet`; see comment in `Packet::new` for details
383+
// NOTE we do NOT check the address of `packet` because the mutable reference ensures it's
384+
// allocated in RAM
384385

385386
// clear related events
386387
self.radio.events_phyend.reset();
@@ -413,8 +414,12 @@ impl<'c> Radio<'c> {
413414
/// This method performs Clear Channel Assessment (CCA) first and sends the `packet` only if the
414415
/// channel is observed to be *clear* (no transmission is currently ongoing), otherwise no
415416
/// packet is transmitted and the `Err` variant is returned
416-
// NOTE we do NOT check the address of `packet`; see comment in `Packet::new` for details
417-
pub fn try_send(&mut self, packet: &Packet) -> Result<(), ()> {
417+
///
418+
/// NOTE this method will *not* modify the `packet` argument. The mutable reference is used to
419+
/// ensure the `packet` buffer is allocated in RAM, which is required by the RADIO peripheral
420+
// NOTE we do NOT check the address of `packet` because the mutable reference ensures it's
421+
// allocated in RAM
422+
pub fn try_send(&mut self, packet: &mut Packet) -> Result<(), ()> {
418423
// enable radio to perform cca
419424
self.put_in_rx_mode();
420425

@@ -471,8 +476,12 @@ impl<'c> Radio<'c> {
471476
/// This is utility method that *consecutively* calls the `try_send` method until it succeeds.
472477
/// Note that this approach is *not* IEEE spec compliant -- there must be delay between failed
473478
/// CCA attempts to be spec compliant
474-
// NOTE we do NOT check the address of `packet`; see comment in `Packet::new` for details
475-
pub fn send(&mut self, packet: &Packet) {
479+
///
480+
/// NOTE this method will *not* modify the `packet` argument. The mutable reference is used to
481+
/// ensure the `packet` buffer is allocated in RAM, which is required by the RADIO peripheral
482+
// NOTE we do NOT check the address of `packet` because the mutable reference ensures it's
483+
// allocated in RAM
484+
pub fn send(&mut self, packet: &mut Packet) {
476485
// enable radio to perform cca
477486
self.put_in_rx_mode();
478487

@@ -529,8 +538,12 @@ impl<'c> Radio<'c> {
529538
/// Sends the specified `packet` without first performing CCA
530539
///
531540
/// Acknowledgment packets must be sent using this method
532-
// NOTE we do NOT check the address of `packet`; see comment in `Packet::new` for details
533-
pub fn send_no_cca(&mut self, packet: &Packet) {
541+
///
542+
/// NOTE this method will *not* modify the `packet` argument. The mutable reference is used to
543+
/// ensure the `packet` buffer is allocated in RAM, which is required by the RADIO peripheral
544+
// NOTE we do NOT check the address of `packet` because the mutable reference ensures it's
545+
// allocated in RAM
546+
pub fn send_no_cca(&mut self, packet: &mut Packet) {
534547
self.put_in_tx_mode();
535548

536549
// clear related events
@@ -731,9 +744,6 @@ impl Packet {
731744
const SIZE: usize = 1 /* PHR */ + Self::MAX_PSDU_LEN as usize;
732745

733746
/// Returns an empty packet (length = 0)
734-
// XXX I believe that be making this *not* `const` it is not possible to place a `Packet` in
735-
// `.rodata` (modulo unsafe `#[link_section]` shenanigans) thus it is not necessary to check the
736-
// address of packet in `Radio.{send,recv}` (EasyDMA can only operate on RAM addresses)
737747
pub fn new() -> Self {
738748
let mut packet = Self {
739749
buffer: [0; Self::SIZE],

0 commit comments

Comments
 (0)