rp2350 0.1.0

Hardware Abstraction Layer for RP2350 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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//! ADC (Analog-to-Digital Converter) HAL for RP2350
//!
//! This module provides a type-safe interface for the RP2350's 12-bit SAR ADC.
//!
//! # Features
//!
//! - 500 kS/s (using 48 MHz clock)
//! - 12-bit resolution (9.2 ENOB typical)
//! - **RP2350A**: 4 analog inputs (GPIO 26-29) + temperature sensor
//! - **RP2350B**: 8 analog inputs (GPIO 40-47) + temperature sensor
//! - Round-robin sampling mode
//! - FIFO with DMA support
//! - One-shot and free-running modes
//! - **Type-safe pin ownership**: Pins are consumed when configured for ADC
//!
//! # GPIO Mapping
//!
//! | Channel | RP2350A GPIO | RP2350B GPIO |
//! |---------|--------------|--------------|
//! | 0       | GPIO 26      | GPIO 40      |
//! | 1       | GPIO 27      | GPIO 41      |
//! | 2       | GPIO 28      | GPIO 42      |
//! | 3       | GPIO 29      | GPIO 43      |
//! | 4       | -            | GPIO 44      |
//! | 5       | -            | GPIO 45      |
//! | 6       | -            | GPIO 46      |
//! | 7       | -            | GPIO 47      |
//! | 4/8     | Temp Sensor  | Temp Sensor  |
//!
//! # Example (Basic Usage)
//!
//! ```ignore
//! use rp2350::adc::Adc;
//! use rp2350::gpio::Pins;
//!
//! let dp = Peripherals::take().unwrap();
//! let pins = Pins::init(dp.IO_BANK0, dp.PADS_BANK0);
//!
//! // Create ADC controller
//! let mut adc = Adc::init(dp.ADC);
//!
//! // Configure pin for ADC - consumes the pin, returns AdcInput
//! let input0 = pins.pin::<26>().into_adc_input();
//!
//! // Read from the configured input
//! let value = adc.read(&input0);
//! let voltage_mv = Adc::to_millivolts(value);
//!
//! // Compile error: pins.pin::<26>() already moved!
//! // pins.pin::<26>().into_output();
//! ```
//!
//! # Example (Temperature Sensor)
//!
//! ```ignore
//! let mut adc = Adc::init(dp.ADC);
//!
//! // Read temperature (no pin needed)
//! let raw = adc.read_temp();
//! let temp_c = Adc::convert_temp_to_celsius(raw);
//! ```
//!
//! # Example (Round-Robin with FIFO)
//!
//! ```ignore
//! let mut adc = Adc::init(dp.ADC);
//!
//! // Configure pins - ownership transferred
//! let input0 = pins.pin::<26>().into_adc_input();
//! let input1 = pins.pin::<27>().into_adc_input();
//!
//! // Setup FIFO
//! adc.fifo_setup(true, false, 1, false, false);
//!
//! // Enable round-robin for channels 0, 1, and temperature
//! adc.set_round_robin(input0.mask() | input1.mask() | (1 << TEMPERATURE_CHANNEL));
//!
//! // Start free-running mode
//! adc.start_free_running();
//!
//! // Read from FIFO
//! while let Some(value) = adc.fifo_pop() {
//!     // Process value...
//! }
//! ```

use crate::Steal;
use crate::gpio::{Function, Pin, Pull};
use crate::reset::Resettable;
use rp2350_pac::ADC;

// ============================================================================
// Constants
// ============================================================================

/// Number of analog input channels (excluding temperature sensor)
#[cfg(not(feature = "rp2350b"))]
pub const NUM_CHANNELS: usize = 4;

/// Number of analog input channels (excluding temperature sensor)
#[cfg(feature = "rp2350b")]
pub const NUM_CHANNELS: usize = 8;

/// Total number of ADC channels including temperature sensor
pub const NUM_ADC_CHANNELS: usize = NUM_CHANNELS + 1;

/// Base GPIO for ADC inputs (RP2350A: GPIO 26)
#[cfg(not(feature = "rp2350b"))]
pub const ADC_BASE_PIN: u8 = 26;

/// Base GPIO for ADC inputs (RP2350B: GPIO 40)
#[cfg(feature = "rp2350b")]
pub const ADC_BASE_PIN: u8 = 40;

/// Temperature sensor channel number (last channel)
pub const TEMPERATURE_CHANNEL: u8 = NUM_CHANNELS as u8;

/// ADC voltage reference (typically 3.3V)
pub const ADC_VREF: f32 = 3.3;

/// ADC maximum value (12-bit)
pub const ADC_MAX: u16 = 4095;

