From e77abc261bf47343d14c4b5e26c80a0136b93e7c Mon Sep 17 00:00:00 2001 From: Daniel Campora Date: Fri, 11 Sep 2015 11:05:58 +0200 Subject: [PATCH] cc3200: Default peripheral ID support on I2C. --- cc3200/mods/pybi2c.c | 65 +++++++++++++++++++++++-------------------- cc3200/qstrdefsport.h | 1 + tests/wipy/i2c.py | 7 +++++ tests/wipy/i2c.py.exp | 2 ++ tests/wipy/uart.py | 1 + 5 files changed, 46 insertions(+), 30 deletions(-) diff --git a/cc3200/mods/pybi2c.c b/cc3200/mods/pybi2c.c index 6c5f0cae54..04ed0d2c29 100644 --- a/cc3200/mods/pybi2c.c +++ b/cc3200/mods/pybi2c.c @@ -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); diff --git a/cc3200/qstrdefsport.h b/cc3200/qstrdefsport.h index 2495e566ad..c73038f68f 100644 --- a/cc3200/qstrdefsport.h +++ b/cc3200/qstrdefsport.h @@ -153,6 +153,7 @@ Q(RX_ANY) // for I2C class Q(I2C) +Q(id) Q(mode) Q(baudrate) Q(pins) diff --git a/tests/wipy/i2c.py b/tests/wipy/i2c.py index 0673b80003..9e26459473 100644 --- a/tests/wipy/i2c.py +++ b/tests/wipy/i2c.py @@ -16,6 +16,13 @@ elif 'WiPy' in machine: else: raise Exception('Board not supported!') +i2c = I2C(0, I2C.MASTER, baudrate=400000) +# try initing without the peripheral id +i2c = I2C() +print(i2c) +i2c = I2C(mode=I2C.MASTER, baudrate=50000, pins=i2c_pins) +print(i2c) + i2c = I2C(0, I2C.MASTER, baudrate=100000) print(i2c) i2c = I2C(0, mode=I2C.MASTER, baudrate=400000) diff --git a/tests/wipy/i2c.py.exp b/tests/wipy/i2c.py.exp index c50cbf0e52..54afbdbd26 100644 --- a/tests/wipy/i2c.py.exp +++ b/tests/wipy/i2c.py.exp @@ -1,4 +1,6 @@ I2C(0, I2C.MASTER, baudrate=100000) +I2C(0, I2C.MASTER, baudrate=50000) +I2C(0, I2C.MASTER, baudrate=100000) I2C(0, I2C.MASTER, baudrate=400000) I2C(0, I2C.MASTER, baudrate=400000) 104 diff --git a/tests/wipy/uart.py b/tests/wipy/uart.py index 2c92de6649..5b01c4518c 100644 --- a/tests/wipy/uart.py +++ b/tests/wipy/uart.py @@ -30,6 +30,7 @@ for uart_id in uart_id_range: uart = UART(baudrate=1000000) uart.sendbreak() +uart = UART(baudrate=1000000) uart = UART() print(uart) uart = UART(baudrate=38400, pins=('GP12', 'GP13'))