diff --git a/cores/arduino/stm32/uart.c b/cores/arduino/stm32/uart.c index 91c9c66d7e..bc246c10c3 100644 --- a/cores/arduino/stm32/uart.c +++ b/cores/arduino/stm32/uart.c @@ -607,12 +607,16 @@ size_t uart_debug_write(uint8_t *data, uint32_t size) index = serial_debug.index; } + HAL_NVIC_DisableIRQ(serial_debug.irq); + while (HAL_UART_Transmit(uart_handlers[index], data, size, TX_TIMEOUT) != HAL_OK) { if ((HAL_GetTick() - tickstart) >= TX_TIMEOUT) { + HAL_NVIC_EnableIRQ(serial_debug.irq); return 0; } } + HAL_NVIC_EnableIRQ(serial_debug.irq); return size; } @@ -682,12 +686,14 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *)) rx_callback[obj->index] = callback; rx_callback_obj[obj->index] = obj; + /* Must disable interrupt to prevent handle lock contention */ + HAL_NVIC_DisableIRQ(obj->irq); + + HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1); + + /* Enable interrupt */ HAL_NVIC_SetPriority(obj->irq, 0, 1); HAL_NVIC_EnableIRQ(obj->irq); - - if (HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1) != HAL_OK) { - return; - } } /** @@ -706,14 +712,15 @@ void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *)) tx_callback[obj->index] = callback; tx_callback_obj[obj->index] = obj; + /* Must disable interrupt to prevent handle lock contention */ + HAL_NVIC_DisableIRQ(obj->irq); + + /* The following function will enable UART_IT_TXE and error interrupts */ + HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], 1); + /* Enable interrupt */ HAL_NVIC_SetPriority(obj->irq, 0, 2); HAL_NVIC_EnableIRQ(obj->irq); - - /* The following function will enable UART_IT_TXE and error interrupts */ - if (HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], 1) != HAL_OK) { - return; - } } /** @@ -790,16 +797,23 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) } #else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { - tmpval = huart->Instance->RDR; /* Clear PE flag */ + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); /* Clear PE flag */ } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { - tmpval = huart->Instance->RDR; /* Clear FE flag */ + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); /* Clear FE flag */ } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { - tmpval = huart->Instance->RDR; /* Clear NE flag */ + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); /* Clear NE flag */ } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { - tmpval = huart->Instance->RDR; /* Clear ORE flag */ + __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); /* Clear ORE flag */ } #endif - + /* Restart receive interrupt after any error */ + uint8_t index = uart_index(huart); + if (index < UART_NUM) { + serial_t *obj = rx_callback_obj[index]; + if (!serial_rx_active(obj)) { + HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1); + } + } UNUSED(tmpval); }