Skip to content

Commit f5641a3

Browse files
Allow querying of actual PWM frequency (#117)
For PWM frequencies close to the clock source there can be quite a difference between requested frequency and actual frequency. Providing a way for the user to check what frequency they actually got can help with debugging.
1 parent 215f395 commit f5641a3

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/timer/pwm.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ macro_rules! pwm {
100100
}
101101

102102
impl Pwm<$TIMX> {
103+
/// Set the PWM frequency. Actual frequency may differ from
104+
/// requested due to precision of input clock. To check actual
105+
/// frequency, call freq.
103106
pub fn set_freq(&mut self, freq: Hertz) {
104107
let ratio = self.clk / freq;
105108
let psc = (ratio - 1) / 0xffff;
@@ -132,6 +135,13 @@ macro_rules! pwm {
132135
pub fn reset(&mut self) {
133136
self.tim.cnt.reset();
134137
}
138+
139+
/// Returns the currently configured frequency
140+
pub fn freq(&self) -> Hertz {
141+
Hertz::from_raw(self.clk.raw()
142+
/ (self.tim.psc.read().bits() + 1)
143+
/ (self.tim.arr.read().bits() + 1))
144+
}
135145
}
136146
)+
137147
}

0 commit comments

Comments
 (0)