From 0efd33f010ebc61e90d833b05e809e1ee7dfda68 Mon Sep 17 00:00:00 2001 From: bcostm Date: Thu, 25 Jan 2018 13:09:57 +0100 Subject: [PATCH 01/18] STM32 serial: move init_uart function at the end of file --- targets/TARGET_STM/serial_api.c | 73 ++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/targets/TARGET_STM/serial_api.c b/targets/TARGET_STM/serial_api.c index 27072730ff..4454f6e742 100644 --- a/targets/TARGET_STM/serial_api.c +++ b/targets/TARGET_STM/serial_api.c @@ -38,6 +38,9 @@ serial_t stdio_uart; extern UART_HandleTypeDef uart_handlers[]; extern uint32_t serial_irq_ids[]; +// Utility functions +HAL_StatusTypeDef init_uart(serial_t *obj); + void serial_init(serial_t *obj, PinName tx, PinName rx) { struct serial_s *obj_s = SERIAL_S(obj); @@ -202,7 +205,6 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) IndexNumber++; #endif - #if defined(LPUART1_BASE) if (obj_s->uart == LPUART_1) { __HAL_RCC_LPUART1_FORCE_RESET(); @@ -423,38 +425,6 @@ void serial_baud(serial_t *obj, int baudrate) } } -HAL_StatusTypeDef init_uart(serial_t *obj) -{ - struct serial_s *obj_s = SERIAL_S(obj); - UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; - huart->Instance = (USART_TypeDef *)(obj_s->uart); - - huart->Init.BaudRate = obj_s->baudrate; - huart->Init.WordLength = obj_s->databits; - huart->Init.StopBits = obj_s->stopbits; - huart->Init.Parity = obj_s->parity; -#if DEVICE_SERIAL_FC - huart->Init.HwFlowCtl = obj_s->hw_flow_ctl; -#else - huart->Init.HwFlowCtl = UART_HWCONTROL_NONE; -#endif - huart->Init.OverSampling = UART_OVERSAMPLING_16; - huart->TxXferCount = 0; - huart->TxXferSize = 0; - huart->RxXferCount = 0; - huart->RxXferSize = 0; - - if (obj_s->pin_rx == NC) { - huart->Init.Mode = UART_MODE_TX; - } else if (obj_s->pin_tx == NC) { - huart->Init.Mode = UART_MODE_RX; - } else { - huart->Init.Mode = UART_MODE_TX_RX; - } - - return HAL_UART_Init(huart); -} - void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { struct serial_s *obj_s = SERIAL_S(obj); @@ -550,4 +520,41 @@ void serial_break_clear(serial_t *obj) (void)obj; } +/****************************************************************************** + * UTILITY FUNCTIONS + ******************************************************************************/ + +HAL_StatusTypeDef init_uart(serial_t *obj) +{ + struct serial_s *obj_s = SERIAL_S(obj); + UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; + huart->Instance = (USART_TypeDef *)(obj_s->uart); + + huart->Init.BaudRate = obj_s->baudrate; + huart->Init.WordLength = obj_s->databits; + huart->Init.StopBits = obj_s->stopbits; + huart->Init.Parity = obj_s->parity; +#if DEVICE_SERIAL_FC + huart->Init.HwFlowCtl = obj_s->hw_flow_ctl; +#else + huart->Init.HwFlowCtl = UART_HWCONTROL_NONE; +#endif + huart->Init.OverSampling = UART_OVERSAMPLING_16; + huart->TxXferCount = 0; + huart->TxXferSize = 0; + huart->RxXferCount = 0; + huart->RxXferSize = 0; + + if (obj_s->pin_rx == NC) { + huart->Init.Mode = UART_MODE_TX; + } else if (obj_s->pin_tx == NC) { + huart->Init.Mode = UART_MODE_RX; + } else { + huart->Init.Mode = UART_MODE_TX_RX; + } + + return HAL_UART_Init(huart); +} + + #endif /* DEVICE_SERIAL */ From 066da18e0d589b84ee0aad622e0e227dbb035284 Mon Sep 17 00:00:00 2001 From: bcostm Date: Thu, 25 Jan 2018 13:26:36 +0100 Subject: [PATCH 02/18] STM32 serial: add get_uart_index utility function --- targets/TARGET_STM/serial_api.c | 82 +++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/targets/TARGET_STM/serial_api.c b/targets/TARGET_STM/serial_api.c index 4454f6e742..dcafe64894 100644 --- a/targets/TARGET_STM/serial_api.c +++ b/targets/TARGET_STM/serial_api.c @@ -556,5 +556,87 @@ HAL_StatusTypeDef init_uart(serial_t *obj) return HAL_UART_Init(huart); } +// Warning: the list of UART/USART in this function must be aligned with the list +// written in serial_init function. +inline int8_t get_uart_index(int uart_base) +{ + uint8_t index = 0; +#if defined(USART1_BASE) + if (uart_base == USART1_BASE) return index; + index++; +#endif + +#if defined(USART2_BASE) + if (uart_base == USART2_BASE) return index; + index++; +#endif + +#if defined(USART3_BASE) + if (uart_base == USART3_BASE) return index; + index++; +#endif + +#if defined(UART4_BASE) + if (uart_base == UART4_BASE) return index; + index++; +#endif + +#if defined(USART4_BASE) + if (uart_base == USART4_BASE) return index; + index++; +#endif + +#if defined(UART5_BASE) + if (uart_base == UART5_BASE) return index; + index++; +#endif + +#if defined(USART5_BASE) + if (uart_base == USART5_BASE) return index; + index++; +#endif + +#if defined(USART6_BASE) + if (uart_base == USART6_BASE) return index; + index++; +#endif + +#if defined(UART7_BASE) + if (uart_base == UART7_BASE) return index; + index++; +#endif + +#if defined(USART7_BASE) + if (uart_base == USART7_BASE) return index; + index++; +#endif + +#if defined(UART8_BASE) + if (uart_base == UART8_BASE) return index; + index++; +#endif + +#if defined(USART8_BASE) + if (uart_base == USART8_BASE) return index; + index++; +#endif + +#if defined(UART9_BASE) + if (uart_base == UART9_BASE) return index; + index++; +#endif + +#if defined(UART10_BASE) + if (uart_base == UART10_BASE) return index; + index++; +#endif + +#if defined(LPUART1_BASE) + if (uart_base == LPUART1_BASE) return index; + index++; +#endif + + return -1; +} #endif /* DEVICE_SERIAL */ From 172c02a856f0ab9f21bd9bf7209eee3eec95cedf Mon Sep 17 00:00:00 2001 From: bcostm Date: Thu, 25 Jan 2018 13:49:45 +0100 Subject: [PATCH 03/18] STM32 serial: use get_uart_index function for F4 devices --- .../TARGET_STM/TARGET_STM32F4/serial_device.c | 62 +++++++++++-------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c index 09e326fac7..800ca94c7a 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c @@ -49,97 +49,106 @@ UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; +// Defined in serial_api.c +inline int8_t get_uart_index(int uart_base); + /****************************************************************************** * INTERRUPTS HANDLING ******************************************************************************/ -static void uart_irq(int id) +static void uart_irq(int uart_base) { - UART_HandleTypeDef * huart = &uart_handlers[id]; - - if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) { - irq_handler(serial_irq_ids[id], TxIrq); + int8_t id = get_uart_index(uart_base); + if (id >= 0) { + UART_HandleTypeDef * huart = &uart_handlers[id]; + if (serial_irq_ids[id] != 0) { + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) { + irq_handler(serial_irq_ids[id], TxIrq); + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { - irq_handler(serial_irq_ids[id], RxIrq); - /* Flag has been cleared when reading the content */ + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { + irq_handler(serial_irq_ids[id], RxIrq); + /* Flag has been cleared when reading the content */ + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { - volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->DR; // Clear ORE flag + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { + volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->DR; // Clear ORE flag + } } } } } +#if defined(USART1_BASE) static void uart1_irq(void) { - uart_irq(0); + uart_irq(USART1_BASE); } +#endif +#if defined(USART2_BASE) static void uart2_irq(void) { - uart_irq(1); + uart_irq(USART2_BASE); } +#endif #if defined(USART3_BASE) static void uart3_irq(void) { - uart_irq(2); + uart_irq(USART3_BASE); } #endif #if defined(UART4_BASE) static void uart4_irq(void) { - uart_irq(3); + uart_irq(UART4_BASE); } #endif #if defined(UART5_BASE) static void uart5_irq(void) { - uart_irq(4); + uart_irq(UART5_BASE); } #endif #if defined(USART6_BASE) static void uart6_irq(void) { - uart_irq(5); + uart_irq(USART6_BASE); } #endif #if defined(UART7_BASE) static void uart7_irq(void) { - uart_irq(6); + uart_irq(UART7_BASE); } #endif #if defined(UART8_BASE) static void uart8_irq(void) { - uart_irq(7); + uart_irq(UART8_BASE); } #endif #if defined(UART9_BASE) static void uart9_irq(void) { - uart_irq(8); + uart_irq(UART9_BASE); } #endif #if defined(UART10_BASE) static void uart10_irq(void) { - uart_irq(9); + uart_irq(UART10_BASE); } #endif @@ -163,7 +172,6 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) irq_n = USART1_IRQn; vector = (uint32_t)&uart1_irq; break; - case 1: irq_n = USART2_IRQn; vector = (uint32_t)&uart2_irq; From 7256af0d5b29ff252ddfab569531300e9cd2f3f7 Mon Sep 17 00:00:00 2001 From: bcostm Date: Thu, 25 Jan 2018 14:58:22 +0100 Subject: [PATCH 04/18] STM32 serial: change serial_get_irq_n function for F4 devices --- .../TARGET_STM/TARGET_STM32F4/serial_device.c | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c index 800ca94c7a..ceb7e4913a 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c @@ -50,7 +50,7 @@ UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; // Defined in serial_api.c -inline int8_t get_uart_index(int uart_base); +int8_t get_uart_index(int uart_base); /****************************************************************************** * INTERRUPTS HANDLING @@ -367,69 +367,68 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable) /** * Get index of serial object TX IRQ, relating it to the physical peripheral. * -* @param obj pointer to serial object +* @param uart_name i.e. UART_1, UART_2, ... * @return internal NVIC TX IRQ index of U(S)ART peripheral */ -static IRQn_Type serial_get_irq_n(serial_t *obj) +static IRQn_Type serial_get_irq_n(UARTName uart_name) { - struct serial_s *obj_s = SERIAL_S(obj); IRQn_Type irq_n; - switch (obj_s->index) { + switch (uart_name) { #if defined(USART1_BASE) - case 0: + case UART_1: irq_n = USART1_IRQn; break; #endif #if defined(USART2_BASE) - case 1: + case UART_2: irq_n = USART2_IRQn; break; #endif #if defined(USART3_BASE) - case 2: + case UART_3: irq_n = USART3_IRQn; break; #endif #if defined(UART4_BASE) - case 3: + case UART_4: irq_n = UART4_IRQn; break; #endif -#if defined(USART5_BASE) - case 4: +#if defined(UART5_BASE) + case UART_5: irq_n = UART5_IRQn; break; #endif #if defined(USART6_BASE) - case 5: + case UART_6: irq_n = USART6_IRQn; break; #endif #if defined(UART7_BASE) - case 6: + case UART_7: irq_n = UART7_IRQn; break; #endif #if defined(UART8_BASE) - case 7: + case UART_8: irq_n = UART8_IRQn; break; #endif #if defined(UART9_BASE) - case 8: + case UART_9: irq_n = UART9_IRQn; break; #endif #if defined(UART10_BASE) - case 9: + case UART_10: irq_n = UART10_IRQn; break; #endif default: irq_n = (IRQn_Type)0; } - + return irq_n; } @@ -474,7 +473,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx serial_enable_event(obj, event, 1); // Set only the wanted events // Enable interrupt - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 1); @@ -524,7 +523,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt serial_rx_buffer_set(obj, rx, rx_length, rx_width); - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 0); From e446c265848ebafa2be665758e24550dfbf78586 Mon Sep 17 00:00:00 2001 From: bcostm Date: Thu, 25 Jan 2018 15:23:14 +0100 Subject: [PATCH 05/18] STM32 serial: use uart_name instead of uart_base --- .../TARGET_STM/TARGET_STM32F4/serial_device.c | 25 +++++++------- targets/TARGET_STM/serial_api.c | 33 ++++++++++--------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c index ceb7e4913a..03a9884cf7 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c @@ -56,9 +56,10 @@ int8_t get_uart_index(int uart_base); * INTERRUPTS HANDLING ******************************************************************************/ -static void uart_irq(int uart_base) +static void uart_irq(UARTName uart_name) { - int8_t id = get_uart_index(uart_base); + int8_t id = get_uart_index(uart_name); + if (id >= 0) { UART_HandleTypeDef * huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { @@ -85,70 +86,70 @@ static void uart_irq(int uart_base) #if defined(USART1_BASE) static void uart1_irq(void) { - uart_irq(USART1_BASE); + uart_irq(UART_1); } #endif #if defined(USART2_BASE) static void uart2_irq(void) { - uart_irq(USART2_BASE); + uart_irq(UART_2); } #endif #if defined(USART3_BASE) static void uart3_irq(void) { - uart_irq(USART3_BASE); + uart_irq(UART_3); } #endif #if defined(UART4_BASE) static void uart4_irq(void) { - uart_irq(UART4_BASE); + uart_irq(UART_4); } #endif #if defined(UART5_BASE) static void uart5_irq(void) { - uart_irq(UART5_BASE); + uart_irq(UART_5); } #endif #if defined(USART6_BASE) static void uart6_irq(void) { - uart_irq(USART6_BASE); + uart_irq(UART_6); } #endif #if defined(UART7_BASE) static void uart7_irq(void) { - uart_irq(UART7_BASE); + uart_irq(UART_7); } #endif #if defined(UART8_BASE) static void uart8_irq(void) { - uart_irq(UART8_BASE); + uart_irq(UART_8); } #endif #if defined(UART9_BASE) static void uart9_irq(void) { - uart_irq(UART9_BASE); + uart_irq(UART_9); } #endif #if defined(UART10_BASE) static void uart10_irq(void) { - uart_irq(UART10_BASE); + uart_irq(UART_10); } #endif diff --git a/targets/TARGET_STM/serial_api.c b/targets/TARGET_STM/serial_api.c index dcafe64894..439f7df9ec 100644 --- a/targets/TARGET_STM/serial_api.c +++ b/targets/TARGET_STM/serial_api.c @@ -558,81 +558,82 @@ HAL_StatusTypeDef init_uart(serial_t *obj) // Warning: the list of UART/USART in this function must be aligned with the list // written in serial_init function. -inline int8_t get_uart_index(int uart_base) +inline int8_t get_uart_index(UARTName uart_name) { uint8_t index = 0; + #if defined(USART1_BASE) - if (uart_base == USART1_BASE) return index; + if (uart_name == UART_1) return index; index++; #endif #if defined(USART2_BASE) - if (uart_base == USART2_BASE) return index; + if (uart_name == UART_2) return index; index++; #endif #if defined(USART3_BASE) - if (uart_base == USART3_BASE) return index; + if (uart_name == UART_3) return index; index++; #endif #if defined(UART4_BASE) - if (uart_base == UART4_BASE) return index; + if (uart_name == UART_4) return index; index++; #endif #if defined(USART4_BASE) - if (uart_base == USART4_BASE) return index; + if (uart_name == UART_4) return index; index++; #endif #if defined(UART5_BASE) - if (uart_base == UART5_BASE) return index; + if (uart_name == UART_5) return index; index++; #endif #if defined(USART5_BASE) - if (uart_base == USART5_BASE) return index; + if (uart_name == UART_5) return index; index++; #endif #if defined(USART6_BASE) - if (uart_base == USART6_BASE) return index; + if (uart_name == UART_6) return index; index++; #endif #if defined(UART7_BASE) - if (uart_base == UART7_BASE) return index; + if (uart_name == UART_7) return index; index++; #endif #if defined(USART7_BASE) - if (uart_base == USART7_BASE) return index; + if (uart_name == UART_7) return index; index++; #endif #if defined(UART8_BASE) - if (uart_base == UART8_BASE) return index; + if (uart_name == UART_8) return index; index++; #endif #if defined(USART8_BASE) - if (uart_base == USART8_BASE) return index; + if (uart_name == UART_8) return index; index++; #endif #if defined(UART9_BASE) - if (uart_base == UART9_BASE) return index; + if (uart_name == UART_9) return index; index++; #endif #if defined(UART10_BASE) - if (uart_base == UART10_BASE) return index; + if (uart_name == UART_10) return index; index++; #endif #if defined(LPUART1_BASE) - if (uart_base == LPUART1_BASE) return index; + if (uart_name == LPUART_1) return index; index++; #endif From 73ffc06ffdcf8dd446f19dcf4c1541880d3a6fad Mon Sep 17 00:00:00 2001 From: bcostm Date: Thu, 25 Jan 2018 16:03:19 +0100 Subject: [PATCH 06/18] STM32 serial: fix linking error --- targets/TARGET_STM/TARGET_STM32F4/serial_device.c | 2 +- targets/TARGET_STM/serial_api.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c index 03a9884cf7..33ab28230a 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c @@ -50,7 +50,7 @@ UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; // Defined in serial_api.c -int8_t get_uart_index(int uart_base); +inline int8_t get_uart_index(UARTName uart_name); /****************************************************************************** * INTERRUPTS HANDLING diff --git a/targets/TARGET_STM/serial_api.c b/targets/TARGET_STM/serial_api.c index 439f7df9ec..2a0ece7ae8 100644 --- a/targets/TARGET_STM/serial_api.c +++ b/targets/TARGET_STM/serial_api.c @@ -558,7 +558,7 @@ HAL_StatusTypeDef init_uart(serial_t *obj) // Warning: the list of UART/USART in this function must be aligned with the list // written in serial_init function. -inline int8_t get_uart_index(UARTName uart_name) +int8_t get_uart_index(UARTName uart_name) { uint8_t index = 0; From a908d28f26d391eeee14234fa2b6b93258cdd563 Mon Sep 17 00:00:00 2001 From: bcostm Date: Mon, 29 Jan 2018 13:25:17 +0100 Subject: [PATCH 07/18] STM32 serial: coding style --- targets/TARGET_STM/serial_api.c | 60 ++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/targets/TARGET_STM/serial_api.c b/targets/TARGET_STM/serial_api.c index 2a0ece7ae8..056369deb7 100644 --- a/targets/TARGET_STM/serial_api.c +++ b/targets/TARGET_STM/serial_api.c @@ -563,77 +563,107 @@ int8_t get_uart_index(UARTName uart_name) uint8_t index = 0; #if defined(USART1_BASE) - if (uart_name == UART_1) return index; + if (uart_name == UART_1) { + return index; + } index++; #endif #if defined(USART2_BASE) - if (uart_name == UART_2) return index; + if (uart_name == UART_2) { + return index; + } index++; #endif #if defined(USART3_BASE) - if (uart_name == UART_3) return index; + if (uart_name == UART_3) { + return index; + } index++; #endif #if defined(UART4_BASE) - if (uart_name == UART_4) return index; + if (uart_name == UART_4) { + return index; + } index++; #endif #if defined(USART4_BASE) - if (uart_name == UART_4) return index; + if (uart_name == UART_4) { + return index; + } index++; #endif #if defined(UART5_BASE) - if (uart_name == UART_5) return index; + if (uart_name == UART_5) { + return index; + } index++; #endif #if defined(USART5_BASE) - if (uart_name == UART_5) return index; + if (uart_name == UART_5) { + return index; + } index++; #endif #if defined(USART6_BASE) - if (uart_name == UART_6) return index; + if (uart_name == UART_6) { + return index; + } index++; #endif #if defined(UART7_BASE) - if (uart_name == UART_7) return index; + if (uart_name == UART_7) { + return index; + } index++; #endif #if defined(USART7_BASE) - if (uart_name == UART_7) return index; + if (uart_name == UART_7) { + return index; + } index++; #endif #if defined(UART8_BASE) - if (uart_name == UART_8) return index; + if (uart_name == UART_8) { + return index; + } index++; #endif #if defined(USART8_BASE) - if (uart_name == UART_8) return index; + if (uart_name == UART_8) { + return index; + } index++; #endif #if defined(UART9_BASE) - if (uart_name == UART_9) return index; + if (uart_name == UART_9) { + return index; + } index++; #endif #if defined(UART10_BASE) - if (uart_name == UART_10) return index; + if (uart_name == UART_10) { + return index; + } index++; #endif #if defined(LPUART1_BASE) - if (uart_name == LPUART_1) return index; + if (uart_name == LPUART_1) { + return index; + } index++; #endif From 2305db6c981449543f6bf68c28281d2f29fbfc95 Mon Sep 17 00:00:00 2001 From: bcostm Date: Thu, 25 Jan 2018 16:43:31 +0100 Subject: [PATCH 08/18] STM32 serial: use uart_name in serial_irq_set function for F4 --- .../TARGET_STM/TARGET_STM32F4/serial_device.c | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c index 33ab28230a..4d70743cfe 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c @@ -168,59 +168,63 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) IRQn_Type irq_n = (IRQn_Type)0; uint32_t vector = 0; - switch (obj_s->index) { - case 0: + switch (obj_s->uart) { +#if defined(USART1_BASE) + case UART_1: irq_n = USART1_IRQn; vector = (uint32_t)&uart1_irq; break; - case 1: +#endif +#if defined(USART2_BASE) + case UART_2: irq_n = USART2_IRQn; vector = (uint32_t)&uart2_irq; break; +#endif #if defined(USART3_BASE) - case 2: + case UART_3: irq_n = USART3_IRQn; vector = (uint32_t)&uart3_irq; break; #endif #if defined(UART4_BASE) - case 3: + case UART_4: irq_n = UART4_IRQn; vector = (uint32_t)&uart4_irq; break; #endif #if defined(UART5_BASE) - case 4: + case UART_5: irq_n = UART5_IRQn; vector = (uint32_t)&uart5_irq; break; #endif #if defined(USART6_BASE) - case 5: + case UART_6: irq_n = USART6_IRQn; vector = (uint32_t)&uart6_irq; break; #endif #if defined(UART7_BASE) - case 6: + case UART_7: irq_n = UART7_IRQn; vector = (uint32_t)&uart7_irq; break; #endif #if defined(UART8_BASE) - case 7: + case UART_8: irq_n = UART8_IRQn; vector = (uint32_t)&uart8_irq; break; #endif #if defined(UART9_BASE) - case 8: + case UART_9: irq_n = UART9_IRQn; vector = (uint32_t)&uart9_irq; break; #endif #if defined(UART10_BASE) - case 9: + case UART_10: irq_n = UART10_IRQn; vector = (uint32_t)&uart10_irq; break; @@ -233,8 +237,8 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) } else { // TxIrq __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); } - NVIC_SetVector(irq_n, vector); - NVIC_EnableIRQ(irq_n); + NVIC_SetVector(irq_n, vector); + NVIC_EnableIRQ(irq_n); } else { // disable int all_disabled = 0; @@ -253,7 +257,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) } if (all_disabled) { - NVIC_DisableIRQ(irq_n); + NVIC_DisableIRQ(irq_n); } } } @@ -284,7 +288,7 @@ void serial_clear(serial_t *obj) { struct serial_s *obj_s = SERIAL_S(obj); UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; - + huart->TxXferCount = 0; huart->RxXferCount = 0; } @@ -293,7 +297,7 @@ void serial_break_set(serial_t *obj) { struct serial_s *obj_s = SERIAL_S(obj); UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; - + HAL_LIN_SendBreak(huart); } From 6974da239c6c2e6ba7be08cd59983bf3d50bdb44 Mon Sep 17 00:00:00 2001 From: bcostm Date: Thu, 25 Jan 2018 16:52:15 +0100 Subject: [PATCH 09/18] STM32 serial: improve irq index management for F1 devices --- .../TARGET_STM/TARGET_STM32F1/serial_device.c | 90 +++++++++++-------- 1 file changed, 55 insertions(+), 35 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F1/serial_device.c b/targets/TARGET_STM/TARGET_STM32F1/serial_device.c index b0747f4d50..b7f90176c9 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F1/serial_device.c @@ -39,48 +39,60 @@ UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; +// Defined in serial_api.c +inline int8_t get_uart_index(UARTName uart_name); + /****************************************************************************** * INTERRUPTS HANDLING ******************************************************************************/ -static void uart_irq(int id) +static void uart_irq(UARTName uart_name) { - UART_HandleTypeDef * huart = &uart_handlers[id]; - - if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) { - irq_handler(serial_irq_ids[id], TxIrq); + int8_t id = get_uart_index(uart_name); + + if (id >= 0) { + UART_HandleTypeDef * huart = &uart_handlers[id]; + if (serial_irq_ids[id] != 0) { + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) { + irq_handler(serial_irq_ids[id], TxIrq); + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { - irq_handler(serial_irq_ids[id], RxIrq); - /* Flag has been cleared when reading the content */ + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { + irq_handler(serial_irq_ids[id], RxIrq); + /* Flag has been cleared when reading the content */ + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET) { - volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->DR; // Clear ORE flag + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET) { + volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->DR; // Clear ORE flag + } } } } } +#if defined(USART1_BASE) static void uart1_irq(void) { - uart_irq(0); + uart_irq(UART_1); } +#endif +#if defined(USART2_BASE) static void uart2_irq(void) { - uart_irq(1); + uart_irq(UART_2); } +#endif +#if defined(USART3_BASE) static void uart3_irq(void) { - uart_irq(2); + uart_irq(UART_3); } +#endif void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) { @@ -97,20 +109,26 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) IRQn_Type irq_n = (IRQn_Type)0; uint32_t vector = 0; +#if defined(USART1_BASE) if (obj_s->uart == UART_1) { irq_n = USART1_IRQn; vector = (uint32_t)&uart1_irq; } +#endif +#if defined(USART2_BASE) if (obj_s->uart == UART_2) { irq_n = USART2_IRQn; vector = (uint32_t)&uart2_irq; } +#endif +#if defined(USART3_BASE) if (obj_s->uart == UART_3) { irq_n = USART3_IRQn; vector = (uint32_t)&uart3_irq; } +#endif if (enable) { if (irq == RxIrq) { @@ -261,31 +279,33 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable) /** * Get index of serial object TX IRQ, relating it to the physical peripheral. * -* @param obj pointer to serial object +* @param uart_name i.e. UART_1, UART_2, ... * @return internal NVIC TX IRQ index of U(S)ART peripheral */ -static IRQn_Type serial_get_irq_n(serial_t *obj) +static IRQn_Type serial_get_irq_n(UARTName uart_name) { - struct serial_s *obj_s = SERIAL_S(obj); IRQn_Type irq_n; - switch (obj_s->index) { - case 0: + switch (uart_name) { +#if defined(USART1_BASE) + case UART_1: irq_n = USART1_IRQn; break; - - case 1: +#endif +#if defined(USART2_BASE) + case UART_2: irq_n = USART2_IRQn; break; - - case 2: +#endif +#if defined(USART3_BASE) + case UART_3: irq_n = USART3_IRQn; break; - +#endif default: irq_n = (IRQn_Type)0; } - + return irq_n; } @@ -330,7 +350,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx serial_enable_event(obj, event, 1); // Set only the wanted events // Enable interrupt - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 1); @@ -380,7 +400,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt serial_rx_buffer_set(obj, rx, rx_length, rx_width); - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 0); @@ -471,8 +491,8 @@ int serial_irq_handler_asynch(serial_t *obj) if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { return_event |= (SERIAL_EVENT_RX_PARITY_ERROR & obj_s->events); } -} - + } + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { return_event |= (SERIAL_EVENT_RX_FRAMING_ERROR & obj_s->events); @@ -535,7 +555,7 @@ void serial_tx_abort_asynch(serial_t *obj) // clear flags __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); - + // reset states huart->TxXferCount = 0; // update handle state From 1fa0557dbf21b831baacbf7bb52871875d8234cd Mon Sep 17 00:00:00 2001 From: bcostm Date: Thu, 25 Jan 2018 17:01:28 +0100 Subject: [PATCH 10/18] STM32 serial: improve irq index management for F2 devices --- .../TARGET_STM/TARGET_STM32F2/serial_device.c | 116 ++++++++++-------- 1 file changed, 64 insertions(+), 52 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F2/serial_device.c b/targets/TARGET_STM/TARGET_STM32F2/serial_device.c index cc74ffb45e..662a0083ac 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F2/serial_device.c @@ -39,83 +39,93 @@ UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; +// Defined in serial_api.c +inline int8_t get_uart_index(UARTName uart_name); + /****************************************************************************** * INTERRUPTS HANDLING ******************************************************************************/ -static void uart_irq(int id) +static void uart_irq(UARTName uart_name) { - UART_HandleTypeDef * huart = &uart_handlers[id]; - - if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) { - irq_handler(serial_irq_ids[id], TxIrq); + int8_t id = get_uart_index(uart_name); + + if (id >= 0) { + UART_HandleTypeDef * huart = &uart_handlers[id]; + if (serial_irq_ids[id] != 0) { + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) { + irq_handler(serial_irq_ids[id], TxIrq); + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { - irq_handler(serial_irq_ids[id], RxIrq); - /* Flag has been cleared when reading the content */ + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { + irq_handler(serial_irq_ids[id], RxIrq); + /* Flag has been cleared when reading the content */ + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { - volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->DR; // Clear ORE flag + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { + volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->DR; // Clear ORE flag + } } } } } +#if defined(USART1_BASE) static void uart1_irq(void) { - uart_irq(0); + uart_irq(UART_1); } +#endif +#if defined(USART2_BASE) static void uart2_irq(void) { - uart_irq(1); + uart_irq(UART_2); } +#endif #if defined(USART3_BASE) static void uart3_irq(void) { - uart_irq(2); + uart_irq(UART_3); } #endif #if defined(UART4_BASE) static void uart4_irq(void) { - uart_irq(3); + uart_irq(UART_4); } #endif #if defined(UART5_BASE) static void uart5_irq(void) { - uart_irq(4); + uart_irq(UART_5); } #endif #if defined(USART6_BASE) static void uart6_irq(void) { - uart_irq(5); + uart_irq(UART_6); } #endif #if defined(UART7_BASE) static void uart7_irq(void) { - uart_irq(6); + uart_irq(UART_7); } #endif #if defined(UART8_BASE) static void uart8_irq(void) { - uart_irq(7); + uart_irq(UART8); } #endif @@ -134,48 +144,51 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) IRQn_Type irq_n = (IRQn_Type)0; uint32_t vector = 0; - switch (obj_s->index) { - case 0: + switch (obj_s->uart) { +#if defined(USART1_BASE) + case UART_1: irq_n = USART1_IRQn; vector = (uint32_t)&uart1_irq; break; - - case 1: +#endif +#if defined(USART2_BASE) + case UART_2: irq_n = USART2_IRQn; vector = (uint32_t)&uart2_irq; break; +#endif #if defined(USART3_BASE) - case 2: + case UART_3: irq_n = USART3_IRQn; vector = (uint32_t)&uart3_irq; break; #endif #if defined(UART4_BASE) - case 3: + case UART_4: irq_n = UART4_IRQn; vector = (uint32_t)&uart4_irq; break; #endif #if defined(UART5_BASE) - case 4: + case UART_5: irq_n = UART5_IRQn; vector = (uint32_t)&uart5_irq; break; #endif #if defined(USART6_BASE) - case 5: + case UART_6: irq_n = USART6_IRQn; vector = (uint32_t)&uart6_irq; break; #endif #if defined(UART7_BASE) - case 6: + case UART_7: irq_n = UART7_IRQn; vector = (uint32_t)&uart7_irq; break; #endif #if defined(UART8_BASE) - case 7: + case UART_8: irq_n = UART8_IRQn; vector = (uint32_t)&uart8_irq; break; @@ -188,8 +201,8 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) } else { // TxIrq __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); } - NVIC_SetVector(irq_n, vector); - NVIC_EnableIRQ(irq_n); + NVIC_SetVector(irq_n, vector); + NVIC_EnableIRQ(irq_n); } else { // disable int all_disabled = 0; @@ -323,55 +336,54 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable) /** * Get index of serial object TX IRQ, relating it to the physical peripheral. * -* @param obj pointer to serial object +* @param uart_name i.e. UART_1, UART_2, ... * @return internal NVIC TX IRQ index of U(S)ART peripheral */ -static IRQn_Type serial_get_irq_n(serial_t *obj) +static IRQn_Type serial_get_irq_n(UARTName uart_name) { - struct serial_s *obj_s = SERIAL_S(obj); IRQn_Type irq_n; - switch (obj_s->index) { + switch (uart_name) { #if defined(USART1_BASE) - case 0: + case UART_1: irq_n = USART1_IRQn; break; #endif #if defined(USART2_BASE) - case 1: + case UART_2: irq_n = USART2_IRQn; break; #endif #if defined(USART3_BASE) - case 2: + case UART_3: irq_n = USART3_IRQn; break; #endif #if defined(UART4_BASE) - case 3: + case UART_4: irq_n = UART4_IRQn; break; #endif #if defined(UART5_BASE) - case 4: + case UART_5: irq_n = UART5_IRQn; break; #endif #if defined(USART6_BASE) - case 5: + case UART_6: irq_n = USART6_IRQn; break; #endif #if defined(UART7_BASE) - case 6: + case UART_7: irq_n = UART7_IRQn; break; #endif #if defined(UART8_BASE) - case 7: + case UART_8: irq_n = UART8_IRQn; break; -#endif +#endif default: irq_n = (IRQn_Type)0; } @@ -421,7 +433,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx serial_enable_event(obj, event, 1); // Set only the wanted events // Enable interrupt - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 1); @@ -471,7 +483,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt serial_rx_buffer_set(obj, rx, rx_length, rx_width); - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 0); @@ -626,7 +638,7 @@ void serial_tx_abort_asynch(serial_t *obj) // clear flags __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); - + // reset states huart->TxXferCount = 0; // update handle state From eeb4d2cd1df53ea59f734ffd9d6b6bfa7d013146 Mon Sep 17 00:00:00 2001 From: bcostm Date: Fri, 26 Jan 2018 10:40:03 +0100 Subject: [PATCH 11/18] STM32 serial: improve irq index management for F3 devices --- .../TARGET_STM/TARGET_STM32F3/serial_device.c | 89 +++++++++++-------- 1 file changed, 52 insertions(+), 37 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F3/serial_device.c b/targets/TARGET_STM/TARGET_STM32F3/serial_device.c index 13ba2eaa7a..244423a400 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F3/serial_device.c @@ -43,62 +43,72 @@ UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; +// Defined in serial_api.c +inline int8_t get_uart_index(UARTName uart_name); + /****************************************************************************** * INTERRUPTS HANDLING ******************************************************************************/ -static void uart_irq(int id) +static void uart_irq(UARTName uart_name) { - UART_HandleTypeDef * huart = &uart_handlers[id]; - - if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) { - irq_handler(serial_irq_ids[id], TxIrq); - } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) { - irq_handler(serial_irq_ids[id], RxIrq); - /* Flag has been cleared when reading the content */ + int8_t id = get_uart_index(uart_name); + + if (id >= 0) { + UART_HandleTypeDef * huart = &uart_handlers[id]; + if (serial_irq_ids[id] != 0) { + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) { + irq_handler(serial_irq_ids[id], TxIrq); } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) { - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); + } + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) { + irq_handler(serial_irq_ids[id], RxIrq); + /* Flag has been cleared when reading the content */ + } + } + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) { + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); + } } } } } +#if defined(USART1_BASE) static void uart1_irq(void) { - uart_irq(0); + uart_irq(UART_1); } +#endif +#if defined(USART2_BASE) static void uart2_irq(void) { - uart_irq(1); + uart_irq(UART_2); } +#endif #if defined(USART3_BASE) static void uart3_irq(void) { - uart_irq(2); + uart_irq(UART_3); } #endif #if defined(UART4_BASE) static void uart4_irq(void) { - uart_irq(3); + uart_irq(UART_4); } #endif #if defined(UART5_BASE) static void uart5_irq(void) { - uart_irq(4); + uart_irq(UART_5); } #endif @@ -117,15 +127,19 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) IRQn_Type irq_n = (IRQn_Type)0; uint32_t vector = 0; +#if defined(USART1_BASE) if (obj_s->uart == UART_1) { irq_n = USART1_IRQn; vector = (uint32_t)&uart1_irq; } +#endif +#if defined(USART2_BASE) if (obj_s->uart == UART_2) { irq_n = USART2_IRQn; vector = (uint32_t)&uart2_irq; } +#endif #if defined(USART3_BASE) if (obj_s->uart == UART_3) { @@ -297,42 +311,43 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable) /** * Get index of serial object TX IRQ, relating it to the physical peripheral. * -* @param obj pointer to serial object +* @param uart_name i.e. UART_1, UART_2, ... * @return internal NVIC TX IRQ index of U(S)ART peripheral */ -static IRQn_Type serial_get_irq_n(serial_t *obj) +static IRQn_Type serial_get_irq_n(UARTName uart_name) { - struct serial_s *obj_s = SERIAL_S(obj); IRQn_Type irq_n; - switch (obj_s->index) { - case 0: + switch (uart_name) { +#if defined(USART1_BASE) + case UART_1: irq_n = USART1_IRQn; break; - - case 1: +#endif +#if defined(USART2_BASE) + case UART_2: irq_n = USART2_IRQn; break; - +#endif #if defined(USART3_BASE) - case 2: + case UART_3: irq_n = USART3_IRQn; break; #endif #if defined(UART4_BASE) - case 3: + case UART_4: irq_n = UART4_IRQn; break; #endif #if defined(UART5_BASE) - case 4: + case UART_5: irq_n = UART5_IRQn; break; #endif default: irq_n = (IRQn_Type)0; } - + return irq_n; } @@ -378,7 +393,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx serial_enable_event(obj, event, 1); // Set only the wanted events // Enable interrupt - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 1); @@ -428,7 +443,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt serial_rx_buffer_set(obj, rx, rx_length, rx_width); - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 0); @@ -586,7 +601,7 @@ void serial_tx_abort_asynch(serial_t *obj) // clear flags __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF); - + // reset states huart->TxXferCount = 0; // update handle state From 689e15cf29672571938c6c364484acb319e6687b Mon Sep 17 00:00:00 2001 From: bcostm Date: Fri, 26 Jan 2018 10:55:07 +0100 Subject: [PATCH 12/18] STM32 serial: improve irq index management for F7 devices --- .../TARGET_STM/TARGET_STM32F7/serial_device.c | 104 +++++++++++------- 1 file changed, 62 insertions(+), 42 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F7/serial_device.c b/targets/TARGET_STM/TARGET_STM32F7/serial_device.c index 9723a8a526..1e88e67ae2 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F7/serial_device.c @@ -38,81 +38,93 @@ UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; +// Defined in serial_api.c +inline int8_t get_uart_index(UARTName uart_name); + /****************************************************************************** * INTERRUPTS HANDLING ******************************************************************************/ -static void uart_irq(int id) +static void uart_irq(UARTName uart_name) { - UART_HandleTypeDef * huart = &uart_handlers[id]; - - if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) { - irq_handler(serial_irq_ids[id], TxIrq); + int8_t id = get_uart_index(uart_name); + + if (id >= 0) { + UART_HandleTypeDef * huart = &uart_handlers[id]; + if (serial_irq_ids[id] != 0) { + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) { + irq_handler(serial_irq_ids[id], TxIrq); + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) { - irq_handler(serial_irq_ids[id], RxIrq); - /* Flag has been cleared when reading the content */ + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) { + irq_handler(serial_irq_ids[id], RxIrq); + /* Flag has been cleared when reading the content */ + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) { - __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF); + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) { + __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF); + } } } } } +#if defined(USART1_BASE) static void uart1_irq(void) { - uart_irq(0); + uart_irq(UART_1); } +#endif +#if defined(USART2_BASE) static void uart2_irq(void) { - uart_irq(1); + uart_irq(UART_2); } +#endif #if defined(USART3_BASE) static void uart3_irq(void) { - uart_irq(2); + uart_irq(UART_3); } #endif #if defined(UART4_BASE) static void uart4_irq(void) { - uart_irq(3); + uart_irq(UART_4); } #endif #if defined(UART5_BASE) static void uart5_irq(void) { - uart_irq(4); + uart_irq(UART_5); } #endif +#if defined(USART6_BASE) static void uart6_irq(void) { - uart_irq(5); + uart_irq(UART_6); } +#endif #if defined(UART7_BASE) static void uart7_irq(void) { - uart_irq(6); + uart_irq(UART_7); } #endif #if defined(UART8_BASE) static void uart8_irq(void) { - uart_irq(7); + uart_irq(UART_8); } #endif @@ -132,15 +144,18 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) uint32_t vector = 0; switch (obj_s->uart) { +#if defined(USART1_BASE) case UART_1: irq_n = USART1_IRQn; vector = (uint32_t)&uart1_irq; break; - +#endif +#if defined(USART2_BASE) case UART_2: irq_n = USART2_IRQn; vector = (uint32_t)&uart2_irq; break; +#endif #if defined(USART3_BASE) case UART_3: irq_n = USART3_IRQn; @@ -159,10 +174,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) vector = (uint32_t)&uart5_irq; break; #endif +#if defined(USART6_BASE) case UART_6: irq_n = USART6_IRQn; vector = (uint32_t)&uart6_irq; break; +#endif #if defined(UART7_BASE) case UART_7: irq_n = UART7_IRQn; @@ -318,55 +335,58 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable) /** * Get index of serial object TX IRQ, relating it to the physical peripheral. * -* @param obj pointer to serial object +* @param uart_name i.e. UART_1, UART_2, ... * @return internal NVIC TX IRQ index of U(S)ART peripheral */ -static IRQn_Type serial_get_irq_n(serial_t *obj) +static IRQn_Type serial_get_irq_n(UARTName uart_name) { - struct serial_s *obj_s = SERIAL_S(obj); IRQn_Type irq_n; - switch (obj_s->index) { - - case 0: + switch (uart_name) { +#if defined(USART1_BASE) + case UART_1: irq_n = USART1_IRQn; break; - - case 1: +#endif +#if defined(USART2_BASE) + case UART_2: irq_n = USART2_IRQn; break; +#endif #if defined(USART3_BASE) - case 2: + case UART_3: irq_n = USART3_IRQn; break; #endif #if defined(UART4_BASE) - case 3: + case UART_4: irq_n = UART4_IRQn; break; #endif #if defined(UART5_BASE) - case 4: + case UART_5: irq_n = UART5_IRQn; break; #endif - case 5: +#if defined(USART6_BASE) + case UART_6: irq_n = USART6_IRQn; break; +#endif #if defined(UART7_BASE) - case 6: + case UART_7: irq_n = UART7_IRQn; break; #endif #if defined(UART8_BASE) - case 7: + case UART_8: irq_n = UART8_IRQn; break; #endif default: irq_n = (IRQn_Type)0; } - + return irq_n; } @@ -411,7 +431,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx serial_enable_event(obj, event, 1); // Set only the wanted events // Enable interrupt - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 1); @@ -461,7 +481,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt serial_rx_buffer_set(obj, rx, rx_length, rx_width); - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 0); From 665d4a800311fb66dcce986cb269c36f94fc3f76 Mon Sep 17 00:00:00 2001 From: bcostm Date: Fri, 26 Jan 2018 11:18:26 +0100 Subject: [PATCH 13/18] STM32 serial: improve irq index management for L1 devices --- .../TARGET_STM/TARGET_STM32L1/serial_device.c | 136 ++++++++++-------- 1 file changed, 79 insertions(+), 57 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32L1/serial_device.c b/targets/TARGET_STM/TARGET_STM32L1/serial_device.c index 797979dd89..60234cb95f 100755 --- a/targets/TARGET_STM/TARGET_STM32L1/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32L1/serial_device.c @@ -39,59 +39,72 @@ UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; +// Defined in serial_api.c +inline int8_t get_uart_index(UARTName uart_name); + /****************************************************************************** * INTERRUPTS HANDLING ******************************************************************************/ -static void uart_irq(int id) +static void uart_irq(UARTName uart_name) { - UART_HandleTypeDef * huart = &uart_handlers[id]; - - if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) { - irq_handler(serial_irq_ids[id], TxIrq); + int8_t id = get_uart_index(uart_name); + + if (id >= 0) { + UART_HandleTypeDef * huart = &uart_handlers[id]; + if (serial_irq_ids[id] != 0) { + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) { + irq_handler(serial_irq_ids[id], TxIrq); + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { - irq_handler(serial_irq_ids[id], RxIrq); - /* Flag has been cleared when reading the content */ + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { + irq_handler(serial_irq_ids[id], RxIrq); + /* Flag has been cleared when reading the content */ + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET) { - volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->DR; // Clear ORE flag + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET) { + volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->DR; // Clear ORE flag + } } } } } +#if defined(USART1_BASE) static void uart1_irq(void) { - uart_irq(0); + uart_irq(UART_1); } +#endif +#if defined(USART2_BASE) static void uart2_irq(void) { - uart_irq(1); + uart_irq(UART_2); } +#endif +#if defined(USART3_BASE) static void uart3_irq(void) { - uart_irq(2); + uart_irq(UART_3); } +#endif #if defined(UART4_BASE) static void uart4_irq(void) { - uart_irq(3); + uart_irq(UART_4); } #endif + #if defined(UART5_BASE) static void uart5_irq(void) { - uart_irq(4); + uart_irq(UART_5); } #endif @@ -110,32 +123,38 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) IRQn_Type irq_n = (IRQn_Type)0; uint32_t vector = 0; - if (obj_s->uart == UART_1) { - irq_n = USART1_IRQn; - vector = (uint32_t)&uart1_irq; - } - - if (obj_s->uart == UART_2) { - irq_n = USART2_IRQn; - vector = (uint32_t)&uart2_irq; - } - - if (obj_s->uart == UART_3) { - irq_n = USART3_IRQn; - vector = (uint32_t)&uart3_irq; - } + switch (obj_s->uart) { +#if defined(USART1_BASE) + case UART_1: + irq_n = USART1_IRQn; + vector = (uint32_t)&uart1_irq; + break; +#endif +#if defined(USART2_BASE) + case UART_2: + irq_n = USART2_IRQn; + vector = (uint32_t)&uart2_irq; + break; +#endif +#if defined(USART3_BASE) + case UART_3: + irq_n = USART3_IRQn; + vector = (uint32_t)&uart3_irq; + break; +#endif #if defined(UART4_BASE) - if (obj_s->uart == UART_4) { - irq_n = UART4_IRQn; - vector = (uint32_t)&uart4_irq; - } + case UART_4: + irq_n = UART4_IRQn; + vector = (uint32_t)&uart4_irq; + break; #endif #if defined(UART5_BASE) - if (obj_s->uart == UART_5) { - irq_n = UART5_IRQn; - vector = (uint32_t)&uart5_irq; - } + case UART_5: + irq_n = UART5_IRQn; + vector = (uint32_t)&uart5_irq; + break; #endif + } if (enable) { if (irq == RxIrq) { @@ -286,40 +305,43 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable) /** * Get index of serial object TX IRQ, relating it to the physical peripheral. * -* @param obj pointer to serial object +* @param uart_name i.e. UART_1, UART_2, ... * @return internal NVIC TX IRQ index of U(S)ART peripheral */ -static IRQn_Type serial_get_irq_n(serial_t *obj) +static IRQn_Type serial_get_irq_n(UARTName uart_name) { - struct serial_s *obj_s = SERIAL_S(obj); IRQn_Type irq_n; - switch (obj_s->index) { - case 0: + switch (uart_name) { +#if defined(USART1_BASE) + case UART_1: irq_n = USART1_IRQn; break; - - case 1: +#endif +#if defined(USART2_BASE) + case UART_2: irq_n = USART2_IRQn; break; - - case 2: +#endif +#if defined(USART3_BASE) + case UART_3: irq_n = USART3_IRQn; break; +#endif #if defined(UART4_BASE) - case 3: + case UART_4: irq_n = UART4_IRQn; break; #endif #if defined(UART5_BASE) - case 4: + case UART_5: irq_n = UART5_IRQn; break; #endif default: irq_n = (IRQn_Type)0; } - + return irq_n; } @@ -364,7 +386,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx serial_enable_event(obj, event, 1); // Set only the wanted events // Enable interrupt - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 1); @@ -414,7 +436,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt serial_rx_buffer_set(obj, rx, rx_length, rx_width); - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 0); From 84269c7ca2379392e82a4ee37fac91307b43bfe8 Mon Sep 17 00:00:00 2001 From: bcostm Date: Fri, 26 Jan 2018 12:52:28 +0100 Subject: [PATCH 14/18] STM32 serial: improve irq index management for L4 devices --- .../TARGET_STM/TARGET_STM32L4/serial_device.c | 143 ++++++++++-------- 1 file changed, 78 insertions(+), 65 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32L4/serial_device.c b/targets/TARGET_STM/TARGET_STM32L4/serial_device.c index 1ae5e9f24f..c56f854be3 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32L4/serial_device.c @@ -45,69 +45,79 @@ UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; +// Defined in serial_api.c +inline int8_t get_uart_index(UARTName uart_name); + /****************************************************************************** * INTERRUPTS HANDLING ******************************************************************************/ -static void uart_irq(int id) +static void uart_irq(UARTName uart_name) { - UART_HandleTypeDef * huart = &uart_handlers[id]; - - if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) { - irq_handler(serial_irq_ids[id], TxIrq); + int8_t id = get_uart_index(uart_name); + + if (id >= 0) { + UART_HandleTypeDef * huart = &uart_handlers[id]; + if (serial_irq_ids[id] != 0) { + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) { + irq_handler(serial_irq_ids[id], TxIrq); + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) { - irq_handler(serial_irq_ids[id], RxIrq); - /* Flag has been cleared when reading the content */ + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) { + irq_handler(serial_irq_ids[id], RxIrq); + /* Flag has been cleared when reading the content */ + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) { - volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->RDR; // Clear ORE flag + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) { + volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->RDR; // Clear ORE flag + } } } } } +#if defined(USART1_BASE) static void uart1_irq(void) { - uart_irq(0); + uart_irq(UART_1); } +#endif +#if defined(USART2_BASE) static void uart2_irq(void) { - uart_irq(1); + uart_irq(UART_2); } +#endif #if defined(USART3_BASE) static void uart3_irq(void) { - uart_irq(2); + uart_irq(UART_3); } #endif #if defined(UART4_BASE) static void uart4_irq(void) { - uart_irq(3); + uart_irq(UART_4); } #endif #if defined(UART5_BASE) static void uart5_irq(void) { - uart_irq(4); + uart_irq(UART_5); } #endif #if defined(LPUART1_BASE) static void lpuart1_irq(void) { - uart_irq(5); + uart_irq(LPUART_1); } #endif @@ -126,41 +136,44 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) IRQn_Type irq_n = (IRQn_Type)0; uint32_t vector = 0; - if (obj_s->uart == UART_1) { - irq_n = USART1_IRQn; - vector = (uint32_t)&uart1_irq; - } - - if (obj_s->uart == UART_2) { - irq_n = USART2_IRQn; - vector = (uint32_t)&uart2_irq; - } + switch (obj_s->uart) { +#if defined(USART1_BASE) + case UART_1: + irq_n = USART1_IRQn; + vector = (uint32_t)&uart1_irq; + break; +#endif +#if defined(USART2_BASE) + case UART_2: + irq_n = USART2_IRQn; + vector = (uint32_t)&uart2_irq; + break; +#endif #if defined(USART3_BASE) - if (obj_s->uart == UART_3) { - irq_n = USART3_IRQn; - vector = (uint32_t)&uart3_irq; - } + case UART_3: + irq_n = USART3_IRQn; + vector = (uint32_t)&uart3_irq; + break; #endif #if defined(UART4_BASE) - if (obj_s->uart == UART_4) { - irq_n = UART4_IRQn; - vector = (uint32_t)&uart4_irq; - } + case UART_4: + irq_n = UART4_IRQn; + vector = (uint32_t)&uart4_irq; + break; #endif - #if defined(UART5_BASE) - if (obj_s->uart == UART_5) { - irq_n = UART5_IRQn; - vector = (uint32_t)&uart5_irq; - } + case UART_5: + irq_n = UART5_IRQn; + vector = (uint32_t)&uart5_irq; + break; #endif - #if defined(LPUART1_BASE) - if (obj_s->uart == LPUART_1) { - irq_n = LPUART1_IRQn; - vector = (uint32_t)&lpuart1_irq; - } + case LPUART_1: + irq_n = LPUART1_IRQn; + vector = (uint32_t)&lpuart1_irq; + break; #endif + } if (enable) { if (irq == RxIrq) { @@ -311,48 +324,48 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable) /** * Get index of serial object TX IRQ, relating it to the physical peripheral. * -* @param obj pointer to serial object +* @param uart_name i.e. UART_1, UART_2, ... * @return internal NVIC TX IRQ index of U(S)ART peripheral */ -static IRQn_Type serial_get_irq_n(serial_t *obj) +static IRQn_Type serial_get_irq_n(UARTName uart_name) { - struct serial_s *obj_s = SERIAL_S(obj); IRQn_Type irq_n; - switch (obj_s->index) { - case 0: + switch (uart_name) { +#if defined(USART1_BASE) + case UART_1: irq_n = USART1_IRQn; break; - - case 1: +#endif +#if defined(USART2_BASE) + case UART_2: irq_n = USART2_IRQn; break; - +#endif #if defined(USART3_BASE) - case 2: + case UART_3: irq_n = USART3_IRQn; break; #endif #if defined(UART4_BASE) - case 3: + case UART_4: irq_n = UART4_IRQn; break; #endif #if defined(UART5_BASE) - case 4: + case UART_5: irq_n = UART5_IRQn; break; #endif #if defined(LPUART1_BASE) - case 5: + case LPUART_1: irq_n = LPUART1_IRQn; break; #endif - default: irq_n = (IRQn_Type)0; } - + return irq_n; } @@ -397,7 +410,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx serial_enable_event(obj, event, 1); // Set only the wanted events // Enable interrupt - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 1); @@ -447,7 +460,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt serial_rx_buffer_set(obj, rx, rx_length, rx_width); - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 0); From 78a551672624f5ef391a508a8cc400b01c3697cc Mon Sep 17 00:00:00 2001 From: bcostm Date: Mon, 29 Jan 2018 11:06:26 +0100 Subject: [PATCH 15/18] STM32 serial: improve irq index management for F0 devices --- .../TARGET_STM/TARGET_STM32F0/serial_device.c | 134 ++++++++++++------ 1 file changed, 88 insertions(+), 46 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F0/serial_device.c b/targets/TARGET_STM/TARGET_STM32F0/serial_device.c index 733021e725..616ff295be 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F0/serial_device.c @@ -47,42 +47,51 @@ UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; +// Defined in serial_api.c +inline int8_t get_uart_index(UARTName uart_name); + /****************************************************************************** * INTERRUPTS HANDLING ******************************************************************************/ -static void uart_irq(int id) +static void uart_irq(UARTName uart_name) { - UART_HandleTypeDef * huart = &uart_handlers[id]; - if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) { - irq_handler(serial_irq_ids[id], TxIrq); + int8_t id = get_uart_index(uart_name); + + if (id >= 0) { + UART_HandleTypeDef * huart = &uart_handlers[id]; + if (serial_irq_ids[id] != 0) { + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) { + irq_handler(serial_irq_ids[id], TxIrq); + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) { - irq_handler(serial_irq_ids[id], RxIrq); - /* Flag has been cleared when reading the content */ + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) { + irq_handler(serial_irq_ids[id], RxIrq); + /* Flag has been cleared when reading the content */ + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) { - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) { + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); + } } } } } +#if defined(USART1_BASE) static void uart1_irq(void) { - uart_irq(0); + uart_irq(UART_1); } +#endif #if defined(USART2_BASE) static void uart2_irq(void) { - uart_irq(1); + uart_irq(UART_2); } #endif @@ -92,43 +101,43 @@ static void uart3_8_irq(void) #if defined(TARGET_STM32F091RC) #if defined(USART3_BASE) if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART3) != RESET) { - uart_irq(2); + uart_irq(UART_3); } #endif #if defined(USART4_BASE) if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART4) != RESET) { - uart_irq(3); + uart_irq(UART_4); } #endif #if defined(USART5_BASE) if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART5) != RESET) { - uart_irq(4); + uart_irq(UART_5); } #endif #if defined(USART6_BASE) if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART6) != RESET) { - uart_irq(5); + uart_irq(UART_6); } #endif #if defined(USART7_BASE) if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART7) != RESET) { - uart_irq(6); + uart_irq(UART_7); } #endif #if defined(USART8_BASE) if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART8) != RESET) { - uart_irq(7); + uart_irq(UART_8); } #endif #else // TARGET_STM32F070RB, TARGET_STM32F072RB #if defined(USART3_BASE) if (USART3->ISR & (UART_FLAG_TXE | UART_FLAG_RXNE | UART_FLAG_ORE)) { - uart_irq(2); + uart_irq(UART_3); } #endif #if defined(USART4_BASE) if (USART4->ISR & (UART_FLAG_TXE | UART_FLAG_RXNE | UART_FLAG_ORE)) { - uart_irq(3); + uart_irq(UART_4); } #endif #endif @@ -149,10 +158,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) IRQn_Type irq_n = (IRQn_Type)0; uint32_t vector = 0; +#if defined(USART1_BASE) if (obj_s->uart == UART_1) { irq_n = USART1_IRQn; vector = (uint32_t)&uart1_irq; } +#endif #if defined(USART2_BASE) if (obj_s->uart == UART_2) { @@ -238,7 +249,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) } if (all_disabled) { - NVIC_DisableIRQ(irq_n); + NVIC_DisableIRQ(irq_n); } } } @@ -351,44 +362,75 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable) /** * Get index of serial object TX IRQ, relating it to the physical peripheral. * -* @param obj pointer to serial object +* @param uart_name i.e. UART_1, UART_2, ... * @return internal NVIC TX IRQ index of U(S)ART peripheral */ -static IRQn_Type serial_get_irq_n(serial_t *obj) +static IRQn_Type serial_get_irq_n(UARTName uart_name) { - struct serial_s *obj_s = SERIAL_S(obj); IRQn_Type irq_n; - switch (obj_s->index) { + switch (uart_name) { + #if defined(USART1_BASE) - case 0: + case UART_1: irq_n = USART1_IRQn; break; #endif + #if defined(USART2_BASE) - case 1: + case UART_2: irq_n = USART2_IRQn; break; #endif + +#if defined(USART3_BASE) + case UART_3: #if defined (TARGET_STM32F091RC) - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: irq_n = USART3_8_IRQn; - break; -#elif !defined (TARGET_STM32F030R8) && !defined (TARGET_STM32F051R8) - case 2: - case 3: +#else irq_n = USART3_4_IRQn; +#endif break; #endif + +#if defined(USART4_BASE) + case UART_4: +#if defined (TARGET_STM32F091RC) + irq_n = USART3_8_IRQn; +#else + irq_n = USART3_4_IRQn; +#endif + break; +#endif + +#if defined(USART5_BASE) + case UART_5: + irq_n = USART3_8_IRQn; + break; +#endif + +#if defined(USART6_BASE) + case UART_6: + irq_n = USART3_8_IRQn; + break; +#endif + +#if defined(USART7_BASE) + case UART_7: + irq_n = USART3_8_IRQn; + break; +#endif + +#if defined(USART8_BASE) + case UART_8: + irq_n = USART3_8_IRQn; + break; +#endif + default: irq_n = (IRQn_Type)0; } - + return irq_n; } @@ -434,7 +476,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx serial_enable_event(obj, event, 1); // Set only the wanted events // Enable interrupt - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 1); @@ -484,7 +526,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt serial_rx_buffer_set(obj, rx, rx_length, rx_width); - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 0); @@ -642,7 +684,7 @@ void serial_tx_abort_asynch(serial_t *obj) // clear flags __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF); - + // reset states huart->TxXferCount = 0; // update handle state From e8454ff522d770666a20bfdc89718c9664782c45 Mon Sep 17 00:00:00 2001 From: bcostm Date: Mon, 29 Jan 2018 14:27:15 +0100 Subject: [PATCH 16/18] STM32 serial: improve irq index management for L0 devices --- .../TARGET_STM/TARGET_STM32L0/serial_device.c | 106 ++++++++++-------- 1 file changed, 61 insertions(+), 45 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32L0/serial_device.c b/targets/TARGET_STM/TARGET_STM32L0/serial_device.c index 33123140ed..63cf3154de 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32L0/serial_device.c @@ -45,29 +45,35 @@ UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; +// Defined in serial_api.c +inline int8_t get_uart_index(UARTName uart_name); + /****************************************************************************** * INTERRUPTS HANDLING ******************************************************************************/ -static void uart_irq(int id) +static void uart_irq(UARTName uart_name) { - UART_HandleTypeDef * huart = &uart_handlers[id]; - - if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) { - irq_handler(serial_irq_ids[id], TxIrq); + int8_t id = get_uart_index(uart_name); + + if (id >= 0) { + UART_HandleTypeDef * huart = &uart_handlers[id]; + if (serial_irq_ids[id] != 0) { + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) { + irq_handler(serial_irq_ids[id], TxIrq); + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) { - irq_handler(serial_irq_ids[id], RxIrq); - /* Flag has been cleared when reading the content */ + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) { + irq_handler(serial_irq_ids[id], RxIrq); + /* Flag has been cleared when reading the content */ + } } - } - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) { - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); + if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { + if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) { + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); + } } } } @@ -76,31 +82,35 @@ static void uart_irq(int id) #if defined(USART1_BASE) static void uart1_irq(void) { - uart_irq(0); + uart_irq(UART_1); } #endif +#if defined(USART2_BASE) static void uart2_irq(void) { - uart_irq(1); -} - -static void lpuart1_irq(void) -{ - uart_irq(2); + uart_irq(UART_2); } +#endif #if defined(USART4_BASE) static void uart4_irq(void) { - uart_irq(3); + uart_irq(UART_4); } #endif #if defined(USART5_BASE) static void uart5_irq(void) { - uart_irq(4); + uart_irq(UART_5); +} +#endif + +#if defined(LPUART1_BASE) +static void lpuart1_irq(void) +{ + uart_irq(LPUART_1); } #endif @@ -126,15 +136,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) } #endif +#if defined(USART2_BASE) if (obj_s->uart == UART_2) { irq_n = USART2_IRQn; vector = (uint32_t)&uart2_irq; } - - if (obj_s->uart == LPUART_1) { - irq_n = RNG_LPUART1_IRQn; - vector = (uint32_t)&lpuart1_irq; - } +#endif #if defined(USART4_BASE) if (obj_s->uart == UART_4) { @@ -150,6 +157,13 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) } #endif +#if defined(LPUART1_BASE) + if (obj_s->uart == LPUART_1) { + irq_n = RNG_LPUART1_IRQn; + vector = (uint32_t)&lpuart1_irq; + } +#endif + if (enable) { if (irq == RxIrq) { __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); @@ -291,41 +305,43 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable) /** * Get index of serial object TX IRQ, relating it to the physical peripheral. * -* @param obj pointer to serial object +* @param uart_name i.e. UART_1, UART_2, ... * @return internal NVIC TX IRQ index of U(S)ART peripheral */ -static IRQn_Type serial_get_irq_n(serial_t *obj) +static IRQn_Type serial_get_irq_n(UARTName uart_name) { - struct serial_s *obj_s = SERIAL_S(obj); IRQn_Type irq_n; - switch (obj_s->index) { + switch (uart_name) { #if defined(USART1_BASE) - case 0: + case UART_1: irq_n = USART1_IRQn; break; #endif - case 1: +#if defined(USART2_BASE) + case UART_2: irq_n = USART2_IRQn; break; - - case 2: - irq_n = RNG_LPUART1_IRQn; - break; +#endif #if defined(USART4_BASE) - case 3: + case UART_4: irq_n = USART4_5_IRQn; break; #endif #if defined(USART5_BASE) - case 4: + case UART_5: irq_n = USART4_5_IRQn; break; +#endif +#if defined(LPUART1_BASE) + case LPUART_1: + irq_n = RNG_LPUART1_IRQn; + break; #endif default: irq_n = (IRQn_Type)0; } - + return irq_n; } @@ -371,7 +387,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx serial_enable_event(obj, event, 1); // Set only the wanted events // Enable interrupt - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 1); @@ -421,7 +437,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt serial_rx_buffer_set(obj, rx, rx_length, rx_width); - IRQn_Type irq_n = serial_get_irq_n(obj); + IRQn_Type irq_n = serial_get_irq_n(obj_s->uart); NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); NVIC_SetPriority(irq_n, 0); From b6efdd58c8351421fc3b7be3378c647f3e223ae5 Mon Sep 17 00:00:00 2001 From: bcostm Date: Mon, 29 Jan 2018 17:22:55 +0100 Subject: [PATCH 17/18] STM32 serial: improve index assignment in serial_init --- targets/TARGET_STM/serial_api.c | 39 +++++---------------------------- 1 file changed, 5 insertions(+), 34 deletions(-) diff --git a/targets/TARGET_STM/serial_api.c b/targets/TARGET_STM/serial_api.c index 056369deb7..8c8f5e87e1 100644 --- a/targets/TARGET_STM/serial_api.c +++ b/targets/TARGET_STM/serial_api.c @@ -44,7 +44,6 @@ HAL_StatusTypeDef init_uart(serial_t *obj); void serial_init(serial_t *obj, PinName tx, PinName rx) { struct serial_s *obj_s = SERIAL_S(obj); - int IndexNumber = 0; uint8_t stdio_config = 0; // Determine the UART to use (UART_1, UART_2, ...) @@ -64,15 +63,13 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) } } - // Enable USART clock + // Reset and enable clock #if defined(USART1_BASE) if (obj_s->uart == UART_1) { __HAL_RCC_USART1_FORCE_RESET(); __HAL_RCC_USART1_RELEASE_RESET(); __HAL_RCC_USART1_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined (USART2_BASE) @@ -80,9 +77,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_USART2_FORCE_RESET(); __HAL_RCC_USART2_RELEASE_RESET(); __HAL_RCC_USART2_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(USART3_BASE) @@ -90,9 +85,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_USART3_FORCE_RESET(); __HAL_RCC_USART3_RELEASE_RESET(); __HAL_RCC_USART3_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(UART4_BASE) @@ -100,9 +93,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_UART4_FORCE_RESET(); __HAL_RCC_UART4_RELEASE_RESET(); __HAL_RCC_UART4_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(USART4_BASE) @@ -110,9 +101,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_USART4_FORCE_RESET(); __HAL_RCC_USART4_RELEASE_RESET(); __HAL_RCC_USART4_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(UART5_BASE) @@ -120,9 +109,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_UART5_FORCE_RESET(); __HAL_RCC_UART5_RELEASE_RESET(); __HAL_RCC_UART5_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(USART5_BASE) @@ -130,9 +117,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_USART5_FORCE_RESET(); __HAL_RCC_USART5_RELEASE_RESET(); __HAL_RCC_USART5_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(USART6_BASE) @@ -140,9 +125,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_USART6_FORCE_RESET(); __HAL_RCC_USART6_RELEASE_RESET(); __HAL_RCC_USART6_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(UART7_BASE) @@ -150,9 +133,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_UART7_FORCE_RESET(); __HAL_RCC_UART7_RELEASE_RESET(); __HAL_RCC_UART7_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(USART7_BASE) @@ -160,9 +141,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_USART7_FORCE_RESET(); __HAL_RCC_USART7_RELEASE_RESET(); __HAL_RCC_USART7_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(UART8_BASE) @@ -170,9 +149,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_UART8_FORCE_RESET(); __HAL_RCC_UART8_RELEASE_RESET(); __HAL_RCC_UART8_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(USART8_BASE) @@ -180,9 +157,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_USART8_FORCE_RESET(); __HAL_RCC_USART8_RELEASE_RESET(); __HAL_RCC_USART8_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(UART9_BASE) @@ -190,9 +165,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_UART9_FORCE_RESET(); __HAL_RCC_UART9_RELEASE_RESET(); __HAL_RCC_UART9_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(UART10_BASE) @@ -200,9 +173,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_UART10_FORCE_RESET(); __HAL_RCC_UART10_RELEASE_RESET(); __HAL_RCC_UART10_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif #if defined(LPUART1_BASE) @@ -210,11 +181,13 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) __HAL_RCC_LPUART1_FORCE_RESET(); __HAL_RCC_LPUART1_RELEASE_RESET(); __HAL_RCC_LPUART1_CLK_ENABLE(); - obj_s->index = IndexNumber; } - IndexNumber++; #endif + // Assign serial object index + obj_s->index = get_uart_index(obj_s->uart); + MBED_ASSERT(obj_s->index >= 0); + // Configure UART pins pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(rx, PinMap_UART_RX); @@ -556,8 +529,6 @@ HAL_StatusTypeDef init_uart(serial_t *obj) return HAL_UART_Init(huart); } -// Warning: the list of UART/USART in this function must be aligned with the list -// written in serial_init function. int8_t get_uart_index(UARTName uart_name) { uint8_t index = 0; From eb4b339c371df7236609d4a6a4d39133f1c9613b Mon Sep 17 00:00:00 2001 From: bcostm Date: Tue, 30 Jan 2018 09:48:53 +0100 Subject: [PATCH 18/18] STM32 serial: add missing function declaration --- targets/TARGET_STM/serial_api.c | 1 + 1 file changed, 1 insertion(+) diff --git a/targets/TARGET_STM/serial_api.c b/targets/TARGET_STM/serial_api.c index 8c8f5e87e1..c1e568d733 100644 --- a/targets/TARGET_STM/serial_api.c +++ b/targets/TARGET_STM/serial_api.c @@ -40,6 +40,7 @@ extern uint32_t serial_irq_ids[]; // Utility functions HAL_StatusTypeDef init_uart(serial_t *obj); +int8_t get_uart_index(UARTName uart_name); void serial_init(serial_t *obj, PinName tx, PinName rx) {