From aad6ee3d8c676c58c9dc390aa410bd8259268f44 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Sun, 15 Sep 2024 22:12:17 +1000 Subject: [PATCH] esp32/machine_i2c: Use IDF function for calculating main timeout. The esp32 I2C driver sets a global timeout during init. The functionality for this is currently only correct for original esp32 and esp32-s2, newer parts have a slightly different argument which is log-base-2 of the timeout. Note that `i2c_set_timeout()` is a thin layer around `i2c_ll_set_tout()` with the latter writing directly to the hardware register. So the timeout argument passed to `i2c_set_timeout()` needs to be in units calculated by `i2c_ll_calculate_timeout_us_to_reg_val()`. Signed-off-by: Andrew Leech --- ports/esp32/machine_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index e8d913def9..74679d01da 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -257,7 +257,7 @@ static void machine_hw_i2c_init(machine_hw_i2c_obj_t *self, bool first_init) { .master.clk_speed = self->freq, }; i2c_param_config(self->port, &conf); - int timeout = I2C_SCLK_FREQ / 1000000 * self->timeout_us; + int timeout = i2c_ll_calculate_timeout_us_to_reg_val(I2C_SCLK_FREQ, self->timeout_us); i2c_set_timeout(self->port, (timeout > I2C_LL_MAX_TIMEOUT) ? I2C_LL_MAX_TIMEOUT : timeout); i2c_driver_install(self->port, I2C_MODE_MASTER, 0, 0, 0); }