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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! Based on `stm32f3xx-hal`.

//! API for the ADC (Analog to Digital Converter)
//!
//! # Examples
//!
//! Check out [examles/adc.rs].
//!
//! It can be built for the STM32F3Discovery running
//! `cargo build --example adc --features=stm32f303xc`
//!
//! [examples/adc.rs]: https://github.com/stm32-rs/stm32f3xx-hal/blob/v0.6.0/examples/adc.rs

use cortex_m::asm;
use embedded_hal::adc::{Channel, OneShot};

use crate::{pac::RCC, traits::ClockCfg};

use paste::paste;

#[cfg(any(feature = "f301", feature = "f302", feature = "f303",))]
use crate::pac::{ADC1, ADC1_2};

#[cfg(any(feature = "f302", feature = "f303",))]
use crate::pac::ADC2;

// todo: what other features support ADC3 and 4? Trim this down as you get errors
#[cfg(any(feature = "f303",))]
use crate::pac::{ADC3, ADC3_4, ADC4};

#[cfg(any(feature = "l4", feature = "l5"))]
use crate::pac::ADC_COMMON;

// todo: Figure out which H7 variants work on ADCs 2, 3, 4.
#[cfg(any(
    feature = "l4x3",
    feature = "h743",
    feature = "h743v",
    feature = "h747cm4",
    feature = "h747cm7",
    feature = "h753",
    feature = "h753v",
    feature = "h7b3",
))]
use crate::pac::ADC1;

#[cfg(any(feature = "l4x1", feature = "l4x2", feature = "l4x5", feature = "l4x6",))]
use crate::pac::{ADC1, ADC2};

#[cfg(any(feature = "l4x5", feature = "l4x6"))]
use crate::pac::ADC3;

#[cfg(feature = "l5")]
use crate::pac::ADC;

const MAX_ADVREGEN_STARTUP_US: u32 = 10;

/// https://github.com/rust-embedded/embedded-hal/issues/267
/// We are simulating an enum due to how the `embedded-hal` trait is set up.
#[allow(non_snake_case)]
pub mod AdcChannel {
    pub struct C1;
    pub struct C2;
    pub struct C3;
    pub struct C4;
    pub struct C5;
    pub struct C6;
    pub struct C7;
    pub struct C8;
    pub struct C9;
    pub struct C10;
    pub struct C11;
    pub struct C12;
    pub struct C13;
    pub struct C14;
    pub struct C15;
    pub struct C16;
    pub struct C17;
    pub struct C18;
}

#[derive(Clone, Copy)]
enum AdcNum {
    One,
    Two,
    Three,
    Four,
}

/// Analog Digital Converter Peripheral
pub struct Adc<ADC> {
    /// ADC Register
    regs: ADC,
    ckmode: ClockMode,
    operation_mode: Option<OperationMode>,
}

/// ADC sampling time
///
/// Each channel can be sampled with a different sample time.
/// There is always an overhead of 13 ADC clock cycles.
/// E.g. For Sampletime T_19 the total conversion time (in ADC clock cycles) is
/// 13 + 19 = 32 ADC Clock Cycles
pub enum SampleTime {
    /// 1.5 ADC clock cycles
    T1,
    /// 2.5 ADC clock cycles
    T2,
    /// 4.5 ADC clock cycles
    T4,
    /// 7.5 ADC clock cycles
    T7,
    /// 19.5 ADC clock cycles
    T19,
    /// 61.5 ADC clock cycles
    T61,
    /// 181.5 ADC clock cycles
    T181,
    /// 601.5 ADC clock cycles
    T601,
}

impl Default for SampleTime {
    /// T_1 is also the reset value.
    fn default() -> Self {
        SampleTime::T1
    }
}

impl SampleTime {
    /// Conversion to bits for SMP
    fn bitcode(&self) -> u8 {
        match self {
            SampleTime::T1 => 0b000,
            SampleTime::T2 => 0b001,
            SampleTime::T4 => 0b010,
            SampleTime::T7 => 0b011,
            SampleTime::T19 => 0b100,
            SampleTime::T61 => 0b101,
            SampleTime::T181 => 0b110,
            SampleTime::T601 => 0b111,
        }
    }
}

