Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix ADC support for STM32L47x/L48x #191

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ use crate::{
use pac::{ADC1, ADC_COMMON};
use stable_deref_trait::StableDeref;

#[cfg(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
))]
use crate::gpio::AnalogPin;

/// Vref internal signal, used for calibration
pub struct Vref;

Expand All @@ -31,6 +39,42 @@ pub struct Vbat;
/// Core temperature internal signal
pub struct Temperature;

// TODO: Avoid this hack
#[cfg(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
))]
impl AnalogPin for Vref {
fn connect_adc(&mut self) {}
fn disconnect_adc(&mut self) {}
}

// TODO: Avoid this hack
#[cfg(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
))]
impl AnalogPin for Vbat {
fn connect_adc(&mut self) {}
fn disconnect_adc(&mut self) {}
}

// TODO: Avoid this hack
#[cfg(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
))]
impl AnalogPin for Temperature {
fn connect_adc(&mut self) {}
fn disconnect_adc(&mut self) {}
}

/// Analog to Digital converter interface
pub struct ADC {
pub(crate) adc: ADC1,
Expand Down Expand Up @@ -447,6 +491,15 @@ where
self.start_conversion();
while !self.has_completed_sequence() {}

#[cfg(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
))]
// Connect the pin to the ADC
channel.connect_adc();

// Read ADC value first time and discard it, as per errata sheet.
// The errata states that if we do conversions slower than 1 kHz, the
// first read ADC value can be corrupted, so we discard it and measure again.
Expand All @@ -458,6 +511,15 @@ where
// Read ADC value
let val = self.get_data();

#[cfg(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
))]
// Disconnect the pin from the ADC
channel.disconnect_adc();

// Disable ADC
self.disable();

Expand Down Expand Up @@ -638,11 +700,28 @@ impl Default for SampleTime {
}
}

#[cfg(not(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
)))]
/// Implemented for all types that represent ADC channels
pub trait Channel: EmbeddedHalChannel<ADC, ID = u8> {
fn set_sample_time(&mut self, adc: &ADC1, sample_time: SampleTime);
}

#[cfg(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
))]
/// Implemented for all types that represent ADC channels
pub trait Channel: EmbeddedHalChannel<ADC, ID = u8> + AnalogPin {
fn set_sample_time(&mut self, adc: &ADC1, sample_time: SampleTime);
}

macro_rules! adc_pins {
(
$(
Expand Down
38 changes: 38 additions & 0 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,44 @@ where
}
}

#[cfg(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
))]
/// Analog Pin
pub trait AnalogPin {
fn connect_adc(&mut self);
fn disconnect_adc(&mut self);
}

#[cfg(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
))]
impl<MODE, HL, const P: char, const N: u8> AnalogPin for Pin<MODE, HL, P, N> {
#[inline(always)]
fn connect_adc(&mut self) {
unsafe {
(*Gpio::<P>::ptr())
.ascr
.modify(|r, w| w.bits(r.bits() | (1 << N)));
}
}

#[inline(always)]
fn disconnect_adc(&mut self) {
unsafe {
(*Gpio::<P>::ptr())
.ascr
.modify(|r, w| w.bits(r.bits() & !(1 << N)));
}
}
}

/// Opaque MODER register
pub struct MODER<const P: char> {
_0: (),
Expand Down