summaryrefslogtreecommitdiff
path: root/embassy-stm32/src/adc/v4.rs
blob: d356d7b667dd3572698e6f57c6296ce4ab2a25d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use core::marker::PhantomData;

use atomic_polyfill::{AtomicU8, Ordering};
use embedded_hal_02::blocking::delay::DelayUs;
use pac::adc::vals::{Adcaldif, Boost, Difsel, Exten, Pcsel};
use pac::adccommon::vals::Presc;

use super::{AdcPin, Instance};
use crate::time::Hertz;
use crate::{pac, Peripheral};

/// Default VREF voltage used for sample conversion to millivolts.
pub const VREF_DEFAULT_MV: u32 = 3300;
/// VREF voltage used for factory calibration of VREFINTCAL register.
pub const VREF_CALIB_MV: u32 = 3300;

pub enum Resolution {
    SixteenBit,
    FourteenBit,
    TwelveBit,
    TenBit,
    EightBit,
}

impl Default for Resolution {
    fn default() -> Self {
        Self::SixteenBit
    }
}

impl Resolution {
    fn res(&self) -> pac::adc::vals::Res {
        match self {
            Resolution::SixteenBit => pac::adc::vals::Res::SIXTEENBIT,
            Resolution::FourteenBit => pac::adc::vals::Res::FOURTEENBITV,
            Resolution::TwelveBit => pac::adc::vals::Res::TWELVEBITV,
            Resolution::TenBit => pac::adc::vals::Res::TENBIT,
            Resolution::EightBit => pac::adc::vals::Res::EIGHTBIT,
        }
    }

    pub fn to_max_count(&self) -> u32 {
        match self {
            Resolution::SixteenBit => (1 << 16) - 1,
            Resolution::FourteenBit => (1 << 14) - 1,
            Resolution::TwelveBit => (1 << 12) - 1,
            Resolution::TenBit => (1 << 10) - 1,
            Resolution::EightBit => (1 << 8) - 1,
        }
    }
}

pub trait InternalChannel<T>: sealed::InternalChannel<T> {}

mod sealed {
    pub trait InternalChannel<T> {
        fn channel(&self) -> u8;
    }
}

// NOTE: Vrefint/Temperature/Vbat are only available on ADC3 on H7, this currently cannot be modeled with stm32-data, so these are available from the software on all ADCs
pub struct VrefInt;
impl<T: Instance> InternalChannel<T> for VrefInt {}
impl<T: Instance> sealed::InternalChannel<T> for VrefInt {
    fn channel(&self) -> u8 {
        19
    }
}

pub struct Temperature;
impl<T: Instance> InternalChannel<T> for Temperature {}
impl<T: Instance> sealed::InternalChannel<T> for Temperature {
    fn channel(&self) -> u8 {
        18
    }
}

pub struct Vbat;
impl<T: Instance> InternalChannel<T> for Vbat {}
impl<T: Instance> sealed::InternalChannel<T> for Vbat {
    fn channel(&self) -> u8 {
        // TODO this should be 14 for H7a/b/35
        17
    }
}

static ADC12_ENABLE_COUNTER: AtomicU8 = AtomicU8::new(0);

