Skip to content

Commit

Permalink
Fix TWI clock Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmortsell committed Sep 1, 2023
1 parent c9fef17 commit 84b40ef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### Removed

### Fixed
- TWIHS: Fix issue with clock frequency calculation.

## [v0.4.2] 2022-11-06

Expand Down
7 changes: 5 additions & 2 deletions hal/src/serial/twi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ impl<M: TwiMeta> Twi<M> {
// Try to find a valid clock configuration. From §43.8.5 we
// have
//
// DIV * 2^CKDIV = (f_pid / f_twi) - 3
// DIV * 2^CKDIV = (f_pid / f_twi / 2) - 3
//
// where DIV = CHDIV = CLDIV.
//
// We thus iterate over possible values of CKDIV and use the
// first valid permutation of (CKIV, DIV), unless options are
// exhausted.
let calc_div = |ckdiv| {
(clk.freq() / conf.freq)
(clk.freq() / conf.freq / 2)
.checked_sub(3)
.map(|v| v / 2u32.pow(ckdiv))
};
Expand Down Expand Up @@ -232,6 +232,9 @@ impl<M: TwiMeta> Twi<M> {
pub struct I2cConfiguration {
/// The frequency of the I²C communication. The periods of high
/// and low clock cycles are equal.
///
/// NOTE: freq is an estimate where the rise time is assumed to be negligible, which is only
/// valid for lower frequencies.
pub freq: Hertz,
}

Expand Down

0 comments on commit 84b40ef

Please sign in to comment.