summaryrefslogtreecommitdiff
path: root/embassy-stm32/src/adc/v2.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-stm32/src/adc/v2.rs')
-rw-r--r--embassy-stm32/src/adc/v2.rs139
1 files changed, 98 insertions, 41 deletions
diff --git a/embassy-stm32/src/adc/v2.rs b/embassy-stm32/src/adc/v2.rs
index 70e3b73b..4fe4ad1f 100644
--- a/embassy-stm32/src/adc/v2.rs
+++ b/embassy-stm32/src/adc/v2.rs
@@ -3,7 +3,9 @@ use core::marker::PhantomData;
use embassy_hal_common::into_ref;
use embedded_hal_02::blocking::delay::DelayUs;
+use super::InternalChannel;
use crate::adc::{AdcPin, Instance};
+use crate::peripherals::ADC1;
use crate::time::Hertz;
use crate::Peripheral;
@@ -12,6 +14,9 @@ pub const VREF_DEFAULT_MV: u32 = 3300;
/// VREF voltage used for factory calibration of VREFINTCAL register.
pub const VREF_CALIB_MV: u32 = 3300;
+/// ADC turn-on time
+pub const ADC_POWERUP_TIME_US: u32 = 3;
+
pub enum Resolution {
TwelveBit,
TenBit,
@@ -46,24 +51,53 @@ impl Resolution {
}
pub struct VrefInt;
-impl<T: Instance> AdcPin<T> for VrefInt {}
-impl<T: Instance> super::sealed::AdcPin<T> for VrefInt {
+impl InternalChannel<ADC1> for VrefInt {}
+impl super::sealed::InternalChannel<ADC1> for VrefInt {
fn channel(&self) -> u8 {
17
}
}
+impl VrefInt {
+ /// Time needed for internal voltage reference to stabilize
+ pub fn start_time_us() -> u32 {
+ 10
+ }
+}
+
pub struct Temperature;
-impl<T: Instance> AdcPin<T> for Temperature {}
-impl<T: Instance> super::sealed::AdcPin<T> for Temperature {
+impl InternalChannel<ADC1> for Temperature {}
+impl super::sealed::InternalChannel<ADC1> for Temperature {
fn channel(&self) -> u8 {
- 16
+ cfg_if::cfg_if! {
+ if #[cfg(any(stm32f40, stm32f41))] {
+ 16
+ } else {
+ 18
+ }
+ }
+ }
+}
+
+impl Temperature {
+ /// Converts temperature sensor reading in millivolts to degrees celcius
+ pub fn to_celcius(sample_mv: u16) -> f32 {
+ // From 6.3.22 Temperature sensor characteristics
+ const V25: i32 = 760; // mV
+ const AVG_SLOPE: f32 = 2.5; // mV/C
+
+ (sample_mv as i32 - V25) as f32 / AVG_SLOPE + 25.0
+ }
+
+ /// Time needed for temperature sensor readings to stabilize
+ pub fn start_time_us() -> u32 {
+ 10
}
}
pub struct Vbat;
-impl<T: Instance> AdcPin<T> for Vbat {}
-impl<T: Instance> super::sealed::AdcPin<T> for Vbat {
+impl InternalChannel<ADC1> for Vbat {}
+impl super::sealed::InternalChannel<ADC1> for Vbat {
fn channel(&self) -> u8 {
18
}
@@ -152,19 +186,16 @@ where
T::enable();
T::reset();
- let presc = unsafe { Prescaler::from_pclk2(T::frequency()) };
+ let presc = Prescaler::from_pclk2(T::frequency());
unsafe {
T::common_regs().ccr().modify(|w| w.set_adcpre(presc.adcpre()));
- }
- unsafe {
- // disable before config is set
T::regs().cr2().modify(|reg| {
- reg.set_adon(crate::pac::adc::vals::Adon::DISABLED);
+ reg.set_adon(crate::pac::adc::vals::Adon::ENABLED);
});
}
- delay.delay_us(20); // TODO?
+ delay.delay_us(ADC_POWERUP_TIME_US);
Self {
sample_time: Default::default(),
@@ -194,6 +225,45 @@ where
((u32::from(sample) * self.vref_mv) / self.resolution.to_max_count()) as u16
}
+ /// Enables internal voltage reference and returns [VrefInt], which can be used in
+ /// [Adc::read_internal()] to perform conversion.
+ pub fn enable_vrefint(&self) -> VrefInt {
+ unsafe {
+ T::common_regs().ccr().modify(|reg| {
+ reg.set_tsvrefe(crate::pac::adccommon::vals::Tsvrefe::ENABLED);
+ });
+ }
+
+ 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 {
+ unsafe {
+ T::common_regs().ccr().modify(|reg| {
+ reg.set_tsvrefe(crate::pac::adccommon::vals::Tsvrefe::ENABLED);
+ });
+ }
+
+ Temperature {}
+ }
+
+ /// Enables vbat input and returns [Vbat], which can be used in
+ /// [Adc::read_internal()] to perform conversion.
+ pub fn enable_vbat(&self) -> Vbat {
+ unsafe {
+ T::common_regs().ccr().modify(|reg| {
+ reg.set_vbate(crate::pac::adccommon::vals::Vbate::ENABLED);
+ });
+ }
+
+ Vbat {}
+ }
+
/// Perform a single conversion.
fn convert(&mut self) -> u16 {
unsafe {
@@ -224,42 +294,29 @@ where
P: crate::gpio::sealed::Pin,
{
unsafe {
- // dissable ADC
- T::regs().cr2().modify(|reg| {
- reg.set_swstart(false);
- });
- T::regs().cr2().modify(|reg| {
- reg.set_adon(crate::pac::adc::vals::Adon::DISABLED);
- });
-
pin.set_as_analog();
- // Configure ADC
- T::regs().cr1().modify(|reg| reg.set_res(self.resolution.res()));
+ self.read_channel(pin.channel())
+ }
+ }
- // Select channel
- T::regs().sqr3().write(|reg| reg.set_sq(0, pin.channel()));
+ pub fn read_internal(&mut self, channel: &mut impl InternalChannel<T>) -> u16 {
+ unsafe { self.read_channel(channel.channel()) }
+ }
- // Configure channel
- Self::set_channel_sample_time(pin.channel(), self.sample_time);
+ unsafe fn read_channel(&mut self, channel: u8) -> u16 {
+ // Configure ADC
+ T::regs().cr1().modify(|reg| reg.set_res(self.resolution.res()));
- // enable adc
- T::regs().cr2().modify(|reg| {
- reg.set_adon(crate::pac::adc::vals::Adon::ENABLED);
- });
+ // Select channel
+ T::regs().sqr3().write(|reg| reg.set_sq(0, channel));
- let val = self.convert();
+ // Configure channel
+ Self::set_channel_sample_time(channel, self.sample_time);
- // dissable ADC
- T::regs().cr2().modify(|reg| {
- reg.set_swstart(false);
- });
- T::regs().cr2().modify(|reg| {
- reg.set_adon(crate::pac::adc::vals::Adon::DISABLED);
- });
+ let val = self.convert();
- val
- }
+ val
}
unsafe fn set_channel_sample_time(ch: u8, sample_time: SampleTime) {