Skip to content

Commit

Permalink
Implement ADC self-calibration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Javier Cardona committed Jan 22, 2024
1 parent 7041775 commit 02f7e6a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ impl Adc<Ready> {
self.precision = precision;
}

/// Trigger internal ADC calibration
///
/// This process is documented in Reference Manual RM377, section 13.3.3
/// Calibration (ADCAL)
pub fn calibrate(&mut self) -> Result<(), Error> {
if self._state != Ready
|| self.rb.cr.read().aden().bit_is_set()
|| self.rb.cfgr1.read().dmaen().bit_is_set()
{
return Err(Error::InvalidAdcState);
}

self.rb.cr.modify(|_, w| w.adcal().set_bit());
for _ in 0..1000 {
if self.rb.isr.read().eocal().is_complete() {
return Ok(());
}
}
Err(Error::InternalCalibrationTimedOut)
}

/// Starts a continuous conversion process
///
/// The `channel` argument specifies which channel should be converted.
Expand Down Expand Up @@ -321,6 +342,7 @@ where
}

/// Indicates that the ADC peripheral is ready
#[derive(PartialEq)]
pub struct Ready;

/// Indicates that the ADC peripheral is performing conversions
Expand Down Expand Up @@ -615,6 +637,12 @@ pub enum Error {
/// just keeps writing more values. It does mean that some values in the
/// buffer were overwritten though.
BufferOverrun,

/// ADC timed out during internal calibration
InternalCalibrationTimedOut,

/// Invalid ADC state for requested operation
InvalidAdcState,
}

macro_rules! int_adc {
Expand Down

0 comments on commit 02f7e6a

Please sign in to comment.