Skip to content

Commit

Permalink
Merge pull request #1061 from tock/optional-cell-nrf51
Browse files Browse the repository at this point in the history
Optional cell nrf51
  • Loading branch information
ppannuto committed Jul 8, 2018
2 parents b086f9c + 97a6e7f commit 78fb61e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
8 changes: 4 additions & 4 deletions chips/nrf51/src/clock.rs
Expand Up @@ -11,7 +11,7 @@
//! * Philip Levis
//! * Date: August 18, 2016

use core::cell::Cell;
use kernel::common::cells::OptionalCell;
use kernel::common::cells::VolatileCell;
use kernel::common::StaticRef;

Expand Down Expand Up @@ -98,19 +98,19 @@ pub trait ClockClient {
/// Clock struct
pub struct Clock {
registers: StaticRef<ClockRegisters>,
client: Cell<Option<&'static ClockClient>>,
client: OptionalCell<&'static ClockClient>,
}

impl Clock {
pub const fn new() -> Clock {
Clock {
registers: CLOCK_BASE,
client: Cell::new(None),
client: OptionalCell::empty(),
}
}

pub fn set_client(&self, client: &'static ClockClient) {
self.client.set(Some(client));
self.client.set(client);
}

pub fn interrupt_enable(&self, interrupt: InterruptField) {
Expand Down
11 changes: 6 additions & 5 deletions chips/nrf51/src/i2c.rs
Expand Up @@ -4,6 +4,7 @@

use core::cell::Cell;
use core::cmp;
use kernel::common::cells::OptionalCell;
use kernel::common::cells::TakeCell;
use kernel::common::regs::{FieldValue, ReadOnly, ReadWrite, WriteOnly};
use kernel::common::StaticRef;
Expand All @@ -16,7 +17,7 @@ use {nrf5x, nrf5x::gpio, nrf5x::pinmux::Pinmux};
/// additional data necessary to implement an asynchronous interface.
pub struct TWIM {
registers: StaticRef<TwimRegisters>,
client: Cell<Option<&'static i2c::I2CHwMasterClient>>,
client: OptionalCell<&'static i2c::I2CHwMasterClient>,
tx_len: Cell<u8>,
rx_len: Cell<u8>,
pos: Cell<usize>,
Expand All @@ -27,7 +28,7 @@ impl TWIM {
const fn new(instance: usize) -> TWIM {
TWIM {
registers: TWIM_BASE[instance],
client: Cell::new(None),
client: OptionalCell::empty(),
tx_len: Cell::new(0),
rx_len: Cell::new(0),
pos: Cell::new(0),
Expand All @@ -36,8 +37,8 @@ impl TWIM {
}

pub fn set_client(&self, client: &'static i2c::I2CHwMasterClient) {
debug_assert!(self.client.get().is_none());
self.client.set(Some(client));
debug_assert!(self.client.is_none());
self.client.set(client);
}

/// Configures an already constructed `TWIM`.
Expand Down Expand Up @@ -94,7 +95,7 @@ impl TWIM {
}

fn reply(&self, result: i2c::Error) {
self.client.get().map(|client| {
self.client.map(|client| {
self.buf.take().map(|buf| {
client.command_complete(buf, result);
});
Expand Down
19 changes: 9 additions & 10 deletions chips/nrf51/src/radio.rs
Expand Up @@ -13,6 +13,7 @@
use core::cell::Cell;
use core::convert::TryFrom;
use kernel;
use kernel::common::cells::OptionalCell;
use kernel::common::cells::VolatileCell;
use kernel::common::StaticRef;
use kernel::hil::ble_advertising;
Expand Down Expand Up @@ -117,17 +118,17 @@ const RADIO_BASE: StaticRef<RadioRegisters> =
pub struct Radio {
registers: StaticRef<RadioRegisters>,
tx_power: Cell<TxPower>,
rx_client: Cell<Option<&'static ble_advertising::RxClient>>,
tx_client: Cell<Option<&'static ble_advertising::TxClient>>,
rx_client: OptionalCell<&'static ble_advertising::RxClient>,
tx_client: OptionalCell<&'static ble_advertising::TxClient>,
}

impl Radio {
pub const fn new() -> Radio {
Radio {
registers: RADIO_BASE,
tx_power: Cell::new(TxPower::ZerodBm),
rx_client: Cell::new(None),
tx_client: Cell::new(None),
rx_client: OptionalCell::empty(),
tx_client: OptionalCell::empty(),
}
}

Expand Down Expand Up @@ -296,17 +297,15 @@ impl Radio {
| nrf5x::constants::RADIO_STATE_TXDISABLE
| nrf5x::constants::RADIO_STATE_TX => {
self.radio_off();
self.tx_client
.get()
.map(|client| client.transmit_event(result));
self.tx_client.map(|client| client.transmit_event(result));
}
nrf5x::constants::RADIO_STATE_RXRU
| nrf5x::constants::RADIO_STATE_RXIDLE
| nrf5x::constants::RADIO_STATE_RXDISABLE
| nrf5x::constants::RADIO_STATE_RX => {
self.radio_off();
unsafe {
self.rx_client.get().map(|client| {
self.rx_client.map(|client| {
// Length is: S0 (1 Byte) + Length (1 Byte) + S1 (0 Bytes) + Payload
// And because the length field is directly read from the packet
// We need to add 2 to length to get the total length
Expand Down Expand Up @@ -369,11 +368,11 @@ impl ble_advertising::BleAdvertisementDriver for Radio {
}

fn set_receive_client(&self, client: &'static ble_advertising::RxClient) {
self.rx_client.set(Some(client));
self.rx_client.set(client);
}

fn set_transmit_client(&self, client: &'static ble_advertising::TxClient) {
self.tx_client.set(Some(client));
self.tx_client.set(client);
}
}

Expand Down
9 changes: 5 additions & 4 deletions chips/nrf51/src/uart.rs
@@ -1,4 +1,5 @@
use core::cell::Cell;
use kernel::common::cells::OptionalCell;
use kernel::common::cells::TakeCell;
use kernel::common::regs::{ReadWrite, WriteOnly};
use kernel::common::StaticRef;
Expand Down Expand Up @@ -149,7 +150,7 @@ const UART_BASE: StaticRef<UartRegisters> =

pub struct UART {
registers: StaticRef<UartRegisters>,
client: Cell<Option<&'static uart::Client>>,
client: OptionalCell<&'static uart::Client>,
buffer: TakeCell<'static, [u8]>,
len: Cell<usize>,
index: Cell<usize>,
Expand All @@ -164,7 +165,7 @@ impl UART {
pub const fn new() -> UART {
UART {
registers: UART_BASE,
client: Cell::new(None),
client: OptionalCell::empty(),
buffer: TakeCell::empty(),
len: Cell::new(0),
index: Cell::new(0),
Expand Down Expand Up @@ -244,7 +245,7 @@ impl UART {
regs.task_stoptx.write(Task::ENABLE::SET);

// Signal client write done
self.client.get().map(|client| {
self.client.map(|client| {
self.buffer.take().map(|buffer| {
client.transmit_complete(buffer, uart::Error::CommandComplete);
});
Expand Down Expand Up @@ -287,7 +288,7 @@ impl UART {

impl uart::UART for UART {
fn set_client(&self, client: &'static uart::Client) {
self.client.set(Some(client));
self.client.set(client);
}

fn init(&self, params: uart::UARTParams) {
Expand Down

0 comments on commit 78fb61e

Please sign in to comment.