stm32: add some docs.
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
//! Analog to Digital (ADC) converter driver.
|
||||
#![macro_use]
|
||||
|
||||
#[cfg(not(adc_f3_v2))]
|
||||
@ -24,6 +25,7 @@ pub use sample_time::SampleTime;
|
||||
|
||||
use crate::peripherals;
|
||||
|
||||
/// Analog to Digital driver.
|
||||
pub struct Adc<'d, T: Instance> {
|
||||
#[allow(unused)]
|
||||
adc: crate::PeripheralRef<'d, T>,
|
||||
@ -75,12 +77,16 @@ pub(crate) mod sealed {
|
||||
}
|
||||
}
|
||||
|
||||
/// ADC instance.
|
||||
#[cfg(not(any(adc_f1, adc_v1, adc_v2, adc_v3, adc_v4, adc_f3, adc_f3_v1_1, adc_g0)))]
|
||||
pub trait Instance: sealed::Instance + crate::Peripheral<P = Self> {}
|
||||
/// ADC instance.
|
||||
#[cfg(any(adc_f1, adc_v1, adc_v2, adc_v3, adc_v4, adc_f3, adc_f3_v1_1, adc_g0))]
|
||||
pub trait Instance: sealed::Instance + crate::Peripheral<P = Self> + crate::rcc::RccPeripheral {}
|
||||
|
||||
/// ADC pin.
|
||||
pub trait AdcPin<T: Instance>: sealed::AdcPin<T> {}
|
||||
/// ADC internal channel.
|
||||
pub trait InternalChannel<T>: sealed::InternalChannel<T> {}
|
||||
|
||||
foreach_adc!(
|
||||
|
@ -1,3 +1,5 @@
|
||||
/// ADC resolution
|
||||
#[allow(missing_docs)]
|
||||
#[cfg(any(adc_v1, adc_v2, adc_v3, adc_g0, adc_f3, adc_f3_v1_1))]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
@ -8,6 +10,8 @@ pub enum Resolution {
|
||||
SixBit,
|
||||
}
|
||||
|
||||
/// ADC resolution
|
||||
#[allow(missing_docs)]
|
||||
#[cfg(adc_v4)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
@ -49,6 +53,9 @@ impl From<Resolution> for crate::pac::adc::vals::Res {
|
||||
}
|
||||
|
||||
impl Resolution {
|
||||
/// Get the maximum reading value for this resolution.
|
||||
///
|
||||
/// This is `2**n - 1`.
|
||||
pub fn to_max_count(&self) -> u32 {
|
||||
match self {
|
||||
#[cfg(adc_v4)]
|
||||
|
@ -32,6 +32,7 @@ const TEMP_CHANNEL: u8 = 18;
|
||||
const VBAT_CHANNEL: u8 = 17;
|
||||
|
||||
// NOTE: Vrefint/Temperature/Vbat are not available on all ADCs, this currently cannot be modeled with stm32-data, so these are available from the software on all ADCs
|
||||
/// Internal voltage reference channel.
|
||||
pub struct VrefInt;
|
||||
impl<T: Instance> InternalChannel<T> for VrefInt {}
|
||||
impl<T: Instance> super::sealed::InternalChannel<T> for VrefInt {
|
||||
@ -40,6 +41,7 @@ impl<T: Instance> super::sealed::InternalChannel<T> for VrefInt {
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal temperature channel.
|
||||
pub struct Temperature;
|
||||
impl<T: Instance> InternalChannel<T> for Temperature {}
|
||||
impl<T: Instance> super::sealed::InternalChannel<T> for Temperature {
|
||||
@ -48,6 +50,7 @@ impl<T: Instance> super::sealed::InternalChannel<T> for Temperature {
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal battery voltage channel.
|
||||
pub struct Vbat;
|
||||
impl<T: Instance> InternalChannel<T> for Vbat {}
|
||||
impl<T: Instance> super::sealed::InternalChannel<T> for Vbat {
|
||||
@ -125,6 +128,7 @@ impl Prescaler {
|
||||
}
|
||||
|
||||
impl<'d, T: Instance> Adc<'d, T> {
|
||||
/// Create a new ADC driver.
|
||||
pub fn new(adc: impl Peripheral<P = T> + 'd, delay: &mut impl DelayUs<u16>) -> Self {
|
||||
embassy_hal_internal::into_ref!(adc);
|
||||
T::enable_and_reset();
|
||||
@ -212,6 +216,7 @@ impl<'d, T: Instance> Adc<'d, T> {
|
||||
});
|
||||
}
|
||||
|
||||
/// Enable reading the voltage reference internal channel.
|
||||
pub fn enable_vrefint(&self) -> VrefInt {
|
||||
T::common_regs().ccr().modify(|reg| {
|
||||
reg.set_vrefen(true);
|
||||
@ -220,6 +225,7 @@ impl<'d, T: Instance> Adc<'d, T> {
|
||||
VrefInt {}
|
||||
}
|
||||
|
||||
/// Enable reading the temperature internal channel.
|
||||
pub fn enable_temperature(&self) -> Temperature {
|
||||
T::common_regs().ccr().modify(|reg| {
|
||||
reg.set_vsenseen(true);
|
||||
@ -228,6 +234,7 @@ impl<'d, T: Instance> Adc<'d, T> {
|
||||
Temperature {}
|
||||
}
|
||||
|
||||
/// Enable reading the vbat internal channel.
|
||||
pub fn enable_vbat(&self) -> Vbat {
|
||||
T::common_regs().ccr().modify(|reg| {
|
||||
reg.set_vbaten(true);
|
||||
@ -236,10 +243,12 @@ impl<'d, T: Instance> Adc<'d, T> {
|
||||
Vbat {}
|
||||
}
|
||||
|
||||
/// Set the ADC sample time.
|
||||
pub fn set_sample_time(&mut self, sample_time: SampleTime) {
|
||||
self.sample_time = sample_time;
|
||||
}
|
||||
|
||||
/// Set the ADC resolution.
|
||||
pub fn set_resolution(&mut self, resolution: Resolution) {
|
||||
T::regs().cfgr().modify(|reg| reg.set_res(resolution.into()));
|
||||
}
|
||||
@ -263,6 +272,7 @@ impl<'d, T: Instance> Adc<'d, T> {
|
||||
T::regs().dr().read().0 as u16
|
||||
}
|
||||
|
||||
/// Read an ADC pin.
|
||||
pub fn read<P>(&mut self, pin: &mut P) -> u16
|
||||
where
|
||||
P: AdcPin<T>,
|
||||
@ -273,6 +283,7 @@ impl<'d, T: Instance> Adc<'d, T> {
|
||||
self.read_channel(pin.channel())
|
||||
}
|
||||
|
||||
/// Read an ADC internal channel.
|
||||
pub fn read_internal(&mut self, channel: &mut impl InternalChannel<T>) -> u16 {
|
||||
self.read_channel(channel.channel())
|
||||
}
|
||||
|
Reference in New Issue
Block a user