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
use core::marker::PhantomData;

use crate::clock::Clocks;
use crate::pac;
use crate::prelude::*;
use crate::time::Hertz;
use num_enum::{IntoPrimitive, TryFromPrimitive};

pub const ADC_MIN_CLK: Hertz = Hertz::from_raw(2_000_000);
pub const ADC_MAX_CLK: Hertz = Hertz::from_raw(12_500_000);

#[derive(Debug, PartialEq, Eq, Copy, Clone, TryFromPrimitive, IntoPrimitive)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum ChannelSelect {
    /// Analogue Input 0 external channel
    AnIn0 = 0,
    /// Analogue Input 1 external channel
    AnIn1 = 1,
    /// Analogue Input 2 external channel
    AnIn2 = 2,
    /// Analogue Input 3 external channel
    AnIn3 = 3,
    /// Analogue Input 4 external channel
    AnIn4 = 4,
    /// Analogue Input 5 external channel
    AnIn5 = 5,
    /// Analogue Input 6 external channel
    AnIn6 = 6,
    /// Analogue Input 7 external channel
    AnIn7 = 7,
    /// DAC 0 internal channel
    Dac0 = 8,
    /// DAC 1 internal channel
    Dac1 = 9,
    /// Internal temperature sensor
    TempSensor = 10,
    /// Internal bandgap 1 V reference
    Bandgap1V = 11,
    /// Internal bandgap 1.5 V reference
    Bandgap1_5V = 12,
    Avdd1_5 = 13,
    Dvdd1_5 = 14,
    /// Internally generated Voltage equal to VREFH / 2
    Vrefp5 = 15,
}

