2023-07-28 13:23:22 +02:00
|
|
|
use embassy_hal_internal::into_ref;
|
2022-01-26 22:39:06 +01:00
|
|
|
use embedded_hal_02::blocking::delay::DelayUs;
|
2021-06-10 21:33:43 +02:00
|
|
|
|
2022-10-27 00:51:12 +02:00
|
|
|
use crate::adc::{Adc, AdcPin, Instance, Resolution, SampleTime};
|
2022-07-23 14:00:19 +02:00
|
|
|
use crate::Peripheral;
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2022-07-27 00:17:26 +02:00
|
|
|
/// Default VREF voltage used for sample conversion to millivolts.
|
|
|
|
pub const VREF_DEFAULT_MV: u32 = 3300;
|
|
|
|
/// VREF voltage used for factory calibration of VREFINTCAL register.
|
|
|
|
pub const VREF_CALIB_MV: u32 = 3000;
|
2021-06-10 21:33:43 +02:00
|
|
|
|
2022-07-27 00:17:26 +02:00
|
|
|
pub struct VrefInt;
|
|
|
|
impl<T: Instance> AdcPin<T> for VrefInt {}
|
|
|
|
impl<T: Instance> super::sealed::AdcPin<T> for VrefInt {
|
2021-06-10 21:33:43 +02:00
|
|
|
fn channel(&self) -> u8 {
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(not(adc_g0))]
|
2021-08-30 21:34:37 +02:00
|
|
|
let val = 0;
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(adc_g0)]
|
2021-08-30 21:34:37 +02:00
|
|
|
let val = 13;
|
|
|
|
val
|
2021-06-10 21:33:43 +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 {
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(not(adc_g0))]
|
2021-08-30 21:34:37 +02:00
|
|
|
let val = 17;
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(adc_g0)]
|
2021-08-30 21:34:37 +02:00
|
|
|
let val = 12;
|
|
|
|
val
|
2021-06-10 21:33:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Vbat;
|
|
|
|
impl<T: Instance> AdcPin<T> for Vbat {}
|
|
|
|
impl<T: Instance> super::sealed::AdcPin<T> for Vbat {
|
|
|
|
fn channel(&self) -> u8 {
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(not(adc_g0))]
|
2021-08-30 21:34:37 +02:00
|
|
|
let val = 18;
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(adc_g0)]
|
2021-08-30 21:34:37 +02:00
|
|
|
let val = 14;
|
|
|
|
val
|
2021-06-10 21:33:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> Adc<'d, T> {
|
2022-10-27 01:36:04 +02:00
|
|
|
pub fn new(adc: impl Peripheral<P = T> + 'd, delay: &mut impl DelayUs<u32>) -> Self {
|
|
|
|
into_ref!(adc);
|
2023-10-11 21:38:41 +02:00
|
|
|
T::enable_and_reset();
|
2023-06-19 03:07:26 +02:00
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
#[cfg(not(adc_g0))]
|
|
|
|
reg.set_deeppwd(false);
|
|
|
|
reg.set_advregen(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
#[cfg(adc_g0)]
|
|
|
|
T::regs().cfgr1().modify(|reg| {
|
|
|
|
reg.set_chselrmod(false);
|
|
|
|
});
|
2021-06-10 21:33:43 +02:00
|
|
|
|
|
|
|
delay.delay_us(20);
|
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_adcal(true);
|
|
|
|
});
|
2021-08-30 21:34:37 +02:00
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
while T::regs().cr().read().adcal() {
|
|
|
|
// spin
|
2021-06-10 21:33:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
delay.delay_us(1);
|
|
|
|
|
2021-07-05 03:09:42 +02:00
|
|
|
Self {
|
2022-10-27 01:36:04 +02:00
|
|
|
adc,
|
2021-07-05 03:09:42 +02:00
|
|
|
sample_time: Default::default(),
|
|
|
|
}
|
2021-06-10 21:33:43 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 00:17:26 +02:00
|
|
|
pub fn enable_vrefint(&self, delay: &mut impl DelayUs<u32>) -> VrefInt {
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(not(adc_g0))]
|
2023-06-19 03:07:26 +02:00
|
|
|
T::common_regs().ccr().modify(|reg| {
|
|
|
|
reg.set_vrefen(true);
|
|
|
|
});
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(adc_g0)]
|
|
|
|
T::regs().ccr().modify(|reg| {
|
|
|
|
reg.set_vrefen(true);
|
|
|
|
});
|
2021-06-10 21:33:43 +02:00
|
|
|
|
|
|
|
// "Table 24. Embedded internal voltage reference" states that it takes a maximum of 12 us
|
|
|
|
// to stabilize the internal voltage reference, we wait a little more.
|
|
|
|
// TODO: delay 15us
|
|
|
|
//cortex_m::asm::delay(20_000_000);
|
|
|
|
delay.delay_us(15);
|
|
|
|
|
2022-07-27 00:17:26 +02:00
|
|
|
VrefInt {}
|
2021-06-10 21:33:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enable_temperature(&self) -> Temperature {
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(not(adc_g0))]
|
2023-06-19 03:07:26 +02:00
|
|
|
T::common_regs().ccr().modify(|reg| {
|
|
|
|
reg.set_ch17sel(true);
|
|
|
|
});
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(adc_g0)]
|
|
|
|
T::regs().ccr().modify(|reg| {
|
|
|
|
reg.set_tsen(true);
|
|
|
|
});
|
2021-06-10 21:33:43 +02:00
|
|
|
|
|
|
|
Temperature {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enable_vbat(&self) -> Vbat {
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(not(adc_g0))]
|
2023-06-19 03:07:26 +02:00
|
|
|
T::common_regs().ccr().modify(|reg| {
|
|
|
|
reg.set_ch18sel(true);
|
|
|
|
});
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(adc_g0)]
|
|
|
|
T::regs().ccr().modify(|reg| {
|
|
|
|
reg.set_vbaten(true);
|
|
|
|
});
|
2021-06-10 21:33:43 +02:00
|
|
|
|
|
|
|
Vbat {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_sample_time(&mut self, sample_time: SampleTime) {
|
|
|
|
self.sample_time = sample_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_resolution(&mut self, resolution: Resolution) {
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(not(adc_g0))]
|
2023-06-19 03:07:26 +02:00
|
|
|
T::regs().cfgr().modify(|reg| reg.set_res(resolution.into()));
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(adc_g0)]
|
2023-06-19 03:07:26 +02:00
|
|
|
T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into()));
|
2021-06-10 21:33:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
/// Convert a raw sample from the `Temperature` to deg C
|
|
|
|
pub fn to_degrees_centigrade(sample: u16) -> f32 {
|
|
|
|
(130.0 - 30.0) / (VtempCal130::get().read() as f32 - VtempCal30::get().read() as f32)
|
|
|
|
* (sample as f32 - VtempCal30::get().read() as f32)
|
|
|
|
+ 30.0
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-08-30 21:33:42 +02:00
|
|
|
/// Perform a single conversion.
|
|
|
|
fn convert(&mut self) -> u16 {
|
2023-06-19 03:07:26 +02:00
|
|
|
T::regs().isr().modify(|reg| {
|
|
|
|
reg.set_eos(true);
|
|
|
|
reg.set_eoc(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Start conversion
|
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_adstart(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
while !T::regs().isr().read().eos() {
|
|
|
|
// spin
|
2021-08-30 21:33:42 +02:00
|
|
|
}
|
2023-06-19 03:07:26 +02:00
|
|
|
|
|
|
|
T::regs().dr().read().0 as u16
|
2021-08-30 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
2021-06-10 21:33:43 +02:00
|
|
|
pub fn read(&mut self, pin: &mut impl AdcPin<T>) -> u16 {
|
2023-06-19 03:07:26 +02:00
|
|
|
// Make sure bits are off
|
|
|
|
while T::regs().cr().read().addis() {
|
|
|
|
// spin
|
2021-06-10 21:33:43 +02:00
|
|
|
}
|
2023-06-19 03:07:26 +02:00
|
|
|
|
|
|
|
// Enable ADC
|
|
|
|
T::regs().isr().modify(|reg| {
|
|
|
|
reg.set_adrdy(true);
|
|
|
|
});
|
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_aden(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
while !T::regs().isr().read().adrdy() {
|
|
|
|
// spin
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure channel
|
|
|
|
Self::set_channel_sample_time(pin.channel(), self.sample_time);
|
|
|
|
|
|
|
|
// Select channel
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(not(adc_g0))]
|
2023-06-19 03:07:26 +02:00
|
|
|
T::regs().sqr1().write(|reg| reg.set_sq(0, pin.channel()));
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(adc_g0)]
|
2023-06-19 03:07:26 +02:00
|
|
|
T::regs().chselr().write(|reg| reg.set_chsel(1 << pin.channel()));
|
|
|
|
|
|
|
|
// Some models are affected by an erratum:
|
|
|
|
// If we perform conversions slower than 1 kHz, the first read ADC value can be
|
|
|
|
// corrupted, so we discard it and measure again.
|
|
|
|
//
|
|
|
|
// STM32L471xx: Section 2.7.3
|
|
|
|
// STM32G4: Section 2.7.3
|
|
|
|
#[cfg(any(rcc_l4, rcc_g4))]
|
|
|
|
let _ = self.convert();
|
|
|
|
|
|
|
|
let val = self.convert();
|
|
|
|
|
|
|
|
T::regs().cr().modify(|reg| reg.set_addis(true));
|
|
|
|
|
|
|
|
val
|
2021-06-10 21:33:43 +02:00
|
|
|
}
|
|
|
|
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(adc_g0)]
|
2023-06-19 03:07:26 +02:00
|
|
|
fn set_channel_sample_time(_ch: u8, sample_time: SampleTime) {
|
2022-10-26 09:28:39 +02:00
|
|
|
T::regs().smpr().modify(|reg| reg.set_smp1(sample_time.into()));
|
2021-08-30 21:34:37 +02:00
|
|
|
}
|
|
|
|
|
2023-09-05 12:10:31 +02:00
|
|
|
#[cfg(not(adc_g0))]
|
2023-06-19 03:07:26 +02:00
|
|
|
fn set_channel_sample_time(ch: u8, sample_time: SampleTime) {
|
2022-10-26 09:28:39 +02:00
|
|
|
let sample_time = sample_time.into();
|
2023-06-29 01:51:19 +02:00
|
|
|
T::regs()
|
|
|
|
.smpr(ch as usize / 10)
|
|
|
|
.modify(|reg| reg.set_smp(ch as usize % 10, sample_time));
|
2021-06-10 21:33:43 +02:00
|
|
|
}
|
|
|
|
}
|