diff options
author | Damien George <damien.p.george@gmail.com> | 2016-12-05 15:31:16 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-12-05 15:31:16 +1100 |
commit | 4c7d799b8244bcffc94cb33eea3d627f51d0ce41 (patch) | |
tree | 37b6154a2d7550b502b5ab1909fce483d941b3ef | |
parent | aaab6a9921a9a874d9a974ecc5c3639002dc286b (diff) | |
download | micropython-4c7d799b8244bcffc94cb33eea3d627f51d0ce41.tar.gz micropython-4c7d799b8244bcffc94cb33eea3d627f51d0ce41.zip |
stmhal/uart: Add check that UART id is valid for the given board.
Previous to this patch trying to construct, but not init, a UART that
didn't exist on the target board would actually succeed. Only when
initialising the UART would it then raise an exception that the UART does
not exist.
This patch adds an explicit check that the constructed UART does in fact
exist for the given board.
-rw-r--r-- | stmhal/uart.c | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/stmhal/uart.c b/stmhal/uart.c index 93956f567f..784b38a684 100644 --- a/stmhal/uart.c +++ b/stmhal/uart.c @@ -109,6 +109,48 @@ void uart_deinit(void) { } } +STATIC bool uart_exists(int uart_id) { + if (uart_id > MP_ARRAY_SIZE(MP_STATE_PORT(pyb_uart_obj_all))) { + // safeguard against pyb_uart_obj_all array being configured too small + return false; + } + switch (uart_id) { + #if defined(MICROPY_HW_UART1_TX) && defined(MICROPY_HW_UART1_RX) + case PYB_UART_1: return true; + #endif + + #if defined(MICROPY_HW_UART2_TX) && defined(MICROPY_HW_UART2_RX) + case PYB_UART_2: return true; + #endif + + #if defined(MICROPY_HW_UART3_TX) && defined(MICROPY_HW_UART3_RX) + case PYB_UART_3: return true; + #endif + + #if defined(MICROPY_HW_UART4_TX) && defined(MICROPY_HW_UART4_RX) + case PYB_UART_4: return true; + #endif + + #if defined(MICROPY_HW_UART5_TX) && defined(MICROPY_HW_UART5_RX) + case PYB_UART_5: return true; + #endif + + #if defined(MICROPY_HW_UART6_TX) && defined(MICROPY_HW_UART6_RX) + case PYB_UART_6: return true; + #endif + + #if defined(MICROPY_HW_UART7_TX) && defined(MICROPY_HW_UART7_RX) + case PYB_UART_7: return true; + #endif + + #if defined(MICROPY_HW_UART8_TX) && defined(MICROPY_HW_UART8_RX) + case PYB_UART_8: return true; + #endif + + default: return false; + } +} + // assumes Init parameters have been set up correctly STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) { USART_TypeDef *UARTx; @@ -650,7 +692,7 @@ STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, mp_uint_t n_args, m } } else { uart_id = mp_obj_get_int(args[0]); - if (uart_id < 1 || uart_id > MP_ARRAY_SIZE(MP_STATE_PORT(pyb_uart_obj_all))) { + if (!uart_exists(uart_id)) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART(%d) does not exist", uart_id)); } } |