/// DMA DREQ number for ADC
pub const ADC_DREQ: u8 = 48;

// ============================================================================
// ADC Input (Type-Safe Pin Wrapper)
// ============================================================================

/// A GPIO pin configured for ADC input.
///
/// This type holds ownership of a pin that has been configured for analog input.
/// The pin cannot be used for other purposes while this exists.
///
/// # Type Parameters
/// * `const CH` - The ADC channel number (compile-time constant)
pub struct AdcInput<const CH: u8> {
    // We don't need to store the pin, just consume ownership
    _marker: core::marker::PhantomData<*const ()>,
}

impl<const CH: u8> AdcInput<CH> {
    /// Get the channel number for this input.
    #[inline]
    pub const fn channel(&self) -> u8 {
        CH
    }

    /// Get the channel mask for round-robin configuration.
    #[inline]
    pub const fn mask(&self) -> u16 {
        1 << CH
    }
}

impl<const CH: u8> core::fmt::Debug for AdcInput<CH> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("AdcInput").field("channel", &CH).finish()
    }
}

// ============================================================================
// ADC Pin Trait
// ============================================================================

/// Trait for pins that can be used as ADC inputs.
///
/// This trait is implemented for GPIO pins that are connected to ADC channels.
/// - **RP2350A**: GPIO 26-29 (ADC channels 0-3)
/// - **RP2350B**: GPIO 40-47 (ADC channels 0-7)
pub trait AdcPin: Sized {
    /// The ADC channel type with const generic
    type Input;

    /// Configure this pin for ADC input and consume it.
    ///
    /// Returns an `AdcInput` that can be used with `Adc::read()`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let input = pins.pin::<26>().into_adc_input();
    /// let value = adc.read(&input);
    /// ```
    fn into_adc_input(self) -> Self::Input;
}

// Implement AdcPin for valid ADC pins
macro_rules! impl_adc_pin {
    ($pin_num:literal, $channel:literal) => {
        impl AdcPin for Pin<$pin_num> {
            type Input = AdcInput<$channel>;

            fn into_adc_input(mut self) -> AdcInput<$channel> {
                // Set to NULL function (high-Z output for ADC)
                self.set_function(Function::Null);
                // Disable pull resistors
                self.set_pull(Pull::None);
                // Initialize pad (clear isolation, enable input)
                self.init_pad();

                AdcInput {
                    _marker: core::marker::PhantomData,
                }
            }
        }
    };
}

// RP2350A ADC pin mappings
#[cfg(feature = "rp2350a")]
mod pin_impls {
    use super::*;
    impl_adc_pin!(26, 0);
    impl_adc_pin!(27, 1);
    impl_adc_pin!(28, 2);
    impl_adc_pin!(29, 3);
}

// RP2350B ADC pin mappings
#[cfg(feature = "rp2350b")]
mod pin_impls {
    use super::*;
    impl_adc_pin!(40, 0);
    impl_adc_pin!(41, 1);
    impl_adc_pin!(42, 2);
    impl_adc_pin!(43, 3);
    impl_adc_pin!(44, 4);
    impl_adc_pin!(45, 5);
    impl_adc_pin!(46, 6);
    impl_adc_pin!(47, 7);
}

// ============================================================================
// ADC Controller
// ============================================================================

/// ADC peripheral controller.
///
/// Provides a type-safe interface for analog-to-digital conversion.
///
/// # Example
///
/// ```ignore
/// let mut adc = Adc::init(dp.ADC);
///
/// // Configure pin for ADC - ownership transferred
/// let input = pins.pin::<26>().into_adc_input();
///
/// // Read from the input
/// let value = adc.read(&input);
/// ```
pub struct Adc {
    adc: ADC,
}

impl Adc {
    /// Initialize the ADC controller.
    ///
    /// Initializes the ADC subsystem by taking it out of reset and enabling it.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let mut adc = Adc::init(dp.ADC);
    /// ```
    pub fn init(adc: ADC) -> Self {
        // Reset peripheral to ensure clean register state
        <rp2350_pac::ADC as Resettable>::init();

        // Enable ADC
        adc.cs.set_en();

        // Wait for ADC to be ready (bounded)
        crate::spin::spin_wait("ADC ready", || adc.cs.ready());

        Self { adc }
    }

    /// Obtain an Adc handle without ownership checks.
    ///
    /// # Safety
    ///
    /// The caller must ensure [`Adc::init()`] has been called first
    /// and that no data races occur.
    #[inline]
    pub unsafe fn steal() -> Steal<Self> {
        Steal(Self {
            adc: unsafe { ADC::steal() },
        })
    }

