Skip to content

Commit

Permalink
Merge #434
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bors[bot] and SnVIZQ authored Aug 26, 2022
2 parents 5c70745 + cab62d7 commit 8f3f3be
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
`PinMode` for modes instead of pins, `HL` trait, other cleanups
- `flash`: add one-cycle delay of reading `BSY` bit after setting `STRT` bit to
fix errata.
- `PwmHz::get_period`: fix computation of return value, prevent division by zero

### Breaking changes

Expand Down
2 changes: 1 addition & 1 deletion src/timer/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ where
let arr = TIM::read_auto_reload();

// Length in ms of an internal clock pulse
clk / (psc * arr)
clk / ((psc + 1) * (arr + 1))
}

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

0 comments on commit 8f3f3be

Please sign in to comment.