Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[target.thumbv8m.main-none-eabihf]
runner = 'probe-rs run --connect-under-reset'
runner = 'probe-rs run --chip STM32H523RETx'
rustflags = [
# LLD (shipped with the Rust toolchain) is used as the default linker
"-C", "link-arg=-Tlink.x",
Expand Down
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ log = { version = "0.4.20", optional = true}
futures-util = { version = "0.3", default-features = false, features = ["async-await-macro"], optional = true}
stm32-usbd = "0.8.0"

##
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] }
nb = "1.0.0"
void = { version = "1.0.2", default-features = false }
cast = { version = "0.3.0", default-features = false }
##

[dev-dependencies]
log = { version = "0.4.20"}
cortex-m-rt = "0.7.3"
Expand All @@ -105,6 +112,10 @@ opt-level = "s" # optimize for binary size
[[example]]
name = "blinky"

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

[[example]]
name = "i2c"
required-features = ["stm32h503"]
Expand Down
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();
}
}
})
}
7 changes: 7 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 @@ -84,6 +87,10 @@ pub mod usb;

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

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

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

pub use crate::delay::DelayExt as _stm32h5xx_hal_delay_DelayExt;
pub use crate::dwt::DwtExt as _stm32h5xx_hal_delay_DwtExt;
Expand All @@ -9,6 +10,7 @@ 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::spi::SpiExt as _stm32h5xx_hal_spi_SpiExt;
pub use crate::timer::TimerExt as _stm32h5xx_hal_timer_TimerExt;
pub use crate::usb::UsbExt as _stm32h5xx_hal_usb_UsbExt;

pub use crate::time::U32Ext as _;
Expand Down
Loading
Loading