Skip to content

Commit

Permalink
Make aes128-ccm, uart, and gpio optional in Nordic (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 authored Feb 17, 2025
1 parent 2c8a347 commit 4d0adf5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
15 changes: 8 additions & 7 deletions crates/runner-nordic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ header = { path = "crates/header", features = ["alloc"] }
nrf52840-hal = { version = "0.18.0", features = ["embedded-hal-02"] }
panic-abort = { version = "0.3.2", optional = true }
panic-probe = { version = "0.3.2", features = ["print-defmt"], optional = true }
typenum = { version = "1.17.0", default-features = false }
typenum = { version = "1.17.0", default-features = false, optional = true }
usb-device = "0.3.2"
usbd-hid = { version = "0.8.2", optional = true }
usbd-serial = { version = "0.2.2", optional = true }
Expand Down Expand Up @@ -52,32 +52,32 @@ features = [
"applet-api-platform-protocol",
"applet-api-platform-update",
"board-api-button",
"board-api-crypto-aes128-ccm",
"board-api-gpio",
"board-api-led",
"board-api-rng",
"board-api-storage",
"board-api-timer",
"board-api-uart",
]

[features]
aes128-ccm = ["_crypto", "dep:typenum", "wasefire-scheduler/board-api-crypto-aes128-ccm"]
gpio = ["wasefire-scheduler/board-api-gpio"]
radio-ble = [
"dep:rubble",
"dep:rubble-nrf5x",
"dep:wasefire-applet-api",
"wasefire-scheduler/board-api-radio-ble",
]
uart = ["wasefire-scheduler/board-api-uart"]
usb-ctap = ["_usb", "dep:usbd-hid", "wasefire-scheduler/board-api-usb-ctap"]
usb-serial = ["_usb", "dep:usbd-serial", "wasefire-scheduler/board-api-usb-serial"]
# Software crypto features.
software-crypto-aes256-gcm = ["wasefire-scheduler/software-crypto-aes256-gcm"]
software-crypto-aes256-gcm = ["_crypto", "wasefire-scheduler/software-crypto-aes256-gcm"]
software-crypto-hmac-sha256 = [
"software-crypto-sha256",
"wasefire-scheduler/software-crypto-hmac-sha256",
]
software-crypto-p256 = ["software-crypto-sha256", "wasefire-scheduler/software-crypto-p256"]
software-crypto-sha256 = ["wasefire-scheduler/software-crypto-sha256"]
software-crypto-sha256 = ["_crypto", "wasefire-scheduler/software-crypto-sha256"]
# Exactly one is enabled by xtask.
debug = [
"dep:defmt",
Expand All @@ -95,7 +95,8 @@ release = ["dep:panic-abort"]
native = ["wasefire-scheduler/native"]
wasm = ["dep:wasefire-interpreter", "wasefire-scheduler/wasm"]
# Internal features.
_full = ["_software-crypto", "radio-ble", "usb-ctap", "usb-serial"]
_crypto = []
_full = ["_software-crypto", "aes128-ccm", "gpio", "radio-ble", "uart", "usb-ctap", "usb-serial"]
_software-crypto = [
"software-crypto-aes256-gcm",
"software-crypto-hmac-sha256",
Expand Down
7 changes: 7 additions & 0 deletions crates/runner-nordic/src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ use crate::{Board, with_state};

pub mod applet;
pub mod button;
#[cfg(feature = "_crypto")]
mod crypto;
mod debug;
#[cfg(feature = "gpio")]
pub mod gpio;
pub mod led;
pub mod platform;
#[cfg(feature = "radio-ble")]
pub mod radio;
mod rng;
pub mod timer;
#[cfg(feature = "uart")]
pub mod uart;
pub mod usb;

Expand All @@ -50,15 +53,18 @@ impl board::Api for Board {
match (x1, x2, x3, x4) {
// The syscall_test example relies on this.
(0, 0, 0, x) => Some(Error::decode(x as i32)),
#[cfg(feature = "gpio")]
(0x80000000, x, y, z) => Some(gpio::syscall(x, y, z)),
_ => None,
}
}

type Applet = applet::Impl;
type Button = button::Impl;
#[cfg(feature = "_crypto")]
type Crypto = crypto::Impl;
type Debug = debug::Impl;
#[cfg(feature = "gpio")]
type Gpio = gpio::Impl;
type Led = led::Impl;
type Platform = platform::Impl;
Expand All @@ -67,6 +73,7 @@ impl board::Api for Board {
type Rng = rng::Impl;
type Storage = crate::storage::Storage;
type Timer = timer::Impl;
#[cfg(feature = "uart")]
type Uart = uart::Impl;
#[cfg(feature = "_usb")]
type Usb = usb::Impl;
Expand Down
2 changes: 2 additions & 0 deletions crates/runner-nordic/src/board/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

use wasefire_board_api::crypto;

#[cfg(feature = "aes128-ccm")]
mod ccm;

pub enum Impl {}

impl crypto::Api for Impl {
#[cfg(feature = "aes128-ccm")]
type Aes128Ccm = ccm::Impl;
#[cfg(feature = "software-crypto-aes256-gcm")]
type Aes256Gcm = crypto::SoftwareAes256Gcm;
Expand Down
50 changes: 31 additions & 19 deletions crates/runner-nordic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ use critical_section::Mutex;
#[cfg(feature = "debug")]
use defmt_rtt as _;
use embedded_hal::digital::InputPin;
#[cfg(feature = "aes128-ccm")]
use nrf52840_hal::ccm::{Ccm, DataRate};
use nrf52840_hal::clocks::{self, ExternalOscillator, Internal, LfOscStopped};
use nrf52840_hal::gpio;
use nrf52840_hal::gpio::{Level, Output, Pin, PushPull};
use nrf52840_hal::gpio::{self, Level, Output, Pin, PushPull};
use nrf52840_hal::gpiote::Gpiote;
use nrf52840_hal::pac::{FICR, Interrupt, interrupt};
use nrf52840_hal::rng::Rng;
Expand All @@ -51,7 +51,7 @@ use panic_probe as _;
#[cfg(feature = "radio-ble")]
use rubble::link::MIN_PDU_BUF;
#[cfg(feature = "radio-ble")]
use rubble_nrf5x::radio::{BleRadio, PacketBuffer};
use rubble_nrf5x::radio::BleRadio;
use usb_device::class::UsbClass;
use usb_device::class_prelude::UsbBusAllocator;
use usb_device::device::{StringDescriptors, UsbDevice, UsbDeviceBuilder, UsbVidPid};
Expand All @@ -73,10 +73,12 @@ use wasefire_one_of::exactly_one_of;
use wasefire_scheduler::Scheduler;

use crate::board::button::{Button, channel};
#[cfg(feature = "gpio")]
use crate::board::gpio::Gpio;
#[cfg(feature = "radio-ble")]
use crate::board::radio::ble::Ble;
use crate::board::timer::Timers;
#[cfg(feature = "uart")]
use crate::board::uart::Uarts;
use crate::board::usb::Usb;
use crate::board::{Events, button, led};
Expand Down Expand Up @@ -106,11 +108,14 @@ struct State {
timers: Timers,
#[cfg(feature = "radio-ble")]
ble: Ble,
#[cfg(feature = "aes128-ccm")]
ccm: Ccm,
#[cfg(feature = "gpio")]
gpios: [Gpio; <board::gpio::Impl as Support<usize>>::SUPPORT],
leds: [Pin<Output<PushPull>>; <led::Impl as Support<usize>>::SUPPORT],
rng: Rng,
storage: Option<Storage>,
#[cfg(feature = "uart")]
uarts: Uarts,
usb_dev: UsbDevice<'static, Usb>,
}
Expand All @@ -127,11 +132,6 @@ fn with_state<R>(f: impl FnOnce(&mut State) -> R) -> R {
fn main() -> ! {
static mut CLOCKS: MaybeUninit<Clocks> = MaybeUninit::uninit();
static mut USB_BUS: MaybeUninit<UsbBusAllocator<Usb>> = MaybeUninit::uninit();
// TX buffer is mandatory even when we only listen.
#[cfg(feature = "radio-ble")]
static mut BLE_TX: MaybeUninit<PacketBuffer> = MaybeUninit::uninit();
#[cfg(feature = "radio-ble")]
static mut BLE_RX: MaybeUninit<PacketBuffer> = MaybeUninit::uninit();

#[cfg(feature = "debug")]
let c = nrf52840_hal::pac::CorePeripherals::take().unwrap();
Expand All @@ -142,6 +142,7 @@ fn main() -> ! {
let p = nrf52840_hal::pac::Peripherals::take().unwrap();
let ficr = p.FICR;
let port0 = gpio::p0::Parts::new(p.P0);
#[cfg(feature = "gpio")]
let port1 = gpio::p1::Parts::new(p.P1);
let buttons = [
Button::new(port0.p0_11.into_pullup_input().degrade()),
Expand All @@ -155,6 +156,7 @@ fn main() -> ! {
port0.p0_15.into_push_pull_output(Level::High).degrade(),
port0.p0_16.into_push_pull_output(Level::High).degrade(),
];
#[cfg(feature = "gpio")]
let gpios = [
Gpio::new(port1.p1_01.degrade(), 1, 1),
Gpio::new(port1.p1_02.degrade(), 1, 2),
Expand Down Expand Up @@ -182,23 +184,27 @@ fn main() -> ! {
.unwrap()
.build();
#[cfg(feature = "radio-ble")]
let radio = BleRadio::new(
p.RADIO,
&ficr,
BLE_TX.write([0; MIN_PDU_BUF]),
BLE_RX.write([0; MIN_PDU_BUF]),
);
#[cfg(feature = "radio-ble")]
let ble = Ble::new(radio, p.TIMER0);
let ble = {
use alloc::boxed::Box;
// TX buffer is mandatory even when we only listen.
let ble_tx = Box::leak(Box::new([0; MIN_PDU_BUF]));
let ble_rx = Box::leak(Box::new([0; MIN_PDU_BUF]));
let radio = BleRadio::new(p.RADIO, &ficr, ble_tx, ble_rx);
Ble::new(radio, p.TIMER0)
};
let rng = Rng::new(p.RNG);
#[cfg(feature = "aes128-ccm")]
let ccm = Ccm::init(p.CCM, p.AAR, DataRate::_1Mbit);
storage::init(p.NVMC);
let storage = Some(Storage::new_store());
crate::board::platform::update::init(Storage::new_other());
crate::board::applet::init(Storage::new_applet());
let uart_rx = port0.p0_28.into_floating_input().degrade();
let uart_tx = port0.p0_29.into_push_pull_output(gpio::Level::High).degrade();
let uarts = Uarts::new(p.UARTE0, uart_rx, uart_tx, p.UARTE1);
#[cfg(feature = "uart")]
let uarts = {
let uart_rx = port0.p0_28.into_floating_input().degrade();
let uart_tx = port0.p0_29.into_push_pull_output(gpio::Level::High).degrade();
Uarts::new(p.UARTE0, uart_rx, uart_tx, p.UARTE1)
};
let events = Events::default();
let state = State {
events,
Expand All @@ -213,11 +219,14 @@ fn main() -> ! {
timers,
#[cfg(feature = "radio-ble")]
ble,
#[cfg(feature = "aes128-ccm")]
ccm,
#[cfg(feature = "gpio")]
gpios,
leds,
rng,
storage,
#[cfg(feature = "uart")]
uarts,
usb_dev,
};
Expand Down Expand Up @@ -254,7 +263,9 @@ interrupts! {
TIMER2 = timer(1),
TIMER3 = timer(2),
TIMER4 = timer(3),
#[cfg(feature = "uart")]
UARTE0_UART0 = uarte(0),
#[cfg(feature = "uart")]
UARTE1 = uarte(1),
USBD = usbd(),
}
Expand Down Expand Up @@ -290,6 +301,7 @@ fn timer(timer: usize) {
})
}

#[cfg(feature = "uart")]
fn uarte(uarte: usize) {
let uart = Id::new(uarte).unwrap();
with_state(|state| {
Expand Down

0 comments on commit 4d0adf5

Please sign in to comment.