diff options
Diffstat (limited to 'examples/nrf52840_blinky/src')
-rw-r--r-- | examples/nrf52840_blinky/src/bin/blinky_rtc.rs | 69 | ||||
-rw-r--r-- | examples/nrf52840_blinky/src/bin/blinky_timer.rs | 66 | ||||
-rw-r--r-- | examples/nrf52840_blinky/src/lib.rs | 37 |
3 files changed, 172 insertions, 0 deletions
diff --git a/examples/nrf52840_blinky/src/bin/blinky_rtc.rs b/examples/nrf52840_blinky/src/bin/blinky_rtc.rs new file mode 100644 index 00000000..e515659e --- /dev/null +++ b/examples/nrf52840_blinky/src/bin/blinky_rtc.rs @@ -0,0 +1,69 @@ +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] +#![feature(type_alias_impl_trait)] + +use nrf52840_blinky::hal; + +#[rtic::app(device = hal::pac, dispatchers = [SWI0_EGU0])] +mod app { + use super::*; + + use hal::gpio::{Level, Output, Pin, PushPull}; + use hal::prelude::*; + + use rtic_monotonics::nrf::rtc::Rtc0 as Mono; + use rtic_monotonics::nrf::rtc::*; + use rtic_monotonics::Monotonic; + + #[shared] + struct Shared {} + + #[local] + struct Local { + led: Pin<Output<PushPull>>, + } + + #[init] + fn init(cx: init::Context) -> (Shared, Local) { + // Configure low frequency clock + hal::clocks::Clocks::new(cx.device.CLOCK).start_lfclk(); + + // Initialize Monotonic + let token = rtic_monotonics::create_nrf_rtc0_monotonic_token!(); + Mono::start(cx.device.RTC0, token); + + // Setup LED + let port0 = hal::gpio::p0::Parts::new(cx.device.P0); + let led = port0.p0_06.into_push_pull_output(Level::Low).degrade(); + + // Schedule the blinking task + blink::spawn().ok(); + + (Shared {}, Local { led }) + } + + #[task(local = [led])] + async fn blink(cx: blink::Context) { + let blink::LocalResources { led, .. } = cx.local; + + let mut next_tick = Mono::now(); + let mut blink_on = false; + loop { + let now = Mono::now(); + let now_ms: fugit::SecsDurationU64 = now.duration_since_epoch().convert(); + defmt::println!("Timer {} ({})", now_ms, now.ticks()); + + blink_on = !blink_on; + if blink_on { + led.set_high().unwrap(); + } else { + led.set_low().unwrap(); + } + + next_tick += 1000.millis(); + Mono::delay_until(next_tick).await; + } + } +} diff --git a/examples/nrf52840_blinky/src/bin/blinky_timer.rs b/examples/nrf52840_blinky/src/bin/blinky_timer.rs new file mode 100644 index 00000000..53ccf4e7 --- /dev/null +++ b/examples/nrf52840_blinky/src/bin/blinky_timer.rs @@ -0,0 +1,66 @@ +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] +#![feature(type_alias_impl_trait)] + +use nrf52840_blinky::hal; + +#[rtic::app(device = hal::pac, dispatchers = [SWI0_EGU0])] +mod app { + use super::*; + + use hal::gpio::{Level, Output, Pin, PushPull}; + use hal::prelude::*; + + use rtic_monotonics::nrf::timer::Timer0 as Mono; + use rtic_monotonics::nrf::timer::*; + use rtic_monotonics::Monotonic; + + #[shared] + struct Shared {} + + #[local] + struct Local { + led: Pin<Output<PushPull>>, + } + + #[init] + fn init(cx: init::Context) -> (Shared, Local) { + // Initialize Monotonic + let token = rtic_monotonics::create_nrf_timer0_monotonic_token!(); + Mono::start(cx.device.TIMER0, token); + + // Setup LED + let port0 = hal::gpio::p0::Parts::new(cx.device.P0); + let led = port0.p0_06.into_push_pull_output(Level::Low).degrade(); + + // Schedule the blinking task + blink::spawn().ok(); + + (Shared {}, Local { led }) + } + + #[task(local = [led])] + async fn blink(cx: blink::Context) { + let blink::LocalResources { led, .. } = cx.local; + + let mut next_tick = Mono::now(); + let mut blink_on = false; + loop { + let now = Mono::now(); + let now_ms: fugit::SecsDurationU64 = now.duration_since_epoch().convert(); + defmt::println!("Timer {} ({})", now_ms, now.ticks()); + + blink_on = !blink_on; + if blink_on { + led.set_high().unwrap(); + } else { + led.set_low().unwrap(); + } + + next_tick += 1000.millis(); + Mono::delay_until(next_tick).await; + } + } +} diff --git a/examples/nrf52840_blinky/src/lib.rs b/examples/nrf52840_blinky/src/lib.rs new file mode 100644 index 00000000..7795f2da --- /dev/null +++ b/examples/nrf52840_blinky/src/lib.rs @@ -0,0 +1,37 @@ +#![no_main] +#![no_std] + +use cortex_m_semihosting::debug; + +use defmt_rtt as _; // global logger + +pub use nrf52840_hal as hal; // memory layout + +use panic_probe as _; + +// same panicking *behavior* as `panic-probe` but doesn't print a panic message +// this prevents the panic message being printed *twice* when `defmt::panic` is invoked +#[defmt::panic_handler] +fn panic() -> ! { + cortex_m::asm::udf() +} + +/// Terminates the application and makes a semihosting-capable debug tool exit +/// with status code 0. +pub fn exit() -> ! { + loop { + debug::exit(debug::EXIT_SUCCESS); + } +} + +/// Hardfault handler. +/// +/// Terminates the application and makes a semihosting-capable debug tool exit +/// with an error. This seems better than the default, which is to spin in a +/// loop. +#[cortex_m_rt::exception] +unsafe fn HardFault(_frame: &cortex_m_rt::ExceptionFrame) -> ! { + loop { + debug::exit(debug::EXIT_FAILURE); + } +} |