Skip to content

Commit 3d22d9d

Browse files
committed
H503
1 parent 6a45311 commit 3d22d9d

File tree

2 files changed

+102
-26
lines changed

2 files changed

+102
-26
lines changed

src/adc/h5.rs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use super::{Adc, Disabled, Temperature, Vbat, Vddcore, Vrefint};
22
use crate::gpio::{self, Analog};
3+
4+
#[cfg(feature = "rm0492")]
5+
use crate::stm32::{ADC1, ADC1 as ADCC};
6+
7+
#[cfg(feature = "rm0481")]
38
use crate::stm32::{ADC1, ADC2, ADCC};
49

510
macro_rules! adc_pins {
@@ -47,36 +52,78 @@ macro_rules! adc_internal {
4752
};
4853
}
4954

55+
#[cfg(feature = "rm0492")]
5056
impl Vddcore {
57+
pub fn enable(_adc: &Adc<ADC1, Disabled>) {
58+
let adc = unsafe { ADC1::steal() };
59+
60+
adc.or().modify(|_, w| w.op1().set_bit());
61+
}
62+
63+
pub fn disable(_adc: &Adc<ADC1, Disabled>) {
64+
let adc = unsafe { ADC1::steal() };
65+
66+
adc.or().modify(|_, w| w.op1().clear_bit());
67+
}
68+
}
69+
#[cfg(feature = "rm0492")]
70+
adc_pins!(ADC1, Vddcore => 16);
71+
72+
#[cfg(feature = "rm0481")]
73+
impl Vddcore {
74+
pub fn enable(_adc: &Adc<ADC2, Disabled>) {
75+
let adc2 = unsafe { ADC1::steal() };
76+
77+
adc2.or().modify(|_, w| w.op0().bit(true));
78+
}
79+
5180
pub fn disable(_adc: &Adc<ADC2, Disabled>) {
5281
let adc2 = unsafe { ADC1::steal() };
5382

5483
adc2.or().modify(|_, w| w.op0().bit(false));
5584
}
5685
}
86+
#[cfg(feature = "rm0481")]
87+
adc_pins!(ADC2, Vddcore => 17);
5788

89+
#[cfg(feature = "rm0492")]
5890
adc_internal!(
5991
[ADC1, ADCC];
6092

6193
Temperature => (16, tsen),
62-
Vrefint => (17, vbaten),
94+
Vrefint => (17, vrefen),
95+
Vbat => (2, vbaten),
96+
);
6397

98+
#[cfg(feature = "rm0481")]
99+
adc_internal!(
100+
[ADC1, ADCC];
101+
102+
Temperature => (16, tsen),
103+
Vrefint => (17, vrefen),
64104
);
65105

106+
#[cfg(feature = "rm0481")]
66107
adc_internal!(
67108
[ADC2, ADCC];
68109

69-
Vbat => (16, vrefen),
110+
Vbat => (16, vbaten),
70111
);
71112

72113
macro_rules! adc_pins_common {
73114
($($input:ty => $chan:expr),+ $(,)*) => {$(
74115
adc_pins!(ADC1, $input => $chan);
116+
117+
#[cfg(feature = "rm0481")]
75118
adc_pins!(ADC2, $input => $chan);
76119
)*};
77120
}
78121

