Skip to content

Commit 4558d0f

Browse files
committed
simplify
1 parent 7e46586 commit 4558d0f

File tree

2 files changed

+73
-124
lines changed

2 files changed

+73
-124
lines changed

examples/rtc_alarm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use panic_halt as _;
99
use rtt_target::{rprintln, rtt_init_print};
1010

1111
use crate::hal::prelude::*;
12-
use crate::hal::rtc::{Alarm, AlarmDay, Event, Rtc};
12+
use crate::hal::rtc::{Alarm, Event, Rtc};
1313
use cortex_m::interrupt::{free, Mutex};
1414
use time::{
1515
macros::{date, time},
@@ -40,7 +40,7 @@ fn main() -> ! {
4040
.unwrap();
4141

4242
// Set alarm A for 1 minute
43-
rtc.set_alarm(Alarm::AlarmA, AlarmDay::Date(today), time!(21:58:32))
43+
rtc.set_alarm(Alarm::AlarmA, today, time!(21:58:32))
4444
.unwrap();
4545
rtc.enable_wakeup(8.secs());
4646
rtc.listen(&mut p.EXTI, Event::AlarmA);

src/adc.rs

Lines changed: 71 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -44,61 +44,45 @@ pub mod config {
4444
/// The place in the sequence a given channel should be captured
4545
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
4646
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
47+
#[repr(u8)]
4748
pub enum Sequence {
4849
/// 1
49-
One,
50+
One = 0,
5051
/// 2
51-
Two,
52+
Two = 1,
5253
/// 3
53-
Three,
54+
Three = 2,
5455
/// 4
55-
Four,
56+
Four = 3,
5657
/// 5
57-
Five,
58+
Five = 4,
5859
/// 6
59-
Six,
60+
Six = 5,
6061
/// 7
61-
Seven,
62+
Seven = 6,
6263
/// 8
63-
Eight,
64+
Eight = 7,
6465
/// 9
65-
Nine,
66+
Nine = 8,
6667
/// 10
67-
Ten,
68+
Ten = 9,
6869
/// 11
69-
Eleven,
70+
Eleven = 10,
7071
/// 12
71-
Twelve,
72+
Twelve = 11,
7273
/// 13
73-
Thirteen,
74+
Thirteen = 12,
7475
/// 14
75-
Fourteen,
76+
Fourteen = 13,
7677
/// 15
77-
Fifteen,
78+
Fifteen = 14,
7879
/// 16
79-
Sixteen,
80+
Sixteen = 15,
8081
}
8182

8283
impl From<Sequence> for u8 {
8384
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 _
10286
}
10387
}
10488

@@ -129,23 +113,24 @@ pub mod config {
129113
/// The number of cycles to sample a given channel for
130114
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
131115
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
116+
#[repr(u8)]
132117
pub enum SampleTime {
133118
/// 3 cycles
134-
Cycles_3,
119+
Cycles_3 = 0,
135120
/// 15 cycles
136-
Cycles_15,
121+
Cycles_15 = 1,
137122
/// 28 cycles
138-
Cycles_28,
123+
Cycles_28 = 2,
139124
/// 56 cycles
140-
Cycles_56,
125+
Cycles_56 = 3,
141126
/// 84 cycles
142-
Cycles_84,
127+
Cycles_84 = 4,
143128
/// 112 cycles
144-
Cycles_112,
129+
Cycles_112 = 5,
145130
/// 144 cycles
146-
Cycles_144,
131+
Cycles_144 = 6,
147132
/// 480 cycles
148-
Cycles_480,
133+
Cycles_480 = 7,
149134
}
150135

151136
impl From<u8> for SampleTime {
@@ -166,144 +151,109 @@ pub mod config {
166151

167152
impl From<SampleTime> for u8 {
168153
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 _
179155
}
180156
}
181157

182158
/// Clock config for the ADC
183159
/// Check the datasheet for the maximum speed the ADC supports
184160
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
185161
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
162+
#[repr(u8)]
186163
pub enum Clock {
187164
/// PCLK2 (APB2) divided by 2
188-
Pclk2_div_2,
165+
Pclk2_div_2 = 0,
189166
/// PCLK2 (APB2) divided by 4
190-
Pclk2_div_4,
167+
Pclk2_div_4 = 1,
191168
/// PCLK2 (APB2) divided by 6
192-
Pclk2_div_6,
169+
Pclk2_div_6 = 2,
193170
/// PCLK2 (APB2) divided by 8
194-
Pclk2_div_8,
171+
Pclk2_div_8 = 3,
195172
}
196173

197174
impl From<Clock> for u8 {
198175
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 _
205177
}
206178
}
207179

208180
/// Resolution to sample at
209181
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
210182
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
183+
#[repr(u8)]
211184
pub enum Resolution {
212185
/// 12-bit
213-
Twelve,
186+
Twelve = 0,
214187
/// 10-bit
215-
Ten,
188+
Ten = 1,
216189
/// 8-bit
217-
Eight,
190+
Eight = 2,
218191
/// 6-bit
219-
Six,
192+
Six = 3,
220193
}
221194
impl From<Resolution> for u8 {
222195
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 _
229197
}
230198
}
231199

232200
/// Possible external triggers the ADC can listen to
233201
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
234202
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
203+
#[repr(u8)]
235204
pub enum ExternalTrigger {
236205
/// TIM1 compare channel 1
237-
Tim_1_cc_1,
206+
Tim_1_cc_1 = 0b0000,
238207
/// TIM1 compare channel 2
239-
Tim_1_cc_2,
208+
Tim_1_cc_2 = 0b0001,
240209
/// TIM1 compare channel 3
241-
Tim_1_cc_3,
210+
Tim_1_cc_3 = 0b0010,
242211
/// TIM2 compare channel 2
243-
Tim_2_cc_2,
212+
Tim_2_cc_2 = 0b0011,
244213
/// TIM2 compare channel 3
245-
Tim_2_cc_3,
214+
Tim_2_cc_3 = 0b0100,
246215
/// TIM2 compare channel 4
247-
Tim_2_cc_4,
216+
Tim_2_cc_4 = 0b0101,
248217
/// TIM2 trigger out
249-
Tim_2_trgo,
218+
Tim_2_trgo = 0b0110,
250219
/// TIM3 compare channel 1
251-
Tim_3_cc_1,
220+
Tim_3_cc_1 = 0b0111,
252221
/// TIM3 trigger out
253-
Tim_3_trgo,
222+
Tim_3_trgo = 0b1000,
254223
/// TIM4 compare channel 4
255-
Tim_4_cc_4,
224+
Tim_4_cc_4 = 0b1001,
256225
/// TIM5 compare channel 1
257-
Tim_5_cc_1,
226+
Tim_5_cc_1 = 0b1010,
258227
/// TIM5 compare channel 2
259-
Tim_5_cc_2,
228+
Tim_5_cc_2 = 0b1011,
260229
/// TIM5 compare channel 3
261-
Tim_5_cc_3,
230+
Tim_5_cc_3 = 0b1100,
262231
/// External interrupt line 11
263-
Exti_11,
232+
Exti_11 = 0b1111,
264233
}
265234
impl From<ExternalTrigger> for u8 {
266235
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 _
283237
}
284238
}
285239

286240
/// Possible trigger modes
287241
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
288242
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
243+
#[repr(u8)]
289244
pub enum TriggerMode {
290245
/// Don't listen to external trigger
291-
Disabled,
246+
Disabled = 0,
292247
/// Listen for rising edges of external trigger
293-
RisingEdge,
248+
RisingEdge = 1,
294249
/// Listen for falling edges of external trigger
295-
FallingEdge,
250+
FallingEdge = 2,
296251
/// Listen for both rising and falling edges of external trigger
297-
BothEdges,
252+
BothEdges = 3,
298253
}
299254
impl From<TriggerMode> for u8 {
300255
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 _
307257
}
308258
}
309259

