Skip to content

Commit 46236eb

Browse files
committed
stm32/machine_i2c_target: Update bindings.
Signed-off-by: Damien George <[email protected]>
1 parent d94eb1e commit 46236eb

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

ports/stm32/machine_i2c_target.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef struct _machine_i2c_target_obj_t {
4141
mp_hal_pin_obj_t sda;
4242
} machine_i2c_target_obj_t;
4343

44-
static const machine_i2c_target_obj_t machine_i2cslave_obj[] = {
44+
static const machine_i2c_target_obj_t machine_i2c_target_obj[] = {
4545
#if defined(MICROPY_HW_I2C1_SCL)
4646
{{&machine_i2c_target_type}, I2C1, I2C1_EV_IRQn, I2C1_ER_IRQn, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA},
4747
#else
@@ -64,19 +64,17 @@ static const machine_i2c_target_obj_t machine_i2cslave_obj[] = {
6464
#endif
6565
};
6666

67-
static machine_i2c_target_data_t i2c_target_data[4];
68-
6967
/******************************************************************************/
7068
// stm32 hardware bindings
7169

7270
static machine_i2c_target_obj_t *get_self(i2c_slave_t *i2c) {
7371
size_t i2c_id = ((uintptr_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
74-
return (machine_i2c_target_obj_t *)&machine_i2cslave_obj[i2c_id];
72+
return (machine_i2c_target_obj_t *)&machine_i2c_target_obj[i2c_id];
7573
}
7674

7775
static machine_i2c_target_data_t *get_data(i2c_slave_t *i2c) {
7876
size_t i2c_id = ((uintptr_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
79-
return &i2c_target_data[i2c_id];
77+
return &machine_i2c_target_data[i2c_id];
8078
}
8179

8280
int i2c_slave_process_addr_match(i2c_slave_t *i2c, int rw) {
@@ -104,6 +102,10 @@ void i2c_slave_process_tx_end(i2c_slave_t *i2c) {
104102
/******************************************************************************/
105103
// I2CTarget port implementation
106104

105+
static inline size_t mp_machine_i2c_target_get_index(machine_i2c_target_obj_t *self) {
106+
return self - &machine_i2c_target_obj[0];
107+
}
108+
107109
static void mp_machine_i2c_target_deinit_all_port(void) {
108110
}
109111

@@ -139,10 +141,6 @@ static mp_obj_t mp_machine_i2c_target_make_new(const mp_obj_type_t *type, size_t
139141
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 7} },
140142
{ MP_QSTR_mem, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
141143
{ MP_QSTR_mem_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
142-
/*
143-
{ MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
144-
{ MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
145-
*/
146144
};
147145
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
148146
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -170,20 +168,20 @@ static mp_obj_t mp_machine_i2c_target_make_new(const mp_obj_type_t *type, size_t
170168
}
171169
} else {
172170
i2c_id = mp_obj_get_int(args[ARG_id].u_obj);
173-
if (i2c_id < 1 || i2c_id > MP_ARRAY_SIZE(machine_i2cslave_obj)
174-
|| machine_i2cslave_obj[i2c_id - 1].base.type == NULL) {
171+
if (i2c_id < 1 || i2c_id > MP_ARRAY_SIZE(machine_i2c_target_obj)
172+
|| machine_i2c_target_obj[i2c_id - 1].base.type == NULL) {
175173
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
176174
MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id));
177175
}
178176
}
179177

180178
// Initialise data.
181-
MP_STATE_PORT(i2c_target_mem_obj)[i2c_id - 1] = args[ARG_mem].u_obj;
182-
machine_i2c_target_data_t *data = &i2c_target_data[i2c_id - 1];
179+
MP_STATE_PORT(machine_i2c_target_mem_obj)[i2c_id - 1] = args[ARG_mem].u_obj;
180+
machine_i2c_target_data_t *data = &machine_i2c_target_data[i2c_id - 1];
183181
machine_i2c_target_data_init(data, args[ARG_mem].u_obj, args[ARG_mem_addrsize].u_int);
184182

185183
// Get static target object.
186-
machine_i2c_target_obj_t *self = (machine_i2c_target_obj_t *)&machine_i2cslave_obj[i2c_id - 1];
184+
machine_i2c_target_obj_t *self = (machine_i2c_target_obj_t *)&machine_i2c_target_obj[i2c_id - 1];
187185

188186
// Initialise the I2C target.
189187
mp_hal_pin_config_alt(self->scl, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, AF_FN_I2C, i2c_id);
@@ -200,7 +198,7 @@ static mp_obj_t mp_machine_i2c_target_make_new(const mp_obj_type_t *type, size_t
200198
static void mp_machine_i2c_target_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
201199
machine_i2c_target_obj_t *self = MP_OBJ_TO_PTR(self_in);
202200
mp_printf(print, "I2CTarget(%u, addr=%u)",
203-
self - &machine_i2cslave_obj[0] + 1,
201+
self - &machine_i2c_target_obj[0] + 1,
204202
(self->i2c->OAR1 >> 1) & 0x7f);
205203
}
206204

ports/stm32/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@
131131
#define MICROPY_PY_MACHINE_I2C (MICROPY_HW_ENABLE_HW_I2C)
132132
#define MICROPY_PY_MACHINE_I2C_TARGET (MICROPY_HW_ENABLE_HW_I2C_TARGET)
133133
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/stm32/machine_i2c_target.c"
134+
#define MICROPY_PY_MACHINE_I2C_TARGET_MAX (4)
135+
#define MICROPY_PY_MACHINE_I2C_TARGET_HARD_IRQ (1)
134136
#define MICROPY_PY_MACHINE_SOFTI2C (1)
135137
#define MICROPY_PY_MACHINE_I2S_INCLUDEFILE "ports/stm32/machine_i2s.c"
136138
#define MICROPY_PY_MACHINE_I2S_CONSTANT_RX (I2S_MODE_MASTER_RX)

0 commit comments

Comments
 (0)