ra-hal 0.3.0

Hardware Abstraction Layer (HAL) for the Renesas RA family of MCUs.
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
use embassy_hal_internal::{Peri, PeripheralType};
use embassy_time::{Duration, block_for};

use crate::{
    adc::{AdcChannelConfig, AverageMode, Resolution},
    constants::{TSN_INTERCEPT, TSN_SLOPE},
    gpio::{Flex, Pin},
    pac,
};

#[cfg(adc12)]
use pac::adc12::{self as adc_pac};
#[cfg(adc14)]
use pac::adc14::{self as adc_pac};
#[cfg(adc16)]
use pac::adc16::{self as adc_pac};

use adc_pac::regs::Adans;

#[cfg(adc16)]
use pac::adc16::{regs::Addiscr, vals::Vrefadcg};

/// Pseudo-channel for the on-die temperature sensor.
///
/// # Notes
///
/// This is mutually exclusive with all other channels.
/// §35.2.13 `TSSA` bit.
pub struct Temperature;

/// Pseudo-channel for `Vref` measurement.
///
/// # Notes
///
/// This is mutually exclusive with all other channels.
/// §35.2.13 `OCSA` bit.
pub struct Vref;

/// GPIO pin that can be used as an `ADC` input channel.
///
/// This struct is used because there is a bit of setup needed to configure the pin for ADC usage.
/// In theory, at some point in the future, we could reset this to GPIO use on drop.
#[allow(private_bounds)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AdcPin {
    adc_channel: u8,
}

/// Multiple GPIO pins that can be used as `ADC` input channels.
///
/// # Notes
///
/// `ADC14` doesn't support arbitrary sequences but does support reading from arbitrary channels in order by channel index.
/// See the [docs](crate::adc::AdcInputPin) for each pin to reveal its associated ADC channel.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AdcSequence<const N: usize> {
    channels: [u8; N],
}

/// For the physical GPIO pins connected to `ADC` input channels.
#[allow(private_bounds)]
pub trait AdcInputPin: SealedAdcInputPin {
    /// ADC channel index `ANnnn`
    const CHANNEL: u8;
}

/// For the physical GPIO pins connected to `ADC` input channels.
pub(crate) trait SealedAdcInputPin: Pin + PeripheralType {}

/// `ADC` channel instance.
///
/// An `AdcChannel` may be:
/// * An individual ADC channel (`ANnnn` in Renesas speak)
/// * A pseudo-channel such as the on-die temperature sensor
/// * A collection of individual channels to be read in sequence
///
/// So R may be either `u16` or `[u16; N]`.
#[allow(private_bounds)]
pub trait AdcChannel<R>: SealedAdcChannel<R> {}

pub(super) trait SealedAdcChannel<R> {
    fn enable<I: super::Instance>(&self, config: AdcChannelConfig);
    fn disable<I: super::Instance>(&self);
    fn read_one<I: super::Instance>(&self) -> R;
    fn address<I: super::Instance>(&self) -> *const R;
}

#[allow(private_bounds)]
impl AdcPin {
    /// Takes ownership of a pin for ADC use.
    #[inline]
    pub fn new<'d, P: AdcInputPin>(pin: Peri<'d, P>) -> Self {
        let mut flex = Flex::new_basic(pin);
        flex.set_as_analog();

        #[cfg(feature = "strict-assert")]
        assert!(P::CHANNEL <= 14 || (P::CHANNEL >= 16 && P::CHANNEL < 25));

        let adc_channel = P::CHANNEL;

        Self { adc_channel }
    }
}

impl<const N: usize> AdcSequence<N> {
    /// Takes ownership of an array of pins for ADC use.
    pub fn new(pins: [AdcPin; N]) -> Self {
        // There are only 26 ADC channels (0..=14, 16..=26).
        #[cfg(feature = "strict-assert")]
        assert!(N <= 26);

        // Taking an array of AdcPin is nice here because they're type erased and properly configured
        // for ADC input.  Will have to revisit this if we ever reset things on AdcPin::drop.  An
        // added benefit is that this won't accept the temp or vref pseudo-channels.

        let channels: [u8; N] = pins.map(|pin| pin.adc_channel);

        Self { channels }
    }

    /// Returns a slice containing the indices of the channels to be read.
    #[inline]
    pub fn channels(&self) -> &[u8] {
        &self.channels
    }
}

impl<const N: usize> AdcChannel<[u16; N]> for AdcSequence<N> {}