79-
// stm32h523
122+
#[cfg(any(
123+
feature = "stm32h503",
124+
feature = "stm32h523",
125+
feature = "stm32h533"
126+
))]
80127
adc_pins_common!(
81128
gpio::PC0<Analog> => 10,
82129
gpio::PC1<Analog> => 11,
@@ -88,23 +135,25 @@ adc_pins_common!(
88135
gpio::PA2<Analog> => 14,
89136
gpio::PA3<Analog> => 15,
90137
gpio::PA4<Analog> => 18,
91-
92138
gpio::PA5<Analog> => 19,
93139
gpio::PA6<Analog> => 3,
94140
gpio::PA7<Analog> => 7,
141+
95142
gpio::PC4<Analog> => 4,
96143
gpio::PC5<Analog> => 8,
97144
gpio::PB0<Analog> => 9,
98145
gpio::PB1<Analog> => 5,
99146
);
100147

148+
#[cfg(feature = "stm32h523")]
101149
adc_pins!(
102150
ADC1,
103151
gpio::PF11<Analog> => 2,
104152
gpio::PF12<Analog> => 6,
105153

106154
);
107155

156+
#[cfg(feature = "stm32h523")]
108157
adc_pins!(
109158
ADC2,
110159
gpio::PF13<Analog> => 2,

src/adc/mod.rs

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ use core::marker::PhantomData;
1515
use core::ops::Deref;
1616

1717
use embedded_hal::delay::DelayNs;
18-
use stm32h5::stm32h523::ADCC;
1918

2019
use crate::rcc::rec::AdcDacClkSelGetter;
21-
use crate::stm32::{ADC1, ADC2};
20+
21+
#[cfg(feature = "rm0492")]
22+
use crate::stm32::{ADC1, ADC1 as ADCC};
23+
24+
#[cfg(feature = "rm0481")]
25+
use crate::stm32::{ADC1, ADC2, ADCC};
2226

2327
use crate::pwr::{self, VoltageScale};
2428
//use crate::rcc::rec::AdcClkSelGetter;
@@ -32,9 +36,13 @@ pub trait Instance:
3236
}
3337

3438
impl crate::Sealed for ADC1 {}
39+
40+
#[cfg(feature = "rm0481")]
3541
impl crate::Sealed for ADC2 {}
3642

3743
impl Instance for ADC1 {}
44+
45+
#[cfg(feature = "rm0481")]
3846
impl Instance for ADC2 {}
3947

4048
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -194,7 +202,7 @@ impl AdcCalLinear {
194202
/// Vref internal signal
195203
#[derive(Default)]
196204
pub struct Vrefint;
197-
/// Vbat internal signal
205+
/// Vbat/4 (Vbat pin input voltage divided by 4) internal signal
198206
#[derive(Default)]
199207
pub struct Vbat;
200208
/// Internal temperature sensor
@@ -263,13 +271,48 @@ fn kernel_clk_unwrap(
263271
}
264272
}
265273

266-
// ADC12 is a unique case where a single reset line is used to control two
267-
// peripherals that have separate peripheral definitions in the SVD.
274+
#[cfg(feature = "rm0492")]
275+
pub fn adc1(
276+
adc1: ADC1,
277+
f_adc: impl Into<Hertz>,
278+
delay: &mut impl DelayNs,
279+
prec: rec::Adc,
280+
clocks: &CoreClocks,
281+
pwrcfg: &pwr::PowerConfiguration,
282+
) -> Adc<ADC1, Disabled> {
283+
// Consume ADC register block, produce ADC1/2 with default settings
284+
let mut adc1 = Adc::<ADC1, Disabled>::default_from_rb(adc1);
285+
286+
// Check adc_ker_ck_input
287+
kernel_clk_unwrap(&prec, clocks);
288+
289+
// Enable AHB clock
290+
let prec = prec.enable();
291+
292+
// Power Down
293+
adc1.power_down();
294+
295+
// Reset peripheral
296+
let prec = prec.reset();
297+
298+
// Power Up, Preconfigure and Calibrate
299+
adc1.power_up(delay);
300+
adc1.configure_clock(f_adc.into(), prec, clocks, pwrcfg); // ADC12_COMMON
301+
adc1.preconfigure();
302+
adc1.calibrate();
303+
304+
// From RM0481:
305+
// This option bit must be set to 1 when ADCx_INP0 or ADCx_INN1 channel is selected
306+
adc1.rb.or().modify(|_, w| w.op0().set_bit());
307+
308+
adc1
309+
}
268310

269311
/// Initialise ADC12 together
270312
///
271313
/// Sets all configurable parameters to one-shot defaults,
272314
/// performs a boot-time calibration.
315+
#[cfg(feature = "rm0481")]
273316
pub fn adc12(
274317
adc1: ADC1,
275318
adc2: ADC2,
@@ -308,28 +351,12 @@ pub fn adc12(
308351

309352
// From RM0481:
310353
// This option bit must be set to 1 when ADCx_INP0 or ADCx_INN1 channel is selected
311-
adc2.rb.or().modify(|_, w| w.op0().set_bit());
354+
adc1.rb.or().modify(|_, w| w.op0().set_bit());
312355
adc2.rb.or().modify(|_, w| w.op0().set_bit());
313356

314357
(adc1, adc2)
315358
}
316359

317-
/// Free both ADC1 and ADC2 along with PREC.
318-
///
319-
/// Since ADC1 and ADC2 are controlled together, they are freed together.
320-
pub fn free_adc12<ED>(
321-
adc1: Adc<ADC1, ED>,
322-
adc2: Adc<ADC2, ED>,
323-
) -> (ADC1, ADC2, rec::Adc) {
324-
(
325-
adc1.rb,
326-
adc2.rb,
327-
rec::Adc {
328-
_marker: PhantomData,
329-
},
330-
)
331-
}
332-
333360
impl<ADC: Instance> AdcExt<ADC> for ADC {
334361
type Rec = rec::Adc;
335362

0 commit comments

Comments
 (0)