stm32f7xx-hal 0.7.0

HAL for the STM32F7xx family of microcontrollers
Documentation
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
//! # API for the Analog to Digital converter

#![allow(dead_code)]

use crate::rcc::{Clocks, Enable, Reset, APB2};

use crate::gpio::{self, Analog};

use crate::pac::{ADC1, ADC2, ADC3, ADC_COMMON};

use cortex_m::asm::delay;
use fugit::HertzU32 as Hertz;

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

#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(non_camel_case_types)]
/// ADC sampling time
///
/// Options for the sampling time, each is T ADC clock cycles.
// 15.13.4 >> ADC sample time register
pub enum SampleTime {
    /// 3 cycles sampling time
    T_3,
    /// 15 cycles sampling time
    T_15,
    /// 28 cycles sampling time
    T_28,
    /// 56 cycles sampling time
    T_56,
    /// 84 cycles sampling time
    T_84,
    /// 112 cycles sampling time
    T_112,
    /// 144 cycles sampling time
    T_144,
    /// 480 cycles sampling time
    T_480,
}

impl Default for SampleTime {
    /// Get the default sample time (currently 56 cycles)
    fn default() -> Self {
        SampleTime::T_56
    }
}

impl From<SampleTime> for u8 {
    fn from(val: SampleTime) -> Self {
        use SampleTime::*;
        match val {
            T_3 => 0,
            T_15 => 1,
            T_28 => 2,
            T_56 => 3,
            T_84 => 4,
            T_112 => 5,
            T_144 => 6,
            T_480 => 7,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
/// ADC data register alignment
pub enum Align {
    /// Right alignment of output data
    Right,
    /// Left alignment of output data
    Left,
}

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

// 15.13.3 ADC control register >> Bit 11 ALIGN: Data alignment
impl From<Align> for bool {
    fn from(val: Align) -> Self {
        match val {
            Align::Right => false,
            Align::Left => true,
        }
    }
}

/////////////////////////////////

macro_rules! adc_pins {
    ($ADC:ident, $($pin:ty => $chan:literal),+ $(,)*) => {
        $(
            impl Channel<$ADC> for $pin {
                type ID = u8;

                fn channel() -> u8 { $chan }
            }
        )+
    };
}

// See "Datasheet - production data"
// Pinouts and pin description (page 66..)
adc_pins!(ADC1,
    gpio::PA0<Analog> => 0,
    gpio::PA1<Analog> => 1,
    gpio::PA2<Analog> => 2,
    gpio::PA3<Analog> => 3,
    gpio::PA4<Analog> => 4,
    gpio::PA5<Analog> => 5,
    gpio::PA6<Analog> => 6,
    gpio::PA7<Analog> => 7,
    gpio::PB0<Analog>  => 8,
    gpio::PB1<Analog>  => 9,
    gpio::PC0<Analog>  => 10,
    gpio::PC1<Analog>  => 11,
    gpio::PC2<Analog>  => 12,
    gpio::PC3<Analog>  => 13,
    gpio::PC4<Analog>  => 14,
    gpio::PC5<Analog>  => 15,
);

adc_pins!(ADC2,
    gpio::PA0<Analog>  => 0,
    gpio::PA1<Analog>  => 1,
    gpio::PA2<Analog>  => 2,
    gpio::PA3<Analog>  => 3,
    gpio::PA4<Analog>  => 4,
    gpio::PA5<Analog>  => 5,
    gpio::PA6<Analog>  => 6,
    gpio::PA7<Analog>  => 7,
    gpio::PB0<Analog>  => 8,
    gpio::PB1<Analog>  => 9,
    gpio::PC0<Analog>  => 10,
    gpio::PC1<Analog>  => 11,
    gpio::PC2<Analog>  => 12,
    gpio::PC3<Analog>  => 13,
    gpio::PC4<Analog>  => 14,
    gpio::PC5<Analog>  => 15,
);

adc_pins!(ADC3,
    gpio::PA0<Analog> => 0,
    gpio::PA1<Analog> => 1,
    gpio::PA2<Analog> => 2,
    gpio::PA3<Analog> => 3,
    gpio::PF6<Analog> => 4,
    gpio::PF7<Analog> => 5,
    gpio::PF8<Analog> => 6,
    gpio::PF9<Analog> => 7,
    gpio::PF10<Analog> => 8,
    gpio::PF3<Analog> => 9,
    gpio::PC0<Analog> => 10,
    gpio::PC1<Analog> => 11,
    gpio::PC2<Analog> => 12,
    gpio::PC3<Analog> => 13,
    gpio::PF4<Analog> => 14,
    gpio::PF5<Analog> => 15,
);

////////////////////////////////////

/// ADC configuration
pub struct Adc<ADC> {
    rb: ADC,
    sample_time: SampleTime,
    align: Align,
    sysclk: Hertz,
}

/// Stored ADC config can be restored using the `Adc::restore_cfg` method
#[derive(Copy, Clone, Debug, PartialEq, Default)]
pub struct StoredConfig(SampleTime, Align);

macro_rules! adc_hal {
    ( $ADC:ident, $adc:ident) => {
        impl Adc<$ADC> {
            /// Init a new Adc
            ///
            /// Sets all configurable parameters to one-shot defaults,
            pub fn $adc(
                adc: $ADC,
                apb2: &mut APB2,
                clocks: &Clocks,
                nb_resolution_bits: u8,
                reset: bool,
            ) -> Self {
                let mut s = Self {
                    rb: adc,
                    sample_time: SampleTime::default(),
                    align: Align::default(),
                    sysclk: clocks.sysclk(),
                };
                <$ADC>::enable(apb2);
                if reset {
                    s.power_down();
                    <$ADC>::reset(apb2);
                }

                s.setup_oneshot();
                s.resolution(nb_resolution_bits);
                s.power_up();

                s
            }

            /// Save current ADC config
            pub fn save_cfg(&mut self) -> StoredConfig {
                StoredConfig(self.sample_time, self.align)
            }

            /// Restore saved ADC config
            pub fn restore_cfg(&mut self, cfg: StoredConfig) {
                self.sample_time = cfg.0;
                self.align = cfg.1;
            }

            /// Reset the ADC config to default, return existing config
            pub fn default_cfg(&mut self) -> StoredConfig {
                let cfg = self.save_cfg();
                self.sample_time = SampleTime::default();
                self.align = Align::default();
                cfg
            }

            /// Set ADC sampling time
            ///
            /// Options can be found in [SampleTime](crate::adc::SampleTime).
            pub fn set_sample_time(&mut self, t_samp: SampleTime) {
                self.sample_time = t_samp;
            }

            /// Set the Adc result alignment
            ///
            /// Options can be found in [Align](crate::adc::Align).
            pub fn set_align(&mut self, align: Align) {
                self.align = align;
            }

            /// Returns the largest possible sample value for the current settings
            pub fn max_sample(&self) -> u16 {
                match self.align {
                    Align::Left => u16::max_value(),
                    Align::Right => (1 << 12) - 1,
                }
            }

            #[inline(always)]
            pub fn set_external_trigger(&mut self, trigger: crate::pac::adc1::cr2::EXTSEL_A) {
                self.rb.cr2.modify(|_, w| w.extsel().variant(trigger))
            }

            fn power_up(&mut self) {
                self.rb.cr2.modify(|_, w| w.adon().set_bit());

                // The reference manual says that a stabilization time is needed after power_up,
                // this time can be found in the datasheets.
                // for STM32F7xx : delay(216_000_000/800_000)= delay(270 cycles) = 1.25us
                delay(self.sysclk.raw() / 800_000);
            }

            // 15.3.1 ADC on-off control
            fn power_down(&mut self) {
                self.rb.cr2.modify(|_, w| w.adon().clear_bit());
            }

            // 15.3.5 Single conversion mode (page: 444)
            // CONT bit >> 0 (continuous)
            // see EXTEN and EXTSEL[3:0]: for triggers (page:471)
            // SWSTART: Start conversion of regular channels
            fn setup_oneshot(&mut self) {
                self.rb
                    .cr2
                    .modify(|_, w| w.cont().clear_bit().swstart().set_bit());

                // SCAN: Scan mode
                // DISCEN: Discontinuous mode on regular channels
                self.rb
                    .cr1
                    .modify(|_, w| w.scan().clear_bit().discen().set_bit());

                // ADC regular sequence register
                // The total number of conversions in the regular group must be written in the L[3:0] bits in the ADC_SQR1 register. (15.3.4 page:444)
                self.rb.sqr1.modify(|_, w| w.l().bits(0b0));
            }

            /// setup the ADC Resolution : Bits 25:24 RES[1:0]
            fn resolution(&mut self, resol_bits: u8) {
                match resol_bits {
                    12 => self.rb.cr1.modify(|_, w| w.res().bits(0b00)),
                    10 => self.rb.cr1.modify(|_, w| w.res().bits(0b01)),
                    8 => self.rb.cr1.modify(|_, w| w.res().bits(0b10)),
                    6 => self.rb.cr1.modify(|_, w| w.res().bits(0b11)),
                    _ => self.rb.cr1.modify(|_, w| w.res().bits(0b00)),
                }
            }

            // See : ADC sample time registers (page: 474)
            fn set_channel_sample_time(&mut self, chan: u8, sample_time: SampleTime) {
                self.rb.smpr2.modify(|r, w| unsafe {
                    w.bits((r.bits() & !0x07) | ((sample_time as u32) & 0x07))
                });
                match chan {
                    0..=9 => {
                        // 3 first bits (we keep other bits) : SMP0[2:0]

                        // SMPchan[2:0]
                        self.rb.smpr2.modify(|r, w| unsafe {
                            w.bits(
                                (r.bits() & !(0x07 << (chan * 3)))
                                    | (((sample_time as u32) & 0x07) << (chan * 3)),
                            )
                        })
                    }

                    //////////////  SMPR1
                    10..=18 => {
                        // 3 first bits (we keep other bits) : SMP10[2:0]

                        // SMPchan[2:0]
                        self.rb.smpr1.modify(|r, w| unsafe {
                            w.bits(
                                (r.bits() & !(0x07 << ((chan - 10) * 3)))
                                    | (((sample_time as u32) & 0x07) << ((chan - 10) * 3)),
                            )
                        })
                    }

                    _ => unreachable!(),
                }
            }

            ////////////////

            fn set_regular_sequence(&mut self, channels: &[u8]) {
                let len = channels.len();
                let bits = channels
                    .iter()
                    .take(6)
                    .enumerate()
                    .fold(0u32, |s, (i, c)| s | ((*c as u32) << (i * 5)));
                self.rb.sqr3.write(|w| unsafe { w.bits(bits) });
                if len > 6 {
                    let bits = channels
                        .iter()
                        .skip(6)
                        .take(6)
                        .enumerate()
                        .fold(0u32, |s, (i, c)| s | ((*c as u32) << (i * 5)));
                    self.rb.sqr2.write(|w| unsafe { w.bits(bits) });
                }
                if len > 12 {
                    let bits = channels
                        .iter()
                        .skip(12)
                        .take(4)
                        .enumerate()
                        .fold(0u32, |s, (i, c)| s | ((*c as u32) << (i * 5)));
                    self.rb.sqr1.write(|w| unsafe { w.bits(bits) });
                }
                self.rb.sqr1.modify(|_, w| w.l().bits((len - 1) as u8));
            }

            fn set_continuous_mode(&mut self, continuous: bool) {
                self.rb.cr2.modify(|_, w| w.cont().bit(continuous));
            }

            fn set_discontinuous_mode(&mut self, channels_count: Option<u8>) {
                self.rb.cr1.modify(|_, w| match channels_count {
                    Some(count) => w.discen().set_bit().discnum().bits(count),
                    None => w.discen().clear_bit(),
                });
            }

            /*
              Performs an ADC conversion

              NOTE: Conversions can be started by writing a 1 to the ADON
              bit in the `CR2` while it is already 1, and no other bits
              are being written in the same operation. This means that
              the EOC bit *might* be set already when entering this function
              which can cause a read of stale values

              The check for `cr2.swstart.bit_is_set` *should* fix it, but
              does not. Therefore, ensure you do not do any no-op modifications
              to `cr2` just before calling this function
            */
            fn convert(&mut self, chan: u8) -> u16 {
                // Dummy read in case something accidentally triggered
                // a conversion by writing to CR2 without changing any
                // of the bits
                self.rb.dr.read().data().bits();

                self.set_channel_sample_time(chan, self.sample_time);
                self.rb.sqr3.modify(|_, w| unsafe { w.sq1().bits(chan) });

                // ADC start conversion of regular sequence
                self.rb
                    .cr2
                    .modify(|_, w| w.swstart().set_bit().align().bit(self.align.into()));
                while self.rb.cr2.read().swstart().bit_is_set() {}
                // ADC wait for conversion results
                while self.rb.sr.read().eoc().bit_is_clear() {}

                let res = self.rb.dr.read().data().bits();
                res
            }

            /// Powers down the ADC, disables the ADC clock and releases the ADC Peripheral
            pub fn release(mut self, apb2: &mut APB2) -> $ADC {
                self.power_down();
                <$ADC>::disable(apb2);
                self.rb
            }
        }

        impl ChannelTimeSequence for Adc<$ADC> {
            #[inline(always)]
            fn set_channel_sample_time(&mut self, chan: u8, sample_time: SampleTime) {
                self.set_channel_sample_time(chan, sample_time);
            }
            #[inline(always)]
            fn set_regular_sequence(&mut self, channels: &[u8]) {
                self.set_regular_sequence(channels);
            }
            #[inline(always)]
            fn set_continuous_mode(&mut self, continuous: bool) {
                self.set_continuous_mode(continuous);
            }
            #[inline(always)]
            fn set_discontinuous_mode(&mut self, channels: Option<u8>) {
                self.set_discontinuous_mode(channels);
            }
        }

        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(PIN::channel());
                Ok(res.into())
            }
        }
    };
}

impl Adc<ADC1> {
    /// Internal reference voltage Vrefint is connected to channel 17 on ADC1.
    /// According to section 6.3.27 "Reference voltage" from STM32F7xx (page:168/252)
    /// datasheets, typical value of this reference voltage is 1210 mV.
    ///
    /// This value is useful when ADC readings need to be converted into voltages.
    /// For instance, reading from any ADC channel can be converted into voltage (mV)
    /// using the following formula:
    ///     v_chan = adc.read(chan) * 1210 / adc.read_vref()
    pub fn read_vref(&mut self, adc_common: &ADC_COMMON) -> u16 {
        ////////////////
        let tsv_off = if adc_common.ccr.read().tsvrefe().bit_is_clear() {
            adc_common.ccr.modify(|_, w| w.vbate().clear_bit());
            adc_common.ccr.modify(|_, w| w.tsvrefe().set_bit());

            // The reference manual says that a stabilization time is needed after the powering the
            // sensor, this time can be found in the datasheets.
            delay(self.sysclk.raw() / 80_000);
            true
        } else {
            false
        };

        //ADC1_IN17
        let val = self.convert(17u8);

        if tsv_off {
            adc_common.ccr.modify(|_, w| w.tsvrefe().clear_bit());
        }

        val
    }

