rtic-bluepill-blinky/src/main.rs

84 lines
2.1 KiB
Rust

#![deny(unsafe_code)]
#![no_main]
#![no_std]
use defmt_rtt as _; // global logger
use panic_probe as _;
use rtic::app;
use stm32f1xx_hal::gpio::PinState;
use stm32f1xx_hal::gpio::{gpioc::PC13, Output, PushPull};
use stm32f1xx_hal::prelude::*;
use systick_monotonic::Systick;
// same panicking *behavior* as `panic-probe` but doesn't print a panic message
// this prevents the panic message being printed *twice* when `defmt::panic` is invoked
#[defmt::panic_handler]
fn panic() -> ! {
cortex_m::asm::udf()
}
#[app(device = stm32f1xx_hal::pac, peripherals = true, dispatchers = [TIM3])]
mod app {
use super::*;
#[shared]
struct Shared {}
#[local]
struct Local {
led: PC13<Output<PushPull>>,
state: bool,
}
#[monotonic(binds = SysTick, default = true)]
type MonoTimer = Systick<1000>;
#[init]
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
// Setup clocks
let mut flash = cx.device.FLASH.constrain();
let rcc = cx.device.RCC.constrain();
defmt::info!("init");
let clocks = rcc
.cfgr
.use_hse(8.MHz())
.sysclk(36.MHz())
.pclk1(36.MHz())
.freeze(&mut flash.acr);
let mono = Systick::new(cx.core.SYST, clocks.pclk1().to_Hz());
// Setup LED
let mut gpioc = cx.device.GPIOC.split();
let led = gpioc
.pc13
.into_push_pull_output_with_state(&mut gpioc.crh, PinState::Low);
// Schedule the blinking task
blink::spawn_after(1.secs().into()).unwrap();
(
Shared {},
Local { led, state: false },
init::Monotonics(mono),
)
}
#[task(local = [led, state])]
fn blink(cx: blink::Context) {
if *cx.local.state {
defmt::info!("blink");
cx.local.led.set_high();
*cx.local.state = false;
} else {
defmt::info!("blonk");
cx.local.led.set_low();
*cx.local.state = true;
}
blink::spawn_after(1.secs().into()).unwrap();
}
}