Skip to content

Commit 1839340

Browse files
committed
rp2/machine_i2c_target: Implement I2CTarget class.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 56d2b47 commit 1839340

File tree

3 files changed

+312
-0
lines changed

3 files changed

+312
-0
lines changed

ports/rp2/machine_i2c_target.c

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
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 "machine_i2c.h"
31+
#include "hardware/i2c.h"
32+
33+
typedef struct _machine_i2c_target_obj_t {
34+
mp_obj_base_t base;
35+
i2c_inst_t *const i2c_inst;
36+
mp_hal_pin_obj_t scl;
37+
mp_hal_pin_obj_t sda;
38+
uint8_t state;
39+
bool stop_pending;
40+
bool irq_active;
41+
} machine_i2c_target_obj_t;
42+
43+
static machine_i2c_target_obj_t machine_i2c_target_obj[] = {
44+
{{&machine_i2c_target_type}, i2c0, MICROPY_HW_I2C0_SCL, MICROPY_HW_I2C0_SDA},
45+
{{&machine_i2c_target_type}, i2c1, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA},
46+
};
47+
48+
/******************************************************************************/
49+
// RP2xxx hardware bindings
50+
51+
static void check_stop_pending(machine_i2c_target_obj_t *self) {
52+
if (self->irq_active) {
53+
return;
54+
}
55+
if (self->stop_pending && !(self->i2c_inst->hw->status & I2C_IC_STATUS_RFNE_BITS)) {
56+
unsigned int i2c_id = self - &machine_i2c_target_obj[0];
57+
machine_i2c_target_data_t *data = &machine_i2c_target_data[i2c_id];
58+
self->stop_pending = false;
59+
self->state = STATE_IDLE;
60+
machine_i2c_target_data_restart_or_stop(data);
61+
}
62+
}
63+
64+
static void i2c_target_handler(i2c_inst_t *i2c) {
65+
unsigned int i2c_id = i2c == i2c0 ? 0 : 1;
66+
machine_i2c_target_obj_t *self = &machine_i2c_target_obj[i2c_id];
67+
machine_i2c_target_data_t *data = &machine_i2c_target_data[i2c_id];
68+
69+
self->irq_active = true;
70+
71+
// Get the interrupt status.
72+
uint32_t intr_stat = i2c->hw->intr_stat;
73+
74+
if (intr_stat & I2C_IC_INTR_STAT_R_TX_ABRT_BITS) {
75+
// Clear the TX_ABRT condition.
76+
(void)i2c->hw->clr_tx_abrt;
77+
}
78+
79+
if (intr_stat & I2C_IC_INTR_STAT_R_START_DET_BITS) {
80+
// Controller sent a start condition.
81+
// Reset all state machines in case something went wrong.
82+
(void)i2c->hw->clr_start_det;
83+
if (self->state != STATE_IDLE) {
84+
machine_i2c_target_data_reset_helper(data);
85+
self->state = STATE_IDLE;
86+
}
87+
}
88+
89+
if (intr_stat & I2C_IC_INTR_STAT_R_RX_FULL_BITS) {
90+
// Data from controller is available for reading.
91+
// Mask interrupt until I2C_DATA_CMD is read from.
92+
i2c->hw->intr_mask &= ~I2C_IC_INTR_MASK_M_RX_FULL_BITS;
93+
if (self->state != STATE_WRITING) {
94+
machine_i2c_target_data_addr_match(data, false);
95+
}
96+
machine_i2c_target_data_write_request(self, data);
97+
self->state = STATE_WRITING;
98+
}
99+
100+
if (intr_stat & (I2C_IC_INTR_STAT_R_RD_REQ_BITS | I2C_IC_INTR_STAT_R_RX_DONE_BITS)) {
101+
// Controller is requesting data.
102+
(void)i2c->hw->clr_rx_done;
103+
(void)i2c->hw->clr_rd_req;
104+
i2c->hw->intr_mask &= ~I2C_IC_INTR_MASK_M_RD_REQ_BITS;
105+
if (self->state != STATE_READING) {
106+
machine_i2c_target_data_addr_match(data, true);
107+
}
108+
machine_i2c_target_data_read_request(self, data);
109+
self->state = STATE_READING;
110+
}
111+
112+
if (intr_stat & I2C_IC_INTR_STAT_R_STOP_DET_BITS) {
113+
// Controller has generated a stop condition.
114+
(void)i2c->hw->clr_stop_det;
115+
if (self->state == STATE_IDLE) {
116+
machine_i2c_target_data_addr_match(data, false);
117+
}
118+
if (i2c->hw->status & I2C_IC_STATUS_RFNE_BITS) {
119+
self->stop_pending = true;
120+
} else {
121+
machine_i2c_target_data_restart_or_stop(data);
122+
self->state = STATE_IDLE;
123+
}
124+
}
125+
126+
self->irq_active = false;
127+
check_stop_pending(self);
128+
}
129+
130+
static void i2c_target_irq_handler(void) {
131+
uint i2c_index = __get_current_exception() - VTABLE_FIRST_IRQ - I2C0_IRQ;
132+
i2c_inst_t *i2c = i2c_get_instance(i2c_index);
133+
i2c_target_handler(i2c);
134+
}
135+
136+
static void i2c_target_init(i2c_inst_t *i2c, uint16_t addr, bool addr_10bit) {
137+
i2c->hw->enable = 0;
138+
139+
// Configure general settings, target address and FIFO levels.
140+
i2c->hw->con =
141+
I2C_IC_CON_RX_FIFO_FULL_HLD_CTRL_BITS
142+
| I2C_IC_CON_STOP_DET_IFADDRESSED_BITS;
143+
if (addr_10bit) {
144+
i2c->hw->con |= I2C_IC_CON_IC_10BITADDR_SLAVE_BITS;
145+
}
146+
i2c->hw->sar = addr;
147+
i2c->hw->tx_tl = 1;
148+
i2c->hw->rx_tl = 0; // interrupt when at least 1 byte is available
149+
(void)i2c->hw->clr_intr;
150+
151+
// Enable interrupts.
152+
i2c->hw->intr_mask =
153+
I2C_IC_INTR_MASK_M_START_DET_BITS
154+
| I2C_IC_INTR_MASK_M_STOP_DET_BITS
155+
| I2C_IC_INTR_MASK_M_RX_DONE_BITS
156+
| I2C_IC_INTR_MASK_M_TX_ABRT_BITS
157+
| I2C_IC_INTR_MASK_M_RD_REQ_BITS
158+
| I2C_IC_INTR_MASK_M_RX_FULL_BITS
159+
;
160+
161+
i2c->hw->enable = 1;
162+
163+
// Enable interrupt for current core.
164+
uint i2c_index = i2c_hw_index(i2c);
165+
uint num = I2C0_IRQ + i2c_index;
166+
irq_set_exclusive_handler(num, i2c_target_irq_handler);
167+
irq_set_enabled(num, true);
168+
}
169+
170+
static void i2c_target_deinit(i2c_inst_t *i2c) {
171+
uint i2c_index = i2c_hw_index(i2c);
172+
uint num = I2C0_IRQ + i2c_index;
173+
irq_set_enabled(num, false);
174+
irq_remove_handler(num, i2c_target_irq_handler);
175+
176+
i2c->hw->intr_mask = 0;
177+
i2c->hw->enable = 0;
178+
i2c->hw->con = I2C_IC_CON_IC_SLAVE_DISABLE_BITS;
179+
}
180+
181+
/******************************************************************************/
182+
// I2CTarget port implementation
183+
184+
static inline size_t mp_machine_i2c_target_get_index(machine_i2c_target_obj_t *self) {
185+
return self - &machine_i2c_target_obj[0];
186+
}
187+
188+
static inline void mp_machine_i2c_target_event_callback(machine_i2c_target_irq_obj_t *irq) {
189+
mp_irq_handler(&irq->base);
190+
}
191+
192+
static size_t mp_machine_i2c_target_read_bytes(machine_i2c_target_obj_t *self, size_t len, uint8_t *buf) {
193+
i2c_hw_t *i2c_hw = self->i2c_inst->hw;
194+
195+
// Read from the RX FIFO.
196+
size_t i = 0;
197+
while (i < len && (i2c_hw->status & I2C_IC_STATUS_RFNE_BITS)) {
198+
buf[i++] = i2c_hw->data_cmd;
199+
}
200+
201+
// Re-enable RX_FULL interrupt.
202+
i2c_hw->intr_mask |= I2C_IC_INTR_MASK_M_RX_FULL_BITS;
203+
204+
check_stop_pending(self);
205+
206+
return i;
207+
}
208+
209+
static size_t mp_machine_i2c_target_write_bytes(machine_i2c_target_obj_t *self, size_t len, const uint8_t *buf) {
210+
i2c_hw_t *i2c_hw = self->i2c_inst->hw;
211+
212+
// Write to the TX FIFO.
213+
size_t i = 0;
214+
while (i < len && (i2c_hw->status & I2C_IC_STATUS_TFNF_BITS)) {
215+
i2c_hw->data_cmd = buf[i++];
216+
}
217+
218+
// Re-enable RD_REQ interrupt.
219+
i2c_hw->intr_mask |= I2C_IC_INTR_MASK_M_RD_REQ_BITS;
220+
221+
return i;
222+
}
223+
224+
static inline void mp_machine_i2c_target_irq_config(machine_i2c_target_obj_t *self, unsigned int trigger) {
225+
(void)self;
226+
(void)trigger;
227+
}
228+
229+
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) {
230+
enum { ARG_id, ARG_addr, ARG_addrsize, ARG_mem, ARG_mem_addrsize, ARG_scl, ARG_sda };
231+
static const mp_arg_t allowed_args[] = {
232+
#ifdef PICO_DEFAULT_I2C
233+
{ MP_QSTR_id, MP_ARG_INT, {.u_int = PICO_DEFAULT_I2C} },
234+
#else
235+
{ MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED },
236+
#endif
237+
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT },
238+
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 7} },
239+
{ MP_QSTR_mem, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
240+
{ MP_QSTR_mem_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
241+
{ MP_QSTR_scl, MICROPY_I2C_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
242+
{ MP_QSTR_sda, MICROPY_I2C_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
243+
};
244+
245+
// Parse args.
246+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
247+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
248+
249+
int i2c_id = args[ARG_id].u_int;
250+
251+
// Check if the I2C bus is valid
252+
if (i2c_id < 0 || i2c_id >= MP_ARRAY_SIZE(machine_i2c_target_obj)) {
253+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2CTarget(%d) doesn't exist"), i2c_id);
254+
}
255+
256+
// Get static peripheral object.
257+
machine_i2c_target_obj_t *self = &machine_i2c_target_obj[i2c_id];
258+
259+
// Set SCL/SDA pins if configured.
260+
if (args[ARG_scl].u_obj != mp_const_none) {
261+
int scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
262+
if (!IS_VALID_SCL(i2c_id, scl)) {
263+
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
264+
}
265+
self->scl = scl;
266+
}
267+
if (args[ARG_sda].u_obj != mp_const_none) {
268+
int sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
269+
if (!IS_VALID_SDA(i2c_id, sda)) {
270+
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
271+
}
272+
self->sda = sda;
273+
}
274+
275+
// Initialise I2C target state and data.
276+
self->state = STATE_IDLE;
277+
self->stop_pending = false;
278+
self->irq_active = false;
279+
MP_STATE_PORT(machine_i2c_target_mem_obj)[i2c_id] = args[ARG_mem].u_obj;
280+
machine_i2c_target_data_t *data = &machine_i2c_target_data[i2c_id];
281+
machine_i2c_target_data_init(data, args[ARG_mem].u_obj, args[ARG_mem_addrsize].u_int);
282+
283+
// Initialise I2C target hardware.
284+
i2c_target_init(self->i2c_inst, args[ARG_addr].u_int, args[ARG_addrsize].u_int == 10);
285+
gpio_set_function(self->scl, GPIO_FUNC_I2C);
286+
gpio_set_function(self->sda, GPIO_FUNC_I2C);
287+
gpio_set_pulls(self->scl, true, 0);
288+
gpio_set_pulls(self->sda, true, 0);
289+
290+
return MP_OBJ_FROM_PTR(self);
291+
}
292+
293+
static void mp_machine_i2c_target_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
294+
machine_i2c_target_obj_t *self = MP_OBJ_TO_PTR(self_in);
295+
i2c_hw_t *i2c_hw = i2c_get_hw(self->i2c_inst);
296+
mp_printf(print, "I2CTarget(%u, addr=%u, scl=%u, sda=%u)",
297+
self - &machine_i2c_target_obj[0], i2c_hw->sar, self->scl, self->sda);
298+
}
299+
300+
static void mp_machine_i2c_target_deinit(machine_i2c_target_obj_t *self) {
301+
gpio_set_function(self->scl, GPIO_FUNC_SIO);
302+
gpio_set_function(self->sda, GPIO_FUNC_SIO);
303+
i2c_target_deinit(self->i2c_inst);
304+
}

ports/rp2/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "py/mperrno.h"
3535
#include "py/mphal.h"
3636
#include "extmod/modbluetooth.h"
37+
#include "extmod/modmachine.h"
3738
#include "extmod/modnetwork.h"
3839
#include "shared/readline/readline.h"
3940
#include "shared/runtime/gchelper.h"
@@ -257,6 +258,9 @@ int main(int argc, char **argv) {
257258
machine_pwm_deinit_all();
258259
machine_pin_deinit();
259260
machine_uart_deinit_all();
261+
#if MICROPY_PY_MACHINE_I2C_TARGET
262+
mp_machine_i2c_target_deinit_all();
263+
#endif
260264
#if MICROPY_PY_THREAD
261265
mp_thread_deinit();
262266
#endif

ports/rp2/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@
171171
#define MICROPY_PY_MACHINE_PWM (1)
172172
#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/rp2/machine_pwm.c"
173173
#define MICROPY_PY_MACHINE_I2C (1)
174+
#define MICROPY_PY_MACHINE_I2C_TARGET (1)
175+
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/rp2/machine_i2c_target.c"
176+
#define MICROPY_PY_MACHINE_I2C_TARGET_MAX (2)
177+
#define MICROPY_PY_MACHINE_I2C_TARGET_HARD_IRQ (1)
174178
#define MICROPY_PY_MACHINE_SOFTI2C (1)
175179
#define MICROPY_PY_MACHINE_I2S (1)
176180
#define MICROPY_PY_MACHINE_I2S_INCLUDEFILE "ports/rp2/machine_i2s.c"

0 commit comments

Comments
 (0)