#[cfg(stm32h7)]
foreach_peripheral!(
    (adc, ADC1) => {
        impl crate::rcc::sealed::RccPeripheral for crate::peripherals::ADC1 {
            fn frequency() -> crate::time::Hertz {
                critical_section::with(|_| unsafe {
                    match crate::rcc::get_freqs().adc {
                        Some(ck) => ck,
                        None => panic!("Invalid ADC clock configuration, AdcClockSource was likely not properly configured.")
                    }
                })
            }

            fn enable() {
                critical_section::with(|_| unsafe {
                    crate::pac::RCC.ahb1enr().modify(|w| w.set_adc12en(true))
                });
                ADC12_ENABLE_COUNTER.fetch_add(1, Ordering::SeqCst);
            }

            fn disable() {
                if ADC12_ENABLE_COUNTER.load(Ordering::SeqCst) == 1 {
                    critical_section::with(|_| unsafe {
                        crate::pac::RCC.ahb1enr().modify(|w| w.set_adc12en(false));
                    })
                }
                ADC12_ENABLE_COUNTER.fetch_sub(1, Ordering::SeqCst);
            }

            fn reset() {
                if ADC12_ENABLE_COUNTER.load(Ordering::SeqCst) == 1 {
                    critical_section::with(|_| unsafe {
                        crate::pac::RCC.ahb1rstr().modify(|w| w.set_adc12rst(true));
                        crate::pac::RCC.ahb1rstr().modify(|w| w.set_adc12rst(false));
                    });
                }
            }
        }

        impl crate::rcc::RccPeripheral for crate::peripherals::ADC1 {}
    };
    (adc, ADC2) => {
        impl crate::rcc::sealed::RccPeripheral for crate::peripherals::ADC2 {
            fn frequency() -> crate::time::Hertz {
                critical_section::with(|_| unsafe {
                    match crate::rcc::get_freqs().adc {
                        Some(ck) => ck,
                        None => panic!("Invalid ADC clock configuration, AdcClockSource was likely not properly configured.")
                    }
                })
            }

            fn enable() {
                critical_section::with(|_| unsafe {
                    crate::pac::RCC.ahb1enr().modify(|w| w.set_adc12en(true))
                });
                ADC12_ENABLE_COUNTER.fetch_add(1, Ordering::SeqCst);
            }

            fn disable() {
                if ADC12_ENABLE_COUNTER.load(Ordering::SeqCst) == 1 {
                    critical_section::with(|_| unsafe {
                        crate::pac::RCC.ahb1enr().modify(|w| w.set_adc12en(false));
                    })
                }
                ADC12_ENABLE_COUNTER.fetch_sub(1, Ordering::SeqCst);
            }

            fn reset() {
                if ADC12_ENABLE_COUNTER.load(Ordering::SeqCst) == 1 {
                    critical_section::with(|_| unsafe {
                        crate::pac::RCC.ahb1rstr().modify(|w| w.set_adc12rst(true));
                        crate::pac::RCC.ahb1rstr().modify(|w| w.set_adc12rst(false));
                    });
                }
            }
        }

        impl crate::rcc::RccPeripheral for crate::peripherals::ADC2 {}
    };
    (adc, ADC3) => {
        impl crate::rcc::sealed::RccPeripheral for crate::peripherals::ADC3 {
            fn frequency() -> crate::time::Hertz {
                critical_section::with(|_| unsafe {
                    match crate::rcc::get_freqs().adc {
                        Some(ck) => ck,
                        None => panic!("Invalid ADC clock configuration, AdcClockSource was likely not properly configured.")
                    }
                })
            }

            fn enable() {
                critical_section::with(|_| unsafe {
                    crate::pac::RCC.ahb4enr().modify(|w| w.set_adc3en(true))
                });
            }

            fn disable() {
                    critical_section::with(|_| unsafe {
                        crate::pac::RCC.ahb4enr().modify(|w| w.set_adc3en(false));
                    })
            }

            fn reset() {
                    critical_section::with(|_| unsafe {
                        crate::pac::RCC.ahb4rstr().modify(|w| w.set_adc3rst(true));
                        crate::pac::RCC.ahb4rstr().modify(|w| w.set_adc3rst(false));
                    });
            }
        }

        impl crate::rcc::RccPeripheral for crate::peripherals::ADC3 {}
    };
);

/// ADC sample time
///
/// The default setting is 2.5 ADC clock cycles.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum SampleTime {
    /// 1.5 ADC clock cycles
    Cycles1_5,

    /// 2.5 ADC clock cycles
    Cycles2_5,

    /// 8.5 ADC clock cycles
    Cycles8_5,

    /// 16.5 ADC clock cycles
    Cycles16_5,

    /// 32.5 ADC clock cycles
    Cycles32_5,

    /// 64.5 ADC clock cycles
    Cycles64_5,

    /// 387.5 ADC clock cycles
    Cycles387_5,

    /// 810.5 ADC clock cycles
    Cycles810_5,
}

impl SampleTime {
    pub(crate) fn sample_time(&self) -> pac::adc::vals::Smp {
        match self {
            SampleTime::Cycles1_5 => pac::adc::vals::Smp::CYCLES1_5,
            SampleTime::Cycles2_5 => pac::adc::vals::Smp::CYCLES2_5,
            SampleTime::Cycles8_5 => pac::adc::vals::Smp::CYCLES8_5,
            SampleTime::Cycles16_5 => pac::adc::vals::Smp::CYCLES16_5,
            SampleTime::Cycles32_5 => pac::adc::vals::Smp::CYCLES32_5,
            SampleTime::Cycles64_5 => pac::adc::vals::Smp::CYCLES64_5,
            SampleTime::Cycles387_5 => pac::adc::vals::Smp::CYCLES387_5,
            SampleTime::Cycles810_5 => pac::adc::vals::Smp::CYCLES810_5,
        }
    }
}