impl<const N: usize> SealedAdcChannel<[u16; N]> for AdcSequence<N> {
    #[inline]
    fn enable<I: super::Instance>(&self, config: AdcChannelConfig) {
        // ADC14 lets us independently adjust the sample time for some, but not all channels.
        // For now, let's just assign the same sample time.

        trace!("ADC14: enable_channel(sequence)");

        let adc = I::regs();

        adc.adexicr().modify(|w| {
            w.set_ocsa(false);
            w.set_tssa(false);
        });

        for channel in self.channels.iter() {
            trace!("  {} enable", channel);

            let index = *channel as usize / 16;
            let offset = *channel as usize % 16;

            adc.adansa(index).write(|w| w.set_ans(offset, true));

            if *channel <= 14 {
                adc.adsstr(*channel as _).write_value(config.sample_time);
            } else {
                adc.adsstrl().write_value(config.sample_time);
            }
        }

        if config.average_mode != AverageMode::Off {
            for channel in self.channels.iter() {
                trace!("  {} add/avg", channel);

                let index = *channel as usize / 16;
                let offset = *channel as usize % 16;

                adc.adads(index).write(|w| w.set_ads(offset, true));
            }
        }
    }

    #[inline]
    fn disable<I: super::Instance>(&self) {
        trace!("ADC14: disable_channel(sequence)");

        let adc = I::regs();

        if adc.adcsr().read().adst() {
            warn!("ADC14: Trying to disable sequence with a conversion running");
        }

        // Turn everything off.
        adc.adansa(0).write_value(Adans(0));
        adc.adansa(1).write_value(Adans(0));
    }

    #[inline]
    fn read_one<I: super::Instance>(&self) -> [u16; N] {
        let adc = I::regs();

        self.channels.map(|chan| adc.addr(chan as _).read())
    }

    #[inline]
    fn address<I: super::Instance>(&self) -> *const [u16; N] {
        // This should probably just get the address of each enabled channel and
        // return that in the array.  This will likely require a DTC implementation
        // instead of DMAC.
        //
        // TODO: Benchmark this.

        todo!("DMA xfers for sequences not yet supported")
    }
}

impl AdcChannel<u16> for AdcPin {}

impl SealedAdcChannel<u16> for AdcPin {
    #[inline]
    fn enable<I: super::Instance>(&self, config: AdcChannelConfig) {
        let adc = I::regs();

        trace!("ADC14: enable_channel({})", self.adc_channel);

        adc.adexicr().modify(|w| {
            w.set_ocsa(false);
            w.set_tssa(false);
        });

        let index = self.adc_channel as usize / 16;
        let offset = self.adc_channel as usize % 16;

        adc.adansa(index).write(|w| w.set_ans(offset, true));

        if config.average_mode != AverageMode::Off {
            adc.adads(index).write(|w| w.set_ads(offset, true))
        }

        if self.adc_channel <= 14 {
            adc.adsstr(self.adc_channel as _)
                .write_value(config.sample_time);
        } else {
            adc.adsstrl().write_value(config.sample_time);
        }
    }

    #[inline]
    fn disable<I: super::Instance>(&self) {
        let adc = I::regs();

        trace!("ADC14: disable_channel({})", self.adc_channel);

        if adc.adcsr().read().adst() {
            warn!(
                "ADC14: Trying to disable chan={} with a conversion running",
                self.adc_channel
            );
        }

        let index = self.adc_channel as usize / 16;
        let offset = self.adc_channel as usize % 16;

        adc.adansa(index).modify(|w| w.set_ans(offset, false));
    }

    #[inline]
    fn read_one<I: super::Instance>(&self) -> u16 {
        let adc = I::regs();

        trace!("ADC14: read_one({})", self.adc_channel);

        adc.addr(self.adc_channel as _).read()
    }

    #[inline]
    fn address<I: super::Instance>(&self) -> *const u16 {
        let adc = I::regs();
        adc.addr(self.adc_channel as _).as_ptr()
    }
}

impl Temperature {
    /// Converts an ADC reading to a temperature in degrees celsius.
    pub fn raw_to_celsius(&self, v_s: u16, vref_mv: u16, width: Resolution) -> i32 {
        // RA4M1: § 48.7 TSN Characteristics

        let tsn = pac::TSN;
        let v_1 = tsn.tscdr().read();

        let calibration_width = cfg_select! {
          adc12 => 4096, // 12-bit
          adc14 => 4096, // 12-bit
          adc16 => 32768, // 15-bit
          _ => compile_error!()
        };

        let measurement_width = match width {
            #[cfg(any(adc12, adc14))]
            Resolution::_12bit => 4_096,
            #[cfg(adc14)]
            Resolution::_14bit => 16_384,
            #[cfg(adc16)]
            Resolution::_16bit => 32_768,
        };

        let v_1 = (u64::from(v_1) * 3_300_000) / calibration_width;
        let v_s = (u64::from(v_s) * u64::from(vref_mv) * 1_000) / measurement_width;

        i32::try_from(((v_s - v_1) as i64 / i64::from(TSN_SLOPE)) + i64::from(TSN_INTERCEPT))
            .expect("Temperature reading overflowed the bounds of an i32")
    }

