2023-09-18 23:31:20 +02:00
|
|
|
use core::future::poll_fn;
|
|
|
|
use core::marker::PhantomData;
|
|
|
|
use core::task::Poll;
|
|
|
|
|
2023-09-05 23:46:57 +02:00
|
|
|
use embassy_hal_internal::into_ref;
|
|
|
|
use embedded_hal_02::blocking::delay::DelayUs;
|
|
|
|
|
|
|
|
use crate::adc::{Adc, AdcPin, Instance, SampleTime};
|
2023-09-18 23:31:20 +02:00
|
|
|
use crate::interrupt::typelevel::Interrupt;
|
2023-09-05 23:46:57 +02:00
|
|
|
use crate::time::Hertz;
|
2023-09-18 23:31:20 +02:00
|
|
|
use crate::{interrupt, Peripheral};
|
2023-09-05 23:46:57 +02:00
|
|
|
|
|
|
|
pub const VDDA_CALIB_MV: u32 = 3300;
|
|
|
|
pub const ADC_MAX: u32 = (1 << 12) - 1;
|
2023-09-10 05:01:51 +02:00
|
|
|
pub const VREF_INT: u32 = 1230;
|
2023-09-05 23:46:57 +02:00
|
|
|
|
2023-09-18 23:31:20 +02:00
|
|
|
/// Interrupt handler.
|
|
|
|
pub struct InterruptHandler<T: Instance> {
|
|
|
|
_phantom: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandler<T> {
|
|
|
|
unsafe fn on_interrupt() {
|
|
|
|
if T::regs().isr().read().eoc() {
|
|
|
|
T::regs().ier().modify(|w| w.set_eocie(false));
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
T::state().waker.wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 23:46:57 +02:00
|
|
|
pub struct Vref;
|
|
|
|
impl<T: Instance> AdcPin<T> for Vref {}
|
|
|
|
impl<T: Instance> super::sealed::AdcPin<T> for Vref {
|
|
|
|
fn channel(&self) -> u8 {
|
|
|
|
18
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:31:20 +02:00
|
|
|
impl Vref {
|
|
|
|
/// The value that vref would be if vdda was at 3300mv
|
|
|
|
pub fn value(&self) -> u16 {
|
|
|
|
crate::pac::VREFINTCAL.data().read().value()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 23:46:57 +02:00
|
|
|
pub struct Temperature;
|
|
|
|
impl<T: Instance> AdcPin<T> for Temperature {}
|
|
|
|
impl<T: Instance> super::sealed::AdcPin<T> for Temperature {
|
|
|
|
fn channel(&self) -> u8 {
|
|
|
|
16
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> Adc<'d, T> {
|
2023-09-18 23:31:20 +02:00
|
|
|
pub fn new(
|
|
|
|
adc: impl Peripheral<P = T> + 'd,
|
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
|
|
|
delay: &mut impl DelayUs<u32>,
|
|
|
|
) -> Self {
|
2023-09-05 23:46:57 +02:00
|
|
|
use crate::pac::adc::vals;
|
|
|
|
|
|
|
|
into_ref!(adc);
|
|
|
|
|
2023-10-11 18:06:43 +02:00
|
|
|
T::reset_and_enable();
|
2023-09-05 23:46:57 +02:00
|
|
|
|
|
|
|
// Enable the adc regulator
|
|
|
|
T::regs().cr().modify(|w| w.set_advregen(vals::Advregen::INTERMEDIATE));
|
|
|
|
T::regs().cr().modify(|w| w.set_advregen(vals::Advregen::ENABLED));
|
|
|
|
|
|
|
|
// Wait for the regulator to stabilize
|
|
|
|
delay.delay_us(10);
|
|
|
|
|
|
|
|
assert!(!T::regs().cr().read().aden());
|
|
|
|
|
|
|
|
// Begin calibration
|
|
|
|
T::regs().cr().modify(|w| w.set_adcaldif(false));
|
|
|
|
T::regs().cr().modify(|w| w.set_adcal(true));
|
|
|
|
|
|
|
|
while T::regs().cr().read().adcal() {}
|
|
|
|
|
2023-09-12 00:27:47 +02:00
|
|
|
// Wait more than 4 clock cycles after adcal is cleared (RM0364 p. 223)
|
2023-09-16 00:36:21 +02:00
|
|
|
delay.delay_us(1 + (6 * 1_000_000 / Self::freq().0));
|
2023-09-12 00:27:47 +02:00
|
|
|
|
2023-09-05 23:46:57 +02:00
|
|
|
// Enable the adc
|
|
|
|
T::regs().cr().modify(|w| w.set_aden(true));
|
|
|
|
|
|
|
|
// Wait until the adc is ready
|
|
|
|
while !T::regs().isr().read().adrdy() {}
|
|
|
|
|
2023-09-18 23:31:20 +02:00
|
|
|
T::Interrupt::unpend();
|
|
|
|
unsafe {
|
|
|
|
T::Interrupt::enable();
|
|
|
|
}
|
|
|
|
|
2023-09-05 23:46:57 +02:00
|
|
|
Self {
|
|
|
|
adc,
|
|
|
|
sample_time: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn freq() -> Hertz {
|
|
|
|
<T as crate::adc::sealed::Instance>::frequency()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sample_time_for_us(&self, us: u32) -> SampleTime {
|
|
|
|
match us * Self::freq().0 / 1_000_000 {
|
|
|
|
0..=1 => SampleTime::Cycles1_5,
|
|
|
|
2..=4 => SampleTime::Cycles4_5,
|
|
|
|
5..=7 => SampleTime::Cycles7_5,
|
|
|
|
8..=19 => SampleTime::Cycles19_5,
|
|
|
|
20..=61 => SampleTime::Cycles61_5,
|
|
|
|
62..=181 => SampleTime::Cycles181_5,
|
|
|
|
_ => SampleTime::Cycles601_5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enable_vref(&self, _delay: &mut impl DelayUs<u32>) -> Vref {
|
|
|
|
T::common_regs().ccr().modify(|w| w.set_vrefen(true));
|
|
|
|
|
|
|
|
Vref {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enable_temperature(&self) -> Temperature {
|
|
|
|
T::common_regs().ccr().modify(|w| w.set_tsen(true));
|
|
|
|
|
|
|
|
Temperature {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_sample_time(&mut self, sample_time: SampleTime) {
|
|
|
|
self.sample_time = sample_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform a single conversion.
|
2023-09-18 23:31:20 +02:00
|
|
|
async fn convert(&mut self) -> u16 {
|
2023-09-05 23:46:57 +02:00
|
|
|
T::regs().isr().write(|_| {});
|
2023-09-18 23:31:20 +02:00
|
|
|
T::regs().ier().modify(|w| w.set_eocie(true));
|
2023-09-05 23:46:57 +02:00
|
|
|
T::regs().cr().modify(|w| w.set_adstart(true));
|
|
|
|
|
2023-09-18 23:31:20 +02:00
|
|
|
poll_fn(|cx| {
|
|
|
|
T::state().waker.register(cx.waker());
|
|
|
|
|
|
|
|
if T::regs().isr().read().eoc() {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
2023-09-05 23:46:57 +02:00
|
|
|
T::regs().isr().write(|_| {});
|
|
|
|
|
2023-09-10 05:01:51 +02:00
|
|
|
T::regs().dr().read().rdata()
|
2023-09-05 23:46:57 +02:00
|
|
|
}
|
|
|
|
|
2023-09-18 23:31:20 +02:00
|
|
|
pub async fn read(&mut self, pin: &mut impl AdcPin<T>) -> u16 {
|
2023-09-05 23:46:57 +02:00
|
|
|
Self::set_channel_sample_time(pin.channel(), self.sample_time);
|
|
|
|
|
|
|
|
// Configure the channel to sample
|
2023-09-10 05:01:51 +02:00
|
|
|
T::regs().sqr1().write(|w| w.set_sq(0, pin.channel()));
|
2023-09-18 23:31:20 +02:00
|
|
|
self.convert().await
|
2023-09-05 23:46:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_channel_sample_time(ch: u8, sample_time: SampleTime) {
|
|
|
|
let sample_time = sample_time.into();
|
|
|
|
if ch <= 9 {
|
2023-09-18 23:31:20 +02:00
|
|
|
T::regs().smpr1().modify(|reg| reg.set_smp(ch as _, sample_time));
|
2023-09-05 23:46:57 +02:00
|
|
|
} else {
|
2023-09-18 23:31:20 +02:00
|
|
|
T::regs().smpr2().modify(|reg| reg.set_smp((ch - 10) as _, sample_time));
|
2023-09-05 23:46:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 03:58:46 +02:00
|
|
|
|
|
|
|
impl<'d, T: Instance> Drop for Adc<'d, T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
use crate::pac::adc::vals;
|
|
|
|
|
|
|
|
T::regs().cr().modify(|w| w.set_adstp(true));
|
|
|
|
|
|
|
|
while T::regs().cr().read().adstp() {}
|
|
|
|
|
|
|
|
T::regs().cr().modify(|w| w.set_addis(true));
|
|
|
|
|
|
|
|
while T::regs().cr().read().aden() {}
|
|
|
|
|
|
|
|
// Disable the adc regulator
|
|
|
|
T::regs().cr().modify(|w| w.set_advregen(vals::Advregen::INTERMEDIATE));
|
|
|
|
T::regs().cr().modify(|w| w.set_advregen(vals::Advregen::DISABLED));
|
|
|
|
|
|
|
|
T::disable();
|
|
|
|
}
|
|
|
|
}
|