Skip to content

Commit 5d2279b

Browse files
committed
extmod/machine_i2c: Add hook to constructor to call port-specific code.
If MICROPY_PY_MACHINE_I2C_MAKE_NEW is defined then it is called when an I2C object is constructed with an id which is not -1.
1 parent 8b74048 commit 5d2279b

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

extmod/machine_i2c.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,22 @@ STATIC void machine_i2c_obj_init_helper(machine_i2c_obj_t *self, mp_uint_t n_arg
286286
}
287287

288288
STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
289-
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
289+
// check the id argument, if given
290+
if (n_args > 0) {
291+
if (args[0] != MP_OBJ_NEW_SMALL_INT(-1)) {
292+
#if defined(MICROPY_PY_MACHINE_I2C_MAKE_NEW)
293+
// dispatch to port-specific constructor
294+
extern mp_obj_t MICROPY_PY_MACHINE_I2C_MAKE_NEW(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
295+
return MICROPY_PY_MACHINE_I2C_MAKE_NEW(type, n_args, n_kw, args);
296+
#else
297+
mp_raise_ValueError("invalid I2C peripheral");
298+
#endif
299+
}
300+
--n_args;
301+
++args;
302+
}
303+
304+
// create new soft I2C object
290305
machine_i2c_obj_t *self = m_new_obj(machine_i2c_obj_t);
291306
self->base.type = &machine_i2c_type;
292307
mp_map_t kw_args;

0 commit comments

Comments
 (0)