@@ -806,7 +756,7 @@ macro_rules! adc {
806756
self.config.clock = clock;
807757
unsafe {
808758
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 _));
810760
}
811761
}
812762

@@ -819,7 +769,7 @@ macro_rules! adc {
819769
config::Resolution::Six => (1 << 6),
820770
};
821771
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 _));
823773
}
824774

825775
/// Sets the DR register alignment to left or right
@@ -843,17 +793,17 @@ macro_rules! adc {
843793
feature = "stm32f411",
844794
))] // TODO: fix pac
845795
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 _)
848798
});
849799
#[cfg(not(any(
850800
feature = "stm32f401",
851801
feature = "stm32f410",
852802
feature = "stm32f411",
853803
)))]
854804
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 _)
857807
);
858808
}
859809

@@ -972,9 +922,8 @@ macro_rules! adc {
972922
}
973923

974924
//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;
978927
match channel {
979928
0..=9 => self.adc_reg.smpr2.modify(|r, w| unsafe { w.bits(replace_bits(r.bits(), ch, 3, st)) }),
980929
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 {
996945

997946
/// Make a converter for samples to millivolts
998947
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;
1000949
let max_sample=self.max_sample;
1001950
move |sample| {
1002951
((u32::from(sample) * calibrated_vdda) / max_sample) as u16

0 commit comments

Comments
 (0)