@@ -44,61 +44,45 @@ pub mod config {
44
44
/// The place in the sequence a given channel should be captured
45
45
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
46
46
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Debug ) ]
47
+ #[ repr( u8 ) ]
47
48
pub enum Sequence {
48
49
/// 1
49
- One ,
50
+ One = 0 ,
50
51
/// 2
51
- Two ,
52
+ Two = 1 ,
52
53
/// 3
53
- Three ,
54
+ Three = 2 ,
54
55
/// 4
55
- Four ,
56
+ Four = 3 ,
56
57
/// 5
57
- Five ,
58
+ Five = 4 ,
58
59
/// 6
59
- Six ,
60
+ Six = 5 ,
60
61
/// 7
61
- Seven ,
62
+ Seven = 6 ,
62
63
/// 8
63
- Eight ,
64
+ Eight = 7 ,
64
65
/// 9
65
- Nine ,
66
+ Nine = 8 ,
66
67
/// 10
67
- Ten ,
68
+ Ten = 9 ,
68
69
/// 11
69
- Eleven ,
70
+ Eleven = 10 ,
70
71
/// 12
71
- Twelve ,
72
+ Twelve = 11 ,
72
73
/// 13
73
- Thirteen ,
74
+ Thirteen = 12 ,
74
75
/// 14
75
- Fourteen ,
76
+ Fourteen = 13 ,
76
77
/// 15
77
- Fifteen ,
78
+ Fifteen = 14 ,
78
79
/// 16
79
- Sixteen ,
80
+ Sixteen = 15 ,
80
81
}
81
82
82
83
impl From < Sequence > for u8 {
83
84
fn from ( s : Sequence ) -> u8 {
84
- match s {
85
- Sequence :: One => 0 ,
86
- Sequence :: Two => 1 ,
87
- Sequence :: Three => 2 ,
88
- Sequence :: Four => 3 ,
89
- Sequence :: Five => 4 ,
90
- Sequence :: Six => 5 ,
91
- Sequence :: Seven => 6 ,
92
- Sequence :: Eight => 7 ,
93
- Sequence :: Nine => 8 ,
94
- Sequence :: Ten => 9 ,
95
- Sequence :: Eleven => 10 ,
96
- Sequence :: Twelve => 11 ,
97
- Sequence :: Thirteen => 12 ,
98
- Sequence :: Fourteen => 13 ,
99
- Sequence :: Fifteen => 14 ,
100
- Sequence :: Sixteen => 15 ,
101
- }
85
+ s as _
102
86
}
103
87
}
104
88
@@ -129,23 +113,24 @@ pub mod config {
129
113
/// The number of cycles to sample a given channel for
130
114
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
131
115
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
116
+ #[ repr( u8 ) ]
132
117
pub enum SampleTime {
133
118
/// 3 cycles
134
- Cycles_3 ,
119
+ Cycles_3 = 0 ,
135
120
/// 15 cycles
136
- Cycles_15 ,
121
+ Cycles_15 = 1 ,
137
122
/// 28 cycles
138
- Cycles_28 ,
123
+ Cycles_28 = 2 ,
139
124
/// 56 cycles
140
- Cycles_56 ,
125
+ Cycles_56 = 3 ,
141
126
/// 84 cycles
142
- Cycles_84 ,
127
+ Cycles_84 = 4 ,
143
128
/// 112 cycles
144
- Cycles_112 ,
129
+ Cycles_112 = 5 ,
145
130
/// 144 cycles
146
- Cycles_144 ,
131
+ Cycles_144 = 6 ,
147
132
/// 480 cycles
148
- Cycles_480 ,
133
+ Cycles_480 = 7 ,
149
134
}
150
135
151
136
impl From < u8 > for SampleTime {
@@ -166,144 +151,109 @@ pub mod config {
166
151
167
152
impl From < SampleTime > for u8 {
168
153
fn from ( l : SampleTime ) -> u8 {
169
- match l {
170
- SampleTime :: Cycles_3 => 0 ,
171
- SampleTime :: Cycles_15 => 1 ,
172
- SampleTime :: Cycles_28 => 2 ,
173
- SampleTime :: Cycles_56 => 3 ,
174
- SampleTime :: Cycles_84 => 4 ,
175
- SampleTime :: Cycles_112 => 5 ,
176
- SampleTime :: Cycles_144 => 6 ,
177
- SampleTime :: Cycles_480 => 7 ,
178
- }
154
+ l as _
179
155
}
180
156
}
181
157
182
158
/// Clock config for the ADC
183
159
/// Check the datasheet for the maximum speed the ADC supports
184
160
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
185
161
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
162
+ #[ repr( u8 ) ]
186
163
pub enum Clock {
187
164
/// PCLK2 (APB2) divided by 2
188
- Pclk2_div_2 ,
165
+ Pclk2_div_2 = 0 ,
189
166
/// PCLK2 (APB2) divided by 4
190
- Pclk2_div_4 ,
167
+ Pclk2_div_4 = 1 ,
191
168
/// PCLK2 (APB2) divided by 6
192
- Pclk2_div_6 ,
169
+ Pclk2_div_6 = 2 ,
193
170
/// PCLK2 (APB2) divided by 8
194
- Pclk2_div_8 ,
171
+ Pclk2_div_8 = 3 ,
195
172
}
196
173
197
174
impl From < Clock > for u8 {
198
175
fn from ( c : Clock ) -> u8 {
199
- match c {
200
- Clock :: Pclk2_div_2 => 0 ,
201
- Clock :: Pclk2_div_4 => 1 ,
202
- Clock :: Pclk2_div_6 => 2 ,
203
- Clock :: Pclk2_div_8 => 3 ,
204
- }
176
+ c as _
205
177
}
206
178
}
207
179
208
180
/// Resolution to sample at
209
181
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
210
182
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
183
+ #[ repr( u8 ) ]
211
184
pub enum Resolution {
212
185
/// 12-bit
213
- Twelve ,
186
+ Twelve = 0 ,
214
187
/// 10-bit
215
- Ten ,
188
+ Ten = 1 ,
216
189
/// 8-bit
217
- Eight ,
190
+ Eight = 2 ,
218
191
/// 6-bit
219
- Six ,
192
+ Six = 3 ,
220
193
}
221
194
impl From < Resolution > for u8 {
222
195
fn from ( r : Resolution ) -> u8 {
223
- match r {
224
- Resolution :: Twelve => 0 ,
225
- Resolution :: Ten => 1 ,
226
- Resolution :: Eight => 2 ,
227
- Resolution :: Six => 3 ,
228
- }
196
+ r as _
229
197
}
230
198
}
231
199
232
200
/// Possible external triggers the ADC can listen to
233
201
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
234
202
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
203
+ #[ repr( u8 ) ]
235
204
pub enum ExternalTrigger {
236
205
/// TIM1 compare channel 1
237
- Tim_1_cc_1 ,
206
+ Tim_1_cc_1 = 0b0000 ,
238
207
/// TIM1 compare channel 2
239
- Tim_1_cc_2 ,
208
+ Tim_1_cc_2 = 0b0001 ,
240
209
/// TIM1 compare channel 3
241
- Tim_1_cc_3 ,
210
+ Tim_1_cc_3 = 0b0010 ,
242
211
/// TIM2 compare channel 2
243
- Tim_2_cc_2 ,
212
+ Tim_2_cc_2 = 0b0011 ,
244
213
/// TIM2 compare channel 3
245
- Tim_2_cc_3 ,
214
+ Tim_2_cc_3 = 0b0100 ,
246
215
/// TIM2 compare channel 4
247
- Tim_2_cc_4 ,
216
+ Tim_2_cc_4 = 0b0101 ,
248
217
/// TIM2 trigger out
249
- Tim_2_trgo ,
218
+ Tim_2_trgo = 0b0110 ,
250
219
/// TIM3 compare channel 1
251
- Tim_3_cc_1 ,
220
+ Tim_3_cc_1 = 0b0111 ,
252
221
/// TIM3 trigger out
253
- Tim_3_trgo ,
222
+ Tim_3_trgo = 0b1000 ,
254
223
/// TIM4 compare channel 4
255
- Tim_4_cc_4 ,
224
+ Tim_4_cc_4 = 0b1001 ,
256
225
/// TIM5 compare channel 1
257
- Tim_5_cc_1 ,
226
+ Tim_5_cc_1 = 0b1010 ,
258
227
/// TIM5 compare channel 2
259
- Tim_5_cc_2 ,
228
+ Tim_5_cc_2 = 0b1011 ,
260
229
/// TIM5 compare channel 3
261
- Tim_5_cc_3 ,
230
+ Tim_5_cc_3 = 0b1100 ,
262
231
/// External interrupt line 11
263
- Exti_11 ,
232
+ Exti_11 = 0b1111 ,
264
233
}
265
234
impl From < ExternalTrigger > for u8 {
266
235
fn from ( et : ExternalTrigger ) -> u8 {
267
- match et {
268
- ExternalTrigger :: Tim_1_cc_1 => 0b0000 ,
269
- ExternalTrigger :: Tim_1_cc_2 => 0b0001 ,
270
- ExternalTrigger :: Tim_1_cc_3 => 0b0010 ,
271
- ExternalTrigger :: Tim_2_cc_2 => 0b0011 ,
272
- ExternalTrigger :: Tim_2_cc_3 => 0b0100 ,
273
- ExternalTrigger :: Tim_2_cc_4 => 0b0101 ,
274
- ExternalTrigger :: Tim_2_trgo => 0b0110 ,
275
- ExternalTrigger :: Tim_3_cc_1 => 0b0111 ,
276
- ExternalTrigger :: Tim_3_trgo => 0b1000 ,
277
- ExternalTrigger :: Tim_4_cc_4 => 0b1001 ,
278
- ExternalTrigger :: Tim_5_cc_1 => 0b1010 ,
279
- ExternalTrigger :: Tim_5_cc_2 => 0b1011 ,
280
- ExternalTrigger :: Tim_5_cc_3 => 0b1100 ,
281
- ExternalTrigger :: Exti_11 => 0b1111 ,
282
- }
236
+ et as _
283
237
}
284
238
}
285
239
286
240
/// Possible trigger modes
287
241
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
288
242
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
243
+ #[ repr( u8 ) ]
289
244
pub enum TriggerMode {
290
245
/// Don't listen to external trigger
291
- Disabled ,
246
+ Disabled = 0 ,
292
247
/// Listen for rising edges of external trigger
293
- RisingEdge ,
248
+ RisingEdge = 1 ,
294
249
/// Listen for falling edges of external trigger
295
- FallingEdge ,
250
+ FallingEdge = 2 ,
296
251
/// Listen for both rising and falling edges of external trigger
297
- BothEdges ,
252
+ BothEdges = 3 ,
298
253
}
299
254
impl From < TriggerMode > for u8 {
300
255
fn from ( tm : TriggerMode ) -> u8 {
301
- match tm {
302
- TriggerMode :: Disabled => 0 ,
303
- TriggerMode :: RisingEdge => 1 ,
304
- TriggerMode :: FallingEdge => 2 ,
305
- TriggerMode :: BothEdges => 3 ,
306
- }
256
+ tm as _
307
257
}
308
258
}
309
259
@@ -806,7 +756,7 @@ macro_rules! adc {
806
756
self . config. clock = clock;
807
757
unsafe {
808
758
let common = & ( * pac:: $common_type:: ptr( ) ) ;
809
- common. ccr. modify( |_, w| w. adcpre( ) . bits( clock. into ( ) ) ) ;
759
+ common. ccr. modify( |_, w| w. adcpre( ) . bits( clock as _ ) ) ;
810
760
}
811
761
}
812
762
@@ -819,7 +769,7 @@ macro_rules! adc {
819
769
config:: Resolution :: Six => ( 1 << 6 ) ,
820
770
} ;
821
771
self . config. resolution = resolution;
822
- self . adc_reg. cr1. modify( |_, w| w. res( ) . bits( resolution. into ( ) ) ) ;
772
+ self . adc_reg. cr1. modify( |_, w| w. res( ) . bits( resolution as _ ) ) ;
823
773
}
824
774
825
775
/// Sets the DR register alignment to left or right
@@ -843,17 +793,17 @@ macro_rules! adc {
843
793
feature = "stm32f411" ,
844
794
) ) ] // TODO: fix pac
845
795
self . adc_reg. cr2. modify( |_, w| unsafe { w
846
- . extsel( ) . bits( extsel. into ( ) )
847
- . exten( ) . bits( edge. into ( ) )
796
+ . extsel( ) . bits( extsel as _ )
797
+ . exten( ) . bits( edge as _ )
848
798
} ) ;
849
799
#[ cfg( not( any(
850
800
feature = "stm32f401" ,
851
801
feature = "stm32f410" ,
852
802
feature = "stm32f411" ,
853
803
) ) ) ]
854
804
self . adc_reg. cr2. modify( |_, w| w
855
- . extsel( ) . bits( extsel. into ( ) )
856
- . exten( ) . bits( edge. into ( ) )
805
+ . extsel( ) . bits( extsel as _ )
806
+ . exten( ) . bits( edge as _ )
857
807
) ;
858
808
}
859
809
@@ -972,9 +922,8 @@ macro_rules! adc {
972
922
}
973
923
974
924
//Set the sample time for the channel
975
- let st = u8 :: from( sample_time) ;
976
- let st = u32 :: from( st) ;
977
- let ch = u32 :: from( channel) ;
925
+ let st = sample_time as u32 ;
926
+ let ch = channel as u32 ;
978
927
match channel {
979
928
0 ..=9 => self . adc_reg. smpr2. modify( |r, w| unsafe { w. bits( replace_bits( r. bits( ) , ch, 3 , st) ) } ) ,
980
929
10 ..=18 => self . adc_reg. smpr1. modify( |r, w| unsafe { w. bits( replace_bits( r. bits( ) , ch-10 , 3 , st) ) } ) ,
@@ -996,7 +945,7 @@ macro_rules! adc {
996
945
997
946
/// Make a converter for samples to millivolts
998
947
pub fn make_sample_to_millivolts( & self ) -> impl Fn ( u16 ) ->u16 {
999
- let calibrated_vdda= self . calibrated_vdda;
948
+ let calibrated_vdda = self . calibrated_vdda;
1000
949
let max_sample=self . max_sample;
1001
950
move |sample| {
1002
951
( ( u32 :: from( sample) * calibrated_vdda) / max_sample) as u16
0 commit comments