|
| 1 | +#![deny(unsafe_code)] |
| 2 | +#![deny(warnings)] |
| 3 | +#![no_main] |
| 4 | +#![no_std] |
| 5 | + |
| 6 | +use panic_rtt_target as _; |
| 7 | +use rtic::app; |
| 8 | +use rtt_target::{rprintln, rtt_init_print}; |
| 9 | +use stm32l4xx_hal::gpio::{gpiob::PB3, Output, PushPull}; |
| 10 | +use stm32l4xx_hal::prelude::*; |
| 11 | +use systick_monotonic::{fugit::Duration, Systick}; |
| 12 | + |
| 13 | +#[app(device = stm32l4xx_hal::pac, dispatchers = [SPI3])] |
| 14 | +mod app { |
| 15 | + use super::*; |
| 16 | + |
| 17 | + #[shared] |
| 18 | + struct Shared {} |
| 19 | + |
| 20 | + #[local] |
| 21 | + struct Local { |
| 22 | + led: PB3<Output<PushPull>>, |
| 23 | + intervals: [u32; 6], |
| 24 | + } |
| 25 | + |
| 26 | + #[monotonic(binds = SysTick, default = true)] |
| 27 | + type MonoTimer = Systick<1000>; |
| 28 | + |
| 29 | + #[init] |
| 30 | + fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { |
| 31 | + // Setup clocks |
| 32 | + let mut flash = cx.device.FLASH.constrain(); |
| 33 | + let mut rcc = cx.device.RCC.constrain(); |
| 34 | + let mut pwr = cx.device.PWR.constrain(&mut rcc.apb1r1); |
| 35 | + let mono = Systick::new(cx.core.SYST, 72_000_000); |
| 36 | + |
| 37 | + rtt_init_print!(); |
| 38 | + rprintln!("init"); |
| 39 | + |
| 40 | + let _clocks = rcc.cfgr.sysclk(72.MHz()).freeze(&mut flash.acr, &mut pwr); |
| 41 | + |
| 42 | + // Setup LED |
| 43 | + let mut gpiob = cx.device.GPIOB.split(&mut rcc.ahb2); |
| 44 | + let mut led = gpiob |
| 45 | + .pb3 |
| 46 | + .into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper); |
| 47 | + led.set_low(); |
| 48 | + |
| 49 | + // Simple heart beat LED on/off sequence |
| 50 | + let intervals: [u32; 6] = [ |
| 51 | + 30, // P Wave |
| 52 | + 40, // PR Segment |
| 53 | + 120, // QRS Complex |
| 54 | + 30, // ST Segment |
| 55 | + 60, // T Wave |
| 56 | + 720, // Rest |
| 57 | + ]; |
| 58 | + |
| 59 | + // Schedule the blinking task |
| 60 | + blink::spawn(0).unwrap(); |
| 61 | + |
| 62 | + (Shared {}, Local { led, intervals }, init::Monotonics(mono)) |
| 63 | + } |
| 64 | + |
| 65 | + #[idle] |
| 66 | + fn idle(_: idle::Context) -> ! { |
| 67 | + loop { |
| 68 | + core::hint::spin_loop(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + #[task(local = [led, intervals])] |
| 73 | + fn blink(cx: blink::Context, state: usize) { |
| 74 | + rprintln!("blink"); |
| 75 | + let duration = cx.local.intervals[state]; |
| 76 | + let next_state = (state + 1) % cx.local.intervals.len(); |
| 77 | + |
| 78 | + cx.local.led.toggle(); |
| 79 | + |
| 80 | + let _ = blink::spawn_after( |
| 81 | + Duration::<u64, 1, 1000>::from_ticks(duration as u64), |
| 82 | + next_state, |
| 83 | + ); |
| 84 | + } |
| 85 | +} |
0 commit comments