@@ -4,9 +4,12 @@ use crate::stm32::*;
4
4
use crate :: time:: { Hertz , MicroSecond } ;
5
5
use core:: marker:: PhantomData ;
6
6
use fugit:: HertzU32 ;
7
+ use fugit:: RateExtU32 ;
7
8
use void:: Void ;
8
9
9
10
pub mod delay;
11
+ #[ cfg( feature = "rtic2" ) ]
12
+ pub mod monotonics;
10
13
pub mod opm;
11
14
pub mod pins;
12
15
pub mod pwm;
@@ -30,6 +33,46 @@ type Channel2 = Channel<1>;
30
33
type Channel3 = Channel < 2 > ;
31
34
type Channel4 = Channel < 3 > ;
32
35
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
+
33
76
pub struct TimerFrequencySettings {
34
77
psc : u16 ,
35
78
arr : u32 ,
@@ -59,6 +102,8 @@ pub(super) mod private {
59
102
use super :: { Rcc , TimerFrequencySettings } ;
60
103
61
104
pub trait TimerCommon {
105
+ type Width : Into < u32 > + From < u16 > ;
106
+
62
107
fn init ( & mut self , rcc : & mut Rcc ) ;
63
108
64
109
fn set_urs ( & mut self ) ;
@@ -80,6 +125,8 @@ pub(super) mod private {
80
125
}
81
126
82
127
impl TimerCommon for SYST {
128
+ type Width = u32 ;
129
+
83
130
fn init ( & mut self , _rcc : & mut Rcc ) {
84
131
self . set_clock_source ( SystClkSource :: Core ) ;
85
132
}
@@ -144,6 +191,8 @@ macro_rules! timers {
144
191
( $( $TIM: ident: ( $tim: ident, $cnt: ident $( , $cnt_h: ident) * ) , ) +) => {
145
192
$(
146
193
impl private:: TimerCommon for $TIM {
194
+ type Width = u16 ; // TODO: Are there any with 32 bits?
195
+
147
196
fn init( & mut self , rcc: & mut Rcc ) {
148
197
$TIM:: enable( rcc) ;
149
198
$TIM:: reset( rcc) ;
0 commit comments