2023-07-28 11:23:22 +00:00
|
|
|
use embassy_hal_internal::into_ref;
|
2022-09-14 03:08:03 +00:00
|
|
|
use embedded_hal_02::blocking::delay::DelayUs;
|
|
|
|
|
2023-04-05 21:11:21 +00:00
|
|
|
use crate::adc::{Adc, AdcPin, Instance, InternalChannel, Resolution, SampleTime};
|
2023-04-05 21:52:32 +00:00
|
|
|
use crate::peripherals::ADC;
|
2023-04-05 21:38:06 +00:00
|
|
|
use crate::Peripheral;
|
2022-09-14 03:08:03 +00:00
|
|
|
|
|
|
|
pub const VDDA_CALIB_MV: u32 = 3300;
|
|
|
|
pub const VREF_INT: u32 = 1230;
|
|
|
|
|
|
|
|
pub struct Vbat;
|
2023-04-05 21:52:32 +00:00
|
|
|
impl InternalChannel<ADC> for Vbat {}
|
|
|
|
impl super::sealed::InternalChannel<ADC> for Vbat {
|
2022-09-14 03:08:03 +00:00
|
|
|
fn channel(&self) -> u8 {
|
|
|
|
18
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Vref;
|
2023-04-05 21:52:32 +00:00
|
|
|
impl InternalChannel<ADC> for Vref {}
|
|
|
|
impl super::sealed::InternalChannel<ADC> for Vref {
|
2022-09-14 03:08:03 +00:00
|
|
|
fn channel(&self) -> u8 {
|
|
|
|
17
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Temperature;
|
2023-04-05 21:52:32 +00:00
|
|
|
impl InternalChannel<ADC> for Temperature {}
|
|
|
|
impl super::sealed::InternalChannel<ADC> for Temperature {
|
2022-09-14 03:08:03 +00:00
|
|
|
fn channel(&self) -> u8 {
|
|
|
|
16
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> Adc<'d, T> {
|
2023-04-05 19:31:32 +00:00
|
|
|
pub fn new(adc: impl Peripheral<P = T> + 'd, delay: &mut impl DelayUs<u32>) -> Self {
|
|
|
|
into_ref!(adc);
|
2023-04-05 20:28:42 +00:00
|
|
|
T::enable();
|
|
|
|
T::reset();
|
2022-09-14 03:08:03 +00:00
|
|
|
|
|
|
|
// Delay 1μs when using HSI14 as the ADC clock.
|
|
|
|
//
|
|
|
|
// Table 57. ADC characteristics
|
|
|
|
// tstab = 14 * 1/fadc
|
|
|
|
delay.delay_us(1);
|
|
|
|
|
|
|
|
let s = Self {
|
2023-04-05 19:31:32 +00:00
|
|
|
adc,
|
2022-09-14 03:08:03 +00:00
|
|
|
sample_time: Default::default(),
|
|
|
|
};
|
|
|
|
s.calibrate();
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enable_vbat(&self, _delay: &mut impl DelayUs<u32>) -> Vbat {
|
|
|
|
// SMP must be ≥ 56 ADC clock cycles when using HSI14.
|
|
|
|
//
|
|
|
|
// 6.3.20 Vbat monitoring characteristics
|
|
|
|
// ts_vbat ≥ 4μs
|
2023-06-19 01:07:26 +00:00
|
|
|
T::regs().ccr().modify(|reg| reg.set_vbaten(true));
|
2022-09-14 03:08:03 +00:00
|
|
|
Vbat
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enable_vref(&self, delay: &mut impl DelayUs<u32>) -> Vref {
|
|
|
|
// Table 28. Embedded internal reference voltage
|
|
|
|
// tstart = 10μs
|
2023-06-19 01:07:26 +00:00
|
|
|
T::regs().ccr().modify(|reg| reg.set_vrefen(true));
|
2022-09-14 03:08:03 +00:00
|
|
|
delay.delay_us(10);
|
|
|
|
Vref
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enable_temperature(&self, delay: &mut impl DelayUs<u32>) -> Temperature {
|
|
|
|
// SMP must be ≥ 56 ADC clock cycles when using HSI14.
|
|
|
|
//
|
|
|
|
// 6.3.19 Temperature sensor characteristics
|
|
|
|
// tstart ≤ 10μs
|
|
|
|
// ts_temp ≥ 4μs
|
2023-06-19 01:07:26 +00:00
|
|
|
T::regs().ccr().modify(|reg| reg.set_tsen(true));
|
2022-09-14 03:08:03 +00:00
|
|
|
delay.delay_us(10);
|
|
|
|
Temperature
|
|
|
|
}
|
|
|
|
|
|
|
|
fn calibrate(&self) {
|
2023-06-19 01:07:26 +00:00
|
|
|
// A.7.1 ADC calibration code example
|
|
|
|
if T::regs().cr().read().aden() {
|
|
|
|
T::regs().cr().modify(|reg| reg.set_addis(true));
|
|
|
|
}
|
|
|
|
while T::regs().cr().read().aden() {
|
|
|
|
// spin
|
|
|
|
}
|
|
|
|
T::regs().cfgr1().modify(|reg| reg.set_dmaen(false));
|
|
|
|
T::regs().cr().modify(|reg| reg.set_adcal(true));
|
|
|
|
while T::regs().cr().read().adcal() {
|
|
|
|
// spin
|
2022-09-14 03:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_sample_time(&mut self, sample_time: SampleTime) {
|
|
|
|
self.sample_time = sample_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_resolution(&mut self, resolution: Resolution) {
|
2023-06-19 01:07:26 +00:00
|
|
|
T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into()));
|
2022-09-14 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 17:03:42 +00:00
|
|
|
pub fn read<P>(&mut self, pin: &mut P) -> u16
|
|
|
|
where
|
|
|
|
P: AdcPin<T> + crate::gpio::sealed::Pin,
|
|
|
|
{
|
2022-10-03 18:18:37 +00:00
|
|
|
let channel = pin.channel();
|
2023-06-19 01:07:26 +00:00
|
|
|
pin.set_as_analog();
|
|
|
|
self.read_channel(channel)
|
2022-10-03 18:18:37 +00:00
|
|
|
}
|
2022-09-14 03:08:03 +00:00
|
|
|
|
2022-10-03 18:18:37 +00:00
|
|
|
pub fn read_internal(&mut self, channel: &mut impl InternalChannel<T>) -> u16 {
|
|
|
|
let channel = channel.channel();
|
2023-06-19 01:07:26 +00:00
|
|
|
self.read_channel(channel)
|
2022-10-03 18:18:37 +00:00
|
|
|
}
|
2022-09-14 03:08:03 +00:00
|
|
|
|
2023-06-19 01:07:26 +00:00
|
|
|
fn read_channel(&mut self, channel: u8) -> u16 {
|
2022-10-03 18:18:37 +00:00
|
|
|
// A.7.2 ADC enable sequence code example
|
|
|
|
if T::regs().isr().read().adrdy() {
|
|
|
|
T::regs().isr().modify(|reg| reg.set_adrdy(true));
|
|
|
|
}
|
|
|
|
T::regs().cr().modify(|reg| reg.set_aden(true));
|
|
|
|
while !T::regs().isr().read().adrdy() {
|
|
|
|
// ES0233, 2.4.3 ADEN bit cannot be set immediately after the ADC calibration
|
|
|
|
// Workaround: When the ADC calibration is complete (ADCAL = 0), keep setting the
|
|
|
|
// ADEN bit until the ADRDY flag goes high.
|
|
|
|
T::regs().cr().modify(|reg| reg.set_aden(true));
|
|
|
|
}
|
2022-09-14 03:08:03 +00:00
|
|
|
|
2022-10-03 18:18:37 +00:00
|
|
|
T::regs().isr().modify(|reg| {
|
|
|
|
reg.set_eoc(true);
|
|
|
|
reg.set_eosmp(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
// A.7.5 Single conversion sequence code example - Software trigger
|
2023-04-05 19:31:32 +00:00
|
|
|
T::regs().chselr().write(|reg| reg.set_chselx(channel as usize, true));
|
|
|
|
T::regs().smpr().modify(|reg| reg.set_smp(self.sample_time.into()));
|
2022-10-03 18:18:37 +00:00
|
|
|
T::regs().cr().modify(|reg| reg.set_adstart(true));
|
|
|
|
while !T::regs().isr().read().eoc() {
|
|
|
|
// spin
|
|
|
|
}
|
|
|
|
let value = T::regs().dr().read().0 as u16;
|
|
|
|
|
|
|
|
// A.7.3 ADC disable code example
|
|
|
|
T::regs().cr().modify(|reg| reg.set_adstp(true));
|
|
|
|
while T::regs().cr().read().adstp() {
|
|
|
|
// spin
|
|
|
|
}
|
|
|
|
T::regs().cr().modify(|reg| reg.set_addis(true));
|
|
|
|
while T::regs().cr().read().aden() {
|
|
|
|
// spin
|
2022-09-14 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 18:18:37 +00:00
|
|
|
value
|
2022-09-14 03:08:03 +00:00
|
|
|
}
|
|
|
|
}
|