Skip to content

Commit 01e570a

Browse files
committed
stm32/machine_i2c_target: Implement I2CTarget class.
Works, tested on PYBV10, PYBD_SF2 and PYBD_SF6: buf = bytearray(16) machine.I2CTargetMemory("X", addr=67, mem=buf) Signed-off-by: Damien George <[email protected]>
1 parent 78d1667 commit 01e570a

File tree

9 files changed

+284
-9
lines changed

9 files changed

+284
-9
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: 86 additions & 9 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,10 @@ static const uint8_t i2c_available =
551552
#endif
552553
;
553554

555+
#if MICROPY_HW_ENABLE_HW_I2C_TARGET
556+
uint8_t i2c_target_enabled;
557+
#endif
558+
554559
int i2c_find_peripheral(mp_obj_t id) {
555560
int i2c_id = 0;
556561
if (mp_obj_is_str(id)) {
@@ -590,20 +595,38 @@ int i2c_find_peripheral(mp_obj_t id) {
590595
return i2c_id;
591596
}
592597

593-
#if MICROPY_PY_PYB_LEGACY
598+
#if MICROPY_HW_ENABLE_HW_I2C_TARGET || MICROPY_PY_PYB_LEGACY
594599

595600
#if defined(MICROPY_HW_I2C1_SCL)
596601
void I2C1_EV_IRQHandler(void) {
597602
MP_STATIC_ASSERT(I2C1_EV_IRQn > 0);
598603
IRQ_ENTER(I2C1_EV_IRQn);
599-
i2c_ev_irq_handler(1);
604+
#if MICROPY_HW_ENABLE_HW_I2C_TARGET
605+
if (i2c_target_enabled & 1) {
606+
i2c_slave_irq_handler(I2C1);
607+
} else
608+
#endif
609+
{
610+
#if MICROPY_PY_PYB_LEGACY
611+
i2c_ev_irq_handler(1);
612+
#endif
613+
}
600614
IRQ_EXIT(I2C1_EV_IRQn);
601615
}
602616

603617
void I2C1_ER_IRQHandler(void) {
604618
MP_STATIC_ASSERT(I2C1_ER_IRQn > 0);
605619
IRQ_ENTER(I2C1_ER_IRQn);
606-
i2c_er_irq_handler(1);
620+
#if MICROPY_HW_ENABLE_HW_I2C_TARGET
621+
if (i2c_target_enabled & 1) {
622+
i2c_slave_irq_handler(I2C1);
623+
} else
624+
#endif
625+
{
626+
#if MICROPY_PY_PYB_LEGACY
627+
i2c_er_irq_handler(1);
628+
#endif
629+
}
607630
IRQ_EXIT(I2C1_ER_IRQn);
608631
}
609632
#endif // defined(MICROPY_HW_I2C1_SCL)
@@ -612,14 +635,32 @@ void I2C1_ER_IRQHandler(void) {
612635
void I2C2_EV_IRQHandler(void) {
613636
MP_STATIC_ASSERT(I2C2_EV_IRQn > 0);
614637
IRQ_ENTER(I2C2_EV_IRQn);
615-
i2c_ev_irq_handler(2);
638+
#if MICROPY_HW_ENABLE_HW_I2C_TARGET
639+
if (i2c_target_enabled & 2) {
640+
i2c_slave_irq_handler(I2C2);
641+
} else
642+
#endif
643+
{
644+
#if MICROPY_PY_PYB_LEGACY
645+
i2c_ev_irq_handler(2);
646+
#endif
647+
}
616648
IRQ_EXIT(I2C2_EV_IRQn);
617649
}
618650

619651
void I2C2_ER_IRQHandler(void) {
620652
MP_STATIC_ASSERT(I2C2_ER_IRQn > 0);
621653
IRQ_ENTER(I2C2_ER_IRQn);
622-
i2c_er_irq_handler(2);
654+
#if MICROPY_HW_ENABLE_HW_I2C_TARGET
655+
if (i2c_target_enabled & 2) {
656+
i2c_slave_irq_handler(I2C2);
657+
} else
658+
#endif
659+
{
660+
#if MICROPY_PY_PYB_LEGACY
661+
i2c_er_irq_handler(2);
662+
#endif
663+
}
623664
IRQ_EXIT(I2C2_ER_IRQn);
624665
}
625666
#endif // defined(MICROPY_HW_I2C2_SCL)
@@ -628,14 +669,32 @@ void I2C2_ER_IRQHandler(void) {
628669
void I2C3_EV_IRQHandler(void) {
629670
MP_STATIC_ASSERT(I2C3_EV_IRQn > 0);
630671
IRQ_ENTER(I2C3_EV_IRQn);
631-
i2c_ev_irq_handler(3);
672+
#if MICROPY_HW_ENABLE_HW_I2C_TARGET
673+
if (i2c_target_enabled & 4) {
674+
i2c_slave_irq_handler(I2C3);
675+
} else
676+
#endif
677+
{
678+
#if MICROPY_PY_PYB_LEGACY
679+
i2c_ev_irq_handler(3);
680+
#endif
681+
}
632682
IRQ_EXIT(I2C3_EV_IRQn);
633683
}
634684

635685
void I2C3_ER_IRQHandler(void) {
636686
MP_STATIC_ASSERT(I2C3_ER_IRQn > 0);
637687
IRQ_ENTER(I2C3_ER_IRQn);
638-
i2c_er_irq_handler(3);
688+
#if MICROPY_HW_ENABLE_HW_I2C_TARGET
689+
if (i2c_target_enabled & 4) {
690+
i2c_slave_irq_handler(I2C3);
691+
} else
692+
#endif
693+
{
694+
#if MICROPY_PY_PYB_LEGACY
695+
i2c_er_irq_handler(3);
696+
#endif
697+
}
639698
IRQ_EXIT(I2C3_ER_IRQn);
640699
}
641700
#endif // defined(MICROPY_HW_I2C3_SCL)
@@ -644,14 +703,32 @@ void I2C3_ER_IRQHandler(void) {
644703
void I2C4_EV_IRQHandler(void) {
645704
MP_STATIC_ASSERT(I2C4_EV_IRQn > 0);
646705
IRQ_ENTER(I2C4_EV_IRQn);
647-
i2c_ev_irq_handler(4);
706+
#if MICROPY_HW_ENABLE_HW_I2C_TARGET
707+
if (i2c_target_enabled & 8) {
708+
i2c_slave_irq_handler(I2C4);
709+
} else
710+
#endif
711+
{
712+
#if MICROPY_PY_PYB_LEGACY
713+
i2c_ev_irq_handler(4);
714+
#endif
715+
}
648716
IRQ_EXIT(I2C4_EV_IRQn);
649717
}
650718

651719
void I2C4_ER_IRQHandler(void) {
652720
MP_STATIC_ASSERT(I2C4_ER_IRQn > 0);
653721
IRQ_ENTER(I2C4_ER_IRQn);
654-
i2c_er_irq_handler(4);
722+
#if MICROPY_HW_ENABLE_HW_I2C_TARGET
723+
if (i2c_target_enabled & 8) {
724+
i2c_slave_irq_handler(I2C4);
725+
} else
726+
#endif
727+
{
728+
#if MICROPY_PY_PYB_LEGACY
729+
i2c_er_irq_handler(4);
730+
#endif
731+
}
655732
IRQ_EXIT(I2C4_ER_IRQn);
656733
}
657734
#endif // defined(MICROPY_HW_I2C4_SCL)

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/irq.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ static inline void restore_irq_pri(uint32_t state) {
177177

178178
#define IRQ_PRI_HSEM NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 10, 0)
179179

180+
#define IRQ_PRI_I2C NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 12, 0)
181+
180182
// Interrupt priority for non-special timers.
181183
#define IRQ_PRI_TIMX NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 13, 0)
182184

ports/stm32/machine_i2c_target.c

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
#include "irq.h"
33+
34+
typedef struct _machine_i2c_target_obj_t {
35+
mp_obj_base_t base;
36+
I2C_TypeDef *i2c;
37+
uint16_t irqn_ev;
38+
uint16_t irqn_er;
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_i2c_target_obj[] = {
44+
#if defined(MICROPY_HW_I2C1_SCL)
45+
{{&machine_i2c_target_type}, I2C1, I2C1_EV_IRQn, I2C1_ER_IRQn, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA},
46+
#else
47+
{{NULL}, NULL, 0, 0, NULL, NULL},
48+
#endif
49+
#if defined(MICROPY_HW_I2C2_SCL)
50+
{{&machine_i2c_target_type}, I2C2, I2C2_EV_IRQn, I2C2_ER_IRQn, MICROPY_HW_I2C2_SCL, MICROPY_HW_I2C2_SDA},
51+
#else
52+
{{NULL}, NULL, 0, 0, NULL, NULL},
53+
#endif
54+
#if defined(MICROPY_HW_I2C3_SCL)
55+
{{&machine_i2c_target_type}, I2C3, I2C3_EV_IRQn, I2C3_ER_IRQn, MICROPY_HW_I2C3_SCL, MICROPY_HW_I2C3_SDA},
56+
#else
57+
{{NULL}, NULL, 0, 0, NULL, NULL},
58+
#endif
59+
#if defined(MICROPY_HW_I2C4_SCL)
60+
{{&machine_i2c_target_type}, I2C4, I2C4_EV_IRQn, I2C4_ER_IRQn, MICROPY_HW_I2C4_SCL, MICROPY_HW_I2C4_SDA},
61+
#else
62+
{{NULL}, NULL, 0, 0, NULL, NULL},
63+
#endif
64+
};
65+
66+
/******************************************************************************/
67+
// stm32 hardware bindings
68+
69+
static machine_i2c_target_obj_t *get_self(i2c_slave_t *i2c) {
70+
size_t i2c_id = ((uintptr_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
71+
return (machine_i2c_target_obj_t *)&machine_i2c_target_obj[i2c_id];
72+
}
73+
74+
static machine_i2c_target_data_t *get_data(i2c_slave_t *i2c) {
75+
size_t i2c_id = ((uintptr_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
76+
return &machine_i2c_target_data[i2c_id];
77+
}
78+
79+
int i2c_slave_process_addr_match(i2c_slave_t *i2c, int rw) {
80+
machine_i2c_target_data_addr_match(get_data(i2c), rw);
81+
return 0;
82+
}
83+
84+
int i2c_slave_process_rx_byte(i2c_slave_t *i2c) {
85+
machine_i2c_target_data_write_request(get_self(i2c), get_data(i2c));
86+
return 0;
87+
}
88+
89+
void i2c_slave_process_rx_end(i2c_slave_t *i2c) {
90+
machine_i2c_target_data_stop(get_data(i2c));
91+
}
92+
93+
void i2c_slave_process_tx_byte(i2c_slave_t *i2c) {
94+
machine_i2c_target_data_read_request(get_self(i2c), get_data(i2c));
95+
}
96+
97+
void i2c_slave_process_tx_end(i2c_slave_t *i2c) {
98+
machine_i2c_target_data_stop(get_data(i2c));
99+
}
100+
101+
/******************************************************************************/
102+
// I2CTarget port implementation
103+
104+
static inline size_t mp_machine_i2c_target_get_index(machine_i2c_target_obj_t *self) {
105+
return self - &machine_i2c_target_obj[0];
106+
}
107+
108+
static inline void mp_machine_i2c_target_event_callback(machine_i2c_target_irq_obj_t *irq) {
109+
mp_irq_handler(&irq->base);
110+
}
111+
112+
static size_t mp_machine_i2c_target_read_bytes(machine_i2c_target_obj_t *self, size_t len, uint8_t *buf) {
113+
if (len > 0) {
114+
buf[0] = i2c_slave_read_byte(self->i2c);
115+
len = 1;
116+
}
117+
return len;
118+
}
119+
120+
static size_t mp_machine_i2c_target_write_bytes(machine_i2c_target_obj_t *self, size_t len, const uint8_t *buf) {
121+
if (len > 0) {
122+
i2c_slave_write_byte(self->i2c, buf[0]);
123+
len = 1;
124+
}
125+
return len;
126+
}
127+
128+
static inline void mp_machine_i2c_target_irq_config(machine_i2c_target_obj_t *self, unsigned int trigger) {
129+
(void)self;
130+
(void)trigger;
131+
}
132+
133+
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) {
134+
// Parse arguments.
135+
enum { ARG_id, ARG_addr, ARG_addrsize, ARG_mem, ARG_mem_addrsize };
136+
static const mp_arg_t allowed_args[] = {
137+
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
138+
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT },
139+
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 7} },
140+
{ MP_QSTR_mem, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
141+
{ MP_QSTR_mem_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
142+
};
143+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
144+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
145+
146+
// Work out I2C bus.
147+
int i2c_id = i2c_find_peripheral(args[ARG_id].u_obj);
148+
149+
// Get static target object.
150+
machine_i2c_target_obj_t *self = (machine_i2c_target_obj_t *)&machine_i2c_target_obj[i2c_id - 1];
151+
152+
// Initialise data.
153+
MP_STATE_PORT(machine_i2c_target_mem_obj)[i2c_id - 1] = args[ARG_mem].u_obj;
154+
machine_i2c_target_data_t *data = &machine_i2c_target_data[i2c_id - 1];
155+
machine_i2c_target_data_init(data, args[ARG_mem].u_obj, args[ARG_mem_addrsize].u_int);
156+
157+
// Initialise the I2C target.
158+
mp_hal_pin_config_alt(self->scl, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, AF_FN_I2C, i2c_id);
159+
mp_hal_pin_config_alt(self->sda, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, AF_FN_I2C, i2c_id);
160+
i2c_slave_init(self->i2c, self->irqn_ev, IRQ_PRI_I2C, args[ARG_addr].u_int);
161+
NVIC_SetPriority(self->irqn_er, IRQ_PRI_I2C);
162+
NVIC_EnableIRQ(self->irqn_er);
163+
164+
i2c_target_enabled |= 1 << (i2c_id - 1);
165+
166+
return MP_OBJ_FROM_PTR(self);
167+
}
168+
169+
static void mp_machine_i2c_target_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
170+
machine_i2c_target_obj_t *self = MP_OBJ_TO_PTR(self_in);
171+
mp_printf(print, "I2CTarget(%u, addr=%u)",
172+
self - &machine_i2c_target_obj[0] + 1,
173+
(self->i2c->OAR1 >> 1) & 0x7f);
174+
}
175+
176+
static void mp_machine_i2c_target_deinit(machine_i2c_target_obj_t *self) {
177+
i2c_slave_shutdown(self->i2c, self->irqn_ev);
178+
NVIC_DisableIRQ(self->irqn_er);
179+
}

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/mboot/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
// IRQ priorities (encoded values suitable for NVIC_SetPriority)
6363
// Most values are defined in irq.h.
64+
#undef IRQ_PRI_I2C
6465
#define IRQ_PRI_I2C (NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 1, 0))
6566

6667
#if defined(MBOOT_CLK_PLLM)

0 commit comments

Comments
 (0)