Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make radio-ble, usb-ctap, and usb-serial optional in Nordic #740

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions crates/runner-nordic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ 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 }
usb-device = "0.3.2"
usbd-hid = "0.8.2"
usbd-serial = "0.2.2"
wasefire-applet-api = { path = "../api" }
usbd-hid = { version = "0.8.2", optional = true }
usbd-serial = { version = "0.2.2", optional = true }
wasefire-applet-api = { path = "../api", optional = true }
wasefire-board-api = { path = "../board" }
wasefire-error = { path = "../error" }
wasefire-interpreter = { path = "../interpreter", optional = true }
Expand All @@ -37,11 +37,13 @@ wasefire-sync = { path = "../sync" }
[dependencies.rubble]
git = "https://github.com/jmichelp/rubble.git"
rev = "d545f4f598d081c0177f38500792e353a7d932a3"
optional = true

[dependencies.rubble-nrf5x]
git = "https://github.com/jmichelp/rubble.git"
rev = "d545f4f598d081c0177f38500792e353a7d932a3"
features = ["52840"]
optional = true

[dependencies.wasefire-scheduler]
path = "../scheduler"
Expand All @@ -53,16 +55,21 @@ features = [
"board-api-crypto-aes128-ccm",
"board-api-gpio",
"board-api-led",
"board-api-radio-ble",
"board-api-rng",
"board-api-storage",
"board-api-timer",
"board-api-uart",
"board-api-usb-ctap",
"board-api-usb-serial",
]

[features]
radio-ble = [
"dep:rubble",
"dep:rubble-nrf5x",
"dep:wasefire-applet-api",
"wasefire-scheduler/board-api-radio-ble",
]
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-p256 = ["software-crypto-sha256", "wasefire-scheduler/software-crypto-p256"]
Expand All @@ -72,7 +79,7 @@ debug = [
"dep:defmt",
"dep:defmt-rtt",
"dep:panic-probe",
"usbd-hid/defmt",
"usbd-hid?/defmt",
"wasefire-board-api/defmt",
"wasefire-error/defmt",
"wasefire-logger/defmt",
Expand All @@ -85,6 +92,7 @@ native = ["wasefire-scheduler/native"]
wasm = ["dep:wasefire-interpreter", "wasefire-scheduler/wasm"]
# Internal features.
_software-crypto = ["software-crypto-aes256-gcm", "software-crypto-p256", "software-crypto-sha256"]
_usb = []

[lints]
clippy.literal-string-with-formatting-args = "allow"
Expand Down
3 changes: 3 additions & 0 deletions crates/runner-nordic/src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod debug;
pub mod gpio;
pub mod led;
pub mod platform;
#[cfg(feature = "radio-ble")]
pub mod radio;
mod rng;
pub mod timer;
Expand Down Expand Up @@ -61,11 +62,13 @@ impl board::Api for Board {
type Gpio = gpio::Impl;
type Led = led::Impl;
type Platform = platform::Impl;
#[cfg(feature = "radio-ble")]
type Radio = radio::Impl;
type Rng = rng::Impl;
type Storage = crate::storage::Storage;
type Timer = timer::Impl;
type Uart = uart::Impl;
#[cfg(feature = "_usb")]
type Usb = usb::Impl;
}

Expand Down
8 changes: 8 additions & 0 deletions crates/runner-nordic/src/board/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
use alloc::boxed::Box;

use nrf52840_hal::usbd::{UsbPeripheral, Usbd};
#[cfg(feature = "_usb")]
use wasefire_board_api::usb::Api;
#[cfg(feature = "usb-ctap")]
use wasefire_board_api::usb::ctap::{Ctap, HasHid, WithHid};
#[cfg(feature = "usb-serial")]
use wasefire_board_api::usb::serial::{HasSerial, Serial, WithSerial};
use wasefire_error::{Code, Error};
use wasefire_protocol_usb::{HasRpc, Rpc};
Expand All @@ -29,8 +32,11 @@ pub enum Impl {}

pub type ProtocolImpl = wasefire_protocol_usb::Impl<'static, Usb, crate::board::usb::Impl>;

#[cfg(feature = "_usb")]
impl Api for Impl {
#[cfg(feature = "usb-ctap")]
type Ctap = WithHid<Impl>;
#[cfg(feature = "usb-serial")]
type Serial = WithSerial<Impl>;
}

Expand All @@ -57,6 +63,7 @@ impl HasRpc<'static, Usb> for Impl {
}
}

#[cfg(feature = "usb-ctap")]
impl HasHid for Impl {
type UsbBus = Usb;

Expand All @@ -65,6 +72,7 @@ impl HasHid for Impl {
}
}

