Skip to content

Commit 850d390

Browse files
committed
zephyr/machine_i2c_target: Implement I2CTarget.
Tested and working on rpi_pico and nucleo_wb55rg. Signed-off-by: Damien George <[email protected]>
1 parent bb676f5 commit 850d390

File tree

5 files changed

+227
-0
lines changed

5 files changed

+227
-0
lines changed

ports/zephyr/boards/nucleo_wb55rg.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CONFIG_NETWORKING=n
44
CONFIG_FLASH=y
55
CONFIG_FLASH_MAP=y
66
CONFIG_I2C=y
7+
CONFIG_I2C_TARGET=y
78
CONFIG_SPI=y
89

910
# Bluetooth

ports/zephyr/boards/rpi_pico.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CONFIG_NETWORKING=n
1313
CONFIG_FLASH=y
1414
CONFIG_FLASH_MAP=y
1515
CONFIG_I2C=y
16+
CONFIG_I2C_TARGET=y
1617
CONFIG_SPI=y
1718

1819
# MicroPython config.

ports/zephyr/machine_i2c_target.c

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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 <zephyr/drivers/i2c.h>
31+
32+
#include "zephyr_device.h"
33+
34+
static machine_i2c_target_data_t i2c_target_data[4];
35+
36+
typedef struct _machine_i2c_target_obj_t {
37+
mp_obj_base_t base;
38+
const struct device *dev;
39+
struct i2c_target_config cfg;
40+
} machine_i2c_target_obj_t;
41+
42+
static bool writing = false;
43+
static bool reading = false;
44+
static uint8_t global_val;
45+
46+
/******************************************************************************/
47+
// zephyr bindings
48+
49+
static int i2c_target_write_requested(struct i2c_target_config *config) {
50+
// printk("wr req\n");
51+
writing = true;
52+
reading = false;
53+
machine_i2c_target_data_addr_match(&i2c_target_data[0], false);
54+
return 0;
55+
}
56+
57+
static int i2c_target_write_received(struct i2c_target_config *config, uint8_t val) {
58+
// printk("wr recv %d\n", val);
59+
global_val = val;
60+
machine_i2c_target_obj_t *self = CONTAINER_OF(config, machine_i2c_target_obj_t, cfg);
61+
machine_i2c_target_data_t *data = &i2c_target_data[0];
62+
machine_i2c_target_data_write_request(self, data);
63+
return 0;
64+
}
65+
66+
// it's possible to get either:
67+
// - req proc proc ... (eg STM32)
68+
// - req proc req proc ... (eg RP2xxx / Design Ware)
69+
static int i2c_target_read_requested(struct i2c_target_config *config, uint8_t *val) {
70+
// printk("rd req\n");
71+
writing = false;
72+
machine_i2c_target_obj_t *self = CONTAINER_OF(config, machine_i2c_target_obj_t, cfg);
73+
machine_i2c_target_data_t *data = &i2c_target_data[0];
74+
if (!reading) {
75+
machine_i2c_target_data_addr_match(data, true);
76+
machine_i2c_target_data_read_request(self, data);
77+
reading = true;
78+
}
79+
*val = global_val;
80+
return 0;
81+
}
82+
83+
static int i2c_target_read_processed(struct i2c_target_config *config, uint8_t *val) {
84+
// printk("rd processed\n");
85+
machine_i2c_target_obj_t *self = CONTAINER_OF(config, machine_i2c_target_obj_t, cfg);
86+
machine_i2c_target_data_t *data = &i2c_target_data[0];
87+
machine_i2c_target_data_read_request(self, data);
88+
*val = global_val;
89+
return 0;
90+
}
91+
92+
// called only on stop, not restart
93+
static int i2c_target_stop(struct i2c_target_config *config) {
94+
// printk("stop\n");
95+
if (!writing && !reading) {
96+
// Assume a stop without a start is a 0-byte write.
97+
machine_i2c_target_data_addr_match(&i2c_target_data[0], false);
98+
}
99+
writing = false;
100+
reading = false;
101+
machine_i2c_target_data_t *data = &i2c_target_data[0];
102+
machine_i2c_target_data_stop(data);
103+
return 0;
104+
}
105+
106+
static struct i2c_target_callbacks i2c_target_callbacks = {
107+
.write_requested = i2c_target_write_requested,
108+
.read_requested = i2c_target_read_requested,
109+
.write_received = i2c_target_write_received,
110+
.read_processed = i2c_target_read_processed,
111+
.stop = i2c_target_stop,
112+
};
113+
114+
/******************************************************************************/
115+
// I2CTarget port implementation
116+
117+
static void mp_machine_i2c_target_deinit_all_port(void) {
118+
}
119+
120+
static void mp_machine_i2c_target_event_callback(machine_i2c_target_irq_obj_t *irq) {
121+
char dummy;
122+
void *orig_top = MP_STATE_THREAD(stack_top);
123+
mp_uint_t orig_limit = MP_STATE_THREAD(stack_limit);
124+
MP_STATE_THREAD(stack_top) = &dummy;
125+
MP_STATE_THREAD(stack_limit) = CONFIG_ISR_STACK_SIZE - 512;
126+
mp_irq_handler(&irq->base);
127+
MP_STATE_THREAD(stack_top) = orig_top;
128+
MP_STATE_THREAD(stack_limit) = orig_limit;
129+
}
130+
131+
static size_t mp_machine_i2c_target_read_bytes(machine_i2c_target_obj_t *self, size_t len, uint8_t *buf) {
132+
buf[0] = global_val;
133+
return 1;
134+
}
135+
136+
static size_t mp_machine_i2c_target_write_bytes(machine_i2c_target_obj_t *self, size_t len, const uint8_t *buf) {
137+
global_val = buf[0];
138+
return 1;
139+
}
140+
141+
static void mp_machine_i2c_target_irq_config(machine_i2c_target_obj_t *self, unsigned int trigger) {
142+
}
143+
144+
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) {
145+
// TODO: reconsider order of arguments
146+
enum { ARG_id, ARG_addr, ARG_addrsize, ARG_mem, ARG_mem_addrsize, ARG_scl, ARG_sda };
147+
static const mp_arg_t allowed_args[] = {
148+
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
149+
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT },
150+
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 7} },
151+
{ MP_QSTR_mem, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
152+
{ MP_QSTR_mem_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
153+
// { MP_QSTR_scl, MICROPY_I2C_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
154+
// { MP_QSTR_sda, MICROPY_I2C_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
155+
};
156+
157+
// Parse args.
158+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
159+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
160+
161+
const struct device *dev = zephyr_device_find(args[ARG_id].u_obj);
162+
163+
machine_i2c_target_obj_t *self = mp_obj_malloc(machine_i2c_target_obj_t, &machine_i2c_target_type);
164+
self->dev = dev;
165+
self->cfg.flags = 0;
166+
self->cfg.address = args[ARG_addr].u_int;
167+
self->cfg.callbacks = &i2c_target_callbacks;
168+
169+
// Initialise data.
170+
MP_STATE_PORT(i2c_target_mem_obj)[0] = args[ARG_mem].u_obj;
171+
machine_i2c_target_data_t *data = &i2c_target_data[0];
172+
machine_i2c_target_data_init(data, args[ARG_mem].u_obj, args[ARG_mem_addrsize].u_int);
173+
174+
#if 0
175+
// Set SCL/SDA pins if configured.
176+
if (args[ARG_scl].u_obj != mp_const_none) {
177+
int scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
178+
if (!IS_VALID_SCL(i2c_id, scl)) {
179+
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
180+
}
181+
self->scl = scl;
182+
}
183+
if (args[ARG_sda].u_obj != mp_const_none) {
184+
int sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
185+
if (!IS_VALID_SDA(i2c_id, sda)) {
186+
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
187+
}
188+
self->sda = sda;
189+
}
190+
#endif
191+
192+
// Initialise the I2C target.
193+
// TODO: don't reinitialize if no arguments given.
194+
int ret = i2c_target_register(self->dev, &self->cfg);
195+
if (ret < 0) {
196+
mp_raise_OSError(-ret);
197+
}
198+
/*
199+
i2c_slave_init(self->i2c_inst, self->addr, i2c_target_handler);
200+
gpio_set_function(self->scl, GPIO_FUNC_I2C);
201+
gpio_set_function(self->sda, GPIO_FUNC_I2C);
202+
gpio_set_pulls(self->scl, true, 0);
203+
gpio_set_pulls(self->sda, true, 0);
204+
*/
205+
206+
return MP_OBJ_FROM_PTR(self);
207+
}
208+
209+
static void mp_machine_i2c_target_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
210+
machine_i2c_target_obj_t *self = MP_OBJ_TO_PTR(self_in);
211+
mp_printf(print, "I2CTarget(%s, addr=%u)",
212+
self->dev->name, self->cfg.address);
213+
}
214+
215+
static void mp_machine_i2c_target_deinit(machine_i2c_target_obj_t *self) {
216+
i2c_target_unregister(self->dev, &self->cfg);
217+
}

