222 lines
6.0 KiB
Rust
Raw Normal View History

use embassy_hal_internal::into_ref;
use embedded_hal_02::blocking::delay::DelayUs;
2021-10-19 15:36:41 +02:00
2022-10-07 14:31:55 +03:00
use super::InternalChannel;
use crate::adc::{Adc, AdcPin, Instance, Resolution, SampleTime};
2022-10-07 14:31:55 +03:00
use crate::peripherals::ADC1;
2022-06-12 22:15:44 +02:00
use crate::time::Hertz;
use crate::Peripheral;
2022-06-12 22:15:44 +02:00
2022-07-27 01:17:26 +03: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 = 3300;
2022-10-07 14:31:55 +03:00
/// ADC turn-on time
pub const ADC_POWERUP_TIME_US: u32 = 3;
2022-07-27 01:17:26 +03:00
pub struct VrefInt;
2022-10-07 14:31:55 +03:00
impl InternalChannel<ADC1> for VrefInt {}
impl super::sealed::InternalChannel<ADC1> for VrefInt {
fn channel(&self) -> u8 {
2022-02-11 17:48:32 +01:00
17
}
}
2022-10-07 14:31:55 +03:00
impl VrefInt {
/// Time needed for internal voltage reference to stabilize
pub fn start_time_us() -> u32 {
10
}
}
pub struct Temperature;
2022-10-07 14:31:55 +03:00
impl InternalChannel<ADC1> for Temperature {}
impl super::sealed::InternalChannel<ADC1> for Temperature {
fn channel(&self) -> u8 {
2022-10-07 14:31:55 +03:00
cfg_if::cfg_if! {
if #[cfg(any(stm32f40, stm32f41))] {
16
} else {
18
}
}
}
}
impl Temperature {
/// Time needed for temperature sensor readings to stabilize
pub fn start_time_us() -> u32 {
10
}
}
pub struct Vbat;
2022-10-07 14:31:55 +03:00
impl InternalChannel<ADC1> for Vbat {}
impl super::sealed::InternalChannel<ADC1> for Vbat {
fn channel(&self) -> u8 {
2022-02-11 17:48:32 +01:00
18
}
}
enum Prescaler {
Div2,
Div4,
Div6,
Div8,
}
impl Prescaler {
fn from_pclk2(freq: Hertz) -> Self {
// Datasheet for both F4 and F7 specifies min frequency 0.6 MHz, typ freq. 30 MHz and max 36 MHz.
const MAX_FREQUENCY: Hertz = Hertz(36_000_000);
let raw_div = freq.0 / MAX_FREQUENCY.0;
match raw_div {
0..=1 => Self::Div2,
2..=3 => Self::Div4,
4..=5 => Self::Div6,
6..=7 => Self::Div8,
2022-06-12 22:15:44 +02:00
_ => panic!("Selected PCLK2 frequency is too high for ADC with largest possible prescaler."),
}
}
fn adcpre(&self) -> crate::pac::adccommon::vals::Adcpre {
match self {
Prescaler::Div2 => crate::pac::adccommon::vals::Adcpre::DIV2,
Prescaler::Div4 => crate::pac::adccommon::vals::Adcpre::DIV4,
Prescaler::Div6 => crate::pac::adccommon::vals::Adcpre::DIV6,
Prescaler::Div8 => crate::pac::adccommon::vals::Adcpre::DIV8,
}
}
}
impl<'d, T> Adc<'d, T>
where
T: Instance,
{
2022-10-26 18:36:04 -05:00
pub fn new(adc: impl Peripheral<P = T> + 'd, delay: &mut impl DelayUs<u32>) -> Self {
into_ref!(adc);
2022-10-07 13:29:56 +03:00
T::enable();
T::reset();
2022-10-07 14:31:55 +03:00
let presc = Prescaler::from_pclk2(T::frequency());
2023-06-19 03:07:26 +02:00
T::common_regs().ccr().modify(|w| w.set_adcpre(presc.adcpre()));
T::regs().cr2().modify(|reg| {
2023-09-11 17:12:54 -05:00
reg.set_adon(true);
2023-06-19 03:07:26 +02:00
});
2022-10-07 14:31:55 +03:00
delay.delay_us(ADC_POWERUP_TIME_US);
Self {
2022-10-26 18:36:04 -05:00
adc,
sample_time: Default::default(),
}
}
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 03:07:26 +02:00
T::regs().cr1().modify(|reg| reg.set_res(resolution.into()));
}
2022-10-07 14:31:55 +03:00
/// Enables internal voltage reference and returns [VrefInt], which can be used in
/// [Adc::read_internal()] to perform conversion.
pub fn enable_vrefint(&self) -> VrefInt {
2023-06-19 03:07:26 +02:00
T::common_regs().ccr().modify(|reg| {
2023-09-14 18:36:03 -05:00
reg.set_tsvrefe(true);
2023-06-19 03:07:26 +02:00
});
2022-10-07 14:31:55 +03:00
VrefInt {}
}
/// Enables internal temperature sensor and returns [Temperature], which can be used in
/// [Adc::read_internal()] to perform conversion.
///
/// On STM32F42 and STM32F43 this can not be used together with [Vbat]. If both are enabled,
/// temperature sensor will return vbat value.
pub fn enable_temperature(&self) -> Temperature {
2023-06-19 03:07:26 +02:00
T::common_regs().ccr().modify(|reg| {
2023-09-14 18:36:03 -05:00
reg.set_tsvrefe(true);
2023-06-19 03:07:26 +02:00
});
2022-10-07 14:31:55 +03:00
Temperature {}
}
/// Enables vbat input and returns [Vbat], which can be used in
/// [Adc::read_internal()] to perform conversion.
pub fn enable_vbat(&self) -> Vbat {
2023-06-19 03:07:26 +02:00
T::common_regs().ccr().modify(|reg| {
2023-09-14 18:36:03 -05:00
reg.set_vbate(true);
2023-06-19 03:07:26 +02:00
});
2022-10-07 14:31:55 +03:00
Vbat {}
}
/// Perform a single conversion.
fn convert(&mut self) -> u16 {
2023-06-19 03:07:26 +02:00
// clear end of conversion flag
T::regs().sr().modify(|reg| {
reg.set_eoc(crate::pac::adc::vals::Eoc::NOTCOMPLETE);
});
// Start conversion
T::regs().cr2().modify(|reg| {
reg.set_swstart(true);
});
while T::regs().sr().read().strt() == crate::pac::adc::vals::Strt::NOTSTARTED {
// spin //wait for actual start
}
2023-06-19 03:07:26 +02:00
while T::regs().sr().read().eoc() == crate::pac::adc::vals::Eoc::NOTCOMPLETE {
// spin //wait for finish
}
T::regs().dr().read().0 as u16
}
pub fn read<P>(&mut self, pin: &mut P) -> u16
where
P: AdcPin<T>,
P: crate::gpio::sealed::Pin,
{
2023-06-19 03:07:26 +02:00
pin.set_as_analog();
2023-06-19 03:07:26 +02:00
self.read_channel(pin.channel())
2022-10-07 14:31:55 +03:00
}
2022-10-07 14:31:55 +03:00
pub fn read_internal(&mut self, channel: &mut impl InternalChannel<T>) -> u16 {
2023-06-19 03:07:26 +02:00
self.read_channel(channel.channel())
2022-10-07 14:31:55 +03:00
}
2023-06-19 03:07:26 +02:00
fn read_channel(&mut self, channel: u8) -> u16 {
2022-10-07 14:31:55 +03:00
// Configure ADC
2022-10-07 14:31:55 +03:00
// Select channel
T::regs().sqr3().write(|reg| reg.set_sq(0, channel));
2022-10-07 14:31:55 +03:00
// Configure channel
Self::set_channel_sample_time(channel, self.sample_time);
2022-10-07 14:31:55 +03:00
let val = self.convert();
2022-10-07 14:31:55 +03:00
val
}
2023-06-19 03:07:26 +02:00
fn set_channel_sample_time(ch: u8, sample_time: SampleTime) {
let sample_time = sample_time.into();
if ch <= 9 {
T::regs().smpr2().modify(|reg| reg.set_smp(ch as _, sample_time));
} else {
T::regs().smpr1().modify(|reg| reg.set_smp((ch - 10) as _, sample_time));
}
}
}
2022-10-07 13:29:56 +03:00
impl<'d, T: Instance> Drop for Adc<'d, T> {
fn drop(&mut self) {
T::disable();
}
}