diff options
author | 2023-12-06 08:49:38 +0100 | |
---|---|---|
committer | 2023-12-06 07:49:38 +0000 | |
commit | 89160b7cb9b3623e0a50f6745296d470fa7ea79d (patch) | |
tree | 1409920c1fe8dec4857cd1e73f2f02485229fcad /examples/nrf52840_blinky/src/bin/blinky_timer.rs | |
parent | 1622f6b953c93c3a680769636b60733f281f1ac0 (diff) | |
download | rtic-89160b7cb9b3623e0a50f6745296d470fa7ea79d.tar.gz rtic-89160b7cb9b3623e0a50f6745296d470fa7ea79d.tar.zst rtic-89160b7cb9b3623e0a50f6745296d470fa7ea79d.zip |
Fix nrf monotonics (#852)
* Fix nrf::timer
* Bootstrap nrf52840-blinky example
* More work on nrf blinky example
* Fix README
* Add asserts for correct timer functionality
* Add correctness check to other monotonics as well
* Update Changelog
* Fix potential timing issues
* Fix race condition in nrf::rtc
* Add changelog
* Add rtc blinky example
* Change rtc example to RC lf clock source
* Add changelog to rtic-time
* Add changelog
* Attempt to fix CI
* Update teensy4-blinky Cargo.lock
Diffstat (limited to 'examples/nrf52840_blinky/src/bin/blinky_timer.rs')
-rw-r--r-- | examples/nrf52840_blinky/src/bin/blinky_timer.rs | 66 |
1 files changed, 66 insertions, 0 deletions
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; + } + } +} |