Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slope method to ADC and depreciate max_sample #344

Merged
merged 1 commit into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn main() -> ! {
info!(
"ADC reading: {}, voltage for nucleo: {}",
data,
data as f32 * (3.3 / adc1.max_sample() as f32)
data as f32 * (3.3 / adc1.slope() as f32)
);
}
}
4 changes: 2 additions & 2 deletions examples/adc12_parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ fn main() -> ! {
info!(
"ADC1 reading: {}, voltage for Daisy pin X: {}",
data1,
data1 as f32 * (3.3 / adc1.max_sample() as f32)
data1 as f32 * (3.3 / adc1.slope() as f32)
);
info!(
"ADC2 reading: {}, voltage for Daisy pin X: {}",
data2,
data2 as f32 * (3.3 / adc2.max_sample() as f32)
data2 as f32 * (3.3 / adc2.slope() as f32)
);
}
}
24 changes: 22 additions & 2 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,12 +787,32 @@ macro_rules! adc_hal {
self.lshift = lshift;
}

/// Returns the largest possible sample value for the current settings
/// Returns the largest possible sample value for the current ADC configuration
///
/// Using this value as the denominator when calculating
/// transfer functions results in a gain error, and thus should
/// be avoided. Use the [slope](#method.slope) method instead.
#[deprecated(since = "0.12.0", note = "See the slope() method instead")]
pub fn max_sample(&self) -> u32 {
((1 << self.get_resolution().number_of_bits() as u32) - 1) << self.get_lshift().value() as u32
}

/// Returns the offset calibration value for single ended channel
/// Returns the slope for the current ADC configuration. 1 LSB = Vref / slope
///
/// This value can be used in calcuations involving the transfer function of
/// the ADC. For example, to calculate an estimate for the
/// applied voltage of an ADC channel referenced to voltage
/// `vref`
///
/// ```
/// let v = adc.read(&ch).unwrap() as f32 * vref / adc.slope() as f32;
/// ```
pub fn slope(&self) -> u32 {
1 << (self.get_resolution().number_of_bits() as u32 + self.get_lshift().value() as u32)
}


/// Returns the offset calibration value for single ended channel
pub fn read_offset_calibration_value(&self) -> AdcCalOffset {
AdcCalOffset(self.rb.calfact.read().calfact_s().bits())
}
Expand Down