Skip to content

Commit ca9609e

Browse files
committed
esp32/machine_i2c_target: Implement I2CTarget.
Only soft IRQs are supported. Signed-off-by: Damien George <[email protected]>
1 parent 48cb1ac commit ca9609e

File tree

4 files changed

+262
-0
lines changed

4 files changed

+262
-0
lines changed

ports/esp32/boards/sdkconfig.base

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,7 @@ CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT=n
139139
# Further limit total sockets in TIME-WAIT when there are many short-lived
140140
# connections.
141141
CONFIG_LWIP_MAX_ACTIVE_TCP=12
142+
143+
# Enable new I2C slave API, and disable conflict check.
144+
CONFIG_I2C_SKIP_LEGACY_CONFLICT_CHECK=y
145+
CONFIG_I2C_ENABLE_SLAVE_DRIVER_VERSION_2=y

ports/esp32/machine_i2c_target.c

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2025 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
// This file is never compiled standalone, it's included directly from
28+
// extmod/machine_i2c_target.c via MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE.
29+
30+
#include "driver/i2c_slave.h"
31+
32+
// These headers are needed to call i2c_ll_txfifo_rst().
33+
#include "hal/i2c_ll.h"
34+
#include "../i2c_private.h"
35+
36+
// TODO default pin configuration below should be shared with machine_i2c.c.
37+
38+
#ifndef MICROPY_HW_I2C0_SCL
39+
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
40+
#define MICROPY_HW_I2C0_SCL (GPIO_NUM_9)
41+
#define MICROPY_HW_I2C0_SDA (GPIO_NUM_8)
42+
#else
43+
#define MICROPY_HW_I2C0_SCL (GPIO_NUM_18)
44+
#define MICROPY_HW_I2C0_SDA (GPIO_NUM_19)
45+
#endif
46+
#endif
47+
48+
#ifndef MICROPY_HW_I2C1_SCL
49+
#if CONFIG_IDF_TARGET_ESP32
50+
#define MICROPY_HW_I2C1_SCL (GPIO_NUM_25)
51+
#define MICROPY_HW_I2C1_SDA (GPIO_NUM_26)
52+
#else
53+
#define MICROPY_HW_I2C1_SCL (GPIO_NUM_9)
54+
#define MICROPY_HW_I2C1_SDA (GPIO_NUM_8)
55+
#endif
56+
#endif
57+
58+
typedef struct _machine_i2c_target_obj_t {
59+
mp_obj_base_t base;
60+
i2c_slave_dev_handle_t handle;
61+
i2c_slave_config_t config;
62+
uint8_t state;
63+
bool stop_pending;
64+
bool irq_active;
65+
int index;
66+
const i2c_slave_rx_done_event_data_t *rx_done_event_data;
67+
} machine_i2c_target_obj_t;
68+
69+
static machine_i2c_target_obj_t machine_i2c_target_obj[I2C_NUM_MAX];
70+
71+
/******************************************************************************/
72+
// ESP-IDF hardware bindings
73+
74+
// Called when the controller is about to read from the TX/send buffer.
75+
static bool i2c_slave_request_cb(i2c_slave_dev_handle_t i2c_slave, const i2c_slave_request_event_data_t *evt_data, void *arg) {
76+
machine_i2c_target_obj_t *self = arg;
77+
machine_i2c_target_data_t *data = &machine_i2c_target_data[self->config.i2c_port];
78+
79+
// Flush hardware TX FIFO to get rid of any data from a previous read.
80+
i2c_ll_txfifo_rst(self->handle->base->hal.dev);
81+
82+
// Perform an entire read transaction, including start, read and stop events.
83+
// We don't know how much data the controller will read, so write the entire
84+
// memory buffer to the TX FIFO.
85+
machine_i2c_target_data_addr_match(data, true);
86+
for (uint32_t i = 0; i < data->mem_len; ++i) {
87+
machine_i2c_target_data_read_request(self, data);
88+
}
89+
machine_i2c_target_data_restart_or_stop(data);
90+
91+
// A higher priority task was not woken up.
92+
return false;
93+
}
94+
95+
// Called when the controller has written into the RX/receive buffer.
96+
static bool i2c_slave_receive_cb(i2c_slave_dev_handle_t i2c_slave, const i2c_slave_rx_done_event_data_t *evt_data, void *arg)
97+
{
98+
machine_i2c_target_obj_t *self = arg;
99+
machine_i2c_target_data_t *data = &machine_i2c_target_data[self->config.i2c_port];
100+
101+
// Perform an entire write transaction, including start, read and stop events.
102+
machine_i2c_target_data_addr_match(data, false);
103+
self->index = 0;
104+
self->rx_done_event_data = evt_data;
105+
while (self->index < self->rx_done_event_data->length) {
106+
machine_i2c_target_data_write_request(self, data);
107+
}
108+
machine_i2c_target_data_restart_or_stop(data);
109+
110+
// A higher priority task was not woken up.
111+
return false;
112+
}
113+
114+
static void i2c_target_init(machine_i2c_target_obj_t *self, machine_i2c_target_data_t *data, uint32_t addr, uint32_t addrsize, bool first_init) {
115+
if (!first_init && self->handle != NULL) {
116+
i2c_del_slave_device(self->handle);
117+
self->handle = NULL;
118+
}
119+
120+
self->config.clk_source = I2C_CLK_SRC_DEFAULT;
121+
self->config.slave_addr = addr;
122+
self->config.send_buf_depth = data->mem_len;
123+
self->config.receive_buf_depth = data->mem_len;
124+
if (addrsize == 7) {
125+
self->config.addr_bit_len = I2C_ADDR_BIT_LEN_7;
126+
} else {
127+
#if SOC_I2C_SUPPORT_10BIT_ADDR
128+
self->config.addr_bit_len = I2C_ADDR_BIT_LEN_10;
129+
#else
130+
mp_raise_ValueError(MP_ERROR_TEXT("10-bit address unsupported"));
131+
#endif
132+
}
133+
self->config.intr_priority = 0; // 0 selects the default
134+
self->config.flags.allow_pd = 0;
135+
self->config.flags.enable_internal_pullup = 1;
136+
137+
ESP_ERROR_CHECK(i2c_new_slave_device(&self->config, &self->handle));
138+
i2c_slave_event_callbacks_t cbs = {
139+
.on_receive = i2c_slave_receive_cb,
140+
.on_request = i2c_slave_request_cb,
141+
};
142+
ESP_ERROR_CHECK(i2c_slave_register_event_callbacks(self->handle, &cbs, self));
143+
}
144+
145+
/******************************************************************************/
146+
// I2CTarget port implementation
147+
148+
static inline size_t mp_machine_i2c_target_get_index(machine_i2c_target_obj_t *self) {
149+
return self->config.i2c_port;
150+
}
151+
152+
static void mp_machine_i2c_target_deinit_all_port(void) {
153+
}
154+
155+
static void mp_machine_i2c_target_event_callback(machine_i2c_target_irq_obj_t *irq) {
156+
mp_irq_handler(&irq->base);
157+
}
158+
159+
static size_t mp_machine_i2c_target_read_bytes(machine_i2c_target_obj_t *self, size_t len, uint8_t *buf) {
160+
size_t i = 0;
161+
while (i < len && self->index < self->rx_done_event_data->length) {
162+
buf[i++] = self->rx_done_event_data->buffer[self->index++];
163+
}
164+
return i;
165+
}
166+
167+
static size_t mp_machine_i2c_target_write_bytes(machine_i2c_target_obj_t *self, size_t len, const uint8_t *buf) {
168+
uint32_t write_len;
169+
i2c_slave_write(self->handle, buf, len, &write_len, 1000);
170+
return write_len;
171+
}
172+
173+
static void mp_machine_i2c_target_irq_config(machine_i2c_target_obj_t *self, unsigned int trigger) {
174+
}
175+
176+
static mp_obj_t mp_machine_i2c_target_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
177+
enum { ARG_id, ARG_addr, ARG_addrsize, ARG_mem, ARG_mem_addrsize, ARG_scl, ARG_sda };
178+
static const mp_arg_t allowed_args[] = {
179+
{ MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
180+
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT },
181+
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 7} },
182+
{ MP_QSTR_mem, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
183+
{ MP_QSTR_mem_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
184+
{ MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
185+
{ MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
186+
};
187+
188+
// Parse args.
189+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
190+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
191+
192+
int i2c_id = args[ARG_id].u_int;
193+
194+
// Check if the I2C bus is valid
195+
if (i2c_id < 0 || i2c_id >= MP_ARRAY_SIZE(machine_i2c_target_obj)) {
196+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2CTarget(%d) doesn't exist"), i2c_id);
197+
}
198+
199+
// Get static peripheral object.
200+
machine_i2c_target_obj_t *self = &machine_i2c_target_obj[i2c_id];
201+
202+
bool first_init = false;
203+
if (self->base.type == NULL) {
204+
// Created for the first time, set default pins
205+
self->base.type = &machine_i2c_target_type;
206+
self->config.i2c_port = i2c_id;
207+
if (self->config.i2c_port == 0) {
208+
self->config.scl_io_num = MICROPY_HW_I2C0_SCL;
209+
self->config.sda_io_num = MICROPY_HW_I2C0_SDA;
210+
} else {
211+
self->config.scl_io_num = MICROPY_HW_I2C1_SCL;
212+
self->config.sda_io_num = MICROPY_HW_I2C1_SDA;
213+
}
214+
first_init = true;
215+
}
216+
217+
// Initialise data.
218+
self->state = STATE_IDLE;
219+
self->stop_pending = false;
220+
self->irq_active = false;
221+
MP_STATE_PORT(machine_i2c_target_mem_obj)[i2c_id] = args[ARG_mem].u_obj;
222+
machine_i2c_target_data_t *data = &machine_i2c_target_data[i2c_id];
223+
machine_i2c_target_data_init(data, args[ARG_mem].u_obj, args[ARG_mem_addrsize].u_int);
224+
225+
// Set SCL/SDA pins if configured.
226+
if (args[ARG_scl].u_obj != mp_const_none) {
227+
self->config.scl_io_num = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
228+
}
229+
if (args[ARG_sda].u_obj != mp_const_none) {
230+
self->config.sda_io_num = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
231+
}
232+
233+
// Initialise the I2C target.
234+
i2c_target_init(self, data, args[ARG_addr].u_int, args[ARG_addrsize].u_int, first_init);
235+
236+
return MP_OBJ_FROM_PTR(self);
237+
}
238+
239+
static void mp_machine_i2c_target_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
240+
machine_i2c_target_obj_t *self = MP_OBJ_TO_PTR(self_in);
241+
mp_printf(print, "I2CTarget(%u, addr=%u, scl=%u, sda=%u)",
242+
self->config.i2c_port, self->config.slave_addr, self->config.scl_io_num, self->config.sda_io_num);
243+
}
244+
245+
static void mp_machine_i2c_target_deinit(machine_i2c_target_obj_t *self) {
246+
if (self->handle != NULL) {
247+
i2c_del_slave_device(self->handle);
248+
self->handle = NULL;
249+
}
250+
}

ports/esp32/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "py/repl.h"
5252
#include "py/gc.h"
5353
#include "py/mphal.h"
54+
#include "extmod/modmachine.h"
5455
#include "shared/readline/readline.h"
5556
#include "shared/runtime/pyexec.h"
5657
#include "shared/timeutils/timeutils.h"
@@ -199,7 +200,11 @@ void mp_task(void *pvParameter) {
199200
machine_pwm_deinit_all();
200201
// TODO: machine_rmt_deinit_all();
201202
machine_pins_deinit();
203+
#if MICROPY_PY_MACHINE_I2C_TARGET
204+
mp_machine_i2c_target_deinit_all();
205+
#endif
202206
machine_deinit();
207+
203208
#if MICROPY_PY_SOCKET_EVENTS
204209
socket_events_deinit();
205210
#endif

ports/esp32/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@
138138
#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/esp32/machine_pwm.c"
139139
#define MICROPY_PY_MACHINE_I2C (1)
140140
#define MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1 (1)
141+
#define MICROPY_PY_MACHINE_I2C_TARGET (SOC_I2C_SUPPORT_SLAVE && !CONFIG_IDF_TARGET_ESP32)
142+
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/esp32/machine_i2c_target.c"
143+
#define MICROPY_PY_MACHINE_I2C_TARGET_MAX (2)
141144
#define MICROPY_PY_MACHINE_SOFTI2C (1)
142145
#define MICROPY_PY_MACHINE_SPI (1)
143146
#define MICROPY_PY_MACHINE_SOFTSPI (1)

0 commit comments

Comments
 (0)