From 3d7a303abde0acd7ca7ef72eb5dfc3991b0f1963 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 8 Feb 2022 17:24:41 +0100 Subject: [PATCH 1/3] Update PACs and example dependencies --- examples/blinky-button-demo/Cargo.toml | 2 +- examples/ccm-demo/Cargo.toml | 2 +- examples/comp-demo/Cargo.toml | 2 +- examples/comp-demo/src/main.rs | 54 ++--- examples/gpiote-demo/Cargo.toml | 3 +- examples/gpiote-demo/src/main.rs | 148 ++++++------ examples/i2s-controller-demo/Cargo.toml | 5 +- examples/i2s-controller-demo/src/main.rs | 184 +++++++-------- examples/i2s-peripheral-demo/Cargo.toml | 2 +- examples/i2s-peripheral-demo/src/main.rs | 75 +++--- examples/lpcomp-demo/Cargo.toml | 2 +- examples/lpcomp-demo/src/main.rs | 86 ++++--- examples/nvmc-demo/Cargo.toml | 2 +- examples/ppi-demo/Cargo.toml | 2 +- examples/pwm-blinky-demo/Cargo.toml | 2 +- examples/pwm-demo/Cargo.toml | 7 +- examples/pwm-demo/src/main.rs | 167 +++++++------ examples/qdec-demo/Cargo.toml | 2 +- examples/qdec-demo/src/main.rs | 43 ++-- examples/rtc-demo/Cargo.toml | 2 +- examples/rtic-demo/Cargo.toml | 2 +- examples/rtic-demo/src/main.rs | 21 +- examples/spi-demo/Cargo.toml | 8 +- examples/spis-demo/Cargo.toml | 2 +- examples/spis-demo/src/main.rs | 67 +++--- examples/twim-demo/Cargo.toml | 3 +- examples/twim-demo/src/main.rs | 99 ++++---- examples/twis-demo/Cargo.toml | 2 +- examples/twis-demo/src/main.rs | 71 +++--- examples/twis-dma-demo/Cargo.toml | 2 +- examples/twis-dma-demo/src/main.rs | 78 +++--- examples/usb/Cargo.toml | 1 - examples/usb/src/bin/serial.rs | 3 +- examples/usb/src/bin/test_class.rs | 3 +- examples/wdt-demo/Cargo.toml | 1 - examples/wdt-demo/src/main.rs | 288 +++++++++-------------- nrf-hal-common/Cargo.toml | 18 +- nrf51-hal/Cargo.toml | 4 +- nrf52810-hal/Cargo.toml | 2 +- nrf52811-hal/Cargo.toml | 2 +- nrf52832-hal/Cargo.toml | 4 +- nrf52833-hal/Cargo.toml | 4 +- nrf52840-hal/Cargo.toml | 2 +- nrf5340-app-hal/Cargo.toml | 4 +- nrf9160-hal/Cargo.toml | 2 +- xtask/src/lib.rs | 2 +- 46 files changed, 723 insertions(+), 764 deletions(-) diff --git a/examples/blinky-button-demo/Cargo.toml b/examples/blinky-button-demo/Cargo.toml index b8b1b67c..f337e594 100644 --- a/examples/blinky-button-demo/Cargo.toml +++ b/examples/blinky-button-demo/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] cortex-m = "0.7.3" cortex-m-rt = "0.7.0" -rtt-target = {version = "0.2.0", features = ["cortex-m"] } +rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52832-hal = { features = ["rt"], path = "../../nrf52832-hal" } [dependencies.embedded-hal] diff --git a/examples/ccm-demo/Cargo.toml b/examples/ccm-demo/Cargo.toml index 950804ce..76340438 100644 --- a/examples/ccm-demo/Cargo.toml +++ b/examples/ccm-demo/Cargo.toml @@ -8,7 +8,7 @@ publish = false [dependencies] cortex-m = "0.7.3" cortex-m-rt = "0.7.0" -rtt-target = {version = "0.2.0", features = ["cortex-m"] } +rtt-target = { version = "0.3.1", features = ["cortex-m"] } rand_core = "0.6.3" nrf52810-hal = { path = "../../nrf52810-hal", features = ["rt"], optional = true } diff --git a/examples/comp-demo/Cargo.toml b/examples/comp-demo/Cargo.toml index 6a584580..c2cedf4a 100644 --- a/examples/comp-demo/Cargo.toml +++ b/examples/comp-demo/Cargo.toml @@ -10,7 +10,7 @@ publish = false [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } +cortex-m-rtic = { version = "1.0.0", default-features = false } rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } diff --git a/examples/comp-demo/src/main.rs b/examples/comp-demo/src/main.rs index ad731cd0..33d47387 100644 --- a/examples/comp-demo/src/main.rs +++ b/examples/comp-demo/src/main.rs @@ -1,33 +1,31 @@ #![no_std] #![no_main] -use embedded_hal::digital::v2::OutputPin; -use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::{ - comp::*, - gpio::{Level, Output, Pin, PushPull}, - }, - nrf52840_hal as hal, - rtt_target::{rprintln, rtt_init_print}, -}; +use {core::panic::PanicInfo, rtt_target::rprintln}; -#[rtic::app(device = crate::hal::pac, peripherals = true)] -const APP: () = { - struct Resources { +#[rtic::app(device = nrf52840_hal::pac, peripherals = true)] +mod app { + use embedded_hal::digital::v2::OutputPin; + use nrf52840_hal::clocks::Clocks; + use nrf52840_hal::comp::*; + use nrf52840_hal::gpio::{self, Level, Output, Pin, PushPull}; + use rtt_target::{rprintln, rtt_init_print}; + + #[shared] + struct Shared {} + + #[local] + struct Local { comp: Comp, led1: Pin>, } #[init] - fn init(ctx: init::Context) -> init::LateResources { - let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); + fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) { + let _clocks = Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); rtt_init_print!(); - let p0 = hal::gpio::p0::Parts::new(ctx.device.P0); + let p0 = gpio::p0::Parts::new(ctx.device.P0); let led1 = p0.p0_13.into_push_pull_output(Level::High).degrade(); let in_pin = p0.p0_30.into_floating_input(); let ref_pin = p0.p0_31.into_floating_input(); @@ -38,31 +36,29 @@ const APP: () = { .enable_interrupt(Transition::Cross) .enable(); - init::LateResources { comp, led1 } + (Shared {}, Local { comp, led1 }, init::Monotonics()) } - #[task(binds = COMP_LPCOMP, resources = [comp, led1])] + #[task(binds = COMP_LPCOMP, local = [comp, led1])] fn on_comp(ctx: on_comp::Context) { - ctx.resources.comp.reset_event(Transition::Cross); - match ctx.resources.comp.read() { + ctx.local.comp.reset_event(Transition::Cross); + match ctx.local.comp.read() { CompResult::Above => { rprintln!("Vin > Vref"); - ctx.resources.led1.set_low().ok(); + ctx.local.led1.set_low().ok(); } CompResult::Below => { rprintln!("Vin < Vref"); - ctx.resources.led1.set_high().ok(); + ctx.local.led1.set_high().ok(); } } } -}; +} #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/examples/gpiote-demo/Cargo.toml b/examples/gpiote-demo/Cargo.toml index 0325366e..dfb7aa08 100644 --- a/examples/gpiote-demo/Cargo.toml +++ b/examples/gpiote-demo/Cargo.toml @@ -9,7 +9,8 @@ edition = "2018" [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } +cortex-m-rtic = { version = "1.0.0", default-features = false } +systick-monotonic = "1.0.0" rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } diff --git a/examples/gpiote-demo/src/main.rs b/examples/gpiote-demo/src/main.rs index 741fdd6a..ca49cc3d 100644 --- a/examples/gpiote-demo/src/main.rs +++ b/examples/gpiote-demo/src/main.rs @@ -1,33 +1,39 @@ #![no_std] #![no_main] -use embedded_hal::digital::v2::InputPin; -use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::{ - gpio::{Input, Level, Pin, PullUp}, - gpiote::*, - ppi::{self, ConfigurablePpi, Ppi}, - }, - nrf52840_hal as hal, - rtic::cyccnt::U32Ext, - rtt_target::{rprintln, rtt_init_print}, -}; - -#[rtic::app(device = crate::hal::pac, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { - struct Resources { +use {core::panic::PanicInfo, nrf52840_hal as hal, rtt_target::rprintln}; + +#[rtic::app(device = crate::hal::pac, peripherals = true, dispatchers = [SWI0_EGU0])] +mod app { + use embedded_hal::digital::v2::InputPin; + use systick_monotonic::*; + use { + hal::{ + gpio::{Input, Level, Pin, PullUp}, + gpiote::*, + ppi::{self, ConfigurablePpi, Ppi}, + }, + nrf52840_hal as hal, + rtt_target::{rprintln, rtt_init_print}, + }; + + #[monotonic(binds = SysTick, default = true)] + type Timer = Systick<1_000_000>; + + #[shared] + struct Shared { gpiote: Gpiote, + } + + #[local] + struct Local { btn1: Pin>, btn3: Pin>, btn4: Pin>, } #[init] - fn init(mut ctx: init::Context) -> init::LateResources { + fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) { let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); rtt_init_print!(); let p0 = hal::gpio::p0::Parts::new(ctx.device.P0); @@ -65,75 +71,63 @@ const APP: () = { ppi0.set_event_endpoint(gpiote.channel2().event()); ppi0.enable(); - // Enable the monotonic timer (CYCCNT) - ctx.core.DCB.enable_trace(); - ctx.core.DWT.enable_cycle_counter(); + let mono = Systick::new(ctx.core.SYST, 64_000_000); rprintln!("Press a button"); - init::LateResources { - gpiote, - btn1, - btn3, - btn4, - } - } - - #[idle] - fn idle(_: idle::Context) -> ! { - loop { - cortex_m::asm::wfi(); - } + ( + Shared { gpiote }, + Local { btn1, btn3, btn4 }, + init::Monotonics(mono), + ) } - #[task(binds = GPIOTE, resources = [gpiote], schedule = [debounce])] - fn on_gpiote(ctx: on_gpiote::Context) { - if ctx.resources.gpiote.channel0().is_event_triggered() { - rprintln!("Interrupt from channel 0 event"); - } - if ctx.resources.gpiote.port().is_event_triggered() { - rprintln!("Interrupt from port event"); - } - // Reset all events - ctx.resources.gpiote.reset_events(); - // Debounce - ctx.schedule.debounce(ctx.start + 3_000_000.cycles()).ok(); + #[task(binds = GPIOTE, shared = [gpiote])] + fn on_gpiote(mut ctx: on_gpiote::Context) { + ctx.shared.gpiote.lock(|gpiote| { + if gpiote.channel0().is_event_triggered() { + rprintln!("Interrupt from channel 0 event"); + } + if gpiote.port().is_event_triggered() { + rprintln!("Interrupt from port event"); + } + // Reset all events + gpiote.reset_events(); + // Debounce + debounce::spawn_after(50.millis()).ok(); + }); } - #[task(resources = [gpiote, btn1, btn3, btn4])] - fn debounce(ctx: debounce::Context) { - let btn1_pressed = ctx.resources.btn1.is_low().unwrap(); - let btn3_pressed = ctx.resources.btn3.is_low().unwrap(); - let btn4_pressed = ctx.resources.btn4.is_low().unwrap(); - - if btn1_pressed { - rprintln!("Button 1 was pressed!"); - // Manually run "task out" operation (toggle) on channel 1 (toggles led1) - ctx.resources.gpiote.channel1().out(); - } - if btn3_pressed { - rprintln!("Button 3 was pressed!"); - // Manually run "task clear" on channel 1 (led1 on) - ctx.resources.gpiote.channel1().clear(); - } - if btn4_pressed { - rprintln!("Button 4 was pressed!"); - // Manually run "task set" on channel 1 (led1 off) - ctx.resources.gpiote.channel1().set(); - } + #[task(shared = [gpiote], local = [btn1, btn3, btn4])] + fn debounce(mut ctx: debounce::Context) { + let btn1_pressed = ctx.local.btn1.is_low().unwrap(); + let btn3_pressed = ctx.local.btn3.is_low().unwrap(); + let btn4_pressed = ctx.local.btn4.is_low().unwrap(); + + ctx.shared.gpiote.lock(|gpiote| { + if btn1_pressed { + rprintln!("Button 1 was pressed!"); + // Manually run "task out" operation (toggle) on channel 1 (toggles led1) + gpiote.channel1().out(); + } + if btn3_pressed { + rprintln!("Button 3 was pressed!"); + // Manually run "task clear" on channel 1 (led1 on) + gpiote.channel1().clear(); + } + if btn4_pressed { + rprintln!("Button 4 was pressed!"); + // Manually run "task set" on channel 1 (led1 off) + gpiote.channel1().set(); + } + }); } - - extern "C" { - fn SWI0_EGU0(); - } -}; +} #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/examples/i2s-controller-demo/Cargo.toml b/examples/i2s-controller-demo/Cargo.toml index 93c97e6f..f68163a6 100644 --- a/examples/i2s-controller-demo/Cargo.toml +++ b/examples/i2s-controller-demo/Cargo.toml @@ -10,10 +10,11 @@ publish = false [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } +cortex-m-rtic = { version = "1.0.0", default-features = false } +systick-monotonic = "1.0.0" rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } -heapless = "0.5.5" +heapless = "0.7.10" small_morse = "0.1.0" [dependencies.embedded-hal] diff --git a/examples/i2s-controller-demo/src/main.rs b/examples/i2s-controller-demo/src/main.rs index b9776809..110cafd2 100644 --- a/examples/i2s-controller-demo/src/main.rs +++ b/examples/i2s-controller-demo/src/main.rs @@ -5,44 +5,44 @@ // Generates Morse code audio signals for text from UART, playing back over I2S // Tested with nRF52840-DK and a UDA1334a DAC -use embedded_hal::digital::v2::{InputPin, OutputPin}; -use heapless::{ - consts::*, - spsc::{Consumer, Producer, Queue}, -}; -use small_morse::{encode, State}; -use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::{ - gpio::{Input, Level, Output, Pin, PullUp, PushPull}, - gpiote::*, - i2s::{self, *}, - pac::{TIMER0, UARTE0}, - timer::Timer, - uarte::{self, *}, - }, - nrf52840_hal as hal, - rtic::cyccnt::U32Ext, - rtt_target::{rprintln, rtt_init_print}, -}; +use {core::panic::PanicInfo, nrf52840_hal as hal, rtt_target::rprintln}; #[repr(align(4))] -struct Aligned(T); +pub struct Aligned(T); + +#[rtic::app(device = crate::hal::pac, peripherals = true, dispatchers = [SWI0_EGU0, SWI1_EGU1])] +mod app { + use crate::{hal, triangle_wave, Aligned}; + use embedded_hal::digital::v2::{InputPin, OutputPin}; + use heapless::spsc::{Consumer, Producer, Queue}; + use small_morse::{encode, State}; + use systick_monotonic::*; + use { + hal::{ + gpio::{Input, Level, Output, Pin, PullUp, PushPull}, + gpiote::*, + i2s::{self, *}, + pac::{TIMER0, UARTE0}, + timer::Timer, + uarte::{self, *}, + }, + rtt_target::{rprintln, rtt_init_print}, + }; + + #[monotonic(binds = SysTick, default = true)] + type Mono = Systick<1_000_000>; + + #[shared] + struct Shared { + speed: u32, + } -#[rtic::app(device = crate::hal::pac, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { - struct Resources { + #[local] + struct Local { signal_buf: &'static [i16; 32], mute_buf: &'static [i16; 32], - #[init(None)] - queue: Option>, - producer: Producer<'static, State, U256>, - consumer: Consumer<'static, State, U256>, - #[init(5_000_000)] - speed: u32, + producer: Producer<'static, State, 257>, + consumer: Consumer<'static, State, 257>, uarte: Uarte, uarte_timer: Timer, gpiote: Gpiote, @@ -52,17 +52,21 @@ const APP: () = { transfer: Option>, } - #[init(resources = [queue], spawn = [tick])] - fn init(mut ctx: init::Context) -> init::LateResources { + #[init(local = [ // The I2S buffer address must be 4 byte aligned. - static mut MUTE_BUF: Aligned<[i16; 32]> = Aligned([0i16; 32]); - static mut SIGNAL_BUF: Aligned<[i16; 32]> = Aligned([0i16; 32]); + static_mute_buf: Aligned<[i16; 32]> = Aligned([0i16; 32]), + static_signal_buf: Aligned<[i16; 32]> = Aligned([0i16; 32]), + queue: Option> = None, + ])] + fn init(mut ctx: init::Context) -> (Shared, Local, init::Monotonics) { + let signal_buf = ctx.local.static_signal_buf; + let mute_buf = ctx.local.static_mute_buf; // Fill signal buffer with triangle waveform, 2 channels interleaved - let len = SIGNAL_BUF.0.len() / 2; + let len = signal_buf.0.len() / 2; for x in 0..len { - SIGNAL_BUF.0[2 * x] = triangle_wave(x as i32, len, 2048, 0, 1) as i16; - SIGNAL_BUF.0[2 * x + 1] = triangle_wave(x as i32, len, 2048, 0, 1) as i16; + signal_buf.0[2 * x] = triangle_wave(x as i32, len, 2048, 0, 1) as i16; + signal_buf.0[2 * x + 1] = triangle_wave(x as i32, len, 2048, 0, 1) as i16; } let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); @@ -107,37 +111,41 @@ const APP: () = { Baudrate::BAUD115200, ); - *ctx.resources.queue = Some(Queue::new()); - let (producer, consumer) = ctx.resources.queue.as_mut().unwrap().split(); + *ctx.local.queue = Some(Queue::new()); + let (producer, consumer) = ctx.local.queue.as_mut().unwrap().split(); + + let mono = Mono::new(ctx.core.SYST, 64_000_000); rprintln!("Morse code generator"); rprintln!("Send me text over UART @ 115_200 baud"); rprintln!("Press button 1 to slow down or button 2 to speed up"); - ctx.spawn.tick().ok(); - - init::LateResources { - producer, - consumer, - gpiote, - btn1, - btn2, - led: p0.p0_13.into_push_pull_output(Level::High).degrade(), - uarte, - uarte_timer: Timer::new(ctx.device.TIMER0), - transfer: i2s.tx(&MUTE_BUF.0).ok(), - signal_buf: &SIGNAL_BUF.0, - mute_buf: &MUTE_BUF.0, - } + tick::spawn().ok(); + + ( + Shared { speed: 5_000_000 }, + Local { + producer, + consumer, + gpiote, + btn1, + btn2, + led: p0.p0_13.into_push_pull_output(Level::High).degrade(), + uarte, + uarte_timer: Timer::new(ctx.device.TIMER0), + transfer: i2s.tx(&mute_buf.0).ok(), + signal_buf: &signal_buf.0, + mute_buf: &mute_buf.0, + }, + init::Monotonics(mono), + ) } - #[idle(resources=[uarte, uarte_timer, producer])] + #[idle(local = [uarte, uarte_timer, producer])] fn idle(ctx: idle::Context) -> ! { - let idle::Resources { - uarte, - uarte_timer, - producer, - } = ctx.resources; + let uarte = ctx.local.uarte; + let uarte_timer = ctx.local.uarte_timer; + let producer = ctx.local.producer; let uarte_rx_buf = &mut [0u8; 64][..]; loop { match uarte.read_timeout(uarte_rx_buf, uarte_timer, 100_000) { @@ -166,49 +174,43 @@ const APP: () = { } } - #[task(resources = [consumer, transfer, signal_buf, mute_buf, led, speed], schedule = [tick])] - fn tick(ctx: tick::Context) { - let (_buf, i2s) = ctx.resources.transfer.take().unwrap().wait(); - match ctx.resources.consumer.dequeue() { + #[task(local = [consumer, transfer, signal_buf, mute_buf, led], shared = [speed])] + fn tick(mut ctx: tick::Context) { + let (_buf, i2s) = ctx.local.transfer.take().unwrap().wait(); + match ctx.local.consumer.dequeue() { Some(State::On) => { // Move TX pointer to signal buffer (sound ON) - *ctx.resources.transfer = i2s.tx(*ctx.resources.signal_buf).ok(); - ctx.resources.led.set_low().ok(); + *ctx.local.transfer = i2s.tx(*ctx.local.signal_buf).ok(); + ctx.local.led.set_low().ok(); } _ => { // Move TX pointer to silent buffer (sound OFF) - *ctx.resources.transfer = i2s.tx(*ctx.resources.mute_buf).ok(); - ctx.resources.led.set_high().ok(); + *ctx.local.transfer = i2s.tx(*ctx.local.mute_buf).ok(); + ctx.local.led.set_high().ok(); } } - ctx.schedule - .tick(ctx.scheduled + ctx.resources.speed.cycles()) - .ok(); + let speed: u64 = ctx.shared.speed.lock(|speed| *speed).into(); + tick::spawn_after(speed.micros()).ok(); } - #[task(binds = GPIOTE, resources = [gpiote, speed], schedule = [debounce])] + #[task(binds = GPIOTE, local = [gpiote])] fn on_gpiote(ctx: on_gpiote::Context) { - ctx.resources.gpiote.reset_events(); - ctx.schedule.debounce(ctx.start + 3_000_000.cycles()).ok(); + ctx.local.gpiote.reset_events(); + debounce::spawn_after(50.millis()).ok(); } - #[task(resources = [btn1, btn2, speed])] - fn debounce(ctx: debounce::Context) { - if ctx.resources.btn1.is_low().unwrap() { + #[task(local = [btn1, btn2], shared = [speed])] + fn debounce(mut ctx: debounce::Context) { + if ctx.local.btn1.is_low().unwrap() { rprintln!("Go slower"); - *ctx.resources.speed += 600_000; + ctx.shared.speed.lock(|speed| *speed += 600_000); } - if ctx.resources.btn2.is_low().unwrap() { + if ctx.local.btn2.is_low().unwrap() { rprintln!("Go faster"); - *ctx.resources.speed -= 600_000; + ctx.shared.speed.lock(|speed| *speed -= 600_000); } } - - extern "C" { - fn SWI0_EGU0(); - fn SWI1_EGU1(); - } -}; +} fn triangle_wave(x: i32, length: usize, amplitude: i32, phase: i32, periods: i32) -> i32 { let length = length as i32; @@ -225,7 +227,5 @@ fn triangle_wave(x: i32, length: usize, amplitude: i32, phase: i32, periods: i32 fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/examples/i2s-peripheral-demo/Cargo.toml b/examples/i2s-peripheral-demo/Cargo.toml index a86133b6..2bed9e41 100644 --- a/examples/i2s-peripheral-demo/Cargo.toml +++ b/examples/i2s-peripheral-demo/Cargo.toml @@ -10,7 +10,7 @@ publish = false [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } +cortex-m-rtic = { version = "1.0.0", default-features = false } rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } diff --git a/examples/i2s-peripheral-demo/src/main.rs b/examples/i2s-peripheral-demo/src/main.rs index 82ba174e..7d82ec15 100644 --- a/examples/i2s-peripheral-demo/src/main.rs +++ b/examples/i2s-peripheral-demo/src/main.rs @@ -4,41 +4,44 @@ // I2S `peripheral mode` demo // Signal average level indicator using an RGB LED (APA102 on ItsyBitsy nRF52840) -use embedded_hal::blocking::spi::Write; -use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::{ +use {core::panic::PanicInfo, nrf52840_hal as hal, rtt_target::rprintln}; + +#[repr(align(4))] +pub struct Aligned(T); + +#[rtic::app(device = crate::hal::pac, peripherals = true)] +mod app { + use crate::hal::{ + self, gpio::Level, i2s::{self, *}, pac::SPIM0, spim::{self, Frequency, Mode as SPIMode, Phase, Polarity, Spim}, - }, - nrf52840_hal as hal, - rtt_target::{rprintln, rtt_init_print}, -}; + }; + use crate::Aligned; + use embedded_hal::blocking::spi::Write; + use rtt_target::{rprintln, rtt_init_print}; -#[repr(align(4))] -struct Aligned(T); + const OFF: [u8; 9] = [0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF]; + const GREEN: [u8; 9] = [0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10, 0x00, 0xFF]; + const ORANGE: [u8; 9] = [0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10, 0x10, 0xFF]; + const RED: [u8; 9] = [0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x10, 0xFF]; -const OFF: [u8; 9] = [0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF]; -const GREEN: [u8; 9] = [0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10, 0x00, 0xFF]; -const ORANGE: [u8; 9] = [0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10, 0x10, 0xFF]; -const RED: [u8; 9] = [0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x10, 0xFF]; + #[shared] + struct Shared {} -#[rtic::app(device = crate::hal::pac, peripherals = true)] -const APP: () = { - struct Resources { + #[local] + struct Local { rgb: Spim, transfer: Option>, } - #[init] - fn init(ctx: init::Context) -> init::LateResources { + #[init(local = [ // The I2S buffer address must be 4 byte aligned. - static mut RX_BUF: Aligned<[i16; 128]> = Aligned([0; 128]); + RX_BUF: Aligned<[i16; 128]> = Aligned([0; 128]), + ])] + fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) { + let RX_BUF = ctx.local.RX_BUF; let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); rtt_init_print!(); @@ -78,15 +81,19 @@ const APP: () = { }, 0, ); - init::LateResources { - rgb, - transfer: i2s.rx(&mut RX_BUF.0).ok(), - } + ( + Shared {}, + Local { + rgb, + transfer: i2s.rx(&mut RX_BUF.0).ok(), + }, + init::Monotonics(), + ) } - #[task(binds = I2S, resources = [rgb, transfer])] + #[task(binds = I2S, local = [rgb, transfer])] fn on_i2s(ctx: on_i2s::Context) { - let (rx_buf, i2s) = ctx.resources.transfer.take().unwrap().wait(); + let (rx_buf, i2s) = ctx.local.transfer.take().unwrap().wait(); if i2s.is_event_triggered(I2SEvent::RxPtrUpdated) { i2s.reset_event(I2SEvent::RxPtrUpdated); // Calculate mono summed average of received buffer @@ -98,18 +105,16 @@ const APP: () = { 10_338..=16_383 => &ORANGE, _ => &RED, }; - as Write>::write(ctx.resources.rgb, color).ok(); + as Write>::write(ctx.local.rgb, color).ok(); } - *ctx.resources.transfer = i2s.rx(rx_buf).ok(); + *ctx.local.transfer = i2s.rx(rx_buf).ok(); } -}; +} #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/examples/lpcomp-demo/Cargo.toml b/examples/lpcomp-demo/Cargo.toml index 157dfe4f..a7dc3d8e 100644 --- a/examples/lpcomp-demo/Cargo.toml +++ b/examples/lpcomp-demo/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } +cortex-m-rtic = { version = "1.0.0", default-features = false } rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } diff --git a/examples/lpcomp-demo/src/main.rs b/examples/lpcomp-demo/src/main.rs index 33129b4d..441a80db 100644 --- a/examples/lpcomp-demo/src/main.rs +++ b/examples/lpcomp-demo/src/main.rs @@ -1,25 +1,27 @@ #![no_std] #![no_main] -use embedded_hal::digital::v2::OutputPin; -use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::{ - gpio::{Level, Output, Pin, PushPull}, - gpiote::Gpiote, - lpcomp::*, - pac::POWER, - }, - nrf52840_hal as hal, - rtt_target::{rprintln, rtt_init_print}, -}; +use {core::panic::PanicInfo, nrf52840_hal as hal, rtt_target::rprintln}; #[rtic::app(device = crate::hal::pac, peripherals = true)] -const APP: () = { - struct Resources { +mod app { + use embedded_hal::digital::v2::OutputPin; + use { + hal::{ + gpio::{Level, Output, Pin, PushPull}, + gpiote::Gpiote, + lpcomp::*, + pac::POWER, + }, + nrf52840_hal as hal, + rtt_target::{rprintln, rtt_init_print}, + }; + + #[shared] + struct Shared {} + + #[local] + struct Local { gpiote: Gpiote, led1: Pin>, lpcomp: LpComp, @@ -27,7 +29,7 @@ const APP: () = { } #[init] - fn init(ctx: init::Context) -> init::LateResources { + fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) { let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); rtt_init_print!(); @@ -73,47 +75,39 @@ const APP: () = { rprintln!("Press button 1 to shut down"); - init::LateResources { - gpiote, - led1, - lpcomp, - power: ctx.device.POWER, - } - } - - #[idle] - fn idle(_: idle::Context) -> ! { - loop { - cortex_m::asm::wfi(); - } + ( + Shared {}, + Local { + gpiote, + led1, + lpcomp, + power: ctx.device.POWER, + }, + init::Monotonics(), + ) } - #[task(binds = GPIOTE, resources = [gpiote, power])] + #[task(binds = GPIOTE, local = [gpiote, power])] fn on_gpiote(ctx: on_gpiote::Context) { - ctx.resources.gpiote.reset_events(); + ctx.local.gpiote.reset_events(); rprintln!("Power OFF"); - ctx.resources - .power - .systemoff - .write(|w| w.systemoff().enter()); + ctx.local.power.systemoff.write(|w| w.systemoff().enter()); } - #[task(binds = COMP_LPCOMP, resources = [lpcomp, led1])] + #[task(binds = COMP_LPCOMP, local = [lpcomp, led1])] fn on_comp(ctx: on_comp::Context) { - ctx.resources.lpcomp.reset_events(); - match ctx.resources.lpcomp.read() { - CompResult::Above => ctx.resources.led1.set_low().ok(), - CompResult::Below => ctx.resources.led1.set_high().ok(), + ctx.local.lpcomp.reset_events(); + match ctx.local.lpcomp.read() { + CompResult::Above => ctx.local.led1.set_low().ok(), + CompResult::Below => ctx.local.led1.set_high().ok(), }; } -}; +} #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/examples/nvmc-demo/Cargo.toml b/examples/nvmc-demo/Cargo.toml index 892a128a..05206680 100644 --- a/examples/nvmc-demo/Cargo.toml +++ b/examples/nvmc-demo/Cargo.toml @@ -11,7 +11,7 @@ publish = false cortex-m = "0.7.3" cortex-m-rt = "0.7.0" embedded-storage = "0.2.0" -rtt-target = {version = "0.2.0", features = ["cortex-m"] } +rtt-target = { version = "0.3.1", features = ["cortex-m"] } panic-probe = { version = "0.3.0", features = ["print-rtt"] } [dependencies.embedded-hal] diff --git a/examples/ppi-demo/Cargo.toml b/examples/ppi-demo/Cargo.toml index 1008fc9c..ac40c497 100644 --- a/examples/ppi-demo/Cargo.toml +++ b/examples/ppi-demo/Cargo.toml @@ -8,7 +8,7 @@ publish = false [dependencies] cortex-m = "0.7.3" cortex-m-rt = "0.7.0" -rtt-target = {version = "0.2.0", features = ["cortex-m"] } +rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52810-hal = { path = "../../nrf52810-hal", features = ["rt"], optional = true } nrf52811-hal = { path = "../../nrf52811-hal", features = ["rt"], optional = true } diff --git a/examples/pwm-blinky-demo/Cargo.toml b/examples/pwm-blinky-demo/Cargo.toml index dcc4668d..d8278a82 100644 --- a/examples/pwm-blinky-demo/Cargo.toml +++ b/examples/pwm-blinky-demo/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] cortex-m = "0.7.3" cortex-m-rt = "0.7.0" -rtt-target = {version = "0.2.0", features = ["cortex-m"] } +rtt-target = { version = "0.3.1", features = ["cortex-m"] } nb = "1.0.0" [dependencies.embedded-hal] diff --git a/examples/pwm-demo/Cargo.toml b/examples/pwm-demo/Cargo.toml index 281f9c56..a3189f7f 100644 --- a/examples/pwm-demo/Cargo.toml +++ b/examples/pwm-demo/Cargo.toml @@ -9,7 +9,8 @@ edition = "2018" [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } +cortex-m-rtic = { version = "1.0.0", default-features = false } +systick-monotonic = "1.0.0" rtt-target = { version = "0.3.1", features = ["cortex-m"] } [dependencies.embedded-hal] @@ -19,7 +20,3 @@ features = ["unproven"] [dependencies.nrf52840-hal] features = ["rt"] path = "../../nrf52840-hal" -optional = true - -[features] -52840 = ["nrf52840-hal"] diff --git a/examples/pwm-demo/src/main.rs b/examples/pwm-demo/src/main.rs index 4b21b648..49488dc7 100644 --- a/examples/pwm-demo/src/main.rs +++ b/examples/pwm-demo/src/main.rs @@ -1,56 +1,65 @@ #![no_std] #![no_main] -use embedded_hal::digital::v2::InputPin; -use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::{ - gpio::{p0::Parts, Input, Level, Pin, PullUp}, - gpiote::Gpiote, - pac::PWM0, - pwm::*, - time::*, - }, - nrf52840_hal as hal, - rtic::cyccnt::U32Ext as _, - rtt_target::{rprintln, rtt_init_print}, -}; +use {core::panic::PanicInfo, nrf52840_hal as hal, rtt_target::rprintln}; -#[derive(Debug, PartialEq)] -pub enum AppStatus { - Idle, - Demo1A, - Demo1B, - Demo1C, - Demo2A, - Demo2B, - Demo2C, - Demo3, - Demo4, -} +#[rtic::app(device = crate::hal::pac, peripherals = true, dispatchers = [SWI0_EGU0])] +mod app { + use embedded_hal::digital::v2::InputPin; + use { + hal::{ + gpio::{p0::Parts, Input, Level, Pin, PullUp}, + gpiote::Gpiote, + pac::PWM0, + pwm::*, + time::*, + }, + nrf52840_hal as hal, + rtt_target::{rprintln, rtt_init_print}, + systick_monotonic::*, + }; + + #[monotonic(binds = SysTick, default = true)] + type Mono = Systick<1_000_000>; -type SeqBuffer = &'static mut [u16; 48]; + #[shared] + struct Shared { + #[lock_free] + pwm: Option>, + } -#[rtic::app(device = crate::hal::pac, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { - struct Resources { + #[local] + struct Local { gpiote: Gpiote, btn1: Pin>, btn2: Pin>, btn3: Pin>, btn4: Pin>, - #[init(AppStatus::Idle)] status: AppStatus, - pwm: Option>, } - #[init] - fn init(mut ctx: init::Context) -> init::LateResources { - static mut BUF0: [u16; 48] = [0u16; 48]; - static mut BUF1: [u16; 48] = [0u16; 48]; + #[derive(Debug, PartialEq)] + pub enum AppStatus { + Idle, + Demo1A, + Demo1B, + Demo1C, + Demo2A, + Demo2B, + Demo2C, + Demo3, + Demo4, + } + + type SeqBuffer = &'static mut [u16; 48]; + + #[init(local = [ + BUF0: [u16; 48] = [0u16; 48], + BUF1: [u16; 48] = [0u16; 48], + ])] + fn init(mut ctx: init::Context) -> (Shared, Local, init::Monotonics) { + let BUF0 = ctx.local.BUF0; + let BUF1 = ctx.local.BUF1; let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); ctx.core.DCB.enable_trace(); @@ -89,14 +98,22 @@ const APP: () = { gpiote.port().input_pin(&btn4).low(); gpiote.port().enable_interrupt(); - init::LateResources { - gpiote, - btn1, - btn2, - btn3, - btn4, - pwm: pwm.load(Some(BUF0), Some(BUF1), false).ok(), - } + let mono = Mono::new(ctx.core.SYST, 64_000_000); + + ( + Shared { + pwm: pwm.load(Some(BUF0), Some(BUF1), false).ok(), + }, + Local { + gpiote, + btn1, + btn2, + btn3, + btn4, + status: AppStatus::Idle, + }, + init::Monotonics(mono), + ) } #[idle] @@ -107,24 +124,24 @@ const APP: () = { } } - #[task(binds = PWM0, resources = [pwm])] + #[task(binds = PWM0, shared = [pwm])] fn on_pwm(ctx: on_pwm::Context) { - let pwm_seq = ctx.resources.pwm.as_ref().unwrap(); + let pwm_seq = ctx.shared.pwm.as_ref().unwrap(); if pwm_seq.is_event_triggered(PwmEvent::Stopped) { pwm_seq.reset_event(PwmEvent::Stopped); rprintln!("PWM generation was stopped"); } } - #[task(binds = GPIOTE, resources = [gpiote], schedule = [debounce])] + #[task(binds = GPIOTE, local = [gpiote])] fn on_gpiote(ctx: on_gpiote::Context) { - ctx.resources.gpiote.reset_events(); - ctx.schedule.debounce(ctx.start + 2_500_000.cycles()).ok(); + ctx.local.gpiote.reset_events(); + debounce::spawn_after(50.millis()).ok(); } - #[task(resources = [btn1, btn2, btn3, btn4, pwm, status])] + #[task(shared = [pwm], local = [btn1, btn2, btn3, btn4, status])] fn debounce(ctx: debounce::Context) { - let (buf0, buf1, pwm) = ctx.resources.pwm.take().unwrap().split(); + let (buf0, buf1, pwm) = ctx.shared.pwm.take().unwrap().split(); let BUF0 = buf0.unwrap(); let BUF1 = buf1.unwrap(); @@ -132,8 +149,8 @@ const APP: () = { let (ch0, ch1, ch2, ch3) = pwm.split_channels(); let (grp0, grp1) = pwm.split_groups(); - let status = ctx.resources.status; - if ctx.resources.btn1.is_low().unwrap() { + let status = ctx.local.status; + if ctx.local.btn1.is_low().unwrap() { match status { AppStatus::Demo1B => { rprintln!("DEMO 1C: Individual channel duty cycle"); @@ -155,8 +172,8 @@ const APP: () = { pwm.set_duty_on_common(max_duty / 10); } } - *ctx.resources.pwm = pwm.load(Some(BUF0), Some(BUF1), false).ok(); - } else if ctx.resources.btn2.is_low().unwrap() { + *ctx.shared.pwm = pwm.load(Some(BUF0), Some(BUF1), false).ok(); + } else if ctx.local.btn2.is_low().unwrap() { match status { AppStatus::Demo2B => { rprintln!("DEMO 2C: Play grouped sequence 4 times"); @@ -174,7 +191,7 @@ const APP: () = { .set_seq_refresh(Seq::Seq0, 30) // Playback rate (periods per step) .set_seq_refresh(Seq::Seq1, 10) .repeat(4); - *ctx.resources.pwm = pwm.load(Some(BUF0), Some(BUF1), true).ok(); + *ctx.shared.pwm = pwm.load(Some(BUF0), Some(BUF1), true).ok(); } AppStatus::Demo2A => { rprintln!("DEMO 2B: Loop individual sequences"); @@ -194,7 +211,7 @@ const APP: () = { .set_seq_refresh(Seq::Seq0, 30) .set_seq_refresh(Seq::Seq1, 30) .loop_inf(); - *ctx.resources.pwm = pwm.load(Some(BUF0), Some(BUF1), true).ok(); + *ctx.shared.pwm = pwm.load(Some(BUF0), Some(BUF1), true).ok(); } _ => { rprintln!("DEMO 2A: Play common sequence once"); @@ -210,10 +227,10 @@ const APP: () = { .set_seq_refresh(Seq::Seq0, 20) .set_seq_refresh(Seq::Seq1, 20) .one_shot(); - *ctx.resources.pwm = pwm.load(Some(BUF0), Some(BUF1), true).ok(); + *ctx.shared.pwm = pwm.load(Some(BUF0), Some(BUF1), true).ok(); } } - } else if ctx.resources.btn3.is_low().unwrap() { + } else if ctx.local.btn3.is_low().unwrap() { match status { AppStatus::Demo3 => { rprintln!("DEMO 3: Next step"); @@ -224,7 +241,7 @@ const APP: () = { pwm.stop(); *status = AppStatus::Idle; } - *ctx.resources.pwm = pwm.load(Some(BUF0), Some(BUF1), false).ok(); + *ctx.shared.pwm = pwm.load(Some(BUF0), Some(BUF1), false).ok(); } _ => { rprintln!("DEMO 3: Manually step through sequence"); @@ -239,10 +256,10 @@ const APP: () = { pwm.set_load_mode(LoadMode::Common) .set_step_mode(StepMode::NextStep) .loop_inf(); - *ctx.resources.pwm = pwm.load(Some(BUF0), Some(BUF1), true).ok(); + *ctx.shared.pwm = pwm.load(Some(BUF0), Some(BUF1), true).ok(); } } - } else if ctx.resources.btn4.is_low().unwrap() { + } else if ctx.local.btn4.is_low().unwrap() { rprintln!("DEMO 4: Waveform mode"); *status = AppStatus::Demo4; let len = BUF0.len() / 4; @@ -261,21 +278,17 @@ const APP: () = { .set_seq_refresh(Seq::Seq0, 150) .set_seq_refresh(Seq::Seq1, 150) .loop_inf(); - *ctx.resources.pwm = pwm.load(Some(BUF0), Some(BUF1), true).ok(); + *ctx.shared.pwm = pwm.load(Some(BUF0), Some(BUF1), true).ok(); } else { - *ctx.resources.pwm = pwm.load(Some(BUF0), Some(BUF1), false).ok(); + *ctx.shared.pwm = pwm.load(Some(BUF0), Some(BUF1), false).ok(); } } - extern "C" { - fn SWI0_EGU0(); + fn triangle_wave(x: usize, length: usize, ampl: i32, phase: i32, y_offset: i32) -> i32 { + let x = x as i32; + let length = length as i32; + ampl - ((2 * (x + phase) * ampl / length) % (2 * ampl) - ampl).abs() + y_offset } -}; - -fn triangle_wave(x: usize, length: usize, ampl: i32, phase: i32, y_offset: i32) -> i32 { - let x = x as i32; - let length = length as i32; - ampl - ((2 * (x + phase) * ampl / length) % (2 * ampl) - ampl).abs() + y_offset } #[inline(never)] @@ -283,7 +296,5 @@ fn triangle_wave(x: usize, length: usize, ampl: i32, phase: i32, y_offset: i32) fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/examples/qdec-demo/Cargo.toml b/examples/qdec-demo/Cargo.toml index 87712ea5..1fb2e14e 100644 --- a/examples/qdec-demo/Cargo.toml +++ b/examples/qdec-demo/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } +cortex-m-rtic = { version = "1.0.0", default-features = false } rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } diff --git a/examples/qdec-demo/src/main.rs b/examples/qdec-demo/src/main.rs index 1b638131..252462c5 100644 --- a/examples/qdec-demo/src/main.rs +++ b/examples/qdec-demo/src/main.rs @@ -1,26 +1,27 @@ #![no_std] #![no_main] -use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::qdec::*, - nrf52840_hal as hal, - rtt_target::{rprintln, rtt_init_print}, -}; +use {core::panic::PanicInfo, nrf52840_hal as hal, rtt_target::rprintln}; #[rtic::app(device = crate::hal::pac, peripherals = true)] -const APP: () = { - struct Resources { +mod app { + use { + hal::qdec::*, + nrf52840_hal as hal, + rtt_target::{rprintln, rtt_init_print}, + }; + + #[shared] + struct Shared {} + + #[local] + struct Local { qdec: Qdec, - #[init(0)] value: i16, } #[init] - fn init(ctx: init::Context) -> init::LateResources { + fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) { let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); rtt_init_print!(); @@ -36,23 +37,21 @@ const APP: () = { .enable_interrupt(NumSamples::_1smpl) .enable(); - init::LateResources { qdec } + (Shared {}, Local { qdec, value: 0 }, init::Monotonics()) } - #[task(binds = QDEC, resources = [qdec, value])] + #[task(binds = QDEC, local = [qdec, value])] fn on_qdec(ctx: on_qdec::Context) { - ctx.resources.qdec.reset_events(); - *ctx.resources.value += ctx.resources.qdec.read(); - rprintln!("Value: {}", ctx.resources.value); + ctx.local.qdec.reset_events(); + *ctx.local.value += ctx.local.qdec.read(); + rprintln!("Value: {}", ctx.local.value); } -}; +} #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/examples/rtc-demo/Cargo.toml b/examples/rtc-demo/Cargo.toml index 37fd5747..4b6d166b 100644 --- a/examples/rtc-demo/Cargo.toml +++ b/examples/rtc-demo/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] cortex-m = "0.7.3" cortex-m-rt = "0.7.0" -rtt-target = {version = "0.2.0", features = ["cortex-m"] } +rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } [dependencies.embedded-hal] diff --git a/examples/rtic-demo/Cargo.toml b/examples/rtic-demo/Cargo.toml index fdf43d36..249bb3ed 100644 --- a/examples/rtic-demo/Cargo.toml +++ b/examples/rtic-demo/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" publish = false [dependencies] -cortex-m-rtic = "0.5.9" +cortex-m-rtic = "1.0.0" panic-semihosting = "0.5.3" cortex-m-semihosting = "0.3.5" diff --git a/examples/rtic-demo/src/main.rs b/examples/rtic-demo/src/main.rs index 6b5e7b98..28b040fa 100644 --- a/examples/rtic-demo/src/main.rs +++ b/examples/rtic-demo/src/main.rs @@ -4,7 +4,6 @@ #[allow(unused_imports)] use panic_semihosting; -use cortex_m_semihosting::{debug, hprintln}; use rtic::app; #[cfg(feature = "51")] @@ -23,10 +22,20 @@ use nrf52832_hal as hal; use nrf52840_hal as hal; #[app(device = crate::hal::pac)] -const APP: () = { +mod app { + use cortex_m_semihosting::{debug, hprintln}; + + #[shared] + struct Shared {} + + #[local] + struct Local {} + #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> (Shared, Local, init::Monotonics) { hprintln!("init").unwrap(); + + (Shared {}, Local {}, init::Monotonics()) } #[idle] @@ -35,8 +44,6 @@ const APP: () = { debug::exit(debug::EXIT_SUCCESS); - loop { - continue; - } + loop {} } -}; +} diff --git a/examples/spi-demo/Cargo.toml b/examples/spi-demo/Cargo.toml index fa032e78..9741dc56 100644 --- a/examples/spi-demo/Cargo.toml +++ b/examples/spi-demo/Cargo.toml @@ -8,7 +8,7 @@ publish = false [dependencies] cortex-m-rt = "0.7.0" panic-halt = "0.2.0" -embedded-hal-spy = "0.0.3" +embedded-hal-spy = "0.0.5" [dependencies.embedded-hal] features = ["unproven"] @@ -18,10 +18,6 @@ version = "0.2" path = "../../nrf52832-hal" optional = true -[dependencies.nrf52832-pac] -version = "0.9.0" -optional = true - [features] -52832 = ["nrf52832-hal", "nrf52832-pac"] +52832 = ["nrf52832-hal"] default = ["52832"] diff --git a/examples/spis-demo/Cargo.toml b/examples/spis-demo/Cargo.toml index d3813af7..770f1dd6 100644 --- a/examples/spis-demo/Cargo.toml +++ b/examples/spis-demo/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } +cortex-m-rtic = { version = "1.0.0", default-features = false } rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } diff --git a/examples/spis-demo/src/main.rs b/examples/spis-demo/src/main.rs index 104a8e4a..f0b85f86 100644 --- a/examples/spis-demo/src/main.rs +++ b/examples/spis-demo/src/main.rs @@ -4,26 +4,32 @@ // Demo for the SPIS module, transmitting the current buffer contents while receiving new data. // Press button to zero the buffer. -use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::{gpiote::Gpiote, pac::SPIS0, spis::*}, - nrf52840_hal as hal, - rtt_target::{rprintln, rtt_init_print}, -}; +use {core::panic::PanicInfo, nrf52840_hal as hal, rtt_target::rprintln}; #[rtic::app(device = crate::hal::pac, peripherals = true)] -const APP: () = { - struct Resources { - gpiote: Gpiote, +mod app { + use { + hal::{gpiote::Gpiote, pac::SPIS0, spis::*}, + nrf52840_hal as hal, + rtt_target::{rprintln, rtt_init_print}, + }; + + #[shared] + struct Shared { + #[lock_free] transfer: Option>, } - #[init] - fn init(ctx: init::Context) -> init::LateResources { - static mut BUF: [u8; 8] = [0; 8]; + #[local] + struct Local { + gpiote: Gpiote, + } + + #[init(local = [ + BUF: [u8; 8] = [0; 8], + ])] + fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) { + let BUF = ctx.local.BUF; let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); rtt_init_print!(); @@ -52,37 +58,38 @@ const APP: () = { gpiote.port().input_pin(&btn).low(); gpiote.port().enable_interrupt(); - init::LateResources { - gpiote, - transfer: spis.transfer(BUF).ok(), - } + ( + Shared { + transfer: spis.transfer(BUF).ok(), + }, + Local { gpiote }, + init::Monotonics(), + ) } - #[task(binds = GPIOTE, resources = [gpiote, transfer])] + #[task(binds = GPIOTE, local = [gpiote], shared = [transfer])] fn on_gpiote(ctx: on_gpiote::Context) { - ctx.resources.gpiote.reset_events(); + ctx.local.gpiote.reset_events(); rprintln!("Reset buffer"); - let (buf, spis) = ctx.resources.transfer.take().unwrap().wait(); + let (buf, spis) = ctx.shared.transfer.take().unwrap().wait(); buf.copy_from_slice(&[0; 8][..]); rprintln!("{:?}", buf); - *ctx.resources.transfer = spis.transfer(buf).ok(); + *ctx.shared.transfer = spis.transfer(buf).ok(); } - #[task(binds = SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0, resources = [transfer])] + #[task(binds = SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0, shared = [transfer])] fn on_spis(ctx: on_spis::Context) { - let (buf, spis) = ctx.resources.transfer.take().unwrap().wait(); + let (buf, spis) = ctx.shared.transfer.take().unwrap().wait(); spis.reset_event(SpisEvent::End); rprintln!("Received: {:?}", buf); - *ctx.resources.transfer = spis.transfer(buf).ok(); + *ctx.shared.transfer = spis.transfer(buf).ok(); } -}; +} #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/examples/twim-demo/Cargo.toml b/examples/twim-demo/Cargo.toml index 634d2463..029a4214 100644 --- a/examples/twim-demo/Cargo.toml +++ b/examples/twim-demo/Cargo.toml @@ -9,7 +9,8 @@ edition = "2018" [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } +cortex-m-rtic = { version = "1.0.0", default-features = false } +systick-monotonic = "1.0.0" rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } diff --git a/examples/twim-demo/src/main.rs b/examples/twim-demo/src/main.rs index 7e6186bf..1bf7314a 100644 --- a/examples/twim-demo/src/main.rs +++ b/examples/twim-demo/src/main.rs @@ -1,30 +1,35 @@ #![no_std] #![no_main] -#[allow(unused_imports)] -use embedded_hal::blocking::i2c::{Read, Write}; -use embedded_hal::digital::v2::InputPin; -use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::{ - gpio::{p0::Parts, Input, Pin, PullUp}, - gpiote::Gpiote, - pac::TWIM0, - twim::*, - }, - nrf52840_hal as hal, - rtic::cyccnt::U32Ext, - rtt_target::{rprintln, rtt_init_print}, -}; +use {core::panic::PanicInfo, nrf52840_hal as hal, rtt_target::rprintln}; -#[rtic::app(device = crate::hal::pac, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { - struct Resources { - twim: Twim, +#[rtic::app(device = crate::hal::pac, peripherals = true, dispatchers = [SWI0_EGU0])] +mod app { + use embedded_hal::digital::v2::InputPin; + use systick_monotonic::*; + use { + hal::{ + gpio::{p0::Parts, Input, Pin, PullUp}, + gpiote::Gpiote, + pac::TWIM0, + twim::*, + }, + nrf52840_hal as hal, + rtt_target::{rprintln, rtt_init_print}, + }; + + #[monotonic(binds = SysTick, default = true)] + type Mono = Systick<1_000_000>; + + #[shared] + struct Shared { + #[lock_free] gpiote: Gpiote, + } + + #[local] + struct Local { + twim: Twim, btn1: Pin>, btn2: Pin>, btn3: Pin>, @@ -32,7 +37,7 @@ const APP: () = { } #[init] - fn init(mut ctx: init::Context) -> init::LateResources { + fn init(mut ctx: init::Context) -> (Shared, Local, init::Monotonics) { let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); ctx.core.DCB.enable_trace(); ctx.core.DWT.enable_cycle_counter(); @@ -55,14 +60,18 @@ const APP: () = { let twim = Twim::new(ctx.device.TWIM0, Pins { scl, sda }, Frequency::K100); - init::LateResources { - twim, - gpiote, - btn1, - btn2, - btn3, - btn4, - } + let mono = Mono::new(ctx.core.SYST, 64_000_000); + ( + Shared { gpiote }, + Local { + twim, + btn1, + btn2, + btn3, + btn4, + }, + init::Monotonics(mono), + ) } #[idle] @@ -76,52 +85,46 @@ const APP: () = { } } - #[task(binds = GPIOTE, resources = [gpiote], schedule = [debounce])] + #[task(binds = GPIOTE, shared = [gpiote])] fn on_gpiote(ctx: on_gpiote::Context) { - ctx.resources.gpiote.reset_events(); - ctx.schedule.debounce(ctx.start + 3_000_000.cycles()).ok(); + ctx.shared.gpiote.reset_events(); + debounce::spawn_after(50.millis()).ok(); } - #[task(resources = [twim, gpiote, btn1, btn2, btn3, btn4])] + #[task(shared = [gpiote], local = [twim, btn1, btn2, btn3, btn4])] fn debounce(ctx: debounce::Context) { - let twim = ctx.resources.twim; - if ctx.resources.btn1.is_low().unwrap() { + let twim = ctx.local.twim; + if ctx.local.btn1.is_low().unwrap() { rprintln!("\nREAD from address 0x1A"); let rx_buf = &mut [0; 8][..]; let res = twim.read(0x1A, rx_buf); rprintln!("Result: {:?}\n{:?}", res, rx_buf); } - if ctx.resources.btn2.is_low().unwrap() { + if ctx.local.btn2.is_low().unwrap() { rprintln!("\nWRITE to address 0x1A"); let tx_buf = [1, 2, 3, 4, 5, 6, 7, 8]; let res = twim.write(0x1A, &tx_buf[..]); rprintln!("Result: {:?}\n{:?}", res, tx_buf); } - if ctx.resources.btn3.is_low().unwrap() { + if ctx.local.btn3.is_low().unwrap() { rprintln!("\nREAD from address 0x1B"); let rx_buf = &mut [0; 4][..]; let res = twim.read(0x1B, rx_buf); rprintln!("Result: {:?}\n{:?}", res, rx_buf); } - if ctx.resources.btn4.is_low().unwrap() { + if ctx.local.btn4.is_low().unwrap() { rprintln!("\nWRITE to address 0x1B"); let tx_buf = [9, 10, 11, 12]; let res = twim.write(0x1B, &tx_buf[..]); rprintln!("Result: {:?}\n{:?}", res, tx_buf); } } - - extern "C" { - fn SWI0_EGU0(); - } -}; +} #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/examples/twis-demo/Cargo.toml b/examples/twis-demo/Cargo.toml index 74feef4d..30e8495a 100644 --- a/examples/twis-demo/Cargo.toml +++ b/examples/twis-demo/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } +cortex-m-rtic = { version = "1.0.0", default-features = false } rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } diff --git a/examples/twis-demo/src/main.rs b/examples/twis-demo/src/main.rs index 36f7b130..b5095bec 100644 --- a/examples/twis-demo/src/main.rs +++ b/examples/twis-demo/src/main.rs @@ -1,39 +1,37 @@ #![no_std] #![no_main] -use embedded_hal::digital::v2::OutputPin; -use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::{ - gpio::{p0::Parts, Level, Output, Pin, PushPull}, - pac::TWIS0, - twis::*, - }, - nrf52840_hal as hal, - rtt_target::{rprintln, rtt_init_print}, -}; - -const ADDR0: u8 = 0x1A; -const ADDR1: u8 = 0x1B; -const BUF0_SZ: usize = 8; -const BUF1_SZ: usize = 4; +use {core::panic::PanicInfo, nrf52840_hal as hal, rtt_target::rprintln}; #[rtic::app(device = crate::hal::pac, peripherals = true)] -const APP: () = { - struct Resources { +mod app { + use embedded_hal::digital::v2::OutputPin; + use { + hal::{ + gpio::{p0::Parts, Level, Output, Pin, PushPull}, + pac::TWIS0, + twis::*, + }, + nrf52840_hal as hal, + rtt_target::{rprintln, rtt_init_print}, + }; + + const ADDR0: u8 = 0x1A; + const ADDR1: u8 = 0x1B; + const BUF0_SZ: usize = 8; + const BUF1_SZ: usize = 4; + + #[shared] + struct Shared {} + + #[local] + struct Local { twis: Twis, - #[init([0; BUF0_SZ])] - buffer0: [u8; BUF0_SZ], - #[init([0; BUF1_SZ])] - buffer1: [u8; BUF1_SZ], led: Pin>, } #[init] - fn init(ctx: init::Context) -> init::LateResources { + fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) { let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); rtt_init_print!(); @@ -48,7 +46,7 @@ const APP: () = { .enable_interrupt(TwiEvent::Read) // Trigger interrupt on READ command .enable(); - init::LateResources { twis, led } + (Shared {}, Local { twis, led }, init::Monotonics()) } #[idle] @@ -59,12 +57,15 @@ const APP: () = { } } - #[task(binds = SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0, resources = [twis, buffer0, buffer1, led])] + #[task(binds = SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0, local = [twis, led, + buffer0: [u8; BUF0_SZ] = [0; BUF0_SZ], + buffer1: [u8; BUF1_SZ] = [0; BUF1_SZ], + ])] fn on_twis(ctx: on_twis::Context) { - let twis = ctx.resources.twis; - let buffer0 = ctx.resources.buffer0; - let buffer1 = ctx.resources.buffer1; - let led = ctx.resources.led; + let twis = ctx.local.twis; + let buffer0 = ctx.local.buffer0; + let buffer1 = ctx.local.buffer1; + let led = ctx.local.led; if twis.is_event_triggered(TwiEvent::Read) { twis.reset_event(TwiEvent::Read); @@ -101,14 +102,12 @@ const APP: () = { } } } -}; +} #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/examples/twis-dma-demo/Cargo.toml b/examples/twis-dma-demo/Cargo.toml index 4453b99e..65bca3a9 100644 --- a/examples/twis-dma-demo/Cargo.toml +++ b/examples/twis-dma-demo/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } +cortex-m-rtic = { version = "1.0.0", default-features = false } rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } diff --git a/examples/twis-dma-demo/src/main.rs b/examples/twis-dma-demo/src/main.rs index b375cde3..75d2e9be 100644 --- a/examples/twis-dma-demo/src/main.rs +++ b/examples/twis-dma-demo/src/main.rs @@ -4,33 +4,40 @@ // Demo of using non-blocking DMA transactions with the // TWIS (Two Wire Interface/I2C in peripheral mode) module. -use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::{gpio::p0::Parts, gpiote::Gpiote, pac::TWIS0, twis::*}, - nrf52840_hal as hal, - rtt_target::{rprintln, rtt_init_print}, -}; - -type DmaBuffer = &'static mut [u8; 8]; - -pub enum TwisTransfer { - Running(Transfer), - Idle((DmaBuffer, Twis)), -} +use {core::panic::PanicInfo, nrf52840_hal as hal, rtt_target::rprintln}; #[rtic::app(device = crate::hal::pac, peripherals = true)] -const APP: () = { - struct Resources { - gpiote: Gpiote, +mod app { + use { + hal::{gpio::p0::Parts, gpiote::Gpiote, pac::TWIS0, twis::*}, + nrf52840_hal as hal, + rtt_target::{rprintln, rtt_init_print}, + }; + + type DmaBuffer = &'static mut [u8; 8]; + + pub enum TwisTransfer { + Running(Transfer), + Idle((DmaBuffer, Twis)), + } + + #[shared] + struct Shared { + #[lock_free] transfer: Option, } - #[init] - fn init(ctx: init::Context) -> init::LateResources { - static mut BUF: [u8; 8] = [0; 8]; + #[local] + struct Local { + gpiote: Gpiote, + } + + #[init(local = [ + BUF: [u8; 8] = [0; 8], + ])] + fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) { + let BUF = ctx.local.BUF; + let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); rtt_init_print!(); rprintln!("Waiting for commands from controller..."); @@ -50,17 +57,20 @@ const APP: () = { gpiote.port().input_pin(&btn).low(); gpiote.port().enable_interrupt(); - init::LateResources { - gpiote, - transfer: Some(TwisTransfer::Idle((BUF, twis))), - } + ( + Shared { + transfer: Some(TwisTransfer::Idle((BUF, twis))), + }, + Local { gpiote }, + init::Monotonics(), + ) } - #[task(binds = GPIOTE, resources = [gpiote, transfer])] + #[task(binds = GPIOTE, local = [gpiote], shared = [transfer])] fn on_gpiote(ctx: on_gpiote::Context) { - ctx.resources.gpiote.reset_events(); + ctx.local.gpiote.reset_events(); rprintln!("Reset buffer"); - let transfer = ctx.resources.transfer; + let transfer = ctx.shared.transfer; let (buf, twis) = match transfer.take().unwrap() { TwisTransfer::Running(t) => t.wait(), TwisTransfer::Idle(t) => t, @@ -70,9 +80,9 @@ const APP: () = { transfer.replace(TwisTransfer::Idle((buf, twis))); } - #[task(binds = SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0, resources = [transfer])] + #[task(binds = SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0, shared = [transfer])] fn on_twis(ctx: on_twis::Context) { - let transfer = ctx.resources.transfer; + let transfer = ctx.shared.transfer; let (buf, twis) = match transfer.take().unwrap() { TwisTransfer::Running(t) => t.wait(), TwisTransfer::Idle(t) => t, @@ -93,14 +103,12 @@ const APP: () = { transfer.replace(TwisTransfer::Idle((buf, twis))); } } -}; +} #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/examples/usb/Cargo.toml b/examples/usb/Cargo.toml index 83c56938..f5093817 100644 --- a/examples/usb/Cargo.toml +++ b/examples/usb/Cargo.toml @@ -8,7 +8,6 @@ publish = false [dependencies] cortex-m-rt = "0.7.0" panic-semihosting = "0.5.3" -nrf52840-pac = "0.10.1" usb-device = "0.2.7" usbd-serial = "0.1.0" diff --git a/examples/usb/src/bin/serial.rs b/examples/usb/src/bin/serial.rs index e64cdd6a..79e7f329 100644 --- a/examples/usb/src/bin/serial.rs +++ b/examples/usb/src/bin/serial.rs @@ -6,13 +6,12 @@ use panic_semihosting as _; use cortex_m_rt::entry; use nrf52840_hal::clocks::Clocks; use nrf52840_hal::usbd::{UsbPeripheral, Usbd}; -use nrf52840_pac::Peripherals; use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; use usbd_serial::{SerialPort, USB_CLASS_CDC}; #[entry] fn main() -> ! { - let periph = Peripherals::take().unwrap(); + let periph = nrf52840_hal::pac::Peripherals::take().unwrap(); let clocks = Clocks::new(periph.CLOCK); let clocks = clocks.enable_ext_hfosc(); diff --git a/examples/usb/src/bin/test_class.rs b/examples/usb/src/bin/test_class.rs index 0980d06c..9010f239 100644 --- a/examples/usb/src/bin/test_class.rs +++ b/examples/usb/src/bin/test_class.rs @@ -6,12 +6,11 @@ use panic_semihosting as _; use cortex_m_rt::entry; use nrf52840_hal::clocks::Clocks; use nrf52840_hal::usbd::{UsbPeripheral, Usbd}; -use nrf52840_pac::Peripherals; use usb_device::test_class::TestClass; #[entry] fn main() -> ! { - let periph = Peripherals::take().unwrap(); + let periph = nrf52840_hal::pac::Peripherals::take().unwrap(); let clocks = Clocks::new(periph.CLOCK); let clocks = clocks.enable_ext_hfosc(); diff --git a/examples/wdt-demo/Cargo.toml b/examples/wdt-demo/Cargo.toml index cff798d4..77a63721 100644 --- a/examples/wdt-demo/Cargo.toml +++ b/examples/wdt-demo/Cargo.toml @@ -10,7 +10,6 @@ publish = false [dependencies] cortex-m = "0.7.3" cortex-m-rt = { version = "0.7.0", features = ["device"] } -cortex-m-rtic = { version = "0.5.9", features = ["cortex-m-7"], default-features = false } rtt-target = { version = "0.3.1", features = ["cortex-m"] } nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } diff --git a/examples/wdt-demo/src/main.rs b/examples/wdt-demo/src/main.rs index 894cbd11..db1aaaf1 100644 --- a/examples/wdt-demo/src/main.rs +++ b/examples/wdt-demo/src/main.rs @@ -19,201 +19,145 @@ use embedded_hal::{ timer::CountDown, }; use { - core::{ - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, - }, - hal::pac::TIMER0, + core::panic::PanicInfo, hal::{ - gpio::{Input, Level, Output, Pin, PullUp, PushPull}, + gpio::Level, timer::Timer, - wdt::{count, handles::HdlN, Parts, Watchdog, WatchdogHandle}, + wdt::{count, Parts, Watchdog}, }, nrf52840_hal as hal, rtt_target::{rprintln, rtt_init_print}, }; -#[rtic::app(device = crate::hal::pac, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { - struct Resources { - btn1: Pin>, - btn2: Pin>, - btn3: Pin>, - btn4: Pin>, - led1: Pin>, - led2: Pin>, - led3: Pin>, - led4: Pin>, - hdl0: WatchdogHandle, - hdl1: WatchdogHandle, - hdl2: WatchdogHandle, - hdl3: WatchdogHandle, - timer: Timer, - } +#[cortex_m_rt::entry] +fn main() -> ! { + let p = hal::pac::Peripherals::take().unwrap(); + let mut core = cortex_m::Peripherals::take().unwrap(); + let _clocks = hal::clocks::Clocks::new(p.CLOCK).enable_ext_hfosc(); + rtt_init_print!(); + let p0 = hal::gpio::p0::Parts::new(p.P0); + + let btn1 = p0.p0_11.into_pullup_input().degrade(); + let btn2 = p0.p0_12.into_pullup_input().degrade(); + let btn3 = p0.p0_24.into_pullup_input().degrade(); + let btn4 = p0.p0_25.into_pullup_input().degrade(); + + let mut led1 = p0.p0_13.into_push_pull_output(Level::High).degrade(); + let mut led2 = p0.p0_14.into_push_pull_output(Level::High).degrade(); + let mut led3 = p0.p0_15.into_push_pull_output(Level::High).degrade(); + let mut led4 = p0.p0_16.into_push_pull_output(Level::High).degrade(); + + let mut timer = Timer::new(p.TIMER0); + + // Create a new watchdog instance + // + // In case the watchdog is already running, just spin and let it expire, since + // we can't configure it anyway. This usually happens when we first program + // the device and the watchdog was previously active + let (hdl0, hdl1, hdl2, hdl3) = match Watchdog::try_new(p.WDT) { + Ok(mut watchdog) => { + // Set the watchdog to timeout after 5 seconds (in 32.768kHz ticks) + watchdog.set_lfosc_ticks(5 * 32768); + + // Activate the watchdog with four handles + let Parts { + watchdog: _watchdog, + handles, + } = watchdog.activate::(); + + handles + } + Err(wdt) => match Watchdog::try_recover::(wdt) { + Ok(Parts { mut handles, .. }) => { + rprintln!("Oops, watchdog already active, but recovering!"); - #[init] - fn init(mut ctx: init::Context) -> init::LateResources { - let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); - rtt_init_print!(); - let p0 = hal::gpio::p0::Parts::new(ctx.device.P0); - - let btn1 = p0.p0_11.into_pullup_input().degrade(); - let btn2 = p0.p0_12.into_pullup_input().degrade(); - let btn3 = p0.p0_24.into_pullup_input().degrade(); - let btn4 = p0.p0_25.into_pullup_input().degrade(); - - let led1 = p0.p0_13.into_push_pull_output(Level::High).degrade(); - let led2 = p0.p0_14.into_push_pull_output(Level::High).degrade(); - let led3 = p0.p0_15.into_push_pull_output(Level::High).degrade(); - let led4 = p0.p0_16.into_push_pull_output(Level::High).degrade(); - - let timer = Timer::new(ctx.device.TIMER0); - - // Create a new watchdog instance - // - // In case the watchdog is already running, just spin and let it expire, since - // we can't configure it anyway. This usually happens when we first program - // the device and the watchdog was previously active - let (hdl0, hdl1, hdl2, hdl3) = match Watchdog::try_new(ctx.device.WDT) { - Ok(mut watchdog) => { - // Set the watchdog to timeout after 5 seconds (in 32.768kHz ticks) - watchdog.set_lfosc_ticks(5 * 32768); - - // Activate the watchdog with four handles - let Parts { - watchdog: _watchdog, - handles, - } = watchdog.activate::(); + // Pet all the dogs quickly to reset to default timeout + handles.0.pet(); + handles.1.pet(); + handles.2.pet(); + handles.3.pet(); handles } - Err(wdt) => match Watchdog::try_recover::(wdt) { - Ok(Parts { mut handles, .. }) => { - rprintln!("Oops, watchdog already active, but recovering!"); - - // Pet all the dogs quickly to reset to default timeout - handles.0.pet(); - handles.1.pet(); - handles.2.pet(); - handles.3.pet(); - - handles - } - Err(_wdt) => { - rprintln!("Oops, watchdog already active, resetting!"); - loop { - continue; - } - } - }, - }; - - // Enable the monotonic timer (CYCCNT) - ctx.core.DCB.enable_trace(); - ctx.core.DWT.enable_cycle_counter(); - - rprintln!("Starting!"); - - if ctx.device.POWER.resetreas.read().dog().is_detected() { - ctx.device.POWER.resetreas.modify(|_r, w| { - // Clear the watchdog reset reason bit - w.dog().set_bit() - }); - rprintln!("Restarted by the dog!"); - } else { - rprintln!("Not restarted by the dog!"); - } - - init::LateResources { - btn1, - btn2, - btn3, - btn4, - led1, - led2, - led3, - led4, - hdl0: hdl0.degrade(), - hdl1: hdl1.degrade(), - hdl2: hdl2.degrade(), - hdl3: hdl3.degrade(), - timer, - } + Err(_wdt) => { + rprintln!("Oops, watchdog already active, resetting!"); + loop {} + } + }, + }; + + // Enable the monotonic timer (CYCCNT) + core.DCB.enable_trace(); + core.DWT.enable_cycle_counter(); + + rprintln!("Starting!"); + + if p.POWER.resetreas.read().dog().is_detected() { + p.POWER.resetreas.modify(|_r, w| { + // Clear the watchdog reset reason bit + w.dog().set_bit() + }); + rprintln!("Restarted by the dog!"); + } else { + rprintln!("Not restarted by the dog!"); } - #[idle(resources = [btn1, btn2, btn3, btn4, led1, led2, led3, led4, hdl0, hdl1, hdl2, hdl3, timer])] - fn idle(mut ctx: idle::Context) -> ! { - let buttons = [ - &ctx.resources.btn1, - &ctx.resources.btn2, - &ctx.resources.btn3, - &ctx.resources.btn4, - ]; - - let leds = [ - &mut ctx.resources.led1, - &mut ctx.resources.led2, - &mut ctx.resources.led3, - &mut ctx.resources.led4, - ]; - - let handles = [ - &mut ctx.resources.hdl0, - &mut ctx.resources.hdl1, - &mut ctx.resources.hdl2, - &mut ctx.resources.hdl3, - ]; - - let timer = &mut ctx.resources.timer; - - let mut cumulative_ticks = 0; - let mut any_pet = false; - - timer.start(1_000_000u32 * 6); - - loop { - let mut petted = 0; - - // Loop through all handles/leds/buttons - for i in 0..4 { - if !handles[i].is_pet() && buttons[i].is_low().unwrap() { - rprintln!("Petting {}", i); - any_pet = true; - handles[i].pet(); - while buttons[i].is_low().unwrap() {} - } - - if handles[i].is_pet() { - petted += 1; - leds[i].set_low().ok(); - } else { - leds[i].set_high().ok(); - } - } + let buttons = [&btn1, &btn2, &btn3, &btn4]; + + let leds = [&mut led1, &mut led2, &mut led3, &mut led4]; + + let handles = [ + &mut hdl0.degrade(), + &mut hdl1.degrade(), + &mut hdl2.degrade(), + &mut hdl3.degrade(), + ]; + + let mut cumulative_ticks = 0; + let mut any_pet = false; - // We must have pet all handles, reset the timer - if any_pet && petted == 0 { - cumulative_ticks = 0; - any_pet = false; - timer.start(1_000_000u32 * 6); + timer.start(1_000_000u32 * 6); + + loop { + let mut petted = 0; + + // Loop through all handles/leds/buttons + for i in 0..4 { + if !handles[i].is_pet() && buttons[i].is_low().unwrap() { + rprintln!("Petting {}", i); + any_pet = true; + handles[i].pet(); + while buttons[i].is_low().unwrap() {} } - // Check whether to update the counter time - let rd = timer.read(); - if (cumulative_ticks + 250_000) <= rd { - cumulative_ticks = rd; - rprintln!("Time left: {}ms", (5_000_000 - rd) / 1_000); + if handles[i].is_pet() { + petted += 1; + leds[i].set_low().ok(); + } else { + leds[i].set_high().ok(); } } + + // We must have pet all handles, reset the timer + if any_pet && petted == 0 { + cumulative_ticks = 0; + any_pet = false; + timer.start(1_000_000u32 * 6); + } + + // Check whether to update the counter time + let rd = timer.read(); + if (cumulative_ticks + 250_000) <= rd { + cumulative_ticks = rd; + rprintln!("Time left: {}ms", (5_000_000 - rd) / 1_000); + } } -}; +} #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { cortex_m::interrupt::disable(); rprintln!("{}", info); - loop { - compiler_fence(Ordering::SeqCst); - } + loop {} } diff --git a/nrf-hal-common/Cargo.toml b/nrf-hal-common/Cargo.toml index 61cbe4b2..c2c47789 100644 --- a/nrf-hal-common/Cargo.toml +++ b/nrf-hal-common/Cargo.toml @@ -11,7 +11,7 @@ authors = [ "John Scarrott ", "Wez Furlong ", "Daniel Egger ", - "Ferdia McKeogh " + "Ferdia McKeogh ", ] categories = ["embedded", "hardware-support", "no-std"] @@ -38,35 +38,35 @@ version = "0.3.0" [dependencies.nrf51-pac] optional = true -version = "0.10.1" +version = "0.11.0" [dependencies.nrf52810-pac] optional = true -version = "0.10.1" +version = "0.11.0" [dependencies.nrf52811-pac] optional = true -version = "0.10.1" +version = "0.11.0" [dependencies.nrf52832-pac] optional = true -version = "0.10.1" +version = "0.11.0" [dependencies.nrf52833-pac] optional = true -version = "0.10.1" +version = "0.11.0" [dependencies.nrf52840-pac] optional = true -version = "0.10.1" +version = "0.11.0" [dependencies.nrf5340-app-pac] optional = true -version = "0.10.1" +version = "0.11.0" [dependencies.nrf9160-pac] optional = true -version = "0.10.1" +version = "0.11.0" [dependencies.nrf-usbd] version = "0.1.0" diff --git a/nrf51-hal/Cargo.toml b/nrf51-hal/Cargo.toml index a71e5c7d..ee8e4fc3 100644 --- a/nrf51-hal/Cargo.toml +++ b/nrf51-hal/Cargo.toml @@ -12,14 +12,14 @@ authors = [ "John Scarrott ", "Wez Furlong ", "Ferdia McKeogh ", - "Daniel Egger " + "Daniel Egger ", ] categories = ["embedded", "hardware-support", "no-std"] keywords = ["arm", "cortex-m", "nrf52", "hal", "nrf51"] license = "MIT OR Apache-2.0" [dependencies] -nrf51-pac = "0.10.1" +nrf51-pac = "0.11.0" [dependencies.nrf-hal-common] path = "../nrf-hal-common" diff --git a/nrf52810-hal/Cargo.toml b/nrf52810-hal/Cargo.toml index 82535cd3..180e6967 100644 --- a/nrf52810-hal/Cargo.toml +++ b/nrf52810-hal/Cargo.toml @@ -18,7 +18,7 @@ keywords = ["arm", "cortex-m", "nrf52", "hal", "nrf52810"] license = "MIT OR Apache-2.0" [dependencies] -nrf52810-pac = "0.10.1" +nrf52810-pac = "0.11.0" [dependencies.nrf-hal-common] path = "../nrf-hal-common" diff --git a/nrf52811-hal/Cargo.toml b/nrf52811-hal/Cargo.toml index a73a8e21..73f3bc67 100644 --- a/nrf52811-hal/Cargo.toml +++ b/nrf52811-hal/Cargo.toml @@ -18,7 +18,7 @@ keywords = ["arm", "cortex-m", "nrf52", "hal", "nrf52811"] license = "MIT OR Apache-2.0" [dependencies] -nrf52811-pac = "0.10.1" +nrf52811-pac = "0.11.0" [dependencies.nrf-hal-common] path = "../nrf-hal-common" diff --git a/nrf52832-hal/Cargo.toml b/nrf52832-hal/Cargo.toml index 82663d17..0d95cb05 100644 --- a/nrf52832-hal/Cargo.toml +++ b/nrf52832-hal/Cargo.toml @@ -7,7 +7,7 @@ readme = "../README.md" repository = "https://github.com/nrf-rs/nrf-hal" authors = [ "James Munns ", - "Hanno Braun " + "Hanno Braun ", ] categories = ["embedded", "hardware-support", "no-std"] @@ -16,7 +16,7 @@ license = "MIT OR Apache-2.0" edition = "2018" [dependencies] -nrf52832-pac = "0.10.1" +nrf52832-pac = "0.11.0" [dependencies.nrf-hal-common] path = "../nrf-hal-common" diff --git a/nrf52833-hal/Cargo.toml b/nrf52833-hal/Cargo.toml index 6fc092e6..dfa800ce 100644 --- a/nrf52833-hal/Cargo.toml +++ b/nrf52833-hal/Cargo.toml @@ -10,7 +10,7 @@ authors = [ "Hanno Braun ", "John Scarrott ", "Wez Furlong ", - "Erik Svensson " + "Erik Svensson ", ] categories = ["embedded", "hardware-support", "no-std"] @@ -19,7 +19,7 @@ license = "MIT OR Apache-2.0" edition = "2018" [dependencies] -nrf52833-pac = "0.10.1" +nrf52833-pac = "0.11.0" [dependencies.nrf-hal-common] path = "../nrf-hal-common" diff --git a/nrf52840-hal/Cargo.toml b/nrf52840-hal/Cargo.toml index b055e8c2..ed28d55b 100644 --- a/nrf52840-hal/Cargo.toml +++ b/nrf52840-hal/Cargo.toml @@ -18,7 +18,7 @@ license = "MIT OR Apache-2.0" edition = "2018" [dependencies] -nrf52840-pac = "0.10.1" +nrf52840-pac = "0.11.0" [dependencies.nrf-hal-common] path = "../nrf-hal-common" diff --git a/nrf5340-app-hal/Cargo.toml b/nrf5340-app-hal/Cargo.toml index 821a4713..6f87cdbb 100644 --- a/nrf5340-app-hal/Cargo.toml +++ b/nrf5340-app-hal/Cargo.toml @@ -7,7 +7,7 @@ readme = "../README.md" repository = "https://github.com/nrf-rs/nrf-hal" authors = [ "Jonathan Pallant (42 Technology) ", - "Sascha Wise Date: Tue, 8 Feb 2022 17:41:04 +0100 Subject: [PATCH 2/3] Update `embedded-{dma,storage}` --- examples/nvmc-demo/Cargo.toml | 2 +- nrf-hal-common/Cargo.toml | 4 ++-- nrf-hal-common/src/nvmc.rs | 19 ++++++++++++++++--- nrf52840-hal-tests/Cargo.toml | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/examples/nvmc-demo/Cargo.toml b/examples/nvmc-demo/Cargo.toml index 05206680..8eea34f0 100644 --- a/examples/nvmc-demo/Cargo.toml +++ b/examples/nvmc-demo/Cargo.toml @@ -10,7 +10,7 @@ publish = false [dependencies] cortex-m = "0.7.3" cortex-m-rt = "0.7.0" -embedded-storage = "0.2.0" +embedded-storage = "0.3.0" rtt-target = { version = "0.3.1", features = ["cortex-m"] } panic-probe = { version = "0.3.0", features = ["print-rtt"] } diff --git a/nrf-hal-common/Cargo.toml b/nrf-hal-common/Cargo.toml index c2c47789..bfb5b198 100644 --- a/nrf-hal-common/Cargo.toml +++ b/nrf-hal-common/Cargo.toml @@ -25,8 +25,8 @@ nb = "1.0.0" fixed = "1.0.0" rand_core = "0.6.3" cfg-if = "1.0.0" -embedded-dma = "0.1.1" -embedded-storage = "0.2.0" +embedded-dma = "0.2.0" +embedded-storage = "0.3.0" [dependencies.void] default-features = false diff --git a/nrf-hal-common/src/nvmc.rs b/nrf-hal-common/src/nvmc.rs index 36e092ec..e548d438 100644 --- a/nrf-hal-common/src/nvmc.rs +++ b/nrf-hal-common/src/nvmc.rs @@ -12,7 +12,9 @@ use crate::pac::NVMC; use crate::pac::NVMC_NS as NVMC; use core::convert::TryInto; -use embedded_storage::nor_flash::{NorFlash, ReadNorFlash}; +use embedded_storage::nor_flash::{ + ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash, +}; type WORD = u32; const WORD_SIZE: usize = core::mem::size_of::(); @@ -108,12 +110,14 @@ where } } +impl ErrorType for Nvmc { + type Error = NvmcError; +} + impl ReadNorFlash for Nvmc where T: Instance, { - type Error = NvmcError; - const READ_SIZE: usize = 1; fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { @@ -208,3 +212,12 @@ pub enum NvmcError { /// An operation was attempted outside the boundaries OutOfBounds, } + +impl NorFlashError for NvmcError { + fn kind(&self) -> NorFlashErrorKind { + match self { + NvmcError::Unaligned => NorFlashErrorKind::NotAligned, + NvmcError::OutOfBounds => NorFlashErrorKind::OutOfBounds, + } + } +} diff --git a/nrf52840-hal-tests/Cargo.toml b/nrf52840-hal-tests/Cargo.toml index 1d3a2752..42a847fb 100644 --- a/nrf52840-hal-tests/Cargo.toml +++ b/nrf52840-hal-tests/Cargo.toml @@ -34,6 +34,6 @@ cortex-m-rt = { version = "0.7.1", features = ["device"] } defmt = "0.3.0" defmt-rtt = "0.3.0" defmt-test = "0.3.0" -embedded-storage = "0.2.0" +embedded-storage = "0.3.0" nrf52840-hal = { path = "../nrf52840-hal" } panic-probe = { version = "0.3.0", features = ["print-defmt"] } From b91791aa0a507ca1efa5aed4d49c2002316cc4f2 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 8 Feb 2022 17:43:47 +0100 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 739c7fa4..2de81c67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,11 +11,13 @@ - Unified how pins are are returned in `free` calls ([#372]). - Improvements to the NVMC driver ([#374]). +- Updated `embedded-dma`, `embedded-storage`, and PACs ([#379]). [#372]: https://github.com/nrf-rs/nrf-hal/pull/372 [#373]: https://github.com/nrf-rs/nrf-hal/pull/373 [#374]: https://github.com/nrf-rs/nrf-hal/pull/374 [#376]: https://github.com/nrf-rs/nrf-hal/pull/376 +[#379]: https://github.com/nrf-rs/nrf-hal/pull/379 ## [0.14.1]