From e02895320e955dedac728b778a6bb3e4cac7f77a Mon Sep 17 00:00:00 2001 From: Brad Campbell Date: Thu, 16 May 2024 12:10:02 -0400 Subject: [PATCH] update to hil::radio::init changes --- boards/components/src/rf233.rs | 34 +++++++++++++++++++++++--- boards/imix/src/main.rs | 19 -------------- capsules/extra/src/rf233.rs | 24 ++++++------------ chips/nrf52840/src/ieee802154_radio.rs | 9 ++----- 4 files changed, 40 insertions(+), 46 deletions(-) diff --git a/boards/components/src/rf233.rs b/boards/components/src/rf233.rs index ed22aa1968..7e83a79e03 100644 --- a/boards/components/src/rf233.rs +++ b/boards/components/src/rf233.rs @@ -25,6 +25,7 @@ use capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice; use capsules_extra::rf233::RF233; use core::mem::MaybeUninit; use kernel::component::Component; +use kernel::hil::radio::RadioConfig; use kernel::hil::spi::{SpiMaster, SpiMasterDevice}; use kernel::hil::{self, radio}; @@ -32,12 +33,25 @@ use kernel::hil::{self, radio}; #[macro_export] macro_rules! rf233_component_static { ($S:ty $(,)?) => {{ - kernel::static_buf!( + let spi_device = kernel::static_buf!( capsules_extra::rf233::RF233< 'static, capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice<'static, $S>, > - ) + ); + // The RF233 radio stack requires our buffers for its SPI operations: + // + // 1. buf: a packet-sized buffer for SPI operations, which is + // used as the read buffer when it writes a packet passed to it and the write + // buffer when it reads a packet into a buffer passed to it. + // 2. rx_buf: buffer to receive packets into + // 3 + 4: two small buffers for performing registers + // operations (one read, one write). + let rf233_buf = kernel::static_buf!([u8; kernel::hil::radio::MAX_BUF_SIZE]); + let rf233_reg_write = kernel::static_buf!([u8; 2]); + let rf233_reg_read = kernel::static_buf!([u8; 2]); + + (spi_device, rf233_buf, rf233_reg_write, rf233_reg_read) };}; } @@ -71,12 +85,23 @@ impl + 'static> RF233Component { } impl + 'static> Component for RF233Component { - type StaticInput = &'static mut MaybeUninit>>; + type StaticInput = ( + &'static mut MaybeUninit>>, + &'static mut MaybeUninit<[u8; hil::radio::MAX_BUF_SIZE]>, + &'static mut MaybeUninit<[u8; 2]>, + &'static mut MaybeUninit<[u8; 2]>, + ); type Output = &'static RF233<'static, VirtualSpiMasterDevice<'static, S>>; fn finalize(self, s: Self::StaticInput) -> Self::Output { - let rf233 = s.write(RF233::new( + let rf233_buf = s.1.write([0; hil::radio::MAX_BUF_SIZE]); + let rf233_reg_write = s.2.write([0; 2]); + let rf233_reg_read = s.3.write([0; 2]); + let rf233 = s.0.write(RF233::new( self.spi, + rf233_buf, + rf233_reg_write, + rf233_reg_read, self.reset, self.sleep, self.irq, @@ -84,6 +109,7 @@ impl + 'static> Component for RF233Component { )); self.ctl.set_client(rf233); self.spi.set_client(rf233); + let _ = rf233.initialize(); rf233 } } diff --git a/boards/imix/src/main.rs b/boards/imix/src/main.rs index 3272ad95eb..b521ab55f9 100644 --- a/boards/imix/src/main.rs +++ b/boards/imix/src/main.rs @@ -172,19 +172,6 @@ struct Imix { systick: cortexm4::systick::SysTick, } -// The RF233 radio stack requires our buffers for its SPI operations: -// -// 1. buf: a packet-sized buffer for SPI operations, which is -// used as the read buffer when it writes a packet passed to it and the write -// buffer when it reads a packet into a buffer passed to it. -// 2. rx_buf: buffer to receive packets into -// 3 + 4: two small buffers for performing registers -// operations (one read, one write). - -static mut RF233_BUF: [u8; radio::MAX_BUF_SIZE] = [0x00; radio::MAX_BUF_SIZE]; -static mut RF233_REG_WRITE: [u8; 2] = [0x00; 2]; -static mut RF233_REG_READ: [u8; 2] = [0x00; 2]; - impl SyscallDriverLookup for Imix { fn with_driver(&self, driver_num: usize, f: F) -> R where @@ -623,12 +610,6 @@ pub unsafe fn main() { aes_mux.register(); peripherals.aes.set_client(aes_mux); - // Can this initialize be pushed earlier, or into component? -pal - let _ = rf233.initialize( - &mut *addr_of_mut!(RF233_BUF), - &mut *addr_of_mut!(RF233_REG_WRITE), - &mut *addr_of_mut!(RF233_REG_READ), - ); let (_, mux_mac) = components::ieee802154::Ieee802154Component::new( board_kernel, capsules_extra::ieee802154::DRIVER_NUM, diff --git a/capsules/extra/src/rf233.rs b/capsules/extra/src/rf233.rs index f705766711..1b9ae7bdb4 100644 --- a/capsules/extra/src/rf233.rs +++ b/capsules/extra/src/rf233.rs @@ -164,7 +164,7 @@ enum InternalState { // packet-length buffers. Since the SPI callback does not distinguish // which buffers are being used, the read_write_done callback checks // which state the stack is in and places the buffers back -// accodingly. A bug here would mean a memory leak and later panic +// accordingly. A bug here would mean a memory leak and later panic // when a buffer that should be present has been lost. // // The finite state machine is tricky for two reasons. First, the @@ -1033,6 +1033,9 @@ impl<'a, S: spi::SpiMasterDevice<'a>> gpio::Client for RF233<'a, S> { impl<'a, S: spi::SpiMasterDevice<'a>> RF233<'a, S> { pub fn new( spi: &'a S, + spi_buf: &'static mut [u8], + reg_write: &'static mut [u8; 2], + reg_read: &'static mut [u8; 2], reset: &'a dyn gpio::Pin, sleep: &'a dyn gpio::Pin, irq: &'a dyn gpio::InterruptPin<'a>, @@ -1067,9 +1070,9 @@ impl<'a, S: spi::SpiMasterDevice<'a>> RF233<'a, S> { pan: Cell::new(0), tx_power: Cell::new(setting_to_power(PHY_TX_PWR)), channel: Cell::new(channel), - spi_rx: TakeCell::empty(), - spi_tx: TakeCell::empty(), - spi_buf: TakeCell::empty(), + spi_rx: TakeCell::new(reg_read), + spi_tx: TakeCell::new(reg_write), + spi_buf: TakeCell::new(spi_buf), } } @@ -1174,18 +1177,7 @@ impl<'a, S: spi::SpiMasterDevice<'a>> RF233<'a, S> { } impl<'a, S: spi::SpiMasterDevice<'a>> radio::RadioConfig<'a> for RF233<'a, S> { - fn initialize( - &self, - buf: &'static mut [u8], - reg_write: &'static mut [u8], - reg_read: &'static mut [u8], - ) -> Result<(), ErrorCode> { - if (buf.len() < radio::MAX_BUF_SIZE || reg_read.len() != 2 || reg_write.len() != 2) { - return Err(ErrorCode::SIZE); - } - self.spi_buf.replace(buf); - self.spi_rx.replace(reg_read); - self.spi_tx.replace(reg_write); + fn initialize(&self) -> Result<(), ErrorCode> { Ok(()) } diff --git a/chips/nrf52840/src/ieee802154_radio.rs b/chips/nrf52840/src/ieee802154_radio.rs index 8d17adf5f1..b75ae24841 100644 --- a/chips/nrf52840/src/ieee802154_radio.rs +++ b/chips/nrf52840/src/ieee802154_radio.rs @@ -28,7 +28,7 @@ use nrf52::constants::TxPower; // basic 15.4 libtock-c apps. // // To aid in future implementations, a simplified and concise version of the nrf52840 radio -// state machine specification is described. Additionally, the state machine this driver seperately +// state machine specification is described. Additionally, the state machine this driver separately // maintains is also described. // // To interact with the radio, tasks are issued to the radio which in turn trigger interrupt events. @@ -1166,12 +1166,7 @@ impl<'a> Radio<'a> { } impl<'a> kernel::hil::radio::RadioConfig<'a> for Radio<'a> { - fn initialize( - &self, - _spi_buf: &'static mut [u8], - _reg_write: &'static mut [u8], - _reg_read: &'static mut [u8], - ) -> Result<(), ErrorCode> { + fn initialize(&self) -> Result<(), ErrorCode> { self.radio_initialize(); Ok(()) }