impl Default for SampleTime {
    fn default() -> Self {
        Self::Cycles1_5
    }
}

// NOTE (unused): The prescaler enum closely copies the hardware capabilities,
// but high prescaling doesn't make a lot of sense in the current implementation and is ommited.
#[allow(unused)]
enum Prescaler {
    NotDivided,
    DividedBy2,
    DividedBy4,
    DividedBy6,
    DividedBy8,
    DividedBy10,
    DividedBy12,
    DividedBy16,
    DividedBy32,
    DividedBy64,
    DividedBy128,
    DividedBy256,
}

impl Prescaler {
    fn from_ker_ck(frequency: Hertz) -> Self {
        let raw_prescaler = frequency.0 / 50_000_000;
        match raw_prescaler {
            0 => Self::NotDivided,
            1 => Self::DividedBy2,
            2..=3 => Self::DividedBy4,
            4..=5 => Self::DividedBy6,
            6..=7 => Self::DividedBy8,
            8..=9 => Self::DividedBy10,
            10..=11 => Self::DividedBy12,
            _ => unimplemented!(),
        }
    }

    fn divisor(&self) -> u32 {
        match self {
            Prescaler::NotDivided => 1,
            Prescaler::DividedBy2 => 2,
            Prescaler::DividedBy4 => 4,
            Prescaler::DividedBy6 => 6,
            Prescaler::DividedBy8 => 8,
            Prescaler::DividedBy10 => 10,
            Prescaler::DividedBy12 => 12,
            Prescaler::DividedBy16 => 16,
            Prescaler::DividedBy32 => 32,
            Prescaler::DividedBy64 => 64,
            Prescaler::DividedBy128 => 128,
            Prescaler::DividedBy256 => 256,
        }
    }

    fn presc(&self) -> Presc {
        match self {
            Prescaler::NotDivided => Presc::DIV1,
            Prescaler::DividedBy2 => Presc::DIV2,
            Prescaler::DividedBy4 => Presc::DIV4,
            Prescaler::DividedBy6 => Presc::DIV6,
            Prescaler::DividedBy8 => Presc::DIV8,
            Prescaler::DividedBy10 => Presc::DIV10,
            Prescaler::DividedBy12 => Presc::DIV12,
            Prescaler::DividedBy16 => Presc::DIV16,
            Prescaler::DividedBy32 => Presc::DIV32,
            Prescaler::DividedBy64 => Presc::DIV64,
            Prescaler::DividedBy128 => Presc::DIV128,
            Prescaler::DividedBy256 => Presc::DIV256,
        }
    }
}

pub struct Adc<'d, T: Instance> {
    sample_time: SampleTime,
    vref_mv: u32,
    resolution: Resolution,
    phantom: PhantomData<&'d mut T>,
}

impl<'d, T: Instance + crate::rcc::RccPeripheral> Adc<'d, T> {
    pub fn new(_peri: impl Peripheral<P = T> + 'd, delay: &mut impl DelayUs<u16>) -> Self {
        embassy_hal_common::into_ref!(_peri);
        T::enable();
        T::reset();

        let prescaler = Prescaler::from_ker_ck(T::frequency());

        unsafe {
            T::common_regs().ccr().modify(|w| w.set_presc(prescaler.presc()));
        }

        let frequency = Hertz(T::frequency().0 / prescaler.divisor());
        info!("ADC frequency set to {} Hz", frequency.0);

        if frequency > Hertz::mhz(50) {
            panic!("Maximal allowed frequency for the ADC is 50 MHz and it varies with different packages, refer to ST docs for more information.");
        }
        let boost = if frequency < Hertz::khz(6_250) {
            Boost::LT6_25
        } else if frequency < Hertz::khz(12_500) {
            Boost::LT12_5
        } else if frequency < Hertz::mhz(25) {
            Boost::LT25
        } else {
            Boost::LT50
        };
        unsafe {
            T::regs().cr().modify(|w| w.set_boost(boost));
        }

        let mut s = Self {
            sample_time: Default::default(),
            vref_mv: VREF_DEFAULT_MV,
            resolution: Resolution::default(),
            phantom: PhantomData,
        };
        s.power_up(delay);
        s.configure_differential_inputs();

        s.calibrate();
        delay.delay_us(1);

        s.enable();
        s.configure();

        s
    }

    fn power_up(&mut self, delay: &mut impl DelayUs<u16>) {
        unsafe {
            T::regs().cr().modify(|reg| {
                reg.set_deeppwd(false);
                reg.set_advregen(true);
            });
        }

        delay.delay_us(10);
    }

