Skip to content

Commit 3d9d138

Browse files
jcard0naJavier Cardona
authored andcommitted
Implement ADC self-calibration (stm32-rs#233)
* Implement ADC self-calibration Implement the ACD self-calibration procedure, which is defined in the RM377 reference manual as follows: The ADC has a calibration feature. During the procedure, the ADC calculates a calibration factor which is internally applied to the ADC until the next ADC power-off. The application must not use the ADC during calibration and must wait until it is complete. Calibration should be performed before starting A/D convers * Wait for hardware to clear ADCAL after calibration The reference manual says that software should wait until ADCAL = 0. Do that instead of being paranoid and eventually timing out if this does not happen. --------- Co-authored-by: Javier Cardona <[email protected]>
1 parent 2b3f9ff commit 3d9d138

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/adc.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ impl Adc<Ready> {
123123
self.precision = precision;
124124
}
125125

126+
/// Trigger internal ADC calibration
127+
///
128+
/// This process is documented in Reference Manual RM377, section 13.3.3
129+
/// Calibration (ADCAL)
130+
pub fn calibrate(&mut self) -> Result<(), Error> {
131+
if self._state != Ready
132+
|| self.rb.cr.read().aden().bit_is_set()
133+
|| self.rb.cfgr1.read().dmaen().bit_is_set()
134+
{
135+
return Err(Error::InvalidAdcState);
136+
}
137+
138+
self.rb.cr.modify(|_, w| w.adcal().set_bit());
139+
while self.rb.cr.read().adcal().bit_is_set() {}
140+
Ok(())
141+
}
142+
126143
/// Starts a continuous conversion process
127144
///
128145
/// The `channel` argument specifies which channel should be converted.
@@ -321,6 +338,7 @@ where
321338
}
322339

323340
/// Indicates that the ADC peripheral is ready
341+
#[derive(PartialEq)]
324342
pub struct Ready;
325343

326344
/// Indicates that the ADC peripheral is performing conversions
@@ -615,6 +633,9 @@ pub enum Error {
615633
/// just keeps writing more values. It does mean that some values in the
616634
/// buffer were overwritten though.
617635
BufferOverrun,
636+
637+
/// Invalid ADC state for requested operation
638+
InvalidAdcState,
618639
}
619640

620641
macro_rules! int_adc {

0 commit comments

Comments
 (0)