@@ -17,6 +17,11 @@ use void::{unreachable, Void};
17
17
#[ cfg( any( feature = "52832" , feature = "52840" ) ) ]
18
18
use crate :: target:: { TIMER3 , TIMER4 } ;
19
19
20
+ use core:: marker:: PhantomData ;
21
+
22
+ pub struct OneShotTimer ;
23
+ pub struct PeriodicTimer ;
24
+
20
25
21
26
/// Interface to a TIMER instance
22
27
///
@@ -25,15 +30,13 @@ use crate::target::{TIMER3, TIMER4};
25
30
///
26
31
/// CC[0] is used for the current/most-recent delay period and CC[1] is used
27
32
/// to grab the current value of the counter at a given instant.
28
- pub struct Timer < T > ( T ) ;
33
+ pub struct Timer < T , U > ( T , PhantomData < U > ) ;
29
34
30
- impl < T > Timer < T >
35
+ impl < T > Timer < T , OneShotTimer >
31
36
where
32
37
T : Instance ,
33
38
{
34
- pub const TICKS_PER_SECOND : u32 = 1_000_000 ;
35
-
36
- pub fn new ( timer : T ) -> Self {
39
+ pub fn new ( timer : T ) -> Timer < T , OneShotTimer > {
37
40
timer
38
41
. shorts
39
42
. write ( |w| w. compare0_clear ( ) . enabled ( ) . compare0_stop ( ) . enabled ( ) ) ;
42
45
) ;
43
46
timer. bitmode . write ( |w| w. bitmode ( ) . _32bit ( ) ) ;
44
47
45
- Timer ( timer)
48
+ Timer :: < T , OneShotTimer > ( timer, PhantomData )
46
49
}
47
50
51
+ pub fn into_periodic ( self ) -> Timer < T , PeriodicTimer > {
52
+ self . 0
53
+ . shorts
54
+ . write ( |w| w. compare0_clear ( ) . enabled ( ) . compare0_stop ( ) . disabled ( ) ) ;
55
+ Timer :: < T , PeriodicTimer > ( self . free ( ) , PhantomData )
56
+ }
57
+ }
58
+
59
+ impl < T > Timer < T , PeriodicTimer >
60
+ where
61
+ T : Instance ,
62
+ {
63
+ pub fn new_periodic ( timer : T ) -> Timer < T , PeriodicTimer > {
64
+ timer
65
+ . shorts
66
+ . write ( |w| w. compare0_clear ( ) . enabled ( ) . compare0_stop ( ) . disabled ( ) ) ;
67
+ timer. prescaler . write (
68
+ |w| unsafe { w. prescaler ( ) . bits ( 4 ) } , // 1 MHz
69
+ ) ;
70
+ timer. bitmode . write ( |w| w. bitmode ( ) . _32bit ( ) ) ;
71
+
72
+ Timer :: < T , PeriodicTimer > ( timer, PhantomData )
73
+ }
74
+
75
+ pub fn into_oneshot ( self ) -> Timer < T , OneShotTimer > {
76
+ self . 0
77
+ . shorts
78
+ . write ( |w| w. compare0_clear ( ) . enabled ( ) . compare0_stop ( ) . enabled ( ) ) ;
79
+ Timer :: < T , OneShotTimer > ( self . free ( ) , PhantomData )
80
+ }
81
+ }
82
+
83
+ impl < T , U > Timer < T , U >
84
+ where
85
+ T : Instance ,
86
+ {
87
+ pub const TICKS_PER_SECOND : u32 = 1_000_000 ;
88
+
48
89
/// Return the raw interface to the underlying timer peripheral
49
90
pub fn free ( self ) -> T {
50
91
self . 0
@@ -103,7 +144,7 @@ where
103
144
}
104
145
}
105
146
106
- impl < T > timer:: CountDown for Timer < T >
147
+ impl < T , U > timer:: CountDown for Timer < T , U >
107
148
where
108
149
T : Instance ,
109
150
{
@@ -164,7 +205,7 @@ where
164
205
}
165
206
}
166
207
167
- impl < T > timer:: Cancel for Timer < T >
208
+ impl < T , U > timer:: Cancel for Timer < T , U >
168
209
where
169
210
T : Instance ,
170
211
{
@@ -178,6 +219,11 @@ where
178
219
}
179
220
}
180
221
222
+ impl < T > timer:: Periodic for Timer < T , PeriodicTimer >
223
+ where
224
+ T : Instance ,
225
+ { }
226
+
181
227
182
228
/// Implemented by all `TIMER` instances
183
229
pub trait Instance : Deref < Target = timer0:: RegisterBlock > {
0 commit comments