Skip to content

Commit

Permalink
esp32/i2c: Use idf function for calculating main timeout.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Leech <[email protected]>
  • Loading branch information
pi-anl committed Sep 15, 2024
1 parent 54b44fe commit 236ba17
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ports/esp32/machine_i2c.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

#define I2C_DEFAULT_TIMEOUT_US (50000) // 50ms

// this formula computes the log2 of "m"
#define BITS_TO_LOG2(m) ((m) - 1) / (((m) - 1) % 255 + 1) / 255 % 255 * 8 + 7 - 86 / (((m) - 1) % 255 + 12)
typedef struct _machine_hw_i2c_obj_t {
mp_obj_base_t base;
i2c_port_t port : 8;
Expand All @@ -81,12 +83,12 @@ static void machine_hw_i2c_init(machine_hw_i2c_obj_t *self, uint32_t freq, uint3
.master.clk_speed = freq,
};
i2c_param_config(self->port, &conf);

TickType_t ticks_to_wait = (xfer_timeout_ms == -1) ? portMAX_DELAY : pdMS_TO_TICKS(xfer_timeout_ms);
if (xSemaphoreTake(bus_handle->bus_lock_mux, ticks_to_wait) != pdTRUE) {
return ESP_ERR_TIMEOUT;
}

int timeout = I2C_SCLK_FREQ / 1000000 * 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);
Expand Down

0 comments on commit 236ba17

Please sign in to comment.