use crate::Steal;
use crate::gpio::{Function, Pin, Pull};
use crate::reset::Resettable;
use rp2350_pac::ADC;
#[cfg(not(feature = "rp2350b"))]
pub const NUM_CHANNELS: usize = 4;
#[cfg(feature = "rp2350b")]
pub const NUM_CHANNELS: usize = 8;
pub const NUM_ADC_CHANNELS: usize = NUM_CHANNELS + 1;
#[cfg(not(feature = "rp2350b"))]
pub const ADC_BASE_PIN: u8 = 26;
#[cfg(feature = "rp2350b")]
pub const ADC_BASE_PIN: u8 = 40;
pub const TEMPERATURE_CHANNEL: u8 = NUM_CHANNELS as u8;
pub const ADC_VREF: f32 = 3.3;
pub const ADC_MAX: u16 = 4095;
pub const ADC_DREQ: u8 = 48;
pub struct AdcInput<const CH: u8> {
_marker: core::marker::PhantomData<*const ()>,
}
impl<const CH: u8> AdcInput<CH> {
#[inline]
pub const fn channel(&self) -> u8 {
CH
}
#[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()
}
}
pub trait AdcPin: Sized {
type Input;
fn into_adc_input(self) -> Self::Input;
}
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> {
self.set_function(Function::Null);
self.set_pull(Pull::None);
self.init_pad();
AdcInput {
_marker: core::marker::PhantomData,
}
}
}
};
}
#[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);
}
#[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);
}
pub struct Adc {
adc: ADC,
}
impl Adc {
pub fn init(adc: ADC) -> Self {
<rp2350_pac::ADC as Resettable>::init();
adc.cs.set_en();
crate::spin::spin_wait("ADC ready", || adc.cs.ready());
Self { adc }
}
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self {
adc: unsafe { ADC::steal() },
})
}
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::adc::RegisterBlock {
&self.adc
}
pub fn read<const CH: u8>(&mut self, _input: &AdcInput<CH>) -> u16 {
self.read_channel(CH)
}
#[inline]
fn read_channel(&mut self, channel: u8) -> u16 {
self.adc.cs.write_ainsel(channel as u32);
self.adc.cs.trigger_start_once();
crate::spin::spin_wait("ADC conversion ready", || self.adc.cs.ready());
self.adc.result.result() as u16
}
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
}
#[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();
}
#[inline]
pub fn read_result(&self) -> u16 {
self.adc.result.result() as u16
}
#[inline]
pub fn is_ready(&self) -> bool {
self.adc.cs.ready()
}
#[inline]
pub fn has_error(&self) -> bool {
self.adc.cs.err()
}
#[inline]
pub fn has_sticky_error(&self) -> bool {
self.adc.cs.err_sticky()
}
#[inline]
pub fn clear_sticky_error(&mut self) {
self.adc.cs.set_err_sticky();
}
#[inline]
pub fn selected_channel(&self) -> u8 {
self.adc.cs.ainsel() as u8
}
#[inline]
pub fn enable_temp_sensor(&mut self) {
self.adc.cs.set_ts_en();
}
#[inline]
pub fn disable_temp_sensor(&mut self) {
self.adc.cs.clr_ts_en();
}
#[inline]
pub fn is_temp_sensor_enabled(&self) -> bool {
self.adc.cs.ts_en()
}
#[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);
}
#[inline]
pub fn disable_round_robin(&mut self) {
self.adc.cs.write_rrobin(0);
}
#[inline]
pub fn round_robin_mask(&self) -> u16 {
self.adc.cs.rrobin() as u16
}
#[inline]
pub fn start_free_running(&mut self) {
self.adc.cs.set_start_many();
}
#[inline]
pub fn stop_free_running(&mut self) {
self.adc.cs.clr_start_many();
}
#[inline]
pub fn is_free_running(&self) -> bool {
self.adc.cs.start_many()
}
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);
}
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);
}
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);
}
#[inline]
pub fn fifo_enable(&mut self) {
self.adc.fcs.set_en();
}
#[inline]
pub fn fifo_disable(&mut self) {
self.adc.fcs.clr_en();
}
#[inline]
pub fn fifo_is_empty(&self) -> bool {
self.adc.fcs.empty()
}
#[inline]
pub fn fifo_is_full(&self) -> bool {
self.adc.fcs.full()
}
#[inline]
pub fn fifo_level(&self) -> u8 {
self.adc.fcs.level() as u8
}
#[inline]
pub fn fifo_has_overflowed(&self) -> bool {
self.adc.fcs.over()
}
#[inline]
pub fn fifo_clear_overflow(&mut self) {
self.adc.fcs.set_over();
}
#[inline]
pub fn fifo_has_underflowed(&self) -> bool {
self.adc.fcs.under()
}
#[inline]
pub fn fifo_clear_underflow(&mut self) {
self.adc.fcs.set_under();
}
pub fn fifo_pop(&mut self) -> Option<u16> {
if self.fifo_is_empty() {
None
} else {
Some(self.adc.fifo.val() as u16)
}
}
pub fn fifo_pop_blocking(&mut self) -> u16 {
while self.fifo_is_empty() {
core::hint::spin_loop();
}
self.adc.fifo.val() as u16
}
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))
}
}
pub fn fifo_drain(&mut self) {
while !self.fifo_is_empty() {
let _ = self.adc.fifo.val();
}
}
#[inline]
pub fn fifo_address(&self) -> *const u32 {
&self.adc.fifo as *const _ as *const u32
}
#[inline]
pub fn fifo_dma_address(&self) -> u32 {
&self.adc.fifo as *const _ as u32
}
pub const DREQ: crate::dma::TreqSel = crate::dma::TreqSel::Adc;
#[inline]
pub const fn dreq() -> u8 {
ADC_DREQ
}
#[inline]
pub fn fifo_irq_raw(&self) -> bool {
self.adc.intr.fifo()
}
#[inline]
pub fn enable_fifo_irq(&mut self) {
self.adc.inte.set_fifo();
}
#[inline]
pub fn disable_fifo_irq(&mut self) {
self.adc.inte.clr_fifo();
}
#[inline]
pub fn fifo_irq_status(&self) -> bool {
self.adc.ints.fifo()
}
#[inline]
pub fn force_fifo_irq(&mut self) {
self.adc.intf.set_fifo();
}
#[inline]
pub fn clear_forced_fifo_irq(&mut self) {
self.adc.intf.clr_fifo();
}
#[inline]
pub fn to_voltage(raw: u16) -> f32 {
(raw as f32) * ADC_VREF / (ADC_MAX as f32 + 1.0)
}
#[inline]
pub fn to_millivolts(raw: u16) -> u32 {
((raw as u32) * 3300) / 4096
}
#[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
}
#[inline]
pub fn release(self) -> ADC {
self.adc
}
}
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()
}
}