    /// Get reference to the underlying register block.
    ///
    /// # Safety
    /// Direct register access can bypass the HAL's safety guarantees.
    #[inline]
    pub unsafe fn regs(&self) -> &rp2350_pac::adc::RegisterBlock {
        &self.adc
    }

    // ========================================================================
    // Single-Shot Conversion
    // ========================================================================

    /// Perform a single conversion on the specified input.
    ///
    /// Blocks until conversion is complete.
    ///
    /// # Arguments
    /// * `input` - Reference to a configured ADC input
    ///
    /// # Example
    ///
    /// ```ignore
    /// let input = pins.pin::<26>().into_adc_input();
    /// let value = adc.read(&input);
    /// let voltage_mv = Adc::to_millivolts(value);
    /// ```
    pub fn read<const CH: u8>(&mut self, _input: &AdcInput<CH>) -> u16 {
        self.read_channel(CH)
    }

    /// Perform a single conversion on the specified channel (internal).
    #[inline]
    fn read_channel(&mut self, channel: u8) -> u16 {
        // Select channel
        self.adc.cs.write_ainsel(channel as u32);

        // Start conversion
        self.adc.cs.trigger_start_once();

        // Wait for completion (bounded)
        crate::spin::spin_wait("ADC conversion ready", || self.adc.cs.ready());

        self.adc.result.result() as u16
    }

    /// Read temperature sensor.
    ///
    /// Enables the temperature sensor for the duration of this reading,
    /// then restores the previous enable state.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let raw = adc.read_temp();
    /// let temp_c = Adc::convert_temp_to_celsius(raw);
    /// ```
    pub fn read_temp(&mut self) -> u16 {
        let was_enabled = self.adc.cs.ts_en();
        self.adc.cs.set_ts_en();
        let result = self.read_channel(TEMPERATURE_CHANNEL);
        if !was_enabled {
            self.adc.cs.clr_ts_en();
        }
        result
    }

    /// Start a conversion on the specified input (non-blocking).
    ///
    /// Use `is_ready()` to check for completion, then `read_result()`.
    #[inline]
    pub fn start_conversion<const CH: u8>(&mut self, _input: &AdcInput<CH>) {
        self.adc.cs.write_ainsel(CH as u32);
        self.adc.cs.trigger_start_once();
    }

    /// Read the result of a completed conversion.
    ///
    /// Only valid after a conversion has completed.
    #[inline]
    pub fn read_result(&self) -> u16 {
        self.adc.result.result() as u16
    }

    // ========================================================================
    // Status
    // ========================================================================

    /// Check if ADC is ready for a conversion.
    #[inline]
    pub fn is_ready(&self) -> bool {
        self.adc.cs.ready()
    }

    /// Check if the most recent conversion had an error.
    #[inline]
    pub fn has_error(&self) -> bool {
        self.adc.cs.err()
    }

    /// Check if any past conversion had an error (sticky).
    #[inline]
    pub fn has_sticky_error(&self) -> bool {
        self.adc.cs.err_sticky()
    }

    /// Clear the sticky error flag.
    #[inline]
    pub fn clear_sticky_error(&mut self) {
        self.adc.cs.set_err_sticky();
    }

    /// Get the currently selected channel.
    #[inline]
    pub fn selected_channel(&self) -> u8 {
        self.adc.cs.ainsel() as u8
    }

    // ========================================================================
    // Temperature Sensor Control
    // ========================================================================

    /// Enable the on-chip temperature sensor.
    #[inline]
    pub fn enable_temp_sensor(&mut self) {
        self.adc.cs.set_ts_en();
    }

    /// Disable the on-chip temperature sensor.
    #[inline]
    pub fn disable_temp_sensor(&mut self) {
        self.adc.cs.clr_ts_en();
    }

    /// Check if temperature sensor is enabled.
    #[inline]
    pub fn is_temp_sensor_enabled(&self) -> bool {
        self.adc.cs.ts_en()
    }

    // ========================================================================
    // Round-Robin Sampling
    // ========================================================================

    /// Enable round-robin sampling.
    ///
    /// # Arguments
    /// * `channel_mask` - Bit mask of channels to sample (bit 0 = channel 0, etc.)
    ///
    /// # Example
    ///
    /// ```ignore
    /// let input0 = pins.pin::<26>().into_adc_input();
    /// let input1 = pins.pin::<27>().into_adc_input();
    ///
    /// // Sample channels 0, 1, and temperature sensor
    /// adc.set_round_robin(input0.mask() | input1.mask() | (1 << TEMPERATURE_CHANNEL));
    /// ```
    #[inline]
    pub fn set_round_robin(&mut self, channel_mask: u16) {
        let mask = channel_mask & ((1 << NUM_ADC_CHANNELS) - 1);
        self.adc.cs.write_rrobin(mask as u32);
    }

