//! Blinks an LED //! //! This assumes that a LED is connected to pc13 as is the case on the blue pill board. //! //! Note: Without additional hardware, PC13 should not be used to drive an LED, see page 5.1.2 of //! the reference manual for an explanation. This is not an issue on the blue pill. #![deny(unsafe_code)] #![no_std] #![no_main] use panic_rtt_target as _; // RTIC requires that unused interrupts are declared in "dispatchers" when // using software tasks; these free interrupts will be used to dispatch the // software tasks. // // For a list, see: // https://docs.rs/stm32f1xx-hal/0.6.1/stm32f1xx_hal/stm32/enum.Interrupt.html #[rtic::app(device = stm32f1xx_hal::stm32, peripherals = true, dispatchers = [TAMPER])] mod app { use core::sync::atomic::{self, Ordering}; use rtt_target::{rprintln, rtt_init_print}; use stm32f1xx_hal::{ prelude::*, stm32, timer::{Event, Timer}, }; use stm32f1xx_hal as hal; // Defining this struct makes shared resources available to tasks; // they will be initialized by the values returned from `init` and // will be wrapped in a `Mutex` and must be accessed via a closure // passed to its `lock` method. // If you annotate a field with #[lock_free] you can opt-out of the // mutex but it may only be shared by tasks at the same priority. #[shared] struct Shared {} // This struct defines local resources (accessed by only one task); // they will be initialized by the values returned from `init` and // can be accessed directly. #[local] struct Local { led1: hal::gpio::gpioc::PC13>, tmr2: hal::timer::CountDownTimer, tmr3: hal::timer::CountDownTimer, } // This task does startup config; the peripherals are passed in thanks to // `peripherals = true` in the app definition. They are the `device` and // `core` fields of `init::Context`. #[init] fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { rtt_init_print!(); rprintln!("init begin"); // Set everything to 8MHz using the external clock let mut flash = cx.device.FLASH.constrain(); let rcc = cx.device.RCC.constrain(); let clocks = rcc .cfgr .use_hse(8.mhz()) .sysclk(8.mhz()) .hclk(8.mhz()) .pclk1(8.mhz()) .pclk2(8.mhz()) .adcclk(8.mhz()) .freeze(&mut flash.acr); // LED is on pin C13, configure it for output let mut gpioc = cx.device.GPIOC.split(); let led1 = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); // Use TIM2 for the beat counter task let mut tmr2 = Timer::tim2(cx.device.TIM2, &clocks).start_count_down(1.hz()); tmr2.listen(Event::Update); // Use TIM3 for the LED blinker task let mut tmr3 = Timer::tim3(cx.device.TIM3, &clocks).start_count_down(2.hz()); tmr3.listen(Event::Update); rprintln!("init end"); (Shared {}, Local { led1, tmr2, tmr3 }, init::Monotonics()) } #[idle] fn idle(_: idle::Context) -> ! { loop { // The compiler may omit this loop without the following atomic::compiler_fence(Ordering::SeqCst); } } // Update the beat counter and periodically display the current count // on the RTT channel // Since `beat` is a local, we can have it initialized. #[task(local = [beat: u32 = 0])] fn beat_update(cx: beat_update::Context) { if *cx.local.beat % 10 == 0 { rprintln!("TIM2 beat = {}", *cx.local.beat); } *cx.local.beat += 1; } // Interrupt task for TIM2, the beat counter timer #[task(binds = TIM2, priority = 2, local = [tmr2])] fn tim2(cx: tim2::Context) { // Delegate the state update to a software task beat_update::spawn().unwrap(); // Restart the timer and clear the interrupt flag cx.local.tmr2.start(1.hz()); cx.local.tmr2.clear_update_interrupt_flag(); } // Interrupt task for TIM3, the LED blink timer #[task(binds = TIM3, priority = 1, local = [led1, tmr3])] fn tim3(cx: tim3::Context) { cx.local.led1.toggle(); cx.local.tmr3.start(2.hz()); cx.local.tmr3.clear_update_interrupt_flag(); } }