-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: master
Are you sure you want to change the base?
Add timer #59
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"] } | ||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've avoided using the |
||
void = { version = "1.0.2", default-features = false } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"} | ||
|
@@ -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"] |
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(); | ||
} | ||
} | ||
}) | ||
} |
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 _}; |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.