    /// Disable round-robin sampling.
    #[inline]
    pub fn disable_round_robin(&mut self) {
        self.adc.cs.write_rrobin(0);
    }

    /// Get current round-robin mask.
    #[inline]
    pub fn round_robin_mask(&self) -> u16 {
        self.adc.cs.rrobin() as u16
    }

    // ========================================================================
    // Free-Running Mode
    // ========================================================================

    /// Start free-running mode.
    ///
    /// The ADC will continuously perform conversions.
    #[inline]
    pub fn start_free_running(&mut self) {
        self.adc.cs.set_start_many();
    }

    /// Stop free-running mode.
    #[inline]
    pub fn stop_free_running(&mut self) {
        self.adc.cs.clr_start_many();
    }

    /// Check if free-running mode is active.
    #[inline]
    pub fn is_free_running(&self) -> bool {
        self.adc.cs.start_many()
    }

    // ========================================================================
    // Clock Divisor
    // ========================================================================

    /// Set the clock divisor for free-running mode.
    ///
    /// Total period = (1 + int + frac/256) cycles.
    /// Note: Minimum 96 cycles needed per conversion.
    ///
    /// # Arguments
    /// * `int_div` - Integer part (0-65535)
    /// * `frac_div` - Fractional part (0-255)
    pub fn set_clkdiv(&mut self, int_div: u16, frac_div: u8) {
        self.adc.div.write_int(int_div as u32);
        self.adc.div.write_frac(frac_div as u32);
    }

    /// Set clock divisor from a floating point value.
    pub fn set_clkdiv_float(&mut self, div: f32) {
        let div = div.clamp(0.0, 65536.0);
        let int_div = div as u16;
        let frac_div = ((div - int_div as f32) * 256.0) as u8;
        self.set_clkdiv(int_div, frac_div);
    }

    // ========================================================================
    // FIFO Operations
    // ========================================================================

    /// Setup the ADC FIFO.
    ///
    /// # Arguments
    /// * `enable` - Enable writing results to FIFO
    /// * `dreq_enable` - Enable DMA requests when FIFO has data
    /// * `threshold` - DMA request/IRQ threshold (0-15)
    /// * `error_in_fifo` - Include error flag in FIFO data
    /// * `byte_shift` - Shift results to one byte for byte DMA
    pub fn fifo_setup(
        &mut self,
        enable: bool,
        dreq_enable: bool,
        threshold: u8,
        error_in_fifo: bool,
        byte_shift: bool,
    ) {
        let mut fcs = self.adc.fcs.read_val();
        fcs = fcs.with_en(enable);
        fcs = fcs.with_dreq_en(dreq_enable);
        fcs = fcs.with_thresh(threshold.min(15) as u32);
        fcs = fcs.with_err(error_in_fifo);
        fcs = fcs.with_shift(byte_shift);
        self.adc.fcs.write_val(fcs);
    }

    /// Enable FIFO.
    #[inline]
    pub fn fifo_enable(&mut self) {
        self.adc.fcs.set_en();
    }

    /// Disable FIFO.
    #[inline]
    pub fn fifo_disable(&mut self) {
        self.adc.fcs.clr_en();
    }

    /// Check if FIFO is empty.
    #[inline]
    pub fn fifo_is_empty(&self) -> bool {
        self.adc.fcs.empty()
    }

    /// Check if FIFO is full.
    #[inline]
    pub fn fifo_is_full(&self) -> bool {
        self.adc.fcs.full()
    }

    /// Get FIFO level (number of entries).
    #[inline]
    pub fn fifo_level(&self) -> u8 {
        self.adc.fcs.level() as u8
    }

    /// Check if FIFO has overflowed (sticky).
    #[inline]
    pub fn fifo_has_overflowed(&self) -> bool {
        self.adc.fcs.over()
    }

    /// Clear FIFO overflow flag.
    #[inline]
    pub fn fifo_clear_overflow(&mut self) {
        self.adc.fcs.set_over();
    }

    /// Check if FIFO has underflowed (sticky).
    #[inline]
    pub fn fifo_has_underflowed(&self) -> bool {
        self.adc.fcs.under()
    }

    /// Clear FIFO underflow flag.
    #[inline]
    pub fn fifo_clear_underflow(&mut self) {
        self.adc.fcs.set_under();
    }

