embassy/embassy-stm32/src/timer/mod.rs

216 lines
6.6 KiB
Rust
Raw Normal View History

2022-06-12 22:15:44 +02:00
use stm32_metapac::timer::vals;
2021-12-08 17:36:40 +01:00
2022-06-12 22:15:44 +02:00
use crate::interrupt::Interrupt;
use crate::rcc::sealed::RccPeripheral as __RccPeri;
use crate::rcc::RccPeripheral;
2021-12-08 17:36:40 +01:00
use crate::time::Hertz;
#[cfg(feature = "unstable-pac")]
pub mod low_level {
pub use super::sealed::*;
}
pub(crate) mod sealed {
use super::*;
pub trait Basic16bitInstance: RccPeripheral {
type Interrupt: Interrupt;
fn regs() -> crate::pac::timer::TimBasic;
2021-12-08 17:36:40 +01:00
fn start(&mut self);
fn stop(&mut self);
fn reset(&mut self);
fn set_frequency(&mut self, frequency: Hertz);
2021-12-08 17:36:40 +01:00
fn clear_update_interrupt(&mut self) -> bool;
fn enable_update_interrupt(&mut self, enable: bool);
}
pub trait GeneralPurpose16bitInstance: Basic16bitInstance {
fn regs_gp16() -> crate::pac::timer::TimGp16;
2021-12-08 17:36:40 +01:00
}
pub trait GeneralPurpose32bitInstance: GeneralPurpose16bitInstance {
fn regs_gp32() -> crate::pac::timer::TimGp32;
2021-12-08 17:36:40 +01:00
fn set_frequency(&mut self, frequency: Hertz);
2021-12-08 17:36:40 +01:00
}
pub trait AdvancedControlInstance: Basic16bitInstance {
fn regs_advanced() -> crate::pac::timer::TimAdv;
2021-12-08 17:36:40 +01:00
}
}
pub trait GeneralPurpose16bitInstance: sealed::GeneralPurpose16bitInstance + 'static {}
pub trait GeneralPurpose32bitInstance: sealed::GeneralPurpose32bitInstance + 'static {}
pub trait AdvancedControlInstance: sealed::AdvancedControlInstance + 'static {}
pub trait Basic16bitInstance: sealed::Basic16bitInstance + 'static {}
#[allow(unused)]
macro_rules! impl_basic_16bit_timer {
($inst:ident, $irq:ident) => {
impl sealed::Basic16bitInstance for crate::peripherals::$inst {
type Interrupt = crate::interrupt::$irq;
fn regs() -> crate::pac::timer::TimBasic {
2021-12-08 17:36:40 +01:00
crate::pac::timer::TimBasic(crate::pac::$inst.0)
}
fn start(&mut self) {
unsafe {
Self::regs().cr1().modify(|r| r.set_cen(true));
2021-12-08 17:36:40 +01:00
}
}
fn stop(&mut self) {
unsafe {
Self::regs().cr1().modify(|r| r.set_cen(false));
2021-12-08 17:36:40 +01:00
}
}
fn reset(&mut self) {
unsafe {
Self::regs().cnt().write(|r| r.set_cnt(0));
2021-12-08 17:36:40 +01:00
}
}
fn set_frequency(&mut self, frequency: Hertz) {
2021-12-08 17:36:40 +01:00
use core::convert::TryInto;
let f = frequency.0;
2021-12-08 17:36:40 +01:00
let timer_f = Self::frequency().0;
let pclk_ticks_per_timer_period = timer_f / f;
let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 16)).try_into());
2022-06-12 22:15:44 +02:00
let arr: u16 = unwrap!((pclk_ticks_per_timer_period / (u32::from(psc) + 1)).try_into());
2021-12-08 17:36:40 +01:00
let regs = Self::regs();
2021-12-08 17:36:40 +01:00
unsafe {
regs.psc().write(|r| r.set_psc(psc));
regs.arr().write(|r| r.set_arr(arr));
regs.cr1().modify(|r| r.set_urs(vals::Urs::COUNTERONLY));
regs.egr().write(|r| r.set_ug(true));
regs.cr1().modify(|r| r.set_urs(vals::Urs::ANYEVENT));
}
}
fn clear_update_interrupt(&mut self) -> bool {
let regs = Self::regs();
2021-12-08 17:36:40 +01:00
unsafe {
let sr = regs.sr().read();
2021-12-08 17:36:40 +01:00
if sr.uif() {
regs.sr().modify(|r| {
2021-12-08 17:36:40 +01:00
r.set_uif(false);
});
true
} else {
false
}
}
}
fn enable_update_interrupt(&mut self, enable: bool) {
unsafe {
Self::regs().dier().write(|r| r.set_uie(enable));
2021-12-08 17:36:40 +01:00
}
}
}
};
}
#[allow(unused)]
macro_rules! impl_32bit_timer {
($inst:ident) => {
impl sealed::GeneralPurpose32bitInstance for crate::peripherals::$inst {
fn regs_gp32() -> crate::pac::timer::TimGp32 {
2021-12-08 17:36:40 +01:00
crate::pac::$inst
}
fn set_frequency(&mut self, frequency: Hertz) {
2021-12-08 17:36:40 +01:00
use core::convert::TryInto;
let f = frequency.0;
2021-12-08 17:36:40 +01:00
let timer_f = Self::frequency().0;
let pclk_ticks_per_timer_period = (timer_f / f) as u64;
let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 32)).try_into());
2022-06-12 22:15:44 +02:00
let arr: u32 = unwrap!(((pclk_ticks_per_timer_period / (psc as u64 + 1)).try_into()));
2021-12-08 17:36:40 +01:00
let regs = Self::regs_gp32();
2021-12-08 17:36:40 +01:00
unsafe {
regs.psc().write(|r| r.set_psc(psc));
regs.arr().write(|r| r.set_arr(arr));
regs.cr1().modify(|r| r.set_urs(vals::Urs::COUNTERONLY));
regs.egr().write(|r| r.set_ug(true));
regs.cr1().modify(|r| r.set_urs(vals::Urs::ANYEVENT));
}
}
}
};
}
foreach_interrupt! {
2021-12-08 17:36:40 +01:00
($inst:ident, timer, TIM_BASIC, UP, $irq:ident) => {
impl_basic_16bit_timer!($inst, $irq);
impl Basic16bitInstance for crate::peripherals::$inst {
}
};
($inst:ident, timer, TIM_GP16, UP, $irq:ident) => {
impl_basic_16bit_timer!($inst, $irq);
impl Basic16bitInstance for crate::peripherals::$inst {
}
impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst {
fn regs_gp16() -> crate::pac::timer::TimGp16 {
2021-12-08 17:36:40 +01:00
crate::pac::$inst
}
}
impl GeneralPurpose16bitInstance for crate::peripherals::$inst {
}
};
($inst:ident, timer, TIM_GP32, UP, $irq:ident) => {
impl_basic_16bit_timer!($inst, $irq);
impl Basic16bitInstance for crate::peripherals::$inst {
}
impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst {
fn regs_gp16() -> crate::pac::timer::TimGp16 {
2021-12-08 17:36:40 +01:00
crate::pac::timer::TimGp16(crate::pac::$inst.0)
}
}
impl GeneralPurpose16bitInstance for crate::peripherals::$inst {
}
impl_32bit_timer!($inst);
impl GeneralPurpose32bitInstance for crate::peripherals::$inst {
}
};
($inst:ident, timer, TIM_ADV, UP, $irq:ident) => {
impl_basic_16bit_timer!($inst, $irq);
impl Basic16bitInstance for crate::peripherals::$inst {
}
impl sealed::AdvancedControlInstance for crate::peripherals::$inst {
fn regs_advanced() -> crate::pac::timer::TimAdv {
2021-12-08 17:36:40 +01:00
crate::pac::$inst
}
}
impl AdvancedControlInstance for crate::peripherals::$inst {
}
};
}