    /// Converts an ADC reading to a temperature in degrees celsius.
    pub fn raw_to_celsius_float(&self, v_s: u16, vref_mv: u16, width: Resolution) -> f32 {
        // RA4M1: § 48.7 TSN Characteristics

        let tsn = pac::TSN;
        let v_1 = tsn.tscdr().read();

        let calibration_width = cfg_select! {
          adc12 => 4096, // 12-bit
          adc14 => 4096, // 12-bit
          adc16 => 32768, // 15-bit
          _ => compile_error!()
        };

        let measurement_width = match width {
            #[cfg(any(adc12, adc14))]
            Resolution::_12bit => 4_096,
            #[cfg(adc14)]
            Resolution::_14bit => 16_384,
            #[cfg(adc16)]
            Resolution::_16bit => 32_768,
        };

        let v_1 = (u64::from(v_1) * 3_300_000) / calibration_width;
        let v_s = (u64::from(v_s) * u64::from(vref_mv) * 1_000) / measurement_width;

        let v_1 = v_1 * 100;
        let v_s = v_s * 100;
        let intercept = TSN_INTERCEPT * 100;

        (((v_s - v_1) as i64 / i64::from(TSN_SLOPE)) + i64::from(intercept)) as f32 / 100.0
    }
}

impl AdcChannel<u16> for Temperature {}

impl SealedAdcChannel<u16> for Temperature {
    #[inline]
    fn enable<I: super::Instance>(&self, config: AdcChannelConfig) {
        let adc = I::regs();

        trace!("ADC14: enable_channel(TEMPERATURE)");

        if adc.adcsr().read().adst() {
            warn!("ADC14: Trying to enable temp with a conversion running");
        }

        adc.adsstrt().write_value(config.sample_time);

        #[cfg(any(adc12, adc14))]
        {
            adc.adhvrefcnt().modify(|r| {
                use adc_pac::vals::Hvsel;

                r.set_hvsel(Hvsel::Avcc0);
            });
        }

        #[cfg(adc16)]
        {
            adc.addiscr().write_value(Addiscr(0xF));

            adc.vrefampcnt().modify(|r| {
                r.set_vrefadcg(Vrefadcg::_11);
                r.set_bgren(true);
            });

            // Table 47.45 Internal reference voltage for 16-bit ADC (VREFADC) characteristics
            block_for(Duration::from_micros(2000));

            adc.vrefampcnt().modify(|r| {
                r.set_oldeten(true);
                r.set_vrefadcen(true);
                r.set_adslp(false);
            });
            block_for(Duration::from_micros(2000));
        }

        adc.adexicr().write(|w| w.set_tssa(true));
        block_for(Duration::from_micros(2000));

        if config.average_mode != AverageMode::Off {
            todo!()
        }
    }

    #[inline]
    fn disable<I: super::Instance>(&self) {
        let adc = I::regs();

        trace!("ADC14: disable_channel(TEMPERATURE)");

        if adc.adcsr().read().adst() {
            warn!("ADC14: Trying to disable temp with a conversion running");
        }

        adc.adexicr().modify(|w| w.set_tssa(false));
    }

    #[inline]
    fn read_one<I: super::Instance>(&self) -> u16 {
        let adc = I::regs();

        trace!("ADC14: read_one(TEMPERATURE)");
        adc.adtsdr().read()
    }

    #[inline]
    fn address<I: super::Instance>(&self) -> *const u16 {
        let adc = I::regs();
        adc.adtsdr().as_ptr()
    }
}

impl AdcChannel<u16> for Vref {}

impl SealedAdcChannel<u16> for Vref {
    #[inline]
    fn enable<I: super::Instance>(&self, config: AdcChannelConfig) {
        let adc = I::regs();

        trace!("ADC14: enable_channel(VREF)");

        if adc.adcsr().read().adst() {
            warn!("ADC14: Trying to enable Vref with a conversion running");
        }

        adc.adsstro().write_value(config.sample_time);

        adc.adexicr().modify(|w| w.set_ocsa(true));

        if config.average_mode != AverageMode::Off {
            todo!()
        }
    }

    #[inline]
    fn disable<I: super::Instance>(&self) {
        let adc = I::regs();

        trace!("ADC14: disable_channel(VREF)");

        if adc.adcsr().read().adst() {
            warn!("ADC14: Trying to disable Vref with a conversion running");
        }

        adc.adexicr().modify(|w| w.set_ocsa(false));
    }

    #[inline]
    fn read_one<I: super::Instance>(&self) -> u16 {
        let adc = I::regs();

        trace!("ADC14: read_one(VREF)");
        adc.adocdr().read()
    }

    #[inline]
    fn address<I: super::Instance>(&self) -> *const u16 {
        let adc = I::regs();
        adc.adocdr().as_ptr()
    }
}

macro_rules! adc_pin {
    ($channel:literal, $pin:ident) => {
        impl crate::adc::channel::AdcInputPin for crate::peripherals::$pin {
            const CHANNEL: u8 = $channel;
        }
        impl crate::adc::channel::SealedAdcInputPin for crate::peripherals::$pin {}
    };
}
pub(crate) use adc_pin;