Skip to content

Commit f218bbc

Browse files
committed
Fix I2C timing.
The std::max() meant that scll was always set to 255, resulting in bit rates that were far too slow. Also, the documentation states that the clock is divided by PRESC+1, not by 2^(PRESC+1).
1 parent 87ea74d commit f218bbc

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/i2c.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ impl Config {
5555
let speed = self.speed.unwrap();
5656
let (psc, scll, sclh, sdadel, scldel) = if speed.0 <= 100_000 {
5757
let psc = 3;
58-
let scll = cmp::max(((i2c_clk.0 >> (psc + 1)) / speed.0) - 1, 255);
58+
let scll = cmp::min((((i2c_clk.0 >> 1) / (psc + 1)) / speed.0) - 1, 255);
5959
let sclh = scll - 4;
6060
let sdadel = 2;
6161
let scldel = 4;
6262
(psc, scll, sclh, sdadel, scldel)
6363
} else {
6464
let psc = 1;
65-
let scll = cmp::max(((i2c_clk.0 >> (psc + 1)) / speed.0) - 1, 255);
65+
let scll = cmp::min((((i2c_clk.0 >> 1) / (psc + 1)) / speed.0) - 1, 255);
6666
let sclh = scll - 6;
6767
let sdadel = 1;
6868
let scldel = 3;

0 commit comments

Comments
 (0)