Skip to content

Commit 8f3f3be

Browse files
bors[bot]SnVIZQ
andauthored
Merge #434
434: Fix computation of timer frequency r=burrbull a=SnVIZQ Retrieving timer frequency using PwmHz::get_period() function can cause division by zero exception for certain types of frequencies which are configured by having zero in any of the PSC or ARR register - current implementation uses "clk / (psc * arr)" expression to compute the frequency. Implementation of compute_arr_presc() sets PSC and ARR registers correctly. Performing inverse computation leads to different expression: "clk / ((psc + 1) * (arr + 1))" which properly computes timer frequency from PSC and ARR registers. This patch uses new/fixed expression for computing timer frequency. Change log was modified. Co-authored-by: Juraj Hercek <snvizq@hck.sk>
2 parents 5c70745 + cab62d7 commit 8f3f3be

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
`PinMode` for modes instead of pins, `HL` trait, other cleanups
1313
- `flash`: add one-cycle delay of reading `BSY` bit after setting `STRT` bit to
1414
fix errata.
15+
- `PwmHz::get_period`: fix computation of return value, prevent division by zero
1516

1617
### Breaking changes
1718

src/timer/pwm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ where
372372
let arr = TIM::read_auto_reload();
373373

374374
// Length in ms of an internal clock pulse
375-
clk / (psc * arr)
375+
clk / ((psc + 1) * (arr + 1))
376376
}
377377

378378
pub fn set_period(&mut self, period: Hertz) {

0 commit comments

Comments
 (0)