Skip to content

Commit 3d0ae76

Browse files
committed
ADC: Implement calibration
1 parent 0c63ec9 commit 3d0ae76

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

examples/adc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ use rt::entry;
1717
#[entry]
1818
fn main() -> ! {
1919
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
20+
let cp = stm32::CorePeripherals::take().expect("cannot take core peripherals");
2021
let mut rcc = dp.RCC.constrain();
22+
let mut delay = cp.SYST.delay(&mut rcc);
2123

2224
let gpioa = dp.GPIOA.split(&mut rcc);
2325

2426
let mut adc = dp.ADC.constrain(&mut rcc);
2527
adc.set_sample_time(SampleTime::T_80);
2628
adc.set_precision(Precision::B_12);
29+
delay.delay(20.us()); // Wait for ADC voltage regulator to stabilize
30+
adc.calibrate();
2731

2832
let mut adc_pin = gpioa.pa0.into_analog();
2933
let mut vtemp = VTemp::new();

src/analog/adc.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ impl Adc {
6868
}
6969
}
7070

71+
/// Runs the calibration routine on the ADC
72+
///
73+
/// Wait for tADCVREG_SETUP (20us on STM32G071x8) after calling `new()`
74+
/// before calibrating, to wait for the ADC voltage regulator to stabilize.
75+
///
76+
/// Do not call if an ADC reading is ongoing.
77+
pub fn calibrate(&mut self) {
78+
self.rb.cr.modify(|_, w| w.adcal().set_bit());
79+
while self.rb.cr.read().adcal().bit_is_set() {}
80+
}
81+
7182
/// Set the Adc sampling time
7283
pub fn set_sample_time(&mut self, t_samp: SampleTime) {
7384
self.sample_time = t_samp;

0 commit comments

Comments
 (0)