#[cfg(feature = "usb-serial")]
impl HasSerial for Impl {
type UsbBus = Usb;

Expand Down
43 changes: 39 additions & 4 deletions crates/runner-nordic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod storage;
#[cfg(feature = "debug")]
mod systick;

use alloc::vec::Vec;
use core::cell::RefCell;
use core::mem::MaybeUninit;

Expand All @@ -47,14 +48,22 @@ use nrf52840_hal::usbd::{UsbPeripheral, Usbd};
use panic_abort as _;
#[cfg(feature = "debug")]
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 usb_device::class::UsbClass;
use usb_device::class_prelude::UsbBusAllocator;
use usb_device::device::{StringDescriptors, UsbDevice, UsbDeviceBuilder, UsbVidPid};
#[cfg(feature = "usb-ctap")]
use usbd_hid::descriptor::{CtapReport, SerializedDescriptor};
#[cfg(feature = "usb-ctap")]
use usbd_hid::hid_class::HIDClass;
#[cfg(feature = "usb-serial")]
use usbd_serial::SerialPort;
#[cfg(feature = "usb-ctap")]
use wasefire_board_api::usb::ctap::Ctap;
#[cfg(feature = "usb-serial")]
use wasefire_board_api::usb::serial::Serial;
use wasefire_board_api::{Id, Support};
#[cfg(feature = "wasm")]
Expand All @@ -65,6 +74,7 @@ use wasefire_scheduler::Scheduler;

use crate::board::button::{Button, channel};
use crate::board::gpio::Gpio;
#[cfg(feature = "radio-ble")]
use crate::board::radio::ble::Ble;
use crate::board::timer::Timers;
use crate::board::uart::Uarts;
Expand All @@ -89,9 +99,12 @@ struct State {
buttons: [Button; <button::Impl as Support<usize>>::SUPPORT],
gpiote: Gpiote,
protocol: wasefire_protocol_usb::Rpc<'static, Usb>,
#[cfg(feature = "usb-ctap")]
ctap: Ctap<'static, Usb>,
#[cfg(feature = "usb-serial")]
serial: Serial<'static, Usb>,
timers: Timers,
#[cfg(feature = "radio-ble")]
ble: Ble,
ccm: Ccm,
gpios: [Gpio; <board::gpio::Impl as Support<usize>>::SUPPORT],
Expand All @@ -115,7 +128,9 @@ 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")]
Expand Down Expand Up @@ -158,18 +173,22 @@ fn main() -> ! {
let usb_bus = UsbBusAllocator::new(Usbd::new(UsbPeripheral::new(p.USBD, clocks)));
let usb_bus = USB_BUS.write(usb_bus);
let protocol = wasefire_protocol_usb::Rpc::new(usb_bus);
#[cfg(feature = "usb-ctap")]
let ctap = Ctap::new(HIDClass::new(usb_bus, CtapReport::desc(), 255));
#[cfg(feature = "usb-serial")]
let serial = Serial::new(SerialPort::new(usb_bus));
let usb_dev = UsbDeviceBuilder::new(usb_bus, UsbVidPid(0x16c0, 0x27dd))
.strings(&[StringDescriptors::new(usb_device::LangID::EN).product("Wasefire")])
.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 rng = Rng::new(p.RNG);
let ccm = Ccm::init(p.CCM, p.AAR, DataRate::_1Mbit);
Expand All @@ -187,9 +206,12 @@ fn main() -> ! {
buttons,
gpiote,
protocol,
#[cfg(feature = "usb-ctap")]
ctap,
#[cfg(feature = "usb-serial")]
serial,
timers,
#[cfg(feature = "radio-ble")]
ble,
ccm,
gpios,
Expand All @@ -210,9 +232,10 @@ fn main() -> ! {
}

macro_rules! interrupts {
($($name:ident = $func:ident($($arg:expr),*$(,)?)),*$(,)?) => {
const INTERRUPTS: &[Interrupt] = &[$(Interrupt::$name),*];
($($(#[$meta:meta])* $name:ident = $func:ident($($arg:expr),*$(,)?)),*$(,)?) => {
const INTERRUPTS: &[Interrupt] = &[$($(#[$meta])* Interrupt::$name),*];
$(
$(#[$meta])*
#[interrupt]
fn $name() {
$func($($arg),*);
Expand All @@ -223,7 +246,9 @@ macro_rules! interrupts {

interrupts! {
GPIOTE = gpiote(),
#[cfg(feature = "radio-ble")]
RADIO = radio(),
#[cfg(feature = "radio-ble")]
TIMER0 = radio_timer(),
TIMER1 = timer(0),
TIMER2 = timer(1),
Expand All @@ -247,10 +272,12 @@ fn gpiote() {
});
}

#[cfg(feature = "radio-ble")]
fn radio() {
with_state(|state| state.ble.tick(|event| state.events.push(event.into())))
}

#[cfg(feature = "radio-ble")]
fn radio_timer() {
with_state(|state| state.ble.tick_timer())
}
Expand All @@ -272,10 +299,18 @@ fn uarte(uarte: usize) {

fn usbd() {
with_state(|state| {
let polled =
state.usb_dev.poll(&mut [&mut state.protocol, state.ctap.class(), state.serial.port()]);
let mut classes = Vec::<&mut dyn UsbClass<_>>::new();
classes.push(&mut state.protocol);
#[cfg(feature = "usb-ctap")]
classes.push(state.ctap.class());
#[cfg(feature = "usb-serial")]
classes.push(state.serial.port());
#[cfg_attr(not(feature = "usb-serial"), allow(unused_variables))]
let polled = state.usb_dev.poll(&mut classes);
state.protocol.tick(|event| state.events.push(event.into()));
#[cfg(feature = "usb-ctap")]
state.ctap.tick(|event| state.events.push(event.into()));
#[cfg(feature = "usb-serial")]
state.serial.tick(polled, |event| state.events.push(event.into()));
});
}
3 changes: 3 additions & 0 deletions crates/runner-nordic/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ ensure_applet
test_helper

cargo check --bin=runner-nordic --target=thumbv7em-none-eabi --features=wasm,debug
cargo check --bin=runner-nordic --target=thumbv7em-none-eabi --features=wasm,debug,radio-ble
cargo check --bin=runner-nordic --target=thumbv7em-none-eabi --features=wasm,debug,usb-ctap
cargo check --bin=runner-nordic --target=thumbv7em-none-eabi --features=wasm,debug,usb-serial
cargo check --bin=runner-nordic --target=thumbv7em-none-eabi --features=wasm,debug,_software-crypto
DEFMT_LOG=trace cargo check --bin=runner-nordic --target=thumbv7em-none-eabi --features=wasm,debug
cargo check --bin=runner-nordic --target=thumbv7em-none-eabi --features=wasm,release
Expand Down
Loading