#[derive(Clone, Copy, PartialEq)]
/// ADC operation mode
// TODO: Implement other modes (DMA, Differential,…)
pub enum OperationMode {
    /// OneShot Mode
    OneShot,
}

#[derive(Clone, Copy, PartialEq)]
/// ADC CkMode
#[repr(u8)]
pub enum ClockMode {
    // /// Use Kernel Clock adc_ker_ck_input divided by PRESC. Asynchronous to AHB clock
    ASYNC = 0b00,
    /// Use AHB clock rcc_hclk3. In this case rcc_hclk must equal sys_d1cpre_ck
    SyncDiv1 = 0b01,
    /// Use AHB clock rcc_hclk3 divided by 2
    SyncDiv2 = 0b10,
    /// Use AHB clock rcc_hclk3 divided by 4
    SyncDiv4 = 0b11,
}

impl Default for ClockMode {
    fn default() -> Self {
        Self::SyncDiv2
    }
}

/// ADC data register alignment
#[derive(Clone, Copy)]
#[repr(u8)]
pub enum Align {
    /// Right alignment of output data
    Right = 0,
    /// Left alignment of output data
    Left = 1,
}

impl Default for Align {
    fn default() -> Self {
        Align::Right
    }
}

// Abstract implementation of ADC functionality
macro_rules! hal {
    ($ADC:ident, $ADC_COMMON:ident, $adc:ident, $adc_num:expr) => {
        impl Adc<$ADC> {
            paste! {
                /// Init a new ADC
                ///
                /// Enables the clock, performs a calibration and enables the ADC
                ///
                /// # Panics
                /// If one of the following occurs:
                /// * the clocksetting is not well defined.
                /// * the clock was already enabled with a different setting
                ///
                pub fn [<new_ $adc _unchecked>]<C: ClockCfg>(
                    regs: $ADC,
                    adc_common : &mut $ADC_COMMON,
                    ckmode: ClockMode,
                    clocks: &C,
                    rcc: &mut RCC,
                ) -> Self {
                    let mut this_adc = Self {
                        regs,
                        ckmode,
                        operation_mode: None,
                    };
                    if !(this_adc.clocks_welldefined(clocks)) {
                        panic!("Clock settings not well defined");
                    }
                    if !(this_adc.enable_clock(adc_common, rcc)){
                        panic!("Clock already enabled with a different setting");
                    }
                    this_adc.set_align(Align::default());
                    // this_adc.calibrate(clocks);  // todo: This is hanging on L4 currently!
                    // Reference Manual: "ADEN bit cannot be set during ADCAL=1
                    // and 4 ADC clock cycle after the ADCAL
                    // bit is cleared by hardware."
                    // this_adc.wait_adc_clk_cycles(4);
                    asm::delay(ckmode as u32 * 4);
                    this_adc.enable();

                    this_adc
                }
            }

             fn enable_clock(&self, common_regs: &mut $ADC_COMMON, rcc: &mut RCC) -> bool {
                 // `common_regs` is the same as `self.regs` for non-f3. On f3, it's a diff block,
                 // eg `adc12`.
                cfg_if::cfg_if! {
                    if #[cfg(any(feature = "f3"))] {
                        match $adc_num {
                            AdcNum::One | AdcNum::Two => {
                                #[cfg(any(feature = "f301"))]
                                if rcc.ahbenr.read().adc1en().is_enabled() {
                                    return (common_regs.ccr.read().ckmode().bits() == self.ckmode as u8);
                                }
                                #[cfg(any(feature = "f301"))]
                                rcc.ahbenr.modify(|_, w| w.adc1en().set_bit());

                                #[cfg(not(any(feature = "f301")))]
                                if rcc.ahbenr.read().adc12en().is_enabled() {
                                    return (common_regs.ccr.read().ckmode().bits() == self.ckmode as u8);
                                }
                                #[cfg(not(any(feature = "f301")))]
                                rcc.ahbenr.modify(|_, w| w.adc12en().set_bit());
                            }
                            AdcNum::Three | AdcNum::Four => {
                                #[cfg(not(any(feature = "f301", feature = "f302")))]
                                if rcc.ahbenr.read().adc34en().is_enabled() {
                                    return (common_regs.ccr.read().ckmode().bits() == self.ckmode as u8);
                                }
                                #[cfg(not(any(feature = "f301", feature = "f302")))]
                                rcc.ahbenr.modify(|_, w| w.adc34en().set_bit());
                            }

                        }

                    } else {
                        if rcc.ahb2enr.read().adcen().bit_is_set() {
                            return (common_regs.ccr.read().ckmode().bits() == self.ckmode as u8);
                        }
                        rcc.ahb2enr.modify(|_, w| w.adcen().set_bit());
                    }
                }

                common_regs.ccr.modify(|_, w| unsafe { w
                    .ckmode().bits(self.ckmode as u8)
                });
                true
            }

            /// Software can use ClockMode::SyncDiv1 only if
            /// hclk and sysclk are the same. (see reference manual 15.3.3)
            fn clocks_welldefined<C: ClockCfg>(&self, clocks: &C) -> bool {
                if (self.ckmode == ClockMode::SyncDiv1) {
                    clocks.hclk() == clocks.sysclk()
                } else {
                    true
                }
            }

            /// sets up adc in one shot mode for a single channel
            pub fn setup_oneshot(&mut self) {
                self.regs.cr.modify(|_, w| w.adstp().set_bit());
                self.regs.isr.modify(|_, w| w.ovr().clear_bit());

                self.regs.cfgr.modify(|_, w| w
                    .cont().clear_bit()  // single conversion mode.
                    .ovrmod().clear_bit()  // preserve DR data
                );

                self.set_sequence_len(1);

                self.operation_mode = Some(OperationMode::OneShot);
            }

            fn set_sequence_len(&mut self, len: u8) {
                if len - 1 >= 16 {
                    panic!("ADC sequence length must be in 1..=16")
                }

                // typo
                cfg_if::cfg_if! {
                    if #[cfg(any(feature = "l4x1", feature = "l4x2", feature = "l4x3", feature = "l4x5"))] {
                        self.regs.sqr1.modify(|_, w| unsafe { w.l3().bits(len - 1) });
                    } else {
                        self.regs.sqr1.modify(|_, w| unsafe { w.l().bits(len - 1) });
                    }
                }
            }

            fn set_align(&self, align: Align) {
                self.regs.cfgr.modify(|_, w| w.align().bit(align as u8 != 0));
            }

            fn enable(&mut self) {
                self.regs.cr.modify(|_, w| w.aden().set_bit());  // Enable
                while self.regs.isr.read().adrdy().bit_is_clear() {}  // Wait until ready
            }

            fn disable(&mut self) {
                self.regs.cr.modify(|_, w| w.addis().set_bit()); // Disable
            }

            /// Calibrate according to 15.3.8 in the Reference Manual
            fn calibrate<C: ClockCfg>(&mut self, clocks: &C) {
                let enabled;
                cfg_if::cfg_if! {
                    if #[cfg(feature = "f3")] {
                        enabled = self.regs.cr.read().advregen().bits() == 1;
                    } else {
                        enabled = self.regs.cr.read().advregen().bit_is_set();
                    }
                }
                if !enabled {
                    self.advregen_enable();
                    self.wait_advregen_startup(clocks);
                }

                self.disable();

                self.regs.cr.modify(|_, w| w
                    .adcaldif().clear_bit()  // single ended. // todo: Cal differential option!
                    .adcal().set_bit()); // start calibration.

                while self.regs.cr.read().adcal().bit_is_set() {}
            }

            fn advregen_enable(&mut self){
                cfg_if::cfg_if! {
                    if #[cfg(feature = "f3")] {
                        // need to go through intermediate first
                        self.regs.cr.modify(|_, w| w.advregen().intermediate());
                        self.regs.cr.modify(|_, w| w.advregen().enabled());  // Enable voltage regulator.
                    } else {
                        self.regs.cr.modify(|_, w| w.advregen().set_bit());  // Enable voltage regulator.
                    }
                }

            }

            /// wait for the advregen to startup.
            ///
            /// This is based on the MAX_ADVREGEN_STARTUP_US of the device.
            fn wait_advregen_startup<C: ClockCfg>(&self, clocks: &C) {
                // Prevents crashes. Not sure why / when it started showing up.
                // https://github.com/rust-embedded/cortex-m/pull/328
                let mut delay = (MAX_ADVREGEN_STARTUP_US * 1_000_000) / clocks.sysclk();
                if delay < 2 {
                    delay = 2;
                }
                asm::delay(delay);
            }

            /// busy ADC read
            fn convert_one(&mut self, chan: u8) -> u16 {
                self.ensure_oneshot();
                self.set_chan_smps(chan, SampleTime::default());
                self.select_single_chan(chan);

                self.regs.cr.modify(|_, w| w.adstart().set_bit());  // Start
                while self.regs.isr.read().eos().bit_is_clear() {}  // wait until complete.
                self.regs.isr.modify(|_, w| w.eos().set_bit());  // Clear
                return self.regs.dr.read().bits() as u16;  // todo make sure you don't need rdata field.
            }

            fn ensure_oneshot(&mut self) {
                if self.operation_mode != Some(OperationMode::OneShot) {
                    self.setup_oneshot();
                }
            }

            /// This should only be invoked with the defined channels for the particular
            /// device. (See Pin/Channel mapping above)
            fn select_single_chan(&self, chan: u8) {
                self.regs.sqr1.modify(|_, w|
                    // NOTE(unsafe): chan is the x in ADCn_INx
                    // Channel as u8 is the ADC channel to use.
                    unsafe { w.sq1().bits(chan) }
                );
            }

            /// Note: only allowed when ADSTART = 0
            // TODO: there are boundaries on how this can be set depending on the hardware.
            fn set_chan_smps(&self, chan: u8, smp: SampleTime) {
                // Channel as u8 is the ADC channel to use.
                unsafe {
                    match chan {
                        1 => self.regs.smpr1.modify(|_, w| w.smp1().bits(smp.bitcode())),
                        2 => self.regs.smpr1.modify(|_, w| w.smp2().bits(smp.bitcode())),
                        3 => self.regs.smpr1.modify(|_, w| w.smp3().bits(smp.bitcode())),
                        4 => self.regs.smpr1.modify(|_, w| w.smp4().bits(smp.bitcode())),
                        5 => self.regs.smpr1.modify(|_, w| w.smp5().bits(smp.bitcode())),
                        6 => self.regs.smpr1.modify(|_, w| w.smp6().bits(smp.bitcode())),
                        7 => self.regs.smpr1.modify(|_, w| w.smp7().bits(smp.bitcode())),
                        8 => self.regs.smpr1.modify(|_, w| w.smp8().bits(smp.bitcode())),
                        9 => self.regs.smpr1.modify(|_, w| w.smp9().bits(smp.bitcode())),
                        11 => self.regs.smpr2.modify(|_, w| w.smp10().bits(smp.bitcode())),
                        12 => self.regs.smpr2.modify(|_, w| w.smp12().bits(smp.bitcode())),
                        13 => self.regs.smpr2.modify(|_, w| w.smp13().bits(smp.bitcode())),
                        14 => self.regs.smpr2.modify(|_, w| w.smp14().bits(smp.bitcode())),
                        15 => self.regs.smpr2.modify(|_, w| w.smp15().bits(smp.bitcode())),
                        16 => self.regs.smpr2.modify(|_, w| w.smp16().bits(smp.bitcode())),
                        17 => self.regs.smpr2.modify(|_, w| w.smp17().bits(smp.bitcode())),
                        18 => self.regs.smpr2.modify(|_, w| w.smp18().bits(smp.bitcode())),
                        _ => unreachable!(),
                    };
                }
            }

        }

        impl<WORD, PIN> OneShot<$ADC, WORD, PIN> for Adc<$ADC>
        where
            WORD: From<u16>,
            PIN: Channel<$ADC, ID = u8>,
            {
                type Error = ();

                fn read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
                    let res = self.convert_one(PIN::channel());
                    return Ok(res.into());
                }
        }

        // todo: This is so janky. There has to be a better way.
        impl Channel<$ADC> for AdcChannel::C1 {
            type ID = u8;
            fn channel() -> u8 { 1 }
        }
        impl Channel<$ADC> for AdcChannel::C2 {
            type ID = u8;
            fn channel() -> u8 { 2 }
        }
        impl Channel<$ADC> for AdcChannel::C3 {
            type ID = u8;
            fn channel() -> u8 { 3}
        }
        impl Channel<$ADC> for AdcChannel::C4 {
            type ID = u8;
            fn channel() -> u8 { 4 }
        }
        impl Channel<$ADC> for AdcChannel::C5 {
            type ID = u8;
            fn channel() -> u8 { 5 }
        }
        impl Channel<$ADC> for AdcChannel::C6 {
            type ID = u8;
            fn channel() -> u8 { 6 }
        }
        impl Channel<$ADC> for AdcChannel::C7 {
            type ID = u8;
            fn channel() -> u8 { 7 }
        }
        impl Channel<$ADC> for AdcChannel::C8 {
            type ID = u8;
            fn channel() -> u8 { 8 }
        }
        impl Channel<$ADC> for AdcChannel::C9 {
            type ID = u8;
            fn channel() -> u8 { 9 }
        }
        impl Channel<$ADC> for AdcChannel::C10 {
            type ID = u8;
            fn channel() -> u8 { 10 }
        }
        impl Channel<$ADC> for AdcChannel::C11 {
            type ID = u8;
            fn channel() -> u8 { 11 }
        }
        impl Channel<$ADC> for AdcChannel::C12 {
            type ID = u8;
            fn channel() -> u8 { 12 }
        }
        impl Channel<$ADC> for AdcChannel::C13 {
            type ID = u8;
            fn channel() -> u8 { 13 }
        }
        impl Channel<$ADC> for AdcChannel::C14 {
            type ID = u8;
            fn channel() -> u8 { 14 }
        }
        impl Channel<$ADC> for AdcChannel::C15 {
            type ID = u8;
            fn channel() -> u8 { 15 }
        }
        impl Channel<$ADC> for AdcChannel::C16 {
            type ID = u8;
            fn channel() -> u8 { 16 }
        }
        impl Channel<$ADC> for AdcChannel::C17 {
            type ID = u8;
            fn channel() -> u8 { 17 }
        }
        impl Channel<$ADC> for AdcChannel::C18 {
            type ID = u8;
            fn channel() -> u8 { 18 }
        }
    }
}

