Skip to content

Commit

Permalink
update to hil::radio::init changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed May 16, 2024
1 parent d878404 commit e028953
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 46 deletions.
34 changes: 30 additions & 4 deletions boards/components/src/rf233.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,33 @@ 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};

// Setup static space for the objects.
#[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)
};};
}

Expand Down Expand Up @@ -71,19 +85,31 @@ impl<S: SpiMaster<'static> + 'static> RF233Component<S> {
}

impl<S: SpiMaster<'static> + 'static> Component for RF233Component<S> {
type StaticInput = &'static mut MaybeUninit<RF233<'static, VirtualSpiMasterDevice<'static, S>>>;
type StaticInput = (
&'static mut MaybeUninit<RF233<'static, VirtualSpiMasterDevice<'static, S>>>,
&'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,
self.channel,
));
self.ctl.set_client(rf233);
self.spi.set_client(rf233);
let _ = rf233.initialize();
rf233
}
}
19 changes: 0 additions & 19 deletions boards/imix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, R>(&self, driver_num: usize, f: F) -> R
where
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 8 additions & 16 deletions capsules/extra/src/rf233.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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(())
}

Expand Down
9 changes: 2 additions & 7 deletions chips/nrf52840/src/ieee802154_radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(())
}
Expand Down

0 comments on commit e028953

Please sign in to comment.