    pub fn bits_to_voltage(&mut self, adc_common: &ADC_COMMON, data: u16) -> u16 {
        let v_chan = (data as u32) * 1210 / (self.read_vref(adc_common) as u32);

        v_chan as u16
    }
}

// Implement adc_hal! for ADC1, ADC2 and ADC3
adc_hal!(ADC1, adc1);

adc_hal!(ADC2, adc2);

adc_hal!(ADC3, adc3);

pub trait ChannelTimeSequence {
    /// Set ADC sampling time for particular channel
    fn set_channel_sample_time(&mut self, chan: u8, sample_time: SampleTime);
    /// ADC Set a Regular Channel Conversion Sequence
    ///
    /// Define a sequence of channels to be converted as a regular group.
    fn set_regular_sequence(&mut self, channels: &[u8]);
    /// Set ADC continuous conversion
    ///
    /// When continuous conversion is enabled conversion does not stop at the last selected group channel but continues again from the first selected group channel.
    fn set_continuous_mode(&mut self, continuous: bool);
    /// Set ADC discontinuous mode
    ///
    /// It can be used to convert a short sequence of conversions (up to 8) which is a part of the regular sequence of conversions.
    fn set_discontinuous_mode(&mut self, channels_count: Option<u8>);
}

/// Set channel sequence and sample times for custom pins
///
/// Example:
/// ```rust, ignore
/// pub struct AdcPins(PA0<Analog>, PA2<Analog>);
/// impl SetChannels<AdcPins> for Adc<ADC1> {
///     fn set_samples(&mut self) {
///         self.set_channel_sample_time(0, adc::SampleTime::T_28);
///         self.set_channel_sample_time(2, adc::SampleTime::T_28);
///     }
///     fn set_sequence(&mut self) {
///         self.set_regular_sequence(&[0, 2, 0, 2]);
///         // Optionally we can set continuous scan mode
///         self.set_continuous_mode(true);
///         // Also we can use discontinuous conversion (3 channels per conversion)
///         self.set_discontinuous_mode(Some(3));
///     }
/// }
/// ```
pub trait SetChannels<PINS>: ChannelTimeSequence {
    fn set_samples(&mut self);
    fn set_sequence(&mut self);
}