Skip to content

Add timer #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ log-semihost = ["log"]
cortex-m = { version = "^0.7.7", features = ["critical-section-single-core"] }
stm32h5 = { package = "stm32h5", version = "0.16.0" }
fugit = "0.3.7"
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] }
Copy link
Member

@astapleton astapleton Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's any reason to support embedded-hal 0.2 at this point. I've intentionally avoided doing so because embedded-hal 1.0 was released before any progress was made on this project.

embedded-hal = "1.0.0"
defmt = { version = "1.0.0", optional = true }
paste = "1.0.15"
log = { version = "0.4.20", optional = true}
nb = "1.0.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've avoided using the nb crate due to previous feedback that it's likely to be deprecated. See this thread: #27 (comment)

void = { version = "1.0.2", default-features = false }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be unnecessary with the embedded-hal 0.2 dependency removed

cast = { version = "0.3.0", default-features = false }

[dev-dependencies]
log = { version = "0.4.20"}
Expand All @@ -89,6 +93,10 @@ opt-level = "s" # optimize for binary size
[[example]]
name = "blinky"

[[example]]
name = "blinky_timer"
required-features = ["stm32h533"]

[[example]]
name = "i2c"
required-features = ["stm32h503"]
84 changes: 84 additions & 0 deletions examples/blinky_timer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#![deny(warnings)]
#![no_main]
#![no_std]

mod utilities;

use core::{
cell::RefCell,
sync::atomic::{AtomicBool, Ordering},
};

use cortex_m::interrupt::Mutex;
use cortex_m::peripheral::NVIC;
use cortex_m_rt::entry;

use stm32h5xx_hal::gpio::gpioa::PA5; // LED pin
use stm32h5xx_hal::gpio::{Output, PushPull};
use stm32h5xx_hal::{pac, pac::interrupt, prelude::*, timer};
use utilities::logger::info;

static LED_IS_ON: AtomicBool = AtomicBool::new(false);
static LED: Mutex<RefCell<Option<PA5<Output<PushPull>>>>> =
Mutex::new(RefCell::new(None));
static TIMER: Mutex<RefCell<Option<timer::Timer<pac::TIM2>>>> =
Mutex::new(RefCell::new(None));

#[entry]
fn main() -> ! {
utilities::logger::init();

let mut cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();

let pwr = dp.PWR.constrain();
let pwrcfg = pwr.vos0().freeze();

// Constrain and Freeze clock
let rcc = dp.RCC.constrain();
let ccdr = rcc.sys_ck(250.MHz()).freeze(pwrcfg, &dp.SBS);

let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA);
let mut led = gpioa.pa5.into_push_pull_output();
led.set_low();

let mut timer = dp.TIM2.timer(2.Hz(), ccdr.peripheral.TIM2, &ccdr.clocks);
timer.listen(timer::Event::TimeOut);

cortex_m::interrupt::free(|cs| {
LED.borrow(cs).replace(Some(led));
TIMER.borrow(cs).replace(Some(timer));
});

info!("Start blinking with timer...");
// Enable TIM2 interrupt
unsafe {
cp.NVIC.set_priority(interrupt::TIM2, 1);
NVIC::unmask::<interrupt>(interrupt::TIM2);
}

loop {
// do_nothing
}
}

/// Handle timer overflow
///
/// The interrupt should be configured at maximum priority, it won't take very long.
#[interrupt]
fn TIM2() {
cortex_m::interrupt::free(|cs| {
if let Some(timer) = TIMER.borrow(cs).borrow_mut().as_mut() {
timer.clear_irq();
}
// Signal that the interrupt fired
let led_is_on = LED_IS_ON.fetch_not(Ordering::Relaxed);
if let Some(led) = LED.borrow(cs).borrow_mut().as_mut() {
if led_is_on {
led.set_low();
} else {
led.set_high();
}
}
})
}
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(non_camel_case_types)]

pub use nb;
pub use nb::block;

#[cfg(not(feature = "device-selected"))]
compile_error!(
"This crate requires one of the following device features enabled:
Expand Down Expand Up @@ -73,6 +76,9 @@ pub mod icache;
#[cfg(feature = "device-selected")]
pub mod delay;

#[cfg(feature = "device-selected")]
pub mod timer;

#[cfg(feature = "device-selected")]
mod sealed {
pub trait Sealed {}
Expand Down
2 changes: 2 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Prelude
pub use embedded_hal_02::prelude::*;

pub use crate::delay::DelayExt as _stm32h5xx_hal_delay_DelayExt;
pub use crate::gpio::GpioExt as _stm32h5xx_hal_gpio_GpioExt;
pub use crate::i2c::I2cExt as _stm32h5xx_hal_i2c_I2cExt;
pub use crate::icache::ICacheExt as _stm32h5xx_hal_icache_ICacheExt;
pub use crate::pwr::PwrExt as _stm32h5xx_hal_pwr_PwrExt;
pub use crate::rcc::RccExt as _stm32h5xx_hal_rcc_RccExt;
pub use crate::timer::TimerExt as _stm32h5xx_hal_timer_TimerExt;

pub use crate::time::U32Ext as _;
pub use fugit::{ExtU32 as _, RateExtU32 as _};
Loading
Loading