esp32/machine_i2c: Fix clocks and timeouts for ESP32-C3, ESP32-S3.

Each SoC family has its own clocks and timings/timeouts. For I2C, the
default source clock is either APB (ESP32, ESP32-S2) or XTAL (ESP32-S3,
ESP32-C3) as shown in the datasheets.  Since
machine_i2c.c/machine_hw_i2c_init() uses the default clk_flags (0), the
alternate low-power clock source is never selected in ESP-IDF
i2c.c/i2c_param_config().  There is not an API in i2c.c to get the source
clock frequency, so a compile-time value is used based on SoC family.

Also, the maximum timeout is different across the SoC families, so use the
I2C_LL_MAX_TIMEOUT constant to eliminate the warning from
i2c_set_timeout().

With these changes, the following results were obtained.  The I2C SCL
frequencies were measured with a Saleae logic analyzer.

ESP32 (TTGO T Dislay)
    I2C(0, scl=22, sda=21, freq=101781)  Measured: 100KHz
    I2C(0, scl=22, sda=21, freq=430107)  Measured: 400KHz
    I2C(0, scl=22, sda=21, freq=1212121) Measured: 941KHz

ESP32-S3 (TTGO T-QT)
    I2C(0, scl=34, sda=33, freq=111111)  Measured: 107KHz
    I2C(0, scl=34, sda=33, freq=444444)  Measured: 400KHz
    I2C(0, scl=34, sda=33, freq=1111111) Measured: 842KHz

ESP32-C3 (XIAO ESP32C3)
    I2C(0, scl=7, sda=6, freq=107816)  Measured: 103KHz
    I2C(0, scl=7, sda=6, freq=444444)  Measured: 380KHz
    I2C(0, scl=7, sda=6, freq=1176470) Measured: 800KHz

(ESP32-S2 board was not available for testing.)
This commit is contained in:
Mark Grosen 2022-09-26 11:28:39 -07:00 committed by Damien George
parent 6643b4f0cc
commit 12f9948151

View File

@ -32,6 +32,13 @@
#include "driver/i2c.h"
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
#include "hal/i2c_ll.h"
#else
#include "soc/i2c_reg.h"
#define I2C_LL_MAX_TIMEOUT I2C_TIME_OUT_REG_V
#endif
#ifndef MICROPY_HW_I2C0_SCL
#define MICROPY_HW_I2C0_SCL (GPIO_NUM_18)
#define MICROPY_HW_I2C0_SDA (GPIO_NUM_19)
@ -47,6 +54,14 @@
#endif
#endif
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
#define I2C_SCLK_FREQ XTAL_CLK_FREQ
#elif CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
#define I2C_SCLK_FREQ I2C_APB_CLK_FREQ
#else
#error "unsupported I2C for ESP32 SoC variant"
#endif
#define I2C_DEFAULT_TIMEOUT_US (10000) // 10ms
typedef struct _machine_hw_i2c_obj_t {
@ -71,7 +86,8 @@ 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);
i2c_set_timeout(self->port, I2C_APB_CLK_FREQ / 1000000 * timeout_us);
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);
}
@ -131,7 +147,7 @@ STATIC void machine_hw_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_p
int h, l;
i2c_get_period(self->port, &h, &l);
mp_printf(print, "I2C(%u, scl=%u, sda=%u, freq=%u)",
self->port, self->scl, self->sda, I2C_APB_CLK_FREQ / (h + l));
self->port, self->scl, self->sda, I2C_SCLK_FREQ / (h + l));
}
mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {