|
| 1 | +#![no_main] |
| 2 | +#![no_std] |
| 3 | + |
| 4 | +use core::fmt::Write; |
| 5 | + |
| 6 | +use panic_rtt_target as _; |
| 7 | +use rtt_target::{rprintln, rtt_init_print}; |
| 8 | + |
| 9 | +use rtic::app; |
| 10 | +use stm32l0xx_hal::{pac, prelude::*, rcc::Config, serial}; |
| 11 | +use systick_monotonic::{ |
| 12 | + fugit::{ExtU32, MillisDurationU64}, |
| 13 | + Systick, |
| 14 | +}; |
| 15 | + |
| 16 | +const INTERVAL_MS: u32 = 500; |
| 17 | + |
| 18 | +#[app( |
| 19 | + device = stm32l0xx_hal::pac, |
| 20 | + peripherals = true, |
| 21 | + dispatchers = [SPI1], |
| 22 | +)] |
| 23 | +mod app { |
| 24 | + use super::*; |
| 25 | + |
| 26 | + // Setting this monotonic as the default |
| 27 | + // enables the shorthand fizzbuzz::spawn_after |
| 28 | + // without having to specify `Mono` as fizzbuzz::Mono::spawn_after( |
| 29 | + #[monotonic(binds = SysTick, default = true)] |
| 30 | + type Tonic = Systick<1000>; |
| 31 | + |
| 32 | + #[local] |
| 33 | + struct Local { |
| 34 | + /// Serial debug output |
| 35 | + serial: serial::Serial<pac::USART2>, |
| 36 | + |
| 37 | + /// Timer interval |
| 38 | + interval: MillisDurationU64, |
| 39 | + |
| 40 | + /// Counter |
| 41 | + counter: usize, |
| 42 | + } |
| 43 | + |
| 44 | + #[shared] |
| 45 | + struct Shared {} |
| 46 | + |
| 47 | + #[init] |
| 48 | + fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { |
| 49 | + // Get peripherals |
| 50 | + let dp: pac::Peripherals = cx.device; |
| 51 | + |
| 52 | + // Clock configuration. Use HSI at 16 MHz. |
| 53 | + let mut rcc = dp.RCC.freeze(Config::hsi16()); |
| 54 | + |
| 55 | + rtt_init_print!(); |
| 56 | + rprintln!("RTT init"); |
| 57 | + |
| 58 | + // GPIO |
| 59 | + let gpiob = dp.GPIOB.split(&mut rcc); |
| 60 | + |
| 61 | + // Initialize serial port(s) |
| 62 | + let mut serial = serial::Serial::usart2( |
| 63 | + dp.USART2, |
| 64 | + gpiob.pb6.into_floating_input(), |
| 65 | + gpiob.pb7.into_floating_input(), |
| 66 | + serial::Config { |
| 67 | + baudrate: 57_600.Bd(), |
| 68 | + wordlength: serial::WordLength::DataBits8, |
| 69 | + parity: serial::Parity::ParityNone, |
| 70 | + stopbits: serial::StopBits::STOP1, |
| 71 | + }, |
| 72 | + &mut rcc, |
| 73 | + ) |
| 74 | + .unwrap(); |
| 75 | + |
| 76 | + // Initialize the timer |
| 77 | + writeln!(serial, "Initialize monotonic timer using SysTick at 1kHz").unwrap(); |
| 78 | + |
| 79 | + let mono = Systick::new(cx.core.SYST, 16_000_000); |
| 80 | + |
| 81 | + let interval: MillisDurationU64 = INTERVAL_MS.millis().into(); |
| 82 | + |
| 83 | + writeln!( |
| 84 | + serial, |
| 85 | + "Schedule task every {} ms / {} ticks", |
| 86 | + interval, |
| 87 | + interval.ticks(), |
| 88 | + ) |
| 89 | + .unwrap(); |
| 90 | + |
| 91 | + // Spawn task "fizzbuzz" |
| 92 | + let _ = fizzbuzz::spawn(); |
| 93 | + |
| 94 | + writeln!(serial, "== Init done ==").unwrap(); |
| 95 | + |
| 96 | + let local = Local { |
| 97 | + serial, |
| 98 | + interval, |
| 99 | + counter: 1, |
| 100 | + }; |
| 101 | + |
| 102 | + (Shared {}, local, init::Monotonics(mono)) |
| 103 | + } |
| 104 | + |
| 105 | + #[task(local = [serial, interval, counter])] |
| 106 | + fn fizzbuzz(cx: fizzbuzz::Context) { |
| 107 | + rprintln!("fizzbuzz!"); |
| 108 | + // Access resources |
| 109 | + let serial = cx.local.serial; |
| 110 | + let now = monotonics::now(); |
| 111 | + let counter = cx.local.counter; |
| 112 | + let interval = cx.local.interval; |
| 113 | + |
| 114 | + // Fancy fizzbuzz implementation |
| 115 | + match (*counter % 3 == 0, *counter % 5 == 0) { |
| 116 | + (true, true) => { |
| 117 | + rprintln!("fizzbuzz (now={:05})", now); |
| 118 | + writeln!(serial, "fizzbuzz (now={:05})", now).unwrap(); |
| 119 | + } |
| 120 | + (true, false) => { |
| 121 | + rprintln!(" fizz (now={:05})", now); |
| 122 | + writeln!(serial, " fizz (now={:05})", now).unwrap(); |
| 123 | + } |
| 124 | + (false, true) => { |
| 125 | + rprintln!(" buzz (now={:05})", now); |
| 126 | + writeln!(serial, " buzz (now={:05})", now).unwrap(); |
| 127 | + } |
| 128 | + _ => { |
| 129 | + rprintln!("{:08} (now={:05})", *counter, now); |
| 130 | + writeln!(serial, "{:08} (now={:05})", *counter, now).unwrap(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Increment counter |
| 135 | + *counter += 1; |
| 136 | + |
| 137 | + // Re-schedule |
| 138 | + let _ = fizzbuzz::spawn_after(*interval); |
| 139 | + } |
| 140 | +} |
0 commit comments