diff options
| author | yn386 <wf.yn386@gmail.com> | 2022-09-19 10:14:27 +0900 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2022-11-18 14:25:19 +1100 |
| commit | a74e4fabeb0d7bc4dc68284047248fa173f5237b (patch) | |
| tree | 073d0f8ecdca49e6563c554902b8e8da87c1152c /ports/stm32 | |
| parent | 65d82066a8267d7dd96d747d09f9ed3883d40078 (diff) | |
stm32/i2c: Fix I2C frequency calc so it doesn't exceed requested rate.
Prior to this commit, the actual I2C frequency can be faster than specified
one and it may exceed the I2C's specification for Fast Mode. The frequency
of SCL should be less than or equal to 400KHz in Fast Mode.
This commit fixes this issue for F4 MCUs by rounding up the division in the
frequency calculation.
Diffstat (limited to 'ports/stm32')
| -rw-r--r-- | ports/stm32/i2c.c | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/ports/stm32/i2c.c b/ports/stm32/i2c.c index c63fe2162..a05a0954a 100644 --- a/ports/stm32/i2c.c +++ b/ports/stm32/i2c.c @@ -70,10 +70,11 @@ int i2c_init(i2c_t *i2c, mp_hal_pin_obj_t scl, mp_hal_pin_obj_t sda, uint32_t fr // SM: MAX(4, PCLK1 / (F * 2)) // FM, 16:9 duty: 0xc000 | MAX(1, (PCLK1 / (F * (16 + 9)))) + // (the PCLK1-1 and +1 at the end is to round the division up) if (freq <= 100000) { - i2c->CCR = MAX(4, PCLK1 / (freq * 2)); + i2c->CCR = MAX(4, ((PCLK1 - 1) / (freq * 2) + 1)); } else { - i2c->CCR = 0xc000 | MAX(1, PCLK1 / (freq * 25)); + i2c->CCR = 0xc000 | MAX(1, ((PCLK1 - 1) / (freq * 25) + 1)); } // SM: 1000ns / (1/PCLK1) + 1 = PCLK1 * 1e-6 + 1 |
