diff --git a/cc3200/mods/pybpin.c b/cc3200/mods/pybpin.c index debce55288..12520ec68f 100644 --- a/cc3200/mods/pybpin.c +++ b/cc3200/mods/pybpin.c @@ -61,6 +61,8 @@ DECLARE PRIVATE FUNCTIONS STATIC pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name); STATIC pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uint port, uint bit); STATIC int8_t pin_obj_find_af (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type); +STATIC int8_t pin_find_af_index (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type); +STATIC void pin_free_af_from_pins (uint8_t fn, uint8_t unit, uint8_t type); STATIC void pin_deassign (pin_obj_t* pin); STATIC void pin_obj_configure (const pin_obj_t *self); STATIC void pin_get_hibernate_pin_and_idx (const pin_obj_t *self, uint *wake_pin, uint *idx); @@ -162,26 +164,12 @@ void pin_config (pin_obj_t *self, int af, uint mode, uint pull, int value, uint pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)pin_obj_configure); } -int8_t pin_find_af_index (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type) { - int8_t af = pin_obj_find_af(pin, fn, unit, type); - if (af < 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); - } - return af; -} - -void pin_free_af_from_pins (uint8_t fn, uint8_t unit, uint8_t type) { - mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)&pin_board_pins_locals_dict); - for (uint i = 0; i < named_map->used - 1; i++) { - pin_obj_t * pin = (pin_obj_t *)named_map->table[i].value; - // af is different than GPIO - if (pin->af > PIN_MODE_0) { - // check if the pin supports the target af - int af = pin_obj_find_af(pin, fn, unit, type); - if (af > 0 && af == pin->af) { - // the pin is assigned to the target af, de-assign it - pin_deassign (pin); - } +void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_t fn, uint32_t unit) { + for (int i = 0; i < n_pins; i++) { + pin_free_af_from_pins(fn, unit, i); + if (pins[i] != mp_const_none) { + pin_obj_t *pin = pin_find(pins[i]); + pin_config (pin, pin_find_af_index(pin, fn, unit, i), 0, pull, -1, PIN_STRENGTH_2MA); } } } @@ -218,6 +206,30 @@ STATIC int8_t pin_obj_find_af (const pin_obj_t* pin, uint8_t fn, uint8_t unit, u return -1; } +STATIC int8_t pin_find_af_index (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type) { + int8_t af = pin_obj_find_af(pin, fn, unit, type); + if (af < 0) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + } + return af; +} + +STATIC void pin_free_af_from_pins (uint8_t fn, uint8_t unit, uint8_t type) { + mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)&pin_board_pins_locals_dict); + for (uint i = 0; i < named_map->used - 1; i++) { + pin_obj_t * pin = (pin_obj_t *)named_map->table[i].value; + // af is different than GPIO + if (pin->af > PIN_MODE_0) { + // check if the pin supports the target af + int af = pin_obj_find_af(pin, fn, unit, type); + if (af > 0 && af == pin->af) { + // the pin is assigned to the target af, de-assign it + pin_deassign (pin); + } + } + } +} + STATIC void pin_deassign (pin_obj_t* pin) { pin_config (pin, PIN_MODE_0, GPIO_DIR_MODE_IN, PIN_TYPE_STD, -1, PIN_STRENGTH_4MA); pin->used = false; diff --git a/cc3200/mods/pybpin.h b/cc3200/mods/pybpin.h index f03a998f53..028818fa51 100644 --- a/cc3200/mods/pybpin.h +++ b/cc3200/mods/pybpin.h @@ -115,11 +115,6 @@ typedef struct { uint8_t used : 1; } pin_obj_t; -typedef struct { - pin_obj_t *pin; - uint8_t af_idx; -} pin_fn_t; - extern const mp_obj_type_t pin_type; typedef struct { @@ -139,7 +134,6 @@ extern const mp_obj_dict_t pin_board_pins_locals_dict; 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); -int8_t pin_find_af_index(const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type); -void pin_free_af_from_pins (uint8_t fn, uint8_t unit, uint8_t type); +void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_t fn, uint32_t unit); #endif // PYBPIN_H_ diff --git a/cc3200/mods/pybuart.c b/cc3200/mods/pybuart.c index 00f009ce67..b4a5606577 100644 --- a/cc3200/mods/pybuart.c +++ b/cc3200/mods/pybuart.c @@ -115,8 +115,7 @@ STATIC pyb_uart_obj_t pyb_uart_obj[PYB_NUM_UARTS] = { {.reg = UARTA0_BASE, .baud {.reg = UARTA1_BASE, .baudrate = 0, .read_buf = NULL, .peripheral = PRCM_UARTA1} }; STATIC const mp_cb_methods_t uart_cb_methods; -STATIC const pin_fn_t pyb_uart_def_pin[PYB_NUM_UARTS][2] = { {{.pin = &pin_GP1, .af_idx = 3}, {.pin = &pin_GP2, .af_idx = 3}}, - {{.pin = &pin_GP3, .af_idx = 6}, {.pin = &pin_GP4, .af_idx = 6}} }; +STATIC const mp_obj_t pyb_uart_def_pin[PYB_NUM_UARTS][2] = { {&pin_GP1, &pin_GP2}, {&pin_GP3, &pin_GP4} }; /****************************************************************************** DEFINE PUBLIC FUNCTIONS @@ -397,46 +396,36 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con // stop bits config |= (args[3].u_int == 1 ? UART_CONFIG_STOP_ONE : UART_CONFIG_STOP_TWO); + // assign the pins mp_obj_t pins_o = args[4].u_obj; uint flowcontrol = UART_FLOWCONTROL_NONE; if (pins_o != mp_const_none) { + mp_obj_t *pins; + mp_uint_t n_pins; if (pins_o == MP_OBJ_NULL) { // use the default pins - pin_config (pyb_uart_def_pin[self->uart_id][PIN_TYPE_UART_TX].pin, pyb_uart_def_pin[self->uart_id][PIN_TYPE_UART_TX].af_idx, - 0, PIN_TYPE_STD_PU, -1, PIN_STRENGTH_2MA); - pin_config (pyb_uart_def_pin[self->uart_id][PIN_TYPE_UART_RX].pin, pyb_uart_def_pin[self->uart_id][PIN_TYPE_UART_RX].af_idx, - 0, PIN_TYPE_STD_PU, -1, PIN_STRENGTH_2MA); + pins = (mp_obj_t *)pyb_uart_def_pin[self->uart_id]; } else { - mp_obj_t *pins_t; - mp_uint_t n_pins; - mp_obj_get_array(pins_o, &n_pins, &pins_t); + mp_obj_get_array(pins_o, &n_pins, &pins); if (n_pins != 2 && n_pins != 4) { goto error; } if (n_pins == 4) { - if (pins_t[PIN_TYPE_UART_RTS] != mp_const_none && pins_t[PIN_TYPE_UART_RX] == mp_const_none) { + if (pins[PIN_TYPE_UART_RTS] != mp_const_none && pins[PIN_TYPE_UART_RX] == mp_const_none) { goto error; // RTS pin given in TX only mode - } else if (pins_t[PIN_TYPE_UART_CTS] != mp_const_none && pins_t[PIN_TYPE_UART_TX] == mp_const_none) { + } else if (pins[PIN_TYPE_UART_CTS] != mp_const_none && pins[PIN_TYPE_UART_TX] == mp_const_none) { goto error; // CTS pin given in RX only mode } else { - if (pins_t[PIN_TYPE_UART_RTS] != mp_const_none) { + if (pins[PIN_TYPE_UART_RTS] != mp_const_none) { flowcontrol |= UART_FLOWCONTROL_RX; } - if (pins_t[PIN_TYPE_UART_CTS] != mp_const_none) { + if (pins[PIN_TYPE_UART_CTS] != mp_const_none) { flowcontrol |= UART_FLOWCONTROL_TX; } } } - // the pins tuple passed looks good so far - for (int i = 0; i < n_pins; i++) { - pin_free_af_from_pins(PIN_FN_UART, self->uart_id, i); - if (pins_t[i] != mp_const_none) { - pin_obj_t *pin = pin_find(pins_t[i]); - pin_config (pin, pin_find_af_index(pin, PIN_FN_UART, self->uart_id, i), - 0, PIN_TYPE_STD_PU, -1, PIN_STRENGTH_2MA); - } - } } + pin_assign_pins_af (pins, n_pins, PIN_TYPE_STD_PU, PIN_FN_UART, self->uart_id); } self->baudrate = baudrate;