|
|
@ -279,19 +279,7 @@ STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// \method init()
|
|
|
|
STATIC const mp_arg_t pyb_i2c_init_args[] = { |
|
|
|
{ MP_QSTR_mode, MP_ARG_INT, {.u_int = PYBI2C_MASTER} }, |
|
|
|
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} }, |
|
|
|
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
|
|
|
}; |
|
|
|
#define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args) |
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[PYB_I2C_INIT_NUM_ARGS]; |
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, PYB_I2C_INIT_NUM_ARGS, pyb_i2c_init_args, args); |
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_arg_val_t *args) { |
|
|
|
// verify that mode is master
|
|
|
|
if (args[0].u_int != PYBI2C_MASTER) { |
|
|
|
goto invalid_args; |
|
|
@ -329,32 +317,49 @@ invalid_args: |
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); |
|
|
|
} |
|
|
|
|
|
|
|
/// \classmethod \constructor(bus, ...)
|
|
|
|
STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
|
|
|
// check arguments
|
|
|
|
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); |
|
|
|
|
|
|
|
// setup the object
|
|
|
|
pyb_i2c_obj_t *self = &pyb_i2c_obj; |
|
|
|
self->base.type = &pyb_i2c_type; |
|
|
|
STATIC const mp_arg_t pyb_i2c_init_args[] = { |
|
|
|
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
|
|
|
{ MP_QSTR_mode, MP_ARG_INT, {.u_int = PYBI2C_MASTER} }, |
|
|
|
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} }, |
|
|
|
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
|
|
|
}; |
|
|
|
#define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args) |
|
|
|
STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) { |
|
|
|
// parse args
|
|
|
|
mp_map_t kw_args; |
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args); |
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_init_args)]; |
|
|
|
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_i2c_init_args, args); |
|
|
|
|
|
|
|
// work out the uart id
|
|
|
|
uint8_t i2c_id; |
|
|
|
if (args[0].u_obj == mp_const_none) { |
|
|
|
// default id
|
|
|
|
i2c_id = 0; |
|
|
|
} else { |
|
|
|
i2c_id = mp_obj_get_int(args[0].u_obj); |
|
|
|
} |
|
|
|
|
|
|
|
// check the peripheral id
|
|
|
|
if (mp_obj_get_int(args[0]) != 0) { |
|
|
|
if (i2c_id != 0) { |
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable)); |
|
|
|
} |
|
|
|
|
|
|
|
if (n_args > 1 || n_kw > 0) { |
|
|
|
// start the peripheral
|
|
|
|
mp_map_t kw_args; |
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
|
|
|
pyb_i2c_init_helper(self, n_args - 1, args + 1, &kw_args); |
|
|
|
} |
|
|
|
// setup the object
|
|
|
|
pyb_i2c_obj_t *self = &pyb_i2c_obj; |
|
|
|
self->base.type = &pyb_i2c_type; |
|
|
|
|
|
|
|
// start the peripheral
|
|
|
|
pyb_i2c_init_helper(self, &args[1]); |
|
|
|
|
|
|
|
return (mp_obj_t)self; |
|
|
|
} |
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { |
|
|
|
return pyb_i2c_init_helper(args[0], n_args - 1, args + 1, kw_args); |
|
|
|
STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_init_args) - 1]; |
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_i2c_init_args[1], args); |
|
|
|
return pyb_i2c_init_helper(pos_args[0], args); |
|
|
|
} |
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init); |
|
|
|
|
|
|
|