    /// Pop a value from the FIFO.
    ///
    /// Returns `None` if FIFO is empty.
    pub fn fifo_pop(&mut self) -> Option<u16> {
        if self.fifo_is_empty() {
            None
        } else {
            Some(self.adc.fifo.val() as u16)
        }
    }

    /// Pop a value from the FIFO, blocking until available.
    pub fn fifo_pop_blocking(&mut self) -> u16 {
        while self.fifo_is_empty() {
            core::hint::spin_loop();
        }
        self.adc.fifo.val() as u16
    }

    /// Pop a value from FIFO with error flag.
    ///
    /// Returns (value, error_flag). Only valid if error_in_fifo was enabled.
    pub fn fifo_pop_with_error(&mut self) -> Option<(u16, bool)> {
        if self.fifo_is_empty() {
            None
        } else {
            let raw = self.adc.fifo.read_bits();
            let value = (raw & 0xFFF) as u16;
            let error = (raw & 0x8000) != 0;
            Some((value, error))
        }
    }

    /// Drain the FIFO, discarding all contents.
    ///
    /// In free-running mode the FIFO is refilled continuously, so this can
    /// never guarantee an empty FIFO — stop free-running
    /// ([`Self::stop_free_running`]) first if you need a deterministic drain.
    pub fn fifo_drain(&mut self) {
        // Drain the FIFO (no idle-wait: in free-running mode the ADC never goes
        // idle, and the old spin_wait would time out).
        while !self.fifo_is_empty() {
            let _ = self.adc.fifo.val();
        }
    }

    /// Get the FIFO address for DMA.
    #[inline]
    pub fn fifo_address(&self) -> *const u32 {
        &self.adc.fifo as *const _ as *const u32
    }

    /// Get the FIFO register address as u32 for DMA configuration.
    #[inline]
    pub fn fifo_dma_address(&self) -> u32 {
        &self.adc.fifo as *const _ as u32
    }

    /// DREQ selection for ADC DMA transfers.
    ///
    /// Use this with a DMA channel's `treq_sel` to pace reads
    /// from the ADC FIFO.
    pub const DREQ: crate::dma::TreqSel = crate::dma::TreqSel::Adc;

    /// Get the DMA DREQ number for ADC.
    #[inline]
    pub const fn dreq() -> u8 {
        ADC_DREQ
    }

    // ========================================================================
    // Interrupts
    // ========================================================================

    /// Check if FIFO interrupt is pending (raw).
    #[inline]
    pub fn fifo_irq_raw(&self) -> bool {
        self.adc.intr.fifo()
    }

    /// Enable FIFO interrupt.
    #[inline]
    pub fn enable_fifo_irq(&mut self) {
        self.adc.inte.set_fifo();
    }

    /// Disable FIFO interrupt.
    #[inline]
    pub fn disable_fifo_irq(&mut self) {
        self.adc.inte.clr_fifo();
    }

    /// Check masked interrupt status.
    #[inline]
    pub fn fifo_irq_status(&self) -> bool {
        self.adc.ints.fifo()
    }

    /// Force FIFO interrupt (for testing).
    #[inline]
    pub fn force_fifo_irq(&mut self) {
        self.adc.intf.set_fifo();
    }

    /// Clear forced FIFO interrupt.
    #[inline]
    pub fn clear_forced_fifo_irq(&mut self) {
        self.adc.intf.clr_fifo();
    }

    // ========================================================================
    // Utility Functions
    // ========================================================================

    /// Convert raw ADC value to voltage.
    #[inline]
    pub fn to_voltage(raw: u16) -> f32 {
        (raw as f32) * ADC_VREF / (ADC_MAX as f32 + 1.0)
    }

    /// Convert raw ADC value to millivolts.
    #[inline]
    pub fn to_millivolts(raw: u16) -> u32 {
        ((raw as u32) * 3300) / 4096
    }

    /// Convert raw ADC temperature value to degrees Celsius.
    ///
    /// Formula: T = 27 - (ADC_voltage - 0.706) / 0.001721
    #[inline]
    pub fn convert_temp_to_celsius(raw: u16) -> f32 {
        let voltage = (raw as f32) * ADC_VREF / (ADC_MAX as f32 + 1.0);
        27.0 - (voltage - 0.706) / 0.001721
    }

    /// Release the ADC peripheral.
    ///
    /// Returns the underlying PAC peripheral for reuse.
    #[inline]
    pub fn release(self) -> ADC {
        self.adc
    }
}

// ============================================================================
// Debug implementation
// ============================================================================

impl core::fmt::Debug for Adc {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Adc")
            .field("selected_channel", &self.selected_channel())
            .field("is_ready", &self.is_ready())
            .finish()
    }
}