diff options
| author | Daniel Campora | 2015-09-07 21:19:11 +0200 |
|---|---|---|
| committer | Daniel Campora | 2015-09-10 08:00:12 +0200 |
| commit | 4d7fa05b43cb3db88a6ecacf8406d2636f202aba (patch) | |
| tree | e9333fd94b157e2274cad16c53d2900216099a43 /cc3200 | |
| parent | 4054c4eadd13fa3aaacb96202cdfb1a666484b5c (diff) | |
cc3200: Improve Pin and UART implementation.
Deassign pins af before assigning. Make uart.any() return the
correct value everytime, this requires interrupts to be always
enabled.
Diffstat (limited to 'cc3200')
| -rw-r--r-- | cc3200/mods/pybpin.c | 64 | ||||
| -rw-r--r-- | cc3200/mods/pybpin.h | 1 | ||||
| -rw-r--r-- | cc3200/mods/pybuart.c | 42 | ||||
| -rw-r--r-- | cc3200/mods/pybuart.h | 6 | ||||
| -rw-r--r-- | cc3200/mptask.c | 1 |
5 files changed, 96 insertions, 18 deletions
diff --git a/cc3200/mods/pybpin.c b/cc3200/mods/pybpin.c index cae2f6894..debce5528 100644 --- a/cc3200/mods/pybpin.c +++ b/cc3200/mods/pybpin.c @@ -60,6 +60,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 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); STATIC void pin_extint_enable (mp_obj_t self_in); @@ -68,6 +70,7 @@ STATIC void pin_extint_register(pin_obj_t *self, uint32_t intmode, uint32_t prio STATIC void pin_validate_mode (uint mode); STATIC void pin_validate_pull (uint pull); STATIC void pin_validate_drive (uint strength); +STATIC void pin_validate_af(const pin_obj_t* pin, int8_t idx, uint8_t *fn, uint8_t *unit, uint8_t *type); STATIC void GPIOA0IntHandler (void); STATIC void GPIOA1IntHandler (void); STATIC void GPIOA2IntHandler (void); @@ -115,9 +118,7 @@ void pin_init0(void) { 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; - pin_config (pin, PIN_MODE_0, GPIO_DIR_MODE_IN, PIN_TYPE_STD, -1, PIN_STRENGTH_2MA); - // mark it as unused again - pin->used = false; + pin_deassign (pin); } #endif } @@ -147,10 +148,12 @@ void pin_config (pin_obj_t *self, int af, uint mode, uint pull, int value, uint if (af != -1) { self->af = af; } + // if value is -1, then we want to keep it as it is if (value != -1) { self->value = value; } + // mark the pin as used self->used = true; pin_obj_configure ((const pin_obj_t *)self); @@ -160,12 +163,27 @@ void pin_config (pin_obj_t *self, int af, uint mode, uint pull, int value, uint } int8_t pin_find_af_index (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type) { - for (int i = 0; i < pin->num_afs; i++) { - if (pin->af_list[i].fn == fn && pin->af_list[i].unit == unit && pin->af_list[i].type == type) { - return pin->af_list[i].idx; + 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); + } } } - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } /****************************************************************************** @@ -191,6 +209,20 @@ STATIC pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uin return NULL; } +STATIC int8_t pin_obj_find_af (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type) { + for (int i = 0; i < pin->num_afs; i++) { + if (pin->af_list[i].fn == fn && pin->af_list[i].unit == unit && pin->af_list[i].type == type) { + return pin->af_list[i].idx; + } + } + return -1; +} + +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; +} + STATIC void pin_obj_configure (const pin_obj_t *self) { uint32_t type; if (self->mode == PIN_TYPE_ANALOG) { @@ -370,6 +402,18 @@ STATIC void pin_validate_drive(uint strength) { } } +STATIC void pin_validate_af(const pin_obj_t* pin, int8_t idx, uint8_t *fn, uint8_t *unit, uint8_t *type) { + for (int i = 0; i < pin->num_afs; i++) { + if (pin->af_list[i].idx == idx) { + *fn = pin->af_list[i].fn; + *unit = pin->af_list[i].unit; + *type = pin->af_list[i].type; + return; + } + } + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); +} + STATIC void GPIOA0IntHandler (void) { EXTI_Handler(GPIOA0_BASE); } @@ -455,6 +499,12 @@ STATIC mp_obj_t pin_obj_init_helper(pin_obj_t *self, mp_uint_t n_args, const mp_ goto invalid_args; } + // check for a valid af and then free it from any other pins + if (af > PIN_MODE_0) { + uint8_t fn, unit, type; + pin_validate_af (self, af, &fn, &unit, &type); + pin_free_af_from_pins(fn, unit, type); + } pin_config (self, af, mode, pull, value, strength); return mp_const_none; diff --git a/cc3200/mods/pybpin.h b/cc3200/mods/pybpin.h index 4c49e63ab..f03a998f5 100644 --- a/cc3200/mods/pybpin.h +++ b/cc3200/mods/pybpin.h @@ -140,5 +140,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); 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); #endif // PYBPIN_H_ diff --git a/cc3200/mods/pybuart.c b/cc3200/mods/pybuart.c index cb010395b..00f009ce6 100644 --- a/cc3200/mods/pybuart.c +++ b/cc3200/mods/pybuart.c @@ -71,11 +71,18 @@ #define PYBUART_RX_BUFFER_LEN (128) +// interrupt triggers +#define E_UART_TRIGGER_RX_ANY (0x01) +#define E_UART_TRIGGER_RX_HALF (0x02) +#define E_UART_TRIGGER_RX_FULL (0x04) +#define E_UART_TRIGGER_TX_DONE (0x08) + /****************************************************************************** DECLARE PRIVATE FUNCTIONS ******************************************************************************/ STATIC void uart_init (pyb_uart_obj_t *self); STATIC bool uart_rx_wait (pyb_uart_obj_t *self); +STATIC void uart_check_init(pyb_uart_obj_t *self); STATIC void UARTGenericIntHandler(uint32_t uart_id); STATIC void UART0IntHandler(void); STATIC void UART1IntHandler(void); @@ -98,6 +105,7 @@ struct _pyb_uart_obj_t { uint16_t read_buf_tail; // indexes first full slot (not full if equals head) byte peripheral; byte irq_trigger; + bool callback_enabled; }; /****************************************************************************** @@ -244,6 +252,7 @@ STATIC bool uart_rx_wait (pyb_uart_obj_t *self) { STATIC void UARTGenericIntHandler(uint32_t uart_id) { pyb_uart_obj_t *self; uint32_t status; + bool exec_callback = false; self = &pyb_uart_obj[uart_id]; status = MAP_UARTIntStatus(self->reg, true); @@ -266,9 +275,23 @@ STATIC void UARTGenericIntHandler(uint32_t uart_id) { } } } - // call the user defined handler - mp_obj_t _callback = mpcallback_find(self); - mpcallback_handler(_callback); + + if (self->irq_trigger & E_UART_TRIGGER_RX_ANY) { + exec_callback = true; + } + + if (exec_callback && self->callback_enabled) { + // call the user defined handler + mp_obj_t _callback = mpcallback_find(self); + mpcallback_handler(_callback); + } + } +} + +STATIC void uart_check_init(pyb_uart_obj_t *self) { + // not initialized + if (!self->baudrate) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible)); } } @@ -287,11 +310,12 @@ STATIC void uart_callback_enable (mp_obj_t self_in) { MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT); MAP_UARTIntEnable(self->reg, UART_INT_RX | UART_INT_RT); } + self->callback_enabled = true; } STATIC void uart_callback_disable (mp_obj_t self_in) { pyb_uart_obj_t *self = self_in; - MAP_UARTIntDisable(self->reg, UART_INT_RX | UART_INT_RT); + self->callback_enabled = false; } /******************************************************************************/ @@ -405,6 +429,7 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con } // 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), @@ -422,6 +447,8 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con uart_init (self); // register it with the sleep module pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)uart_init); + // enable the callback + uart_callback_new (self, mp_const_none, INT_PRIORITY_LVL_3, E_UART_TRIGGER_RX_ANY); return mp_const_none; @@ -478,12 +505,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_deinit_obj, pyb_uart_deinit); STATIC mp_obj_t pyb_uart_any(mp_obj_t self_in) { pyb_uart_obj_t *self = self_in; + uart_check_init(self); return mp_obj_new_int(uart_rx_any(self)); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_any_obj, pyb_uart_any); STATIC mp_obj_t pyb_uart_sendbreak(mp_obj_t self_in) { pyb_uart_obj_t *self = self_in; + uart_check_init(self); // send a break signal for at least 2 complete frames MAP_UARTBreakCtl(self->reg, true); UtilsDelay(UTILS_DELAY_US_TO_COUNT(PYBUART_2_FRAMES_TIME_US(self->baudrate))); @@ -498,6 +527,7 @@ STATIC mp_obj_t pyb_uart_callback (mp_uint_t n_args, const mp_obj_t *pos_args, m // check if any parameters were passed pyb_uart_obj_t *self = pos_args[0]; + uart_check_init(self); mp_obj_t _callback = mpcallback_find((mp_obj_t)self); if (kw_args->used > 0) { @@ -547,6 +577,7 @@ STATIC MP_DEFINE_CONST_DICT(pyb_uart_locals_dict, pyb_uart_locals_dict_table); STATIC mp_uint_t pyb_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { pyb_uart_obj_t *self = self_in; byte *buf = buf_in; + uart_check_init(self); // make sure we want at least 1 char if (size == 0) { @@ -574,6 +605,7 @@ STATIC mp_uint_t pyb_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, i STATIC mp_uint_t pyb_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { pyb_uart_obj_t *self = self_in; const char *buf = buf_in; + uart_check_init(self); // write the data if (!uart_tx_strn(self, buf, size)) { @@ -585,6 +617,8 @@ STATIC mp_uint_t pyb_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { pyb_uart_obj_t *self = self_in; mp_uint_t ret; + uart_check_init(self); + if (request == MP_IOCTL_POLL) { mp_uint_t flags = arg; ret = 0; diff --git a/cc3200/mods/pybuart.h b/cc3200/mods/pybuart.h index 476fc11a4..89f3edc0e 100644 --- a/cc3200/mods/pybuart.h +++ b/cc3200/mods/pybuart.h @@ -28,12 +28,6 @@ #ifndef PYBUART_H_ #define PYBUART_H_ -// interrupt triggers -#define E_UART_TRIGGER_RX_ANY (0x01) -#define E_UART_TRIGGER_RX_HALF (0x02) -#define E_UART_TRIGGER_RX_FULL (0x04) -#define E_UART_TRIGGER_TX_DONE (0x08) - typedef enum { PYB_UART_0 = 0, PYB_UART_1 = 1, diff --git a/cc3200/mptask.c b/cc3200/mptask.c index 87990b43c..dd3a76224 100644 --- a/cc3200/mptask.c +++ b/cc3200/mptask.c @@ -145,7 +145,6 @@ soft_reset: mp_obj_new_int(MICROPY_STDIO_UART_BAUD), }; pyb_stdio_uart = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args); - uart_callback_new (pyb_stdio_uart, mp_const_none, INT_PRIORITY_LVL_3, E_UART_TRIGGER_RX_ANY); #else pyb_stdio_uart = MP_OBJ_NULL; #endif |
