@@ -4,9 +4,12 @@ use crate::stm32::*;
44use crate :: time:: { Hertz , MicroSecond } ;
55use core:: marker:: PhantomData ;
66use fugit:: HertzU32 ;
7+ use fugit:: RateExtU32 ;
78use void:: Void ;
89
910pub mod delay;
11+ #[ cfg( feature = "rtic2" ) ]
12+ pub mod monotonics;
1013pub mod opm;
1114pub mod pins;
1215pub mod pwm;
@@ -30,6 +33,46 @@ type Channel2 = Channel<1>;
3033type Channel3 = Channel < 2 > ;
3134type Channel4 = Channel < 3 > ;
3235
36+ /// Timer wrapper for fixed precision timers.
37+ ///
38+ /// Uses `fugit::TimerDurationU32` for most of operations
39+ pub struct FTimer < TIM , const FREQ : u32 > {
40+ tim : TIM ,
41+ }
42+
43+ /// `FTimer` with precision of 1 μs (1 MHz sampling)
44+ pub type FTimerUs < TIM > = FTimer < TIM , 1_000_000 > ;
45+
46+ /// `FTimer` with precision of 1 ms (1 kHz sampling)
47+ ///
48+ /// NOTE: don't use this if your system frequency more than 65 MHz
49+ pub type FTimerMs < TIM > = FTimer < TIM , 1_000 > ;
50+
51+ impl < TIM : private:: TimerBase , const FREQ : u32 > FTimer < TIM , FREQ > {
52+ /// Initialize timer
53+ pub fn new ( mut tim : TIM , rcc : & mut Rcc ) -> Self {
54+ tim. init ( rcc) ;
55+ tim. set_freq ( FREQ . Hz ( ) , rcc. clocks . apb_tim_clk ) ;
56+
57+ Self { tim }
58+ }
59+
60+ /*/// Creates `Counter` that implements [embedded_hal_02::timer::CountDown]
61+ pub fn counter(self) -> Counter<TIM, FREQ> {
62+ Counter(self)
63+ }
64+
65+ /// Creates `Delay` that implements [embedded_hal_02::blocking::delay] traits
66+ pub fn delay(self) -> Delay<TIM, FREQ> {
67+ Delay(self)
68+ }*/
69+
70+ /// Releases the TIM peripheral
71+ pub fn release ( self ) -> TIM {
72+ self . tim
73+ }
74+ }
75+
3376pub struct TimerFrequencySettings {
3477 psc : u16 ,
3578 arr : u32 ,
@@ -59,6 +102,8 @@ pub(super) mod private {
59102 use super :: { Rcc , TimerFrequencySettings } ;
60103
61104 pub trait TimerCommon {
105+ type Width : Into < u32 > + From < u16 > ;
106+
62107 fn init ( & mut self , rcc : & mut Rcc ) ;
63108
64109 fn set_urs ( & mut self ) ;
@@ -80,6 +125,8 @@ pub(super) mod private {
80125 }
81126
82127 impl TimerCommon for SYST {
128+ type Width = u32 ;
129+
83130 fn init ( & mut self , _rcc : & mut Rcc ) {
84131 self . set_clock_source ( SystClkSource :: Core ) ;
85132 }
@@ -144,6 +191,8 @@ macro_rules! timers {
144191 ( $( $TIM: ident: ( $tim: ident, $cnt: ident $( , $cnt_h: ident) * ) , ) +) => {
145192 $(
146193 impl private:: TimerCommon for $TIM {
194+ type Width = u16 ; // TODO: Are there any with 32 bits?
195+
147196 fn init( & mut self , rcc: & mut Rcc ) {
148197 $TIM:: enable( rcc) ;
149198 $TIM:: reset( rcc) ;
0 commit comments