Skip to content

Commit a08424d

Browse files
authored
adc: add support for hardware oversampling (#122)
Signed-off-by: Jean-Baptiste Theou <[email protected]>
1 parent 1d03c22 commit a08424d

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

src/adc.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,79 @@ pub mod config {
694694
}
695695
}
696696

697+
/// Possible oversampling shift
698+
#[derive(Debug, Clone, Copy)]
699+
pub enum OverSamplingShift {
700+
/// No right shift
701+
NoShift,
702+
/// Shift of 1 toward the right
703+
Shift_1,
704+
/// Shift of 2 toward the right
705+
Shift_2,
706+
/// Shift of 3 toward the right
707+
Shift_3,
708+
/// Shift of 4 toward the right
709+
Shift_4,
710+
/// Shift of 5 toward the right
711+
Shift_5,
712+
/// Shift of 6 toward the right
713+
Shift_6,
714+
/// Shift of 7 toward the right
715+
Shift_7,
716+
/// Shift of 8 toward the right
717+
Shift_8,
718+
}
719+
impl From<OverSamplingShift> for u8 {
720+
fn from(oss: OverSamplingShift) -> u8 {
721+
match oss {
722+
OverSamplingShift::NoShift => 0,
723+
OverSamplingShift::Shift_1 => 1,
724+
OverSamplingShift::Shift_2 => 2,
725+
OverSamplingShift::Shift_3 => 3,
726+
OverSamplingShift::Shift_4 => 4,
727+
OverSamplingShift::Shift_5 => 5,
728+
OverSamplingShift::Shift_6 => 6,
729+
OverSamplingShift::Shift_7 => 7,
730+
OverSamplingShift::Shift_8 => 8,
731+
}
732+
}
733+
}
734+
735+
/// Possible oversampling modes
736+
#[derive(Debug, Clone, Copy)]
737+
pub enum OverSampling {
738+
/// Oversampling 2x
739+
Ratio_2,
740+
/// Oversampling 4x
741+
Ratio_4,
742+
/// Oversampling 8x
743+
Ratio_8,
744+
/// Oversampling 16x
745+
Ratio_16,
746+
/// Oversampling 32x
747+
Ratio_32,
748+
/// Oversampling 64x
749+
Ratio_64,
750+
/// Oversampling 128x
751+
Ratio_128,
752+
/// Oversampling 256x
753+
Ratio_256,
754+
}
755+
impl From<OverSampling> for u8 {
756+
fn from(os: OverSampling) -> u8 {
757+
match os {
758+
OverSampling::Ratio_2 => 0,
759+
OverSampling::Ratio_4 => 1,
760+
OverSampling::Ratio_8 => 2,
761+
OverSampling::Ratio_16 => 3,
762+
OverSampling::Ratio_32 => 4,
763+
OverSampling::Ratio_64 => 5,
764+
OverSampling::Ratio_128 => 6,
765+
OverSampling::Ratio_256 => 7,
766+
}
767+
}
768+
}
769+
697770
/// Possible trigger modes
698771
#[derive(Debug, Clone, Copy)]
699772
pub enum TriggerMode {
@@ -1653,6 +1726,15 @@ macro_rules! adc {
16531726
self.adc_reg.cfgr.modify(|_, w| w.res().bits(resolution.into()));
16541727
}
16551728

1729+
1730+
/// Enable oversampling
1731+
#[inline(always)]
1732+
pub fn set_oversampling(&mut self, oversampling: config::OverSampling, shift: config::OverSamplingShift) {
1733+
self.adc_reg.cfgr2.modify(|_, w| unsafe { w.ovsr().bits(oversampling.into())
1734+
.ovss().bits(shift.into())
1735+
.rovse().set_bit()});
1736+
}
1737+
16561738
/// Sets the DR register alignment to left or right
16571739
#[inline(always)]
16581740
pub fn set_align(&mut self, align: config::Align) {
@@ -2219,6 +2301,12 @@ macro_rules! adc {
22192301
self.adc.set_clock(clock)
22202302
}
22212303

2304+
/// Sets the oversampling
2305+
#[inline(always)]
2306+
pub fn set_oversampling(&mut self, oversampling: config::OverSampling, shift: config::OverSamplingShift) {
2307+
self.adc.set_oversampling(oversampling, shift)
2308+
}
2309+
22222310
/// Sets the sampling resolution
22232311
#[inline(always)]
22242312
pub fn set_resolution(&mut self, resolution: config::Resolution) {

0 commit comments

Comments
 (0)