Skip to content

Commit 5934b75

Browse files
committed
ADC: add set_clock_source
1 parent e4dc211 commit 5934b75

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/analog/adc.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,32 @@ pub enum SampleTime {
4646
T_160 = 0b111,
4747
}
4848

49+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
50+
pub enum ClockSource {
51+
Pclk(PclkDiv),
52+
Async(AsyncClockDiv),
53+
}
54+
55+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
56+
pub enum PclkDiv {
57+
PclkD1 = 3,
58+
PclkD2 = 1,
59+
PclkD4 = 2,
60+
}
61+
62+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
63+
pub enum AsyncClockDiv {
64+
AsyncD1 = 0,
65+
AsyncD2 = 1,
66+
AsyncD4 = 2,
67+
AsyncD8 = 3,
68+
AsyncD16 = 4,
69+
AsyncD32 = 5,
70+
AsyncD64 = 6,
71+
AsyncD128 = 7,
72+
AsyncD256 = 8,
73+
}
74+
4975
/// Analog to Digital converter interface
5076
pub struct Adc {
5177
rb: ADC,
@@ -72,6 +98,21 @@ impl Adc {
7298
}
7399
}
74100

101+
pub fn set_clock_source(&mut self, clock_source: ClockSource) {
102+
match clock_source {
103+
ClockSource::Pclk(div) => self
104+
.rb
105+
.cfgr2
106+
.modify(|_, w| unsafe { w.ckmode().bits(div as u8) }),
107+
ClockSource::Async(div) => {
108+
self.rb.cfgr2.modify(|_, w| unsafe { w.ckmode().bits(0) });
109+
self.rb
110+
.ccr
111+
.modify(|_, w| unsafe { w.presc().bits(div as u8) });
112+
}
113+
}
114+
}
115+
75116
/// Runs the calibration routine on the ADC
76117
///
77118
/// Wait for tADCVREG_SETUP (20us on STM32G071x8) after calling [`Self::new()`] before calibrating, to wait for the

0 commit comments

Comments
 (0)