    fn configure_differential_inputs(&mut self) {
        unsafe {
            T::regs().difsel().modify(|w| {
                for n in 0..20 {
                    w.set_difsel(n, Difsel::SINGLEENDED);
                }
            })
        };
    }

    fn calibrate(&mut self) {
        unsafe {
            T::regs().cr().modify(|w| {
                w.set_adcaldif(Adcaldif::SINGLEENDED);
                w.set_adcallin(true);
            });

            T::regs().cr().modify(|w| w.set_adcal(true));

            while T::regs().cr().read().adcal() {}
        }
    }

    fn enable(&mut self) {
        unsafe {
            T::regs().isr().write(|w| w.set_adrdy(true));
            T::regs().cr().modify(|w| w.set_aden(true));
            while !T::regs().isr().read().adrdy() {}
            T::regs().isr().write(|w| w.set_adrdy(true));
        }
    }

    fn configure(&mut self) {
        // single conversion mode, software trigger
        unsafe {
            T::regs().cfgr().modify(|w| {
                w.set_cont(false);
                w.set_exten(Exten::DISABLED);
            })
        }
    }

    pub fn enable_vrefint(&self) -> VrefInt {
        unsafe {
            T::common_regs().ccr().modify(|reg| {
                reg.set_vrefen(true);
            });
        }

        VrefInt {}
    }

    pub fn enable_temperature(&self) -> Temperature {
        unsafe {
            T::common_regs().ccr().modify(|reg| {
                reg.set_vsenseen(true);
            });
        }

        Temperature {}
    }

    pub fn enable_vbat(&self) -> Vbat {
        unsafe {
            T::common_regs().ccr().modify(|reg| {
                reg.set_vbaten(true);
            });
        }

        Vbat {}
    }

    pub fn set_sample_time(&mut self, sample_time: SampleTime) {
        self.sample_time = sample_time;
    }

    pub fn set_resolution(&mut self, resolution: Resolution) {
        self.resolution = resolution;
    }

    /// Set VREF value in millivolts. This value is used for [to_millivolts()] sample conversion.
    ///
    /// Use this if you have a known precise VREF (VDDA) pin reference voltage.
    pub fn set_vref_mv(&mut self, vref_mv: u32) {
        self.vref_mv = vref_mv;
    }

    /// Convert a measurement to millivolts
    pub fn to_millivolts(&self, sample: u16) -> u16 {
        ((u32::from(sample) * self.vref_mv) / self.resolution.to_max_count()) as u16
    }

    /// Perform a single conversion.
    fn convert(&mut self) -> u16 {
        unsafe {
            T::regs().isr().modify(|reg| {
                reg.set_eos(true);
                reg.set_eoc(true);
            });

            // Start conversion
            T::regs().cr().modify(|reg| {
                reg.set_adstart(true);
            });

            while !T::regs().isr().read().eos() {
                // spin
            }

            T::regs().dr().read().0 as u16
        }
    }

    pub fn read<P>(&mut self, pin: &mut P) -> u16
    where
        P: AdcPin<T>,
        P: crate::gpio::sealed::Pin,
    {
        unsafe {
            pin.set_as_analog();

            self.read_channel(pin.channel())
        }
    }

    pub fn read_internal(&mut self, channel: &mut impl InternalChannel<T>) -> u16 {
        unsafe { self.read_channel(channel.channel()) }
    }

    unsafe fn read_channel(&mut self, channel: u8) -> u16 {
        // Configure ADC
        T::regs().cfgr().modify(|reg| reg.set_res(self.resolution.res()));

        // Configure channel
        Self::set_channel_sample_time(channel, self.sample_time);

        T::regs().cfgr2().modify(|w| w.set_lshift(0));
        T::regs()
            .pcsel()
            .write(|w| w.set_pcsel(channel as _, Pcsel::PRESELECTED));
        T::regs().sqr1().write(|reg| {
            reg.set_sq(0, channel);
            reg.set_l(0);
        });

        self.convert()
    }

    unsafe fn set_channel_sample_time(ch: u8, sample_time: SampleTime) {
        if ch <= 9 {
            T::regs()
                .smpr(0)
                .modify(|reg| reg.set_smp(ch as _, sample_time.sample_time()));
        } else {
            T::regs()
                .smpr(1)
                .modify(|reg| reg.set_smp((ch - 10) as _, sample_time.sample_time()));
        }
    }
}