1#[cfg_attr(feature = "defmt", derive(defmt::Format))]
5#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
6#[repr(u8)]
7pub enum Sequence {
8 One = 0,
10 Two = 1,
12 Three = 2,
14 Four = 3,
16 Five = 4,
18 Six = 5,
20 Seven = 6,
22 Eight = 7,
24 Nine = 8,
26 Ten = 9,
28 Eleven = 10,
30 Twelve = 11,
32 Thirteen = 12,
34 Fourteen = 13,
36 Fifteen = 14,
38 Sixteen = 15,
40}
41
42impl From<Sequence> for u8 {
43 fn from(s: Sequence) -> u8 {
44 s as _
45 }
46}
47
48impl From<u8> for Sequence {
49 fn from(bits: u8) -> Self {
50 match bits {
51 0 => Sequence::One,
52 1 => Sequence::Two,
53 2 => Sequence::Three,
54 3 => Sequence::Four,
55 4 => Sequence::Five,
56 5 => Sequence::Six,
57 6 => Sequence::Seven,
58 7 => Sequence::Eight,
59 8 => Sequence::Nine,
60 9 => Sequence::Ten,
61 10 => Sequence::Eleven,
62 11 => Sequence::Twelve,
63 12 => Sequence::Thirteen,
64 13 => Sequence::Fourteen,
65 14 => Sequence::Fifteen,
66 15 => Sequence::Sixteen,
67 _ => unimplemented!(),
68 }
69 }
70}
71
72#[cfg_attr(feature = "defmt", derive(defmt::Format))]
74#[derive(Copy, Clone, PartialEq, Eq, Debug)]
75#[repr(u8)]
76pub enum SampleTime {
77 Cycles_3 = 0,
79 Cycles_15 = 1,
81 Cycles_28 = 2,
83 Cycles_56 = 3,
85 Cycles_84 = 4,
87 Cycles_112 = 5,
89 Cycles_144 = 6,
91 Cycles_480 = 7,
93}
94
95impl From<u8> for SampleTime {
96 fn from(f: u8) -> SampleTime {
97 match f {
98 0 => SampleTime::Cycles_3,
99 1 => SampleTime::Cycles_15,
100 2 => SampleTime::Cycles_28,
101 3 => SampleTime::Cycles_56,
102 4 => SampleTime::Cycles_84,
103 5 => SampleTime::Cycles_112,
104 6 => SampleTime::Cycles_144,
105 7 => SampleTime::Cycles_480,
106 _ => unimplemented!(),
107 }
108 }
109}
110
111impl From<SampleTime> for u8 {
112 fn from(l: SampleTime) -> u8 {
113 l as _
114 }
115}
116
117#[cfg_attr(feature = "defmt", derive(defmt::Format))]
120#[derive(Copy, Clone, PartialEq, Eq, Debug)]
121#[repr(u8)]
122pub enum Clock {
123 Pclk2_div_2 = 0,
125 Pclk2_div_4 = 1,
127 Pclk2_div_6 = 2,
129 Pclk2_div_8 = 3,
131}
132
133impl From<Clock> for u8 {
134 fn from(c: Clock) -> u8 {
135 c as _
136 }
137}
138
139#[cfg_attr(feature = "defmt", derive(defmt::Format))]
141#[derive(Copy, Clone, PartialEq, Eq, Debug)]
142#[repr(u8)]
143pub enum Resolution {
144 Twelve = 0,
146 Ten = 1,
148 Eight = 2,
150 Six = 3,
152}
153impl From<Resolution> for u8 {
154 fn from(r: Resolution) -> u8 {
155 r as _
156 }
157}
158
159#[cfg_attr(feature = "defmt", derive(defmt::Format))]
161#[derive(Copy, Clone, PartialEq, Eq, Debug)]
162#[repr(u8)]
163pub enum ExternalTrigger {
164 Tim_1_cc_1 = 0b0000,
166 Tim_1_cc_2 = 0b0001,
168 Tim_1_cc_3 = 0b0010,
170 Tim_2_cc_2 = 0b0011,
172 Tim_2_cc_3 = 0b0100,
174 Tim_2_cc_4 = 0b0101,
176 Tim_2_trgo = 0b0110,
178 Tim_3_cc_1 = 0b0111,
180 Tim_3_trgo = 0b1000,
182 Tim_4_cc_4 = 0b1001,
184 Tim_5_cc_1 = 0b1010,
186 Tim_5_cc_2 = 0b1011,
188 Tim_5_cc_3 = 0b1100,
190 Exti_11 = 0b1111,
192}
193impl From<ExternalTrigger> for u8 {
194 fn from(et: ExternalTrigger) -> u8 {
195 et as _
196 }
197}
198
199#[cfg_attr(feature = "defmt", derive(defmt::Format))]
201#[derive(Copy, Clone, PartialEq, Eq, Debug)]
202#[repr(u8)]
203pub enum TriggerMode {
204 Disabled = 0,
206 RisingEdge = 1,
208 FallingEdge = 2,
210 BothEdges = 3,
212}
213impl From<TriggerMode> for u8 {
214 fn from(tm: TriggerMode) -> u8 {
215 tm as _
216 }
217}
218
219#[cfg_attr(feature = "defmt", derive(defmt::Format))]
221#[derive(Copy, Clone, PartialEq, Eq, Debug)]
222pub enum Align {
223 Right,
225 Left,
227}
228impl From<Align> for bool {
229 fn from(a: Align) -> bool {
230 match a {
231 Align::Right => false,
232 Align::Left => true,
233 }
234 }
235}
236
237#[cfg_attr(feature = "defmt", derive(defmt::Format))]
239#[derive(Copy, Clone, PartialEq, Eq, Debug)]
240pub enum Scan {
241 Disabled,
243 Enabled,
245}
246impl From<Scan> for bool {
247 fn from(s: Scan) -> bool {
248 match s {
249 Scan::Disabled => false,
250 Scan::Enabled => true,
251 }
252 }
253}
254
255#[cfg_attr(feature = "defmt", derive(defmt::Format))]
257#[derive(Copy, Clone, PartialEq, Eq, Debug)]
258pub enum Continuous {
259 Single,
261 Continuous,
263}
264impl From<Continuous> for bool {
265 fn from(c: Continuous) -> bool {
266 match c {
267 Continuous::Single => false,
268 Continuous::Continuous => true,
269 }
270 }
271}
272
273#[cfg_attr(feature = "defmt", derive(defmt::Format))]
275#[derive(Copy, Clone, PartialEq, Eq, Debug)]
276pub enum Dma {
277 Disabled,
279 Single,
281 Continuous,
283}
284
285#[cfg_attr(feature = "defmt", derive(defmt::Format))]
287#[derive(Copy, Clone, PartialEq, Eq, Debug)]
288pub enum Eoc {
289 Disabled,
291 Conversion,
293 Sequence,
295}
296
297#[cfg_attr(feature = "defmt", derive(defmt::Format))]
301#[derive(Copy, Clone, PartialEq, Eq, Debug)]
302pub struct AdcConfig {
303 pub(crate) clock: Clock,
304 pub(crate) resolution: Resolution,
305 pub(crate) align: Align,
306 pub(crate) scan: Scan,
307 pub(crate) external_trigger: (TriggerMode, ExternalTrigger),
308 pub(crate) continuous: Continuous,
309 pub(crate) dma: Dma,
310 pub(crate) end_of_conversion_interrupt: Eoc,
311 pub(crate) default_sample_time: SampleTime,
312 pub(crate) vdda: Option<u32>,
313}
314
315impl AdcConfig {
316 pub fn clock(mut self, clock: Clock) -> Self {
318 self.clock = clock;
319 self
320 }
321 pub fn resolution(mut self, resolution: Resolution) -> Self {
323 self.resolution = resolution;
324 self
325 }
326 pub fn align(mut self, align: Align) -> Self {
328 self.align = align;
329 self
330 }
331 pub fn scan(mut self, scan: Scan) -> Self {
333 self.scan = scan;
334 self
335 }
336 pub fn external_trigger(mut self, trigger_mode: TriggerMode, trigger: ExternalTrigger) -> Self {
338 self.external_trigger = (trigger_mode, trigger);
339 self
340 }
341 pub fn continuous(mut self, continuous: Continuous) -> Self {
343 self.continuous = continuous;
344 self
345 }
346 pub fn dma(mut self, dma: Dma) -> Self {
348 self.dma = dma;
349 self
350 }
351 pub fn end_of_conversion_interrupt(mut self, end_of_conversion_interrupt: Eoc) -> Self {
353 self.end_of_conversion_interrupt = end_of_conversion_interrupt;
354 self
355 }
356 pub fn default_sample_time(mut self, default_sample_time: SampleTime) -> Self {
358 self.default_sample_time = default_sample_time;
359 self
360 }
361
362 pub fn reference_voltage(mut self, vdda_mv: u32) -> Self {
367 self.vdda = Some(vdda_mv);
368 self
369 }
370}
371
372impl Default for AdcConfig {
373 fn default() -> Self {
374 Self {
375 clock: Clock::Pclk2_div_2,
376 resolution: Resolution::Twelve,
377 align: Align::Right,
378 scan: Scan::Disabled,
379 external_trigger: (TriggerMode::Disabled, ExternalTrigger::Tim_1_cc_1),
380 continuous: Continuous::Single,
381 dma: Dma::Disabled,
382 end_of_conversion_interrupt: Eoc::Disabled,
383 default_sample_time: SampleTime::Cycles_480,
384 vdda: None,
385 }
386 }
387}