bitflags::bitflags! {
    pub struct MultiChannelSelect: u16 {
        const AnIn0 = 1;
        const AnIn1 = 1 << 1;
        const AnIn2 = 1 << 2;
        const AnIn3 = 1 << 3;
        const AnIn4 = 1 << 4;
        const AnIn5 = 1 << 5;
        const AnIn6 = 1 << 6;
        const AnIn7 = 1 << 7;
        const Dac0 = 1 << 8;
        const Dac1 = 1 << 9;
        const TempSensor = 1 << 10;
        const Bandgap1V = 1 << 11;
        const Bandgap1_5V = 1 << 12;
        const Avdd1_5 = 1 << 13;
        const Dvdd1_5 = 1 << 14;
        const Vrefp5 = 1 << 15;
    }
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AdcEmptyError;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct InvalidChannelRangeError;

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct BufferTooSmallError;

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum AdcRangeReadError {
    InvalidChannelRange(InvalidChannelRangeError),
    BufferTooSmall(BufferTooSmallError),
}

impl From<InvalidChannelRangeError> for AdcRangeReadError {
    fn from(value: InvalidChannelRangeError) -> Self {
        AdcRangeReadError::InvalidChannelRange(value)
    }
}

impl From<BufferTooSmallError> for AdcRangeReadError {
    fn from(value: BufferTooSmallError) -> Self {
        AdcRangeReadError::BufferTooSmall(value)
    }
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ChannelValue {
    /// If the channel tag is enabled, this field will contain the determined channel tag.
    channel: ChannelSelect,
    /// Raw value.
    value: u16,
}

impl Default for ChannelValue {
    fn default() -> Self {
        Self {
            channel: ChannelSelect::AnIn0,
            value: Default::default(),
        }
    }
}

impl ChannelValue {
    #[inline]
    pub fn value(&self) -> u16 {
        self.value
    }

    #[inline]
    pub fn channel(&self) -> ChannelSelect {
        self.channel
    }
}

pub enum ChannelTagEnabled {}
pub enum ChannelTagDisabled {}

pub struct Adc<TagEnabled = ChannelTagDisabled> {
    adc: pac::Adc,
    phantom: PhantomData<TagEnabled>,
}

impl Adc<ChannelTagEnabled> {}

impl Adc<ChannelTagDisabled> {
    pub fn new(syscfg: &mut pac::Sysconfig, adc: pac::Adc, clocks: &Clocks) -> Self {
        Self::generic_new(syscfg, adc, clocks)
    }

    pub fn trigger_and_read_single_channel(&self, ch: ChannelSelect) -> Result<u16, AdcEmptyError> {
        self.generic_trigger_and_read_single_channel(ch)
            .map(|v| v & 0xfff)
    }

    /// Perform a sweep for a specified range of ADC channels.
    ///
    /// Returns the number of read values which were written to the passed RX buffer.
    pub fn sweep_and_read_range(
        &self,
        lower_bound_idx: u8,
        upper_bound_idx: u8,
        rx_buf: &mut [u16],
    ) -> Result<(), AdcRangeReadError> {
        self.generic_prepare_range_sweep_and_wait_until_ready(
            lower_bound_idx,
            upper_bound_idx,
            rx_buf.len(),
        )?;
        for i in 0..self.adc.status().read().fifo_entry_cnt().bits() {
            rx_buf[i as usize] = self.adc.fifo_data().read().bits() as u16 & 0xfff;
        }
        Ok(())
    }

    pub fn sweep_and_read_multiselect(
        &self,
        ch_select: MultiChannelSelect,
        rx_buf: &mut [u16],
    ) -> Result<(), BufferTooSmallError> {
        self.generic_prepare_multiselect_sweep_and_wait_until_ready(ch_select, rx_buf.len())?;
        for i in 0..self.adc.status().read().fifo_entry_cnt().bits() {
            rx_buf[i as usize] = self.adc.fifo_data().read().bits() as u16 & 0xfff;
        }
        Ok(())
    }

    pub fn try_read_single_value(&self) -> nb::Result<Option<u16>, ()> {
        self.generic_try_read_single_value()
            .map(|v| v.map(|v| v & 0xfff))
    }
}

impl Adc<ChannelTagEnabled> {
    pub fn new_with_channel_tag(
        syscfg: &mut pac::Sysconfig,
        adc: pac::Adc,
        clocks: &Clocks,
    ) -> Self {
        let mut adc = Self::generic_new(syscfg, adc, clocks);
        adc.enable_channel_tag();
        adc
    }

    pub fn trigger_and_read_single_channel(
        &self,
        ch: ChannelSelect,
    ) -> Result<ChannelValue, AdcEmptyError> {
        self.generic_trigger_and_read_single_channel(ch)
            .map(|v| self.create_channel_value(v))
    }

    pub fn try_read_single_value(&self) -> nb::Result<Option<ChannelValue>, ()> {
        self.generic_try_read_single_value()
            .map(|v| v.map(|v| self.create_channel_value(v)))
    }

    /// Perform a sweep for a specified range of ADC channels.
    ///
    /// Returns the number of read values which were written to the passed RX buffer.
    pub fn sweep_and_read_range(
        &self,
        lower_bound_idx: u8,
        upper_bound_idx: u8,
        rx_buf: &mut [ChannelValue],
    ) -> Result<usize, AdcRangeReadError> {
        self.generic_prepare_range_sweep_and_wait_until_ready(
            lower_bound_idx,
            upper_bound_idx,
            rx_buf.len(),
        )?;
        let fifo_entry_count = self.adc.status().read().fifo_entry_cnt().bits();
        for i in 0..core::cmp::min(fifo_entry_count, rx_buf.len() as u8) {
            rx_buf[i as usize] =
                self.create_channel_value(self.adc.fifo_data().read().bits() as u16);
        }
        Ok(fifo_entry_count as usize)
    }

    pub fn sweep_and_read_multiselect(
        &self,
        ch_select: MultiChannelSelect,
        rx_buf: &mut [ChannelValue],
    ) -> Result<(), BufferTooSmallError> {
        self.generic_prepare_multiselect_sweep_and_wait_until_ready(ch_select, rx_buf.len())?;
        for i in 0..self.adc.status().read().fifo_entry_cnt().bits() {
            rx_buf[i as usize] =
                self.create_channel_value(self.adc.fifo_data().read().bits() as u16);
        }
        Ok(())
    }

    #[inline]
    pub fn create_channel_value(&self, raw_value: u16) -> ChannelValue {
        ChannelValue {
            value: raw_value & 0xfff,
            channel: ChannelSelect::try_from(((raw_value >> 12) & 0xf) as u8).unwrap(),
        }
    }
}

impl<TagEnabled> Adc<TagEnabled> {
    fn generic_new(syscfg: &mut pac::Sysconfig, adc: pac::Adc, _clocks: &Clocks) -> Self {
        syscfg.enable_peripheral_clock(crate::clock::PeripheralSelect::Adc);
        adc.ctrl().write(|w| unsafe { w.bits(0) });
        let adc = Self {
            adc,
            phantom: PhantomData,
        };
        adc.clear_fifo();
        adc
    }

    #[inline(always)]
    fn enable_channel_tag(&mut self) {
        self.adc.ctrl().modify(|_, w| w.chan_tag_en().set_bit());
    }

    #[inline(always)]
    fn disable_channel_tag(&mut self) {
        self.adc.ctrl().modify(|_, w| w.chan_tag_en().clear_bit());
    }

    #[inline(always)]
    pub fn channel_tag_enabled(&self) -> bool {
        self.adc.ctrl().read().chan_tag_en().bit_is_set()
    }

    #[inline(always)]
    pub fn clear_fifo(&self) {
        self.adc.fifo_clr().write(|w| unsafe { w.bits(1) });
    }

    pub fn generic_try_read_single_value(&self) -> nb::Result<Option<u16>, ()> {
        if self.adc.status().read().adc_busy().bit_is_set() {
            return Err(nb::Error::WouldBlock);
        }
        if self.adc.status().read().fifo_entry_cnt().bits() == 0 {
            return Ok(None);
        }
        Ok(Some(self.adc.fifo_data().read().bits() as u16))
    }

    fn generic_trigger_single_channel(&self, ch: ChannelSelect) {
        self.adc.ctrl().modify(|_, w| {
            w.ext_trig_en().clear_bit();
            unsafe {
                // N + 1 conversions, so set set 0 here.
                w.conv_cnt().bits(0);
                w.chan_en().bits(1 << ch as u8)
            }
        });
        self.clear_fifo();

        self.adc.ctrl().modify(|_, w| w.manual_trig().set_bit());
    }

    fn generic_prepare_range_sweep_and_wait_until_ready(
        &self,
        lower_bound_idx: u8,
        upper_bound_idx: u8,
        buf_len: usize,
    ) -> Result<(), AdcRangeReadError> {
        if (lower_bound_idx > 15 || upper_bound_idx > 15) || lower_bound_idx > upper_bound_idx {
            return Err(InvalidChannelRangeError.into());
        }
        let ch_count = upper_bound_idx - lower_bound_idx + 1;
        if buf_len < ch_count as usize {
            return Err(BufferTooSmallError.into());
        }
        let mut ch_select = 0;
        for i in lower_bound_idx..upper_bound_idx + 1 {
            ch_select |= 1 << i;
        }
        self.generic_trigger_sweep(ch_select);
        cortex_m::asm::nop();
        cortex_m::asm::nop();
        while self.adc.status().read().adc_busy().bit_is_set() {
            cortex_m::asm::nop();
        }
        Ok(())
    }

    fn generic_prepare_multiselect_sweep_and_wait_until_ready(
        &self,
        ch_select: MultiChannelSelect,
        buf_len: usize,
    ) -> Result<(), BufferTooSmallError> {
        let ch_select = ch_select.bits();
        let ch_count = ch_select.count_ones();
        if buf_len < ch_count as usize {
            return Err(BufferTooSmallError);
        }
        self.generic_trigger_sweep(ch_select);
        while self.adc.status().read().adc_busy().bit_is_set() {
            cortex_m::asm::nop();
        }
        Ok(())
    }

    fn generic_trigger_sweep(&self, ch_select: u16) {
        let ch_num = ch_select.count_ones() as u8;
        assert!(ch_num > 0);
        self.adc.ctrl().modify(|_, w| {
            w.ext_trig_en().clear_bit();
            unsafe {
                // N + 1 conversions.
                w.conv_cnt().bits(0);
                w.chan_en().bits(ch_select);
                w.sweep_en().set_bit()
            }
        });
        self.clear_fifo();

        self.adc.ctrl().modify(|_, w| w.manual_trig().set_bit());
    }

    fn generic_trigger_and_read_single_channel(
        &self,
        ch: ChannelSelect,
    ) -> Result<u16, AdcEmptyError> {
        self.generic_trigger_single_channel(ch);
        nb::block!(self.generic_try_read_single_value())
            .unwrap()
            .ok_or(AdcEmptyError)
    }
}

impl From<Adc<ChannelTagDisabled>> for Adc<ChannelTagEnabled> {
    fn from(value: Adc<ChannelTagDisabled>) -> Self {
        let mut adc = Self {
            adc: value.adc,
            phantom: PhantomData,
        };
        adc.enable_channel_tag();
        adc
    }
}

impl From<Adc<ChannelTagEnabled>> for Adc<ChannelTagDisabled> {
    fn from(value: Adc<ChannelTagEnabled>) -> Self {
        let mut adc = Self {
            adc: value.adc,
            phantom: PhantomData,
        };
        adc.disable_channel_tag();
        adc
    }
}