Created
February 25, 2024 17:51
-
-
Save barbu110/c4ef21059a145f9474e2052871af7187 to your computer and use it in GitHub Desktop.
Code to make an LED (on PA5) blink on a STM32L476RGTx using timer interrupts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_main] | |
#![no_std] | |
#![feature(type_alias_impl_trait)] | |
use hal; | |
use hal::clocks::Clocks; | |
use hal::gpio::Pin; | |
use hal::{ | |
gpio::{PinMode, Port}, | |
pac::TIM5, | |
timer::{Timer, TimerInterrupt}, | |
}; | |
use panic_halt as _; | |
use rtic::app; | |
#[app(device = hal::pac, peripherals = true)] | |
mod app { | |
use cortex_m::asm; | |
use super::*; | |
#[shared] | |
struct Shared {} | |
#[local] | |
struct Local { | |
led: Pin, | |
led_timer: Timer<TIM5>, | |
} | |
#[init] | |
fn init(cx: init::Context) -> (Shared, Local) { | |
// let dp = pac::Peripherals::take().unwrap(); | |
let dp = cx.device; | |
let clock_cfg = Clocks::default(); | |
clock_cfg.setup().unwrap(); | |
let led = Pin::new(Port::A, 5, PinMode::Output); | |
let mut timer = Timer::new_tim5(dp.TIM5, 2., Default::default(), &clock_cfg); | |
timer.enable_interrupt(TimerInterrupt::Update); | |
timer.enable(); | |
( | |
Shared {}, | |
Local { | |
led_timer: timer, | |
led, | |
}, | |
) | |
} | |
#[idle()] | |
fn idle(_: idle::Context) -> ! { | |
loop { | |
asm::nop(); | |
} | |
} | |
#[task(binds = TIM5, local = [led_timer, led], priority = 1)] | |
fn blink_led(cx: blink_led::Context) { | |
cx.local.led_timer.clear_interrupt(TimerInterrupt::Update); | |
cx.local.led.toggle(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment