Skip to content

Commit

Permalink
Fix TWI clock Configuration (#59)
Browse files Browse the repository at this point in the history
Updated the frequency configuration and related documentation.
Added comment in I2cConfiguration noting that it will not be accurate for higher frequencies.
  • Loading branch information
martinmortsell authored Sep 15, 2023
1 parent 53bdfa0 commit 0a1da34
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 @@ -14,6 +14,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 0a1da34

Please sign in to comment.