From 7d6b6f66811b4424f9e353205b4416c4c64d772e Mon Sep 17 00:00:00 2001 From: Daniel Campora Date: Fri, 11 Sep 2015 09:33:19 +0200 Subject: cc3200: Make UART choose default id when not given. --- cc3200/mods/pybpin.c | 10 ++++++++ cc3200/mods/pybpin.h | 1 + cc3200/mods/pybuart.c | 65 ++++++++++++++++++++++++++++++++------------------- cc3200/qstrdefsport.h | 1 + 4 files changed, 53 insertions(+), 24 deletions(-) (limited to 'cc3200') diff --git a/cc3200/mods/pybpin.c b/cc3200/mods/pybpin.c index 12520ec68..faf81273f 100644 --- a/cc3200/mods/pybpin.c +++ b/cc3200/mods/pybpin.c @@ -174,6 +174,16 @@ void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_ } } +uint8_t pin_find_peripheral_unit (const mp_obj_t pin, uint8_t fn, uint8_t type) { + pin_obj_t *pin_o = pin_find(pin); + for (int i = 0; i < pin_o->num_afs; i++) { + if (pin_o->af_list[i].fn == fn && pin_o->af_list[i].type == type) { + return pin_o->af_list[i].unit; + } + } + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); +} + /****************************************************************************** DEFINE PRIVATE FUNCTIONS ******************************************************************************/ diff --git a/cc3200/mods/pybpin.h b/cc3200/mods/pybpin.h index 028818fa5..3d46a0b61 100644 --- a/cc3200/mods/pybpin.h +++ b/cc3200/mods/pybpin.h @@ -135,5 +135,6 @@ void pin_init0(void); void pin_config(pin_obj_t *self, int af, uint mode, uint type, int value, uint strength); pin_obj_t *pin_find(mp_obj_t user_obj); void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_t fn, uint32_t unit); +uint8_t pin_find_peripheral_unit (const mp_obj_t pin, uint8_t fn, uint8_t type); #endif // PYBPIN_H_ diff --git a/cc3200/mods/pybuart.c b/cc3200/mods/pybuart.c index d28c041f3..7fd647c1d 100644 --- a/cc3200/mods/pybuart.c +++ b/cc3200/mods/pybuart.c @@ -352,18 +352,7 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k } } -STATIC const mp_arg_t pyb_uart_init_args[] = { - { MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, }, - { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, - { MP_QSTR_parity, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, - { MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, - { MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, -}; -STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_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[MP_ARRAY_SIZE(pyb_uart_init_args)]; - mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(pyb_uart_init_args), pyb_uart_init_args, args); - +STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_arg_val_t *args) { // get the baudrate if (args[0].u_int <= 0) { goto error; @@ -445,12 +434,41 @@ error: nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } -STATIC mp_obj_t pyb_uart_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_ARRAY_SIZE(pyb_uart_init_args), true); +STATIC const mp_arg_t pyb_uart_init_args[] = { + { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 9600} }, + { MP_QSTR_bits, MP_ARG_INT, {.u_int = 8} }, + { MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_stop, MP_ARG_INT, {.u_int = 1} }, + { MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, +}; +STATIC mp_obj_t pyb_uart_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_uart_init_args)]; + mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_uart_init_args, args); // work out the uart id - int32_t uart_id = mp_obj_get_int(args[0]); + uint8_t uart_id; + if (args[0].u_obj == mp_const_none) { + if (args[5].u_obj != MP_OBJ_NULL) { + mp_obj_t *pins; + mp_uint_t n_pins = 2; + mp_obj_get_array(args[5].u_obj, &n_pins, &pins); + // check the Tx pin (or the Rx if Tx is None) + if (pins[0] == mp_const_none) { + uart_id = pin_find_peripheral_unit(pins[1], PIN_FN_UART, PIN_TYPE_UART_RX); + } else { + uart_id = pin_find_peripheral_unit(pins[0], PIN_FN_UART, PIN_TYPE_UART_TX); + } + } else { + // default id + uart_id = 0; + } + } else { + uart_id = mp_obj_get_int(args[0].u_obj); + } if (uart_id < PYB_UART_0 || uart_id > PYB_UART_1) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable)); @@ -461,18 +479,17 @@ STATIC mp_obj_t pyb_uart_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t self->base.type = &pyb_uart_type; self->uart_id = uart_id; - 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_uart_init_helper(self, n_args - 1, args + 1, &kw_args); - } + // start the peripheral + pyb_uart_init_helper(self, &args[1]); return self; } -STATIC mp_obj_t pyb_uart_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { - return pyb_uart_init_helper(args[0], n_args - 1, args + 1, kw_args); +STATIC mp_obj_t pyb_uart_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_uart_init_args) - 1]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_uart_init_args[1], args); + return pyb_uart_init_helper(pos_args[0], args); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_init_obj, 1, pyb_uart_init); diff --git a/cc3200/qstrdefsport.h b/cc3200/qstrdefsport.h index 0aa3fdff8..2495e566a 100644 --- a/cc3200/qstrdefsport.h +++ b/cc3200/qstrdefsport.h @@ -143,6 +143,7 @@ Q(init) Q(deinit) Q(any) Q(sendbreak) +Q(id) Q(baudrate) Q(bits) Q(stop) -- cgit v1.2.3