|
| 1 | +//! A minimal blinky example using `MonotonicRtc`. |
| 2 | +#![no_main] |
| 3 | +#![no_std] |
| 4 | + |
| 5 | +use hal::pac; |
| 6 | +use nrf52840_hal as hal; |
| 7 | +use panic_halt as _; |
| 8 | +#[rtic::app(device = pac, dispatchers = [UARTE1])] |
| 9 | +mod app { |
| 10 | + use super::*; |
| 11 | + use cortex_m::asm; |
| 12 | + use embedded_hal::digital::{OutputPin, StatefulOutputPin}; |
| 13 | + use hal::{ |
| 14 | + gpio::{p0::Parts, Level, Output, Pin, PushPull}, |
| 15 | + monotonic::MonotonicRtc, |
| 16 | + }; |
| 17 | + use pac::RTC0; |
| 18 | + use rtt_target::{rprintln, rtt_init_print}; |
| 19 | + |
| 20 | + #[monotonic(binds = RTC0, default = true)] |
| 21 | + type MyMono = MonotonicRtc<RTC0, 32_768>; |
| 22 | + |
| 23 | + #[shared] |
| 24 | + struct Shared {} |
| 25 | + |
| 26 | + #[local] |
| 27 | + struct Local { |
| 28 | + led: Pin<Output<PushPull>>, |
| 29 | + } |
| 30 | + |
| 31 | + #[init] |
| 32 | + fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { |
| 33 | + rtt_init_print!(); |
| 34 | + rprintln!("init"); |
| 35 | + |
| 36 | + let p0 = Parts::new(cx.device.P0); |
| 37 | + let led = p0.p0_13.into_push_pull_output(Level::High).degrade(); |
| 38 | + |
| 39 | + let clocks = hal::clocks::Clocks::new(cx.device.CLOCK); |
| 40 | + let clocks = clocks.start_lfclk(); |
| 41 | + // Will throw error if freq is invalid |
| 42 | + let mono = MyMono::new(cx.device.RTC0, &clocks).unwrap(); |
| 43 | + |
| 44 | + blink::spawn().ok(); |
| 45 | + (Shared {}, Local { led }, init::Monotonics(mono)) |
| 46 | + } |
| 47 | + |
| 48 | + #[idle] |
| 49 | + fn idle(_: idle::Context) -> ! { |
| 50 | + loop { |
| 51 | + rprintln!("idle"); |
| 52 | + // Put core to sleep until next interrupt |
| 53 | + asm::wfe(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + #[task(local = [led])] |
| 58 | + fn blink(ctx: blink::Context) { |
| 59 | + rprintln!("Blink!"); |
| 60 | + let led = ctx.local.led; |
| 61 | + // Note this unwrap is safe since is_set_low is allways Ok |
| 62 | + if led.is_set_low().unwrap() { |
| 63 | + led.set_high().ok(); |
| 64 | + } else { |
| 65 | + led.set_low().ok(); |
| 66 | + } |
| 67 | + // spawn after current time + 1 second |
| 68 | + blink::spawn_after(fugit::ExtU32::millis(1000)).ok(); |
| 69 | + } |
| 70 | +} |
0 commit comments