diff --git a/firmware/Cargo.toml b/firmware/Cargo.toml index 621d1982..7eb6c9a9 100644 --- a/firmware/Cargo.toml +++ b/firmware/Cargo.toml @@ -14,21 +14,21 @@ rust-version.workspace = true [features] -default = [ - "mcu-esp32c3", - "imu-stubbed", - "log-rtt", - "net-wifi", - "fusion-stubbed", -] # default = [ -# "mcu-nrf52840", +# "mcu-esp32c3", # "imu-stubbed", # "log-rtt", -# "net-stubbed", +# "net-wifi", # "fusion-stubbed", -# "nrf-boot-s140", # ] +default = [ + "mcu-nrf52840", + "imu-stubbed", + "log-rtt", + "net-ble", + "fusion-stubbed", + "nrf-boot-s140", +] # default = ["mcu-esp32", "imu-stubbed", "log-uart", "net-wifi", "fusion-stubbed"] # Supported microcontrollers @@ -67,9 +67,9 @@ mcu-nrf52832 = [ ] # Wi-fi dependencies -net-wifi = ["esp-wifi/wifi", "dep:smoltcp"] # use wifi -net-ble = ["esp-wifi/ble", "dep:bleps", "dep:bleps-macros"] -net-stubbed = [] # Stubs out network +net-wifi = ["esp-wifi/wifi", "dep:smoltcp"] # use wifi +net-ble = ["esp-wifi?/ble", "dep:bleps", "dep:bleps-macros"] +net-stubbed = [] # Stubs out network # Supported IMUs imu-bmi160 = [] diff --git a/firmware/build.rs b/firmware/build.rs index cc390630..02098747 100644 --- a/firmware/build.rs +++ b/firmware/build.rs @@ -125,7 +125,7 @@ impl MemoryLayout { const S140: MemoryLayout = MemoryLayout { mbr_size: 0x1000, sd_flash_size: 0x26000, - sd_ram_size: 0x8, + sd_ram_size: 50_000, }; /// Softdevice 132. const S132: MemoryLayout = MemoryLayout { diff --git a/firmware/crates/panic_defmt/src/lib.rs b/firmware/crates/panic_defmt/src/lib.rs index 43c7410f..aa4bedf6 100644 --- a/firmware/crates/panic_defmt/src/lib.rs +++ b/firmware/crates/panic_defmt/src/lib.rs @@ -13,10 +13,8 @@ fn panic(info: &core::panic::PanicInfo) -> ! { if let Some(location) = info.location() { let (file, line, column) = (location.file(), location.line(), location.column()); - error!( - "A panic occured in '{}', at line {}, column {}", - file, line, column - ); + error!("Panic in '{}'", file); + error!("Panic was at line {}, column {}", line, column); } else { error!("A panic occured at an unknown location"); } diff --git a/firmware/src/main.rs b/firmware/src/main.rs index 420b04ed..58b038b5 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -68,8 +68,13 @@ fn main() -> ! { .unwrap(); s.spawn(crate::networking::network_task(packets)).unwrap(); s.spawn(crate::imu::imu_task(quat, p.i2c, p.delay)).unwrap(); + #[cfg(bbq)] s.spawn(logger_task(bbq, bbq_peripheral)).unwrap(); + + #[cfg(any(feature = "nrf-boot-s140", feature = "nrf-boot-s132"))] + s.spawn(crate::networking::ble::ඞ::softdevice_task()) + .unwrap() }); } diff --git a/firmware/src/networking/ble/mod.rs b/firmware/src/networking/ble/mod.rs index 06b77699..f59a0a90 100644 --- a/firmware/src/networking/ble/mod.rs +++ b/firmware/src/networking/ble/mod.rs @@ -1,3 +1,7 @@ -#[cfg(feature = "net-ble")] +#[cfg(mcu_f_esp32)] #[path = "esp.rs"] pub mod ඞ; + +#[cfg(mcu_f_nrf52)] +#[path = "nrf.rs"] +pub mod ඞ; diff --git a/firmware/src/networking/ble/nrf.rs b/firmware/src/networking/ble/nrf.rs new file mode 100644 index 00000000..d13d8a8f --- /dev/null +++ b/firmware/src/networking/ble/nrf.rs @@ -0,0 +1,36 @@ +use defmt::{debug, error}; +use embassy_futures::yield_now; +use embassy_time::Duration; +use nrf_softdevice::Softdevice; + +use crate::networking::protocol::Packets; + +pub async fn network_task(_packets: &Packets) -> ! { + debug!("Starting BLE loop"); + loop { + yield_now().await; + } +} + +#[embassy_executor::task] +pub async fn softdevice_task() -> ! { + error!("softdevice task"); + embassy_time::Timer::after(Duration::from_millis(1000)).await; + + #[cfg(any(feature = "nrf-boot-s140", feature = "nrf-boot-s132"))] + let sd = crate::networking::ble::ඞ::init_softdevice().await; + error!("after init"); + + embassy_time::Timer::after(Duration::from_millis(1000)).await; + sd.run().await +} + +pub async fn init_softdevice() -> &'static mut Softdevice { + let config = nrf_softdevice::Config::default(); + error!("after config"); + + embassy_time::Timer::after(Duration::from_millis(1000)).await; + let sd = Softdevice::enable(&config); + error!("enabled softdevice"); + sd +} diff --git a/firmware/src/peripherals/nrf52.rs b/firmware/src/peripherals/nrf52.rs index 8158845b..69e7d314 100644 --- a/firmware/src/peripherals/nrf52.rs +++ b/firmware/src/peripherals/nrf52.rs @@ -6,6 +6,8 @@ use crate::aliases::ඞ::UsbDriverConcrete; use defmt::debug; use embassy_nrf::interrupt; +use embassy_nrf::interrupt::InterruptExt; +use embassy_nrf::interrupt::Priority; use embassy_nrf::twim::{self, Twim}; use embassy_nrf::uarte::{self, Uarte}; use paste::paste; @@ -24,7 +26,14 @@ pub fn get_peripherals() -> Peripherals< UartConcrete<'static>, UsbDriverConcrete<'static>, > { - let p = embassy_nrf::init(Default::default()); + // With softdevice, certain interrupt priorities are used by softdevice. + // https://github.com/embassy-rs/nrf-softdevice#interrupt-priority + // + // We therefore have to set interrupt priorities manually. + let mut cfg = embassy_nrf::config::Config::default(); + // cfg.gpiote_interrupt_priority = Priority::P2; + cfg.time_interrupt_priority = Priority::P2; + let p = embassy_nrf::init(cfg); // Fix issue on rev 3 boards where AP is protected, preventing debugging/rtt. #[cfg(feature = "mcu-nrf52840")] // TODO: Add nrf52832 support @@ -111,6 +120,7 @@ pub fn get_peripherals() -> Peripherals< let twim = { let config = twim::Config::default(); let irq = interrupt::take!(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); + irq.set_priority(Priority::P3); Twim::new( p.TWISPI0, irq, @@ -126,6 +136,7 @@ pub fn get_peripherals() -> Peripherals< let uarte = { let irq = interrupt::take!(UARTE0_UART0); + irq.set_priority(Priority::P3); let mut config = uarte::Config::default(); config.parity = uarte::Parity::EXCLUDED; config.baudrate = uarte::Baudrate::BAUD115200; @@ -142,7 +153,9 @@ pub fn get_peripherals() -> Peripherals< let usb_driver = { use embassy_nrf::usb::{self, Driver}; let irq = interrupt::take!(USBD); + irq.set_priority(Priority::P3); let power_irq = interrupt::take!(POWER_CLOCK); + power_irq.set_priority(Priority::P3); let d = Driver::new(p.USBD, irq, usb::PowerUsb::new(power_irq)); debug!("Initialized usb_driver"); d