Skip to content

Commit e9f1c32

Browse files
committed
stm32/machine_i2c_target: Implement I2CTargetMemory.
Works, tested on PYBD_SF6: buf = bytearray(16) machine.I2CTargetMemory("X", 67, buf) Signed-off-by: Damien George <[email protected]>
1 parent e1760bc commit e9f1c32

File tree

9 files changed

+273
-10
lines changed

9 files changed

+273
-10
lines changed

ports/stm32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ SRC_C += \
266266
bufhelper.c \
267267
dma.c \
268268
i2c.c \
269+
i2cslave.c \
269270
pyb_i2c.c \
270271
spi.c \
271272
pyb_spi.c \

ports/stm32/i2c.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "py/mphal.h"
3030
#include "py/runtime.h"
3131
#include "i2c.h"
32+
#include "i2cslave.h"
3233

3334
#if MICROPY_HW_ENABLE_HW_I2C
3435

@@ -551,6 +552,8 @@ static const uint8_t i2c_available =
551552
#endif
552553
;
553554

555+
uint8_t i2c_target_enabled;
556+
554557
int i2c_find_peripheral(mp_obj_t id) {
555558
int i2c_id = 0;
556559
if (mp_obj_is_str(id)) {
@@ -596,7 +599,11 @@ int i2c_find_peripheral(mp_obj_t id) {
596599
void I2C1_EV_IRQHandler(void) {
597600
MP_STATIC_ASSERT(I2C1_EV_IRQn > 0);
598601
IRQ_ENTER(I2C1_EV_IRQn);
599-
i2c_ev_irq_handler(1);
602+
if (i2c_target_enabled & 1) {
603+
i2c_slave_ev_irq_handler(I2C1);
604+
} else {
605+
i2c_ev_irq_handler(1);
606+
}
600607
IRQ_EXIT(I2C1_EV_IRQn);
601608
}
602609

@@ -612,7 +619,11 @@ void I2C1_ER_IRQHandler(void) {
612619
void I2C2_EV_IRQHandler(void) {
613620
MP_STATIC_ASSERT(I2C2_EV_IRQn > 0);
614621
IRQ_ENTER(I2C2_EV_IRQn);
615-
i2c_ev_irq_handler(2);
622+
if (i2c_target_enabled & 2) {
623+
i2c_slave_ev_irq_handler(I2C2);
624+
} else {
625+
i2c_ev_irq_handler(2);
626+
}
616627
IRQ_EXIT(I2C2_EV_IRQn);
617628
}
618629

@@ -628,7 +639,11 @@ void I2C2_ER_IRQHandler(void) {
628639
void I2C3_EV_IRQHandler(void) {
629640
MP_STATIC_ASSERT(I2C3_EV_IRQn > 0);
630641
IRQ_ENTER(I2C3_EV_IRQn);
631-
i2c_ev_irq_handler(3);
642+
if (i2c_target_enabled & 4) {
643+
i2c_slave_ev_irq_handler(I2C3);
644+
} else {
645+
i2c_ev_irq_handler(3);
646+
}
632647
IRQ_EXIT(I2C3_EV_IRQn);
633648
}
634649

@@ -644,7 +659,11 @@ void I2C3_ER_IRQHandler(void) {
644659
void I2C4_EV_IRQHandler(void) {
645660
MP_STATIC_ASSERT(I2C4_EV_IRQn > 0);
646661
IRQ_ENTER(I2C4_EV_IRQn);
647-
i2c_ev_irq_handler(4);
662+
if (i2c_target_enabled & 8) {
663+
i2c_slave_ev_irq_handler(I2C4);
664+
} else {
665+
i2c_ev_irq_handler(4);
666+
}
648667
IRQ_EXIT(I2C4_EV_IRQn);
649668
}
650669

ports/stm32/i2c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ extern I2C_HandleTypeDef I2CHandle4;
4646
extern const mp_obj_type_t pyb_i2c_type;
4747
extern const pyb_i2c_obj_t pyb_i2c_obj[4];
4848

49+
extern uint8_t i2c_target_enabled;
50+
4951
void i2c_init0(void);
5052
int pyb_i2c_init(I2C_HandleTypeDef *i2c);
5153
int pyb_i2c_init_freq(const pyb_i2c_obj_t *self, mp_int_t freq);

ports/stm32/i2cslave.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ void i2c_slave_ev_irq_handler(i2c_slave_t *i2c) {
4545
i2c_slave_process_addr_match(i2c, (sr2 >> I2C_SR2_TRA_Pos) & 1);
4646
}
4747
if (sr1 & I2C_SR1_TXE) {
48-
i2c->DR = i2c_slave_process_tx_byte(i2c);
48+
// This callback must call i2c_slave_write_byte.
49+
i2c_slave_process_tx_byte(i2c);
4950
}
5051
if (sr1 & I2C_SR1_RXNE) {
51-
i2c_slave_process_rx_byte(i2c, i2c->DR);
52+
// This callback must call i2c_slave_read_byte.
53+
i2c_slave_process_rx_byte(i2c);
5254
}
5355
if (sr1 & I2C_SR1_STOPF) {
5456
// STOPF only set at end of RX mode (in TX mode AF is set on NACK)
@@ -80,10 +82,12 @@ void i2c_slave_ev_irq_handler(i2c_slave_t *i2c) {
8082
i2c_slave_process_addr_match(i2c, (i2c->ISR >> I2C_ISR_DIR_Pos) & 1);
8183
}
8284
if (isr & I2C_ISR_TXIS) {
83-
i2c->TXDR = i2c_slave_process_tx_byte(i2c);
85+
// This callback must call i2c_slave_write_byte.
86+
i2c_slave_process_tx_byte(i2c);
8487
}
8588
if (isr & I2C_ISR_RXNE) {
86-
i2c_slave_process_rx_byte(i2c, i2c->RXDR);
89+
// This callback must call i2c_slave_read_byte.
90+
i2c_slave_process_rx_byte(i2c);
8791
}
8892
if (isr & I2C_ISR_STOPF) {
8993
// STOPF only set for STOP condition, not a repeated START

ports/stm32/i2cslave.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include STM32_HAL_H
3030

31+
#if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32WB)
32+
3133
#if !defined(I2C2_BASE)
3234
// This MCU doesn't have I2C2_BASE, define it so that the i2c_idx calculation works.
3335
#define I2C2_BASE (I2C1_BASE + ((I2C3_BASE - I2C1_BASE) / 2))
@@ -78,13 +80,31 @@ static inline void i2c_slave_shutdown(i2c_slave_t *i2c, int irqn) {
7880
NVIC_DisableIRQ(irqn);
7981
}
8082

83+
static inline void i2c_slave_write_byte(i2c_slave_t *i2c, uint8_t value) {
84+
#if defined(STM32F4)
85+
i2c->DR = value;
86+
#else
87+
i2c->TXDR = value;
88+
#endif
89+
}
90+
91+
static inline uint8_t i2c_slave_read_byte(i2c_slave_t *i2c) {
92+
#if defined(STM32F4)
93+
return i2c->DR;
94+
#else
95+
return i2c->RXDR;
96+
#endif
97+
}
98+
8199
void i2c_slave_ev_irq_handler(i2c_slave_t *i2c);
82100

83101
// These should be provided externally
84102
int i2c_slave_process_addr_match(i2c_slave_t *i2c, int rw);
85-
int i2c_slave_process_rx_byte(i2c_slave_t *i2c, uint8_t val);
103+
int i2c_slave_process_rx_byte(i2c_slave_t *i2c);
86104
void i2c_slave_process_rx_end(i2c_slave_t *i2c);
87-
uint8_t i2c_slave_process_tx_byte(i2c_slave_t *i2c);
105+
void i2c_slave_process_tx_byte(i2c_slave_t *i2c);
88106
void i2c_slave_process_tx_end(i2c_slave_t *i2c);
89107

108+
#endif
109+
90110
#endif // MICROPY_INCLUDED_STM32_I2CSLAVE_H

ports/stm32/machine_i2c_target.c

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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 "i2c.h"
31+
#include "i2cslave.h"
32+
33+
#define IRQ_PRI_I2C NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 13, 0)
34+
35+
typedef struct _machine_i2c_target_obj_t {
36+
mp_obj_base_t base;
37+
I2C_TypeDef *i2c;
38+
int irqn;
39+
mp_hal_pin_obj_t scl;
40+
mp_hal_pin_obj_t sda;
41+
} machine_i2c_target_obj_t;
42+
43+
static const machine_i2c_target_obj_t machine_i2cslave_obj[] = {
44+
#if defined(MICROPY_HW_I2C1_SCL)
45+
{{&machine_i2c_target_type}, I2C1, I2C1_EV_IRQn, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA},
46+
#else
47+
{{NULL}, NULL, 0, NULL, NULL},
48+
#endif
49+
#if defined(MICROPY_HW_I2C2_SCL)
50+
{{&machine_i2c_target_type}, I2C2, I2C2_EV_IRQn, MICROPY_HW_I2C2_SCL, MICROPY_HW_I2C2_SDA},
51+
#else
52+
{{NULL}, NULL, 0, NULL, NULL},
53+
#endif
54+
#if defined(MICROPY_HW_I2C3_SCL)
55+
{{&machine_i2c_target_type}, I2C3, I2C3_EV_IRQn, MICROPY_HW_I2C3_SCL, MICROPY_HW_I2C3_SDA},
56+
#else
57+
{{NULL}, NULL, 0, NULL, NULL},
58+
#endif
59+
#if defined(MICROPY_HW_I2C4_SCL)
60+
{{&machine_i2c_target_type}, I2C4, I2C4_EV_IRQn, MICROPY_HW_I2C4_SCL, MICROPY_HW_I2C4_SDA},
61+
#else
62+
{{NULL}, NULL, 0, NULL, NULL},
63+
#endif
64+
};
65+
66+
static machine_i2c_target_data_t i2c_target_data[4];
67+
68+
/******************************************************************************/
69+
// stm32 hardware bindings
70+
71+
static machine_i2c_target_obj_t *get_self(i2c_slave_t *i2c) {
72+
size_t i2c_id = ((uintptr_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
73+
return (machine_i2c_target_obj_t *)&machine_i2cslave_obj[i2c_id];
74+
}
75+
76+
static machine_i2c_target_data_t *get_data(i2c_slave_t *i2c) {
77+
size_t i2c_id = ((uintptr_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
78+
return &i2c_target_data[i2c_id];
79+
}
80+
81+
int i2c_slave_process_addr_match(i2c_slave_t *i2c, int rw) {
82+
machine_i2c_target_data_addr_match(get_data(i2c), rw);
83+
return 0;
84+
}
85+
86+
int i2c_slave_process_rx_byte(i2c_slave_t *i2c) {
87+
machine_i2c_target_data_write_request(get_self(i2c), get_data(i2c));
88+
return 0;
89+
}
90+
91+
void i2c_slave_process_rx_end(i2c_slave_t *i2c) {
92+
machine_i2c_target_data_stop(get_data(i2c));
93+
}
94+
95+
void i2c_slave_process_tx_byte(i2c_slave_t *i2c) {
96+
machine_i2c_target_data_read_request(get_self(i2c), get_data(i2c));
97+
}
98+
99+
void i2c_slave_process_tx_end(i2c_slave_t *i2c) {
100+
machine_i2c_target_data_stop(get_data(i2c));
101+
}
102+
103+
/******************************************************************************/
104+
// I2CTarget port implementation
105+
106+
static void mp_machine_i2c_target_deinit_all_port(void) {
107+
}
108+
109+
static void mp_machine_i2c_target_event_callback(machine_i2c_target_irq_obj_t *irq) {
110+
mp_irq_handler(&irq->base);
111+
}
112+
113+
static mp_int_t mp_machine_i2c_target_read_bytes(machine_i2c_target_obj_t *self, size_t len, uint8_t *buf) {
114+
if (len > 0) {
115+
buf[0] = i2c_slave_read_byte(self->i2c);
116+
len = 1;
117+
}
118+
return len;
119+
}
120+
121+
static mp_int_t mp_machine_i2c_target_write_bytes(machine_i2c_target_obj_t *self, size_t len, const uint8_t *buf) {
122+
if (len > 0) {
123+
i2c_slave_write_byte(self->i2c, buf[0]);
124+
len = 1;
125+
}
126+
return len;
127+
}
128+
129+
static void mp_machine_i2c_target_irq_config(machine_i2c_target_obj_t *self, unsigned int trigger) {
130+
}
131+
132+
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) {
133+
// parse args
134+
enum { ARG_id, ARG_addr, ARG_addrsize, ARG_mem, ARG_mem_addrsize };
135+
static const mp_arg_t allowed_args[] = {
136+
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
137+
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT },
138+
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 7} },
139+
{ MP_QSTR_mem, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
140+
{ MP_QSTR_mem_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
141+
/*
142+
{ MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
143+
{ MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
144+
*/
145+
};
146+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
147+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
148+
149+
// Work out i2c bus
150+
int i2c_id = 0;
151+
if (MP_OBJ_IS_STR(args[ARG_id].u_obj)) {
152+
const char *port = mp_obj_str_get_str(args[ARG_id].u_obj);
153+
if (0) {
154+
#ifdef MICROPY_HW_I2C1_NAME
155+
} else if (strcmp(port, MICROPY_HW_I2C1_NAME) == 0) {
156+
i2c_id = 1;
157+
#endif
158+
#ifdef MICROPY_HW_I2C2_NAME
159+
} else if (strcmp(port, MICROPY_HW_I2C2_NAME) == 0) {
160+
i2c_id = 2;
161+
#endif
162+
#ifdef MICROPY_HW_I2C3_NAME
163+
} else if (strcmp(port, MICROPY_HW_I2C3_NAME) == 0) {
164+
i2c_id = 3;
165+
#endif
166+
} else {
167+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
168+
MP_ERROR_TEXT("I2C(%s) doesn't exist"), port));
169+
}
170+
} else {
171+
i2c_id = mp_obj_get_int(args[ARG_id].u_obj);
172+
if (i2c_id < 1 || i2c_id > MP_ARRAY_SIZE(machine_i2cslave_obj)
173+
|| machine_i2cslave_obj[i2c_id - 1].base.type == NULL) {
174+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
175+
MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id));
176+
}
177+
}
178+
179+
// Initialise data.
180+
MP_STATE_PORT(i2c_target_mem_obj)[i2c_id - 1] = args[ARG_mem].u_obj;
181+
machine_i2c_target_data_t *data = &i2c_target_data[i2c_id - 1];
182+
machine_i2c_target_data_init(data, args[ARG_mem].u_obj, args[ARG_mem_addrsize].u_int);
183+
184+
// Get static target object.
185+
machine_i2c_target_obj_t *self = (machine_i2c_target_obj_t *)&machine_i2cslave_obj[i2c_id - 1];
186+
187+
// Initialise the I2C target.
188+
mp_hal_pin_config_alt(self->scl, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, AF_FN_I2C, i2c_id);
189+
mp_hal_pin_config_alt(self->sda, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, AF_FN_I2C, i2c_id);
190+
i2c_slave_init(self->i2c, self->irqn, IRQ_PRI_I2C, args[ARG_addr].u_int);
191+
192+
i2c_target_enabled |= 1 << (i2c_id - 1);
193+
194+
return MP_OBJ_FROM_PTR(self);
195+
}
196+
197+
static void mp_machine_i2c_target_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
198+
machine_i2c_target_obj_t *self = MP_OBJ_TO_PTR(self_in);
199+
mp_printf(print, "I2CTarget(%u, addr=%u)",
200+
self - &machine_i2cslave_obj[0] + 1,
201+
(self->i2c->OAR1 >> 1) & 0x7f);
202+
}
203+
204+
static void mp_machine_i2c_target_deinit(machine_i2c_target_obj_t *self) {
205+
i2c_slave_shutdown(self->i2c, self->irqn);
206+
}

ports/stm32/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,9 @@ void stm32_main(uint32_t reset_mode) {
746746
#if MICROPY_PY_PYB_LEGACY && MICROPY_HW_ENABLE_HW_I2C
747747
pyb_i2c_deinit_all();
748748
#endif
749+
#if MICROPY_PY_MACHINE_I2C_TARGET
750+
mp_machine_i2c_target_deinit_all();
751+
#endif
749752
#if MICROPY_HW_ENABLE_CAN
750753
pyb_can_deinit_all();
751754
#endif

ports/stm32/mpconfigboard_common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,14 @@
639639
#if defined(MICROPY_HW_I2C1_SCL) || defined(MICROPY_HW_I2C2_SCL) \
640640
|| defined(MICROPY_HW_I2C3_SCL) || defined(MICROPY_HW_I2C4_SCL)
641641
#define MICROPY_HW_ENABLE_HW_I2C (1)
642+
#if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32WB)
643+
#define MICROPY_HW_ENABLE_HW_I2C_TARGET (1)
644+
#else
645+
#define MICROPY_HW_ENABLE_HW_I2C_TARGET (0)
646+
#endif
642647
#else
643648
#define MICROPY_HW_ENABLE_HW_I2C (0)
649+
#define MICROPY_HW_ENABLE_HW_I2C_TARGET (0)
644650
#endif
645651

646652
// Enable CAN if there are any peripherals defined

ports/stm32/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@
129129
#define MICROPY_PY_MACHINE_PULSE (1)
130130
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
131131
#define MICROPY_PY_MACHINE_I2C (MICROPY_HW_ENABLE_HW_I2C)
132+
#define MICROPY_PY_MACHINE_I2C_TARGET (MICROPY_HW_ENABLE_HW_I2C_TARGET)
133+
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/stm32/machine_i2c_target.c"
132134
#define MICROPY_PY_MACHINE_SOFTI2C (1)
133135
#define MICROPY_PY_MACHINE_I2S_INCLUDEFILE "ports/stm32/machine_i2s.c"
134136
#define MICROPY_PY_MACHINE_I2S_CONSTANT_RX (I2S_MODE_MASTER_RX)

0 commit comments

Comments
 (0)