// todo: l and h. and rest of f3

#[cfg(any(feature = "f301", feature = "f302", feature = "f303",))]
hal!(ADC1, ADC1_2, adc1, AdcNum::One);

#[cfg(any(feature = "f302", feature = "f303",))]
hal!(ADC2, ADC1_2, adc2, AdcNum::Two);

#[cfg(any(feature = "f303"))]
hal!(ADC3, ADC3_4, adc3, AdcNum::Three);

#[cfg(any(feature = "f303"))]
hal!(ADC4, ADC3_4, adc4, AdcNum::Four);

#[cfg(any(
    feature = "l4x1",
    feature = "l4x2",
    feature = "l4x3",
    feature = "l4x5",
    feature = "l4x6",
))]
hal!(ADC1, ADC_COMMON, adc1, AdcNum::One);

#[cfg(any(feature = "l4x1", feature = "l4x2", feature = "l4x5", feature = "l4x6",))]
hal!(ADC2, ADC_COMMON, adc2, AdcNum::Two);

#[cfg(any(feature = "l4x5", feature = "l4x6",))]
hal!(ADC3, ADC_COMMON, adc3, AdcNum::Three);

cfg_if::cfg_if! {
    if #[cfg(any(
        feature = "l5",
    ))] {
        // todo: How many channels does L5 have? This isn't type checked
        hal!(ADC, ADC_COMMON, adc, AdcNum:One);  // Todo: Channel 2. Fight through that chan imp to do it.
    }
}

// todo: Beyond ADC1 for H7.
#[cfg(any(
    feature = "h743",
    feature = "h743v",
    feature = "h747cm4",
    feature = "h747cm7",
    feature = "h753",
    feature = "h753v",
    feature = "h7b3",
))]
hal!(ADC1, ADC1, adc1, AdcNum::One);