Skip to content

Commit 970b066

Browse files
committed
WIP: Fix ADC support for STM32L47x/L48x
1 parent 2f64d51 commit 970b066

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/adc.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use crate::{
1313
rcc::{AHB2, CCIPR},
1414
};
1515

16+
// TODO: Only on STM32L47x/L48x.
17+
use crate::gpio::AnalogPin;
18+
1619
#[cfg(any(feature = "stm32l4x1", feature = "stm32l4x2", feature = "stm32l4x3",))]
1720
use pac::ADC as ADC1;
1821

@@ -101,7 +104,8 @@ impl ADC {
101104
}
102105
}
103106

104-
impl<C> OneShot<ADC, u16, C> for ADC
107+
// TODO: AnalogPin only on STM32L47x/L48x.
108+
impl<C: AnalogPin> OneShot<ADC, u16, C> for ADC
105109
where
106110
C: Channel,
107111
{
@@ -126,6 +130,10 @@ where
126130
// Configure channel
127131
channel.set_sample_time(&self.inner, self.sample_time);
128132

133+
// TODO: Only on STM32L47x/L48x.
134+
// Connect the pin to the ADC
135+
channel.connect_adc();
136+
129137
// Select channel
130138
self.inner.sqr1.write(|w| {
131139
// This is sound, as all `Channel` implementations set valid values.
@@ -157,6 +165,10 @@ where
157165
// Read ADC value
158166
let val = self.inner.dr.read().bits() as u16;
159167

168+
// TODO: Only on STM32L47x/L48x.
169+
// Disconnect the pin from the ADC
170+
channel.disconnect_adc();
171+
160172
// Disable ADC
161173
self.inner.cr.modify(|_, w| w.addis().set_bit());
162174

src/gpio.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ pub trait ExtiPin {
132132
fn check_interrupt(&mut self) -> bool;
133133
}
134134

135+
// TODO: Only on STM32L47x/L48x.
136+
/// Analog Pin
137+
pub trait AnalogPin {
138+
fn connect_adc(&mut self);
139+
fn disconnect_adc(&mut self);
140+
}
141+
135142
macro_rules! doc_comment {
136143
($x:expr, $($tt:tt)*) => {
137144
#[doc = $x]
@@ -188,6 +195,9 @@ macro_rules! gpio {
188195
PullDown, PullUp, PushPull, State, Speed,
189196
};
190197

198+
// TODO: Only on STM32L47x/L48x.
199+
use super::AnalogPin;
200+
191201
/// GPIO parts
192202
pub struct Parts {
193203
/// Opaque AFRH register
@@ -732,6 +742,17 @@ macro_rules! gpio {
732742
}
733743
}
734744

745+
// TODO: Only on STM32L47x/L48x.
746+
impl AnalogPin for $PXi<Analog> {
747+
fn connect_adc(&mut self) {
748+
unsafe { (*$GPIOX::ptr()).ascr.modify(|r, w| w.bits(r.bits() | (1 << $i))); }
749+
}
750+
751+
fn disconnect_adc(&mut self) {
752+
unsafe { (*$GPIOX::ptr()).ascr.modify(|r, w| w.bits(r.bits() & !(1 << $i))); }
753+
}
754+
}
755+
735756
impl<MODE> $PXi<MODE> {
736757
impl_into_af! {
737758
$PXi $AFR $i,

0 commit comments

Comments
 (0)