Skip to content

Commit

Permalink
Merge pull request tock#3937 from tweedegolf/const-gpio-create
Browse files Browse the repository at this point in the history
make TakeCell::empty() const, and make nrf52840 GPIO pin initialization a const fn
  • Loading branch information
lschuermann authored Apr 7, 2024
2 parents e4b4975 + d888b8b commit 868d0b1
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion chips/nrf52/src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ pub struct Adc<'a> {
}

impl<'a> Adc<'a> {
pub fn new(voltage_reference_in_mv: usize) -> Self {
pub const fn new(voltage_reference_in_mv: usize) -> Self {
Self {
registers: SAADC_BASE,
reference: Cell::new(voltage_reference_in_mv),
Expand Down
2 changes: 1 addition & 1 deletion chips/nrf52/src/ble_radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ pub struct Radio<'a> {
}

impl<'a> Radio<'a> {
pub fn new() -> Radio<'a> {
pub const fn new() -> Radio<'a> {
Radio {
registers: RADIO_BASE,
tx_power: Cell::new(TxPower::ZerodBm),
Expand Down
6 changes: 3 additions & 3 deletions chips/nrf52/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum Speed {
}

impl<'a> TWI<'a> {
fn new(registers: StaticRef<TwiRegisters>) -> Self {
const fn new(registers: StaticRef<TwiRegisters>) -> Self {
Self {
registers,
client: OptionalCell::empty(),
Expand All @@ -55,11 +55,11 @@ impl<'a> TWI<'a> {
}
}

pub fn new_twi0() -> Self {
pub const fn new_twi0() -> Self {
TWI::new(INSTANCES[0])
}

pub fn new_twi1() -> Self {
pub const fn new_twi1() -> Self {
TWI::new(INSTANCES[1])
}

Expand Down
2 changes: 1 addition & 1 deletion chips/nrf52/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub struct SPIM<'a> {
}

impl<'a> SPIM<'a> {
pub fn new(instance: usize) -> SPIM<'a> {
pub const fn new(instance: usize) -> SPIM<'a> {
SPIM {
registers: INSTANCES[instance],
client: OptionalCell::empty(),
Expand Down
2 changes: 1 addition & 1 deletion chips/nrf52/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub struct UARTParams {
impl<'a> Uarte<'a> {
/// Constructor
// This should only be constructed once
pub fn new(regs: StaticRef<UarteRegisters>) -> Uarte<'a> {
pub const fn new(regs: StaticRef<UarteRegisters>) -> Uarte<'a> {
Uarte {
registers: regs,
tx_client: OptionalCell::empty(),
Expand Down
2 changes: 1 addition & 1 deletion chips/nrf52840/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use nrf52::gpio::{GPIOPin, Pin, Port};

pub const NUM_PINS: usize = 48;

pub fn nrf52840_gpio_create<'a>() -> Port<'a, NUM_PINS> {
pub const fn nrf52840_gpio_create<'a>() -> Port<'a, NUM_PINS> {
Port::new([
GPIOPin::new(Pin::P0_00),
GPIOPin::new(Pin::P0_01),
Expand Down
2 changes: 1 addition & 1 deletion chips/nrf5x/src/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub struct AesECB<'a> {
}

impl<'a> AesECB<'a> {
pub fn new() -> AesECB<'a> {
pub const fn new() -> AesECB<'a> {
AesECB {
registers: AESECB_BASE,
mode: Cell::new(AESMode::CTR),
Expand Down
2 changes: 1 addition & 1 deletion chips/nrf5x/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ impl<'a, const N: usize> IndexMut<Pin> for Port<'a, N> {
}

impl<'a, const N: usize> Port<'a, N> {
pub fn new(pins: [GPIOPin<'a>; N]) -> Self {
pub const fn new(pins: [GPIOPin<'a>; N]) -> Self {
Self { pins }
}

Expand Down
10 changes: 6 additions & 4 deletions libraries/tock-cells/src/take_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ pub struct TakeCell<'a, T: 'a + ?Sized> {
}

impl<'a, T: ?Sized> TakeCell<'a, T> {
pub fn empty() -> TakeCell<'a, T> {
TakeCell {
val: Cell::new(None),
}
const EMPTY: Self = Self {
val: Cell::new(None),
};

pub const fn empty() -> TakeCell<'a, T> {
Self::EMPTY
}

/// Creates a new `TakeCell` containing `value`
Expand Down

0 comments on commit 868d0b1

Please sign in to comment.