2019-07-09 23:43:52 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "extmod/machine_i2c.h"
|
2020-09-16 00:09:49 -04:00
|
|
|
#include "modmachine.h"
|
2019-07-09 23:43:52 -04:00
|
|
|
|
|
|
|
#include "driver/i2c.h"
|
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.)
2022-09-26 14:28:39 -04:00
|
|
|
#include "hal/i2c_ll.h"
|
|
|
|
|
2021-05-13 23:23:44 -04:00
|
|
|
#ifndef MICROPY_HW_I2C0_SCL
|
|
|
|
#define MICROPY_HW_I2C0_SCL (GPIO_NUM_18)
|
|
|
|
#define MICROPY_HW_I2C0_SDA (GPIO_NUM_19)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MICROPY_HW_I2C1_SCL
|
2021-02-18 05:24:34 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2021-05-13 23:23:44 -04:00
|
|
|
#define MICROPY_HW_I2C1_SCL (GPIO_NUM_25)
|
|
|
|
#define MICROPY_HW_I2C1_SDA (GPIO_NUM_26)
|
2021-02-18 05:24:34 -05:00
|
|
|
#else
|
2021-05-13 23:23:44 -04:00
|
|
|
#define MICROPY_HW_I2C1_SCL (GPIO_NUM_9)
|
|
|
|
#define MICROPY_HW_I2C1_SDA (GPIO_NUM_8)
|
|
|
|
#endif
|
2021-02-18 05:24:34 -05:00
|
|
|
#endif
|
2019-07-09 23:43:52 -04:00
|
|
|
|
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.)
2022-09-26 14:28:39 -04:00
|
|
|
#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
|
|
|
|
|
2023-04-12 21:58:44 -04:00
|
|
|
#define I2C_DEFAULT_TIMEOUT_US (50000) // 50ms
|
2019-07-09 23:43:52 -04:00
|
|
|
|
|
|
|
typedef struct _machine_hw_i2c_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
i2c_port_t port : 8;
|
|
|
|
gpio_num_t scl : 8;
|
|
|
|
gpio_num_t sda : 8;
|
|
|
|
} machine_hw_i2c_obj_t;
|
|
|
|
|
|
|
|
STATIC machine_hw_i2c_obj_t machine_hw_i2c_obj[I2C_NUM_MAX];
|
|
|
|
|
|
|
|
STATIC void machine_hw_i2c_init(machine_hw_i2c_obj_t *self, uint32_t freq, uint32_t timeout_us, bool first_init) {
|
|
|
|
if (!first_init) {
|
|
|
|
i2c_driver_delete(self->port);
|
|
|
|
}
|
|
|
|
i2c_config_t conf = {
|
|
|
|
.mode = I2C_MODE_MASTER,
|
|
|
|
.sda_io_num = self->sda,
|
|
|
|
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
|
|
|
.scl_io_num = self->scl,
|
|
|
|
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
|
|
|
.master.clk_speed = freq,
|
|
|
|
};
|
|
|
|
i2c_param_config(self->port, &conf);
|
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.)
2022-09-26 14:28:39 -04:00
|
|
|
int timeout = I2C_SCLK_FREQ / 1000000 * timeout_us;
|
|
|
|
i2c_set_timeout(self->port, (timeout > I2C_LL_MAX_TIMEOUT) ? I2C_LL_MAX_TIMEOUT : timeout);
|
2019-07-09 23:43:52 -04:00
|
|
|
i2c_driver_install(self->port, I2C_MODE_MASTER, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int machine_hw_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags) {
|
|
|
|
machine_hw_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
2022-05-31 21:58:30 -04:00
|
|
|
int data_len = 0;
|
|
|
|
|
|
|
|
if (flags & MP_MACHINE_I2C_FLAG_WRITE1) {
|
|
|
|
i2c_master_start(cmd);
|
|
|
|
i2c_master_write_byte(cmd, addr << 1, true);
|
|
|
|
i2c_master_write(cmd, bufs->buf, bufs->len, true);
|
|
|
|
data_len += bufs->len;
|
|
|
|
--n;
|
|
|
|
++bufs;
|
|
|
|
}
|
|
|
|
|
2019-07-09 23:43:52 -04:00
|
|
|
i2c_master_start(cmd);
|
|
|
|
i2c_master_write_byte(cmd, addr << 1 | (flags & MP_MACHINE_I2C_FLAG_READ), true);
|
|
|
|
|
|
|
|
for (; n--; ++bufs) {
|
|
|
|
if (flags & MP_MACHINE_I2C_FLAG_READ) {
|
|
|
|
i2c_master_read(cmd, bufs->buf, bufs->len, n == 0 ? I2C_MASTER_LAST_NACK : I2C_MASTER_ACK);
|
|
|
|
} else {
|
|
|
|
if (bufs->len != 0) {
|
|
|
|
i2c_master_write(cmd, bufs->buf, bufs->len, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data_len += bufs->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & MP_MACHINE_I2C_FLAG_STOP) {
|
|
|
|
i2c_master_stop(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO proper timeout
|
2023-05-08 19:52:54 -04:00
|
|
|
esp_err_t err = i2c_master_cmd_begin(self->port, cmd, 100 * (1 + data_len) / portTICK_PERIOD_MS);
|
2019-07-09 23:43:52 -04:00
|
|
|
i2c_cmd_link_delete(cmd);
|
|
|
|
|
|
|
|
if (err == ESP_FAIL) {
|
|
|
|
return -MP_ENODEV;
|
|
|
|
} else if (err == ESP_ERR_TIMEOUT) {
|
|
|
|
return -MP_ETIMEDOUT;
|
|
|
|
} else if (err != ESP_OK) {
|
|
|
|
return -abs(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// MicroPython bindings for machine API
|
|
|
|
|
|
|
|
STATIC void machine_hw_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
machine_hw_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
int h, l;
|
|
|
|
i2c_get_period(self->port, &h, &l);
|
|
|
|
mp_printf(print, "I2C(%u, scl=%u, sda=%u, freq=%u)",
|
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.)
2022-09-26 14:28:39 -04:00
|
|
|
self->port, self->scl, self->sda, I2C_SCLK_FREQ / (h + l));
|
2019-07-09 23:43:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-09-25 00:47:34 -04:00
|
|
|
MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args);
|
|
|
|
|
2019-07-09 23:43:52 -04:00
|
|
|
// Parse args
|
|
|
|
enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
2020-10-27 23:28:17 -04:00
|
|
|
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
2019-07-09 23:43:52 -04:00
|
|
|
{ MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} },
|
|
|
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = I2C_DEFAULT_TIMEOUT_US} },
|
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
// Get I2C bus
|
|
|
|
mp_int_t i2c_id = mp_obj_get_int(args[ARG_id].u_obj);
|
|
|
|
if (!(I2C_NUM_0 <= i2c_id && i2c_id < I2C_NUM_MAX)) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
|
2019-07-09 23:43:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get static peripheral object
|
|
|
|
machine_hw_i2c_obj_t *self = (machine_hw_i2c_obj_t *)&machine_hw_i2c_obj[i2c_id];
|
|
|
|
|
|
|
|
bool first_init = false;
|
|
|
|
if (self->base.type == NULL) {
|
|
|
|
// Created for the first time, set default pins
|
2022-10-06 14:20:03 -04:00
|
|
|
self->base.type = &machine_i2c_type;
|
2019-07-09 23:43:52 -04:00
|
|
|
self->port = i2c_id;
|
|
|
|
if (self->port == I2C_NUM_0) {
|
2021-05-13 23:23:44 -04:00
|
|
|
self->scl = MICROPY_HW_I2C0_SCL;
|
|
|
|
self->sda = MICROPY_HW_I2C0_SDA;
|
2019-07-09 23:43:52 -04:00
|
|
|
} else {
|
2021-05-13 23:23:44 -04:00
|
|
|
self->scl = MICROPY_HW_I2C1_SCL;
|
|
|
|
self->sda = MICROPY_HW_I2C1_SDA;
|
2019-07-09 23:43:52 -04:00
|
|
|
}
|
|
|
|
first_init = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set SCL/SDA pins if given
|
|
|
|
if (args[ARG_scl].u_obj != MP_OBJ_NULL) {
|
2023-06-07 09:10:11 -04:00
|
|
|
self->scl = machine_pin_get_id(args[ARG_scl].u_obj);
|
2019-07-09 23:43:52 -04:00
|
|
|
}
|
|
|
|
if (args[ARG_sda].u_obj != MP_OBJ_NULL) {
|
2023-06-07 09:10:11 -04:00
|
|
|
self->sda = machine_pin_get_id(args[ARG_sda].u_obj);
|
2019-07-09 23:43:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise the I2C peripheral
|
|
|
|
machine_hw_i2c_init(self, args[ARG_freq].u_int, args[ARG_timeout].u_int, first_init);
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_machine_i2c_p_t machine_hw_i2c_p = {
|
2022-06-16 21:57:57 -04:00
|
|
|
.transfer_supports_write1 = true,
|
2019-07-09 23:43:52 -04:00
|
|
|
.transfer = machine_hw_i2c_transfer,
|
|
|
|
};
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
2022-10-06 14:20:03 -04:00
|
|
|
machine_i2c_type,
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_QSTR_I2C,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 10:31:23 -04:00
|
|
|
make_new, machine_hw_i2c_make_new,
|
2021-07-14 00:38:38 -04:00
|
|
|
print, machine_hw_i2c_print,
|
|
|
|
protocol, &machine_hw_i2c_p,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &mp_machine_i2c_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|