ports/zephyr/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "shared/runtime/pyexec.h"
5454
#include "shared/readline/readline.h"
5555
#include "extmod/modbluetooth.h"
56+
#include "extmod/modmachine.h"
5657

5758
#if MICROPY_VFS
5859
#include "extmod/vfs.h"
@@ -197,6 +198,9 @@ int real_main(void) {
197198
#if MICROPY_PY_MACHINE
198199
machine_pin_deinit();
199200
#endif
201+
#if MICROPY_PY_MACHINE_I2C_TARGET
202+
mp_machine_i2c_target_deinit_all();
203+
#endif
200204

201205
#if MICROPY_PY_THREAD
202206
mp_thread_deinit();

ports/zephyr/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
#define MICROPY_PY_MACHINE (1)
6767
#define MICROPY_PY_MACHINE_INCLUDEFILE "ports/zephyr/modmachine.c"
6868
#define MICROPY_PY_MACHINE_I2C (1)
69+
#ifdef CONFIG_I2C_TARGET
70+
#define MICROPY_PY_MACHINE_I2C_TARGET (1)
71+
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/zephyr/machine_i2c_target.c"
72+
#endif
6973
#define MICROPY_PY_MACHINE_SOFTI2C (1)
7074
#define MICROPY_PY_MACHINE_SPI (1)
7175
#define MICROPY_PY_MACHINE_SPI_MSB (SPI_TRANSFER_MSB)

0 commit comments

Comments
 (0)