From 84b40ef3286e0d89a30494abe39912f16bfc9ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20M=C3=B6rtsell?= Date: Fri, 1 Sep 2023 15:31:34 +0200 Subject: [PATCH] Fix TWI clock Configuration --- CHANGELOG.md | 1 + hal/src/serial/twi.rs | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1470504..5c9dbb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### Removed ### Fixed +- TWIHS: Fix issue with clock frequency calculation. ## [v0.4.2] 2022-11-06 diff --git a/hal/src/serial/twi.rs b/hal/src/serial/twi.rs index e60b799..2401cfc 100644 --- a/hal/src/serial/twi.rs +++ b/hal/src/serial/twi.rs @@ -109,7 +109,7 @@ impl Twi { // 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. // @@ -117,7 +117,7 @@ impl Twi { // 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)) }; @@ -232,6 +232,9 @@ impl Twi { 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, }