From 85711eb09e1ad4f37a2ff659b7dc8bd9a83d01f8 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Mon, 10 Jul 2017 18:23:17 +0200 Subject: [PATCH 1/2] STM32: Serial - do no clear RXNE flag The RXNE flag is getting cleared when reading Data Register so it should not be cleared here. Especially in case of high data rate, another byte of data could have received during irq_handler call and clearing the flag would read and discard this data which would be lost for application. --- targets/TARGET_STM/TARGET_STM32F0/serial_device.c | 3 +-- targets/TARGET_STM/TARGET_STM32F1/serial_device.c | 2 +- targets/TARGET_STM/TARGET_STM32F2/serial_device.c | 2 +- targets/TARGET_STM/TARGET_STM32F3/serial_device.c | 5 ++--- targets/TARGET_STM/TARGET_STM32F4/serial_device.c | 2 +- targets/TARGET_STM/TARGET_STM32F7/serial_device.c | 2 +- targets/TARGET_STM/TARGET_STM32L0/serial_device.c | 2 +- targets/TARGET_STM/TARGET_STM32L1/serial_device.c | 2 +- targets/TARGET_STM/TARGET_STM32L4/serial_device.c | 4 ++-- 9 files changed, 11 insertions(+), 13 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F0/serial_device.c b/targets/TARGET_STM/TARGET_STM32F0/serial_device.c index fcb82cc8a2..ccce38abbf 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F0/serial_device.c @@ -273,8 +273,7 @@ static void uart_irq(int id) 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); - volatile uint32_t tmpval = huart->Instance->RDR; // Clear RXNE flag - UNUSED(tmpval); + /* Flag has been cleared when reading the content */ } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { diff --git a/targets/TARGET_STM/TARGET_STM32F1/serial_device.c b/targets/TARGET_STM/TARGET_STM32F1/serial_device.c index d541a0d584..b91deaf36e 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F1/serial_device.c @@ -168,7 +168,7 @@ static void uart_irq(int id) 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); - __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_RXNE); + /* Flag has been cleared when reading the content */ } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { diff --git a/targets/TARGET_STM/TARGET_STM32F2/serial_device.c b/targets/TARGET_STM/TARGET_STM32F2/serial_device.c index 8e2fc3b365..9b334908a4 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F2/serial_device.c @@ -255,7 +255,7 @@ static void uart_irq(int id) 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); - __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_RXNE); + /* Flag has been cleared when reading the content */ } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { diff --git a/targets/TARGET_STM/TARGET_STM32F3/serial_device.c b/targets/TARGET_STM/TARGET_STM32F3/serial_device.c index 32f5415414..eaa491483e 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F3/serial_device.c @@ -218,9 +218,8 @@ static void uart_irq(int id) } 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); - volatile uint32_t tmpval = huart->Instance->RDR; // Clear RXNE flag - UNUSED(tmpval); + 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) { diff --git a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c index 18bc9532b3..84b4667fbd 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c @@ -282,7 +282,7 @@ static void uart_irq(int id) 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); - __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_RXNE); + /* Flag has been cleared when reading the content */ } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { diff --git a/targets/TARGET_STM/TARGET_STM32F7/serial_device.c b/targets/TARGET_STM/TARGET_STM32F7/serial_device.c index c021fbc817..fcc7a05ef7 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F7/serial_device.c @@ -247,7 +247,7 @@ static void uart_irq(int id) 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); - volatile uint32_t tmpval = huart->Instance->RDR; // Clear RXNE + /* Flag has been cleared when reading the content */ } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { diff --git a/targets/TARGET_STM/TARGET_STM32L0/serial_device.c b/targets/TARGET_STM/TARGET_STM32L0/serial_device.c index a1d645775f..adc808d245 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32L0/serial_device.c @@ -210,7 +210,7 @@ static void uart_irq(int id) 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); - volatile uint32_t tmpval = huart->Instance->RDR; // Clear RXNE flag + /* Flag has been cleared when reading the content */ } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { diff --git a/targets/TARGET_STM/TARGET_STM32L1/serial_device.c b/targets/TARGET_STM/TARGET_STM32L1/serial_device.c index fef55bed57..f21b10fcc2 100755 --- a/targets/TARGET_STM/TARGET_STM32L1/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32L1/serial_device.c @@ -199,7 +199,7 @@ static void uart_irq(int id) 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); - __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_RXNE); + /* Flag has been cleared when reading the content */ } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { diff --git a/targets/TARGET_STM/TARGET_STM32L4/serial_device.c b/targets/TARGET_STM/TARGET_STM32L4/serial_device.c index 211df49ab0..bcb9bc5e74 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32L4/serial_device.c @@ -225,8 +225,8 @@ static void uart_irq(int id) } 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); - __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_RXNE); + 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) { From b5cbaaa0d09ffc77a74bed83a35ca72828b455eb Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Mon, 10 Jul 2017 18:33:46 +0200 Subject: [PATCH 2/2] STM32: Serial - use TXE as tx_irq instead of TC TXE indicates that a byte can be written to UART register for sending, while TC indicates that last byte was completely sent. So the TXE flag can be used in case of interrupt based Serial communication, to allow faster and efficient application buffer emptying. Also TXE flag will be erased from the interrupt when writing to register. In case there is nothing to write in the register, the application is expected to disable the interrupt. --- targets/TARGET_STM/TARGET_STM32F0/serial_device.c | 9 ++++----- targets/TARGET_STM/TARGET_STM32F1/serial_device.c | 9 ++++----- targets/TARGET_STM/TARGET_STM32F2/serial_device.c | 9 ++++----- targets/TARGET_STM/TARGET_STM32F3/serial_device.c | 9 ++++----- targets/TARGET_STM/TARGET_STM32F4/serial_device.c | 9 ++++----- targets/TARGET_STM/TARGET_STM32F7/serial_device.c | 9 ++++----- targets/TARGET_STM/TARGET_STM32L0/serial_device.c | 9 ++++----- targets/TARGET_STM/TARGET_STM32L1/serial_device.c | 9 ++++----- targets/TARGET_STM/TARGET_STM32L4/serial_device.c | 11 +++++------ 9 files changed, 37 insertions(+), 46 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F0/serial_device.c b/targets/TARGET_STM/TARGET_STM32F0/serial_device.c index ccce38abbf..600642db78 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F0/serial_device.c @@ -264,10 +264,9 @@ static void uart_irq(int id) UART_HandleTypeDef * huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) { + 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); - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF); } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { @@ -418,7 +417,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) if (irq == RxIrq) { __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); } else { // TxIrq - __HAL_UART_ENABLE_IT(huart, UART_IT_TC); + __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -432,7 +431,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) all_disabled = 1; } } else { // TxIrq - __HAL_UART_DISABLE_IT(huart, UART_IT_TC); + __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); // Check if RxIrq is disabled too if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) { all_disabled = 1; diff --git a/targets/TARGET_STM/TARGET_STM32F1/serial_device.c b/targets/TARGET_STM/TARGET_STM32F1/serial_device.c index b91deaf36e..d427407f6d 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F1/serial_device.c @@ -159,10 +159,9 @@ static void uart_irq(int id) UART_HandleTypeDef * huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) { + 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); - __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { @@ -228,7 +227,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) if (irq == RxIrq) { __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); } else { // TxIrq - __HAL_UART_ENABLE_IT(huart, UART_IT_TC); + __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -242,7 +241,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) all_disabled = 1; } } else { // TxIrq - __HAL_UART_DISABLE_IT(huart, UART_IT_TC); + __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); // Check if RxIrq is disabled too if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) { all_disabled = 1; diff --git a/targets/TARGET_STM/TARGET_STM32F2/serial_device.c b/targets/TARGET_STM/TARGET_STM32F2/serial_device.c index 9b334908a4..c1d1682693 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F2/serial_device.c @@ -246,10 +246,9 @@ static void uart_irq(int id) UART_HandleTypeDef * huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) { + 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); - __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { @@ -385,7 +384,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) if (irq == RxIrq) { __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); } else { // TxIrq - __HAL_UART_ENABLE_IT(huart, UART_IT_TC); + __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -399,7 +398,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) all_disabled = 1; } } else { // TxIrq - __HAL_UART_DISABLE_IT(huart, UART_IT_TC); + __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); // Check if RxIrq is disabled too if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) { all_disabled = 1; diff --git a/targets/TARGET_STM/TARGET_STM32F3/serial_device.c b/targets/TARGET_STM/TARGET_STM32F3/serial_device.c index eaa491483e..be5660a086 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F3/serial_device.c @@ -210,10 +210,9 @@ static void uart_irq(int id) UART_HandleTypeDef * huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) { + 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); - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF); } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { @@ -311,7 +310,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) if (irq == RxIrq) { __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); } else { // TxIrq - __HAL_UART_ENABLE_IT(huart, UART_IT_TC); + __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -325,7 +324,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) all_disabled = 1; } } else { // TxIrq - __HAL_UART_DISABLE_IT(huart, UART_IT_TC); + __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); // Check if RxIrq is disabled too if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) { all_disabled = 1; diff --git a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c index 84b4667fbd..0cf5d9be77 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F4/serial_device.c @@ -273,10 +273,9 @@ static void uart_irq(int id) UART_HandleTypeDef * huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) { + 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); - __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { @@ -438,7 +437,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) if (irq == RxIrq) { __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); } else { // TxIrq - __HAL_UART_ENABLE_IT(huart, UART_IT_TC); + __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -452,7 +451,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) all_disabled = 1; } } else { // TxIrq - __HAL_UART_DISABLE_IT(huart, UART_IT_TC); + __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); // Check if RxIrq is disabled too if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) { all_disabled = 1; diff --git a/targets/TARGET_STM/TARGET_STM32F7/serial_device.c b/targets/TARGET_STM/TARGET_STM32F7/serial_device.c index fcc7a05ef7..34f4fc7bbe 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32F7/serial_device.c @@ -238,10 +238,9 @@ static void uart_irq(int id) UART_HandleTypeDef * huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) { + 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); - __HAL_UART_CLEAR_IT(huart, UART_CLEAR_TCF); } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { @@ -373,7 +372,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) if (irq == RxIrq) { __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); } else { // TxIrq - __HAL_UART_ENABLE_IT(huart, UART_IT_TC); + __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -387,7 +386,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) all_disabled = 1; } } else { // TxIrq - __HAL_UART_DISABLE_IT(huart, UART_IT_TC); + __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); // Check if RxIrq is disabled too if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) { all_disabled = 1; diff --git a/targets/TARGET_STM/TARGET_STM32L0/serial_device.c b/targets/TARGET_STM/TARGET_STM32L0/serial_device.c index adc808d245..cef163dd9c 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32L0/serial_device.c @@ -201,10 +201,9 @@ static void uart_irq(int id) UART_HandleTypeDef * huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) { + 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); - __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF); } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { @@ -302,7 +301,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) if (irq == RxIrq) { __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); } else { // TxIrq - __HAL_UART_ENABLE_IT(huart, UART_IT_TC); + __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -316,7 +315,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) all_disabled = 1; } } else { // TxIrq - __HAL_UART_DISABLE_IT(huart, UART_IT_TC); + __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); // Check if RxIrq is disabled too if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) { all_disabled = 1; diff --git a/targets/TARGET_STM/TARGET_STM32L1/serial_device.c b/targets/TARGET_STM/TARGET_STM32L1/serial_device.c index f21b10fcc2..3859270e22 100755 --- a/targets/TARGET_STM/TARGET_STM32L1/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32L1/serial_device.c @@ -190,10 +190,9 @@ static void uart_irq(int id) UART_HandleTypeDef * huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { - if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) { + 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); - __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { @@ -284,7 +283,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) if (irq == RxIrq) { __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); } else { // TxIrq - __HAL_UART_ENABLE_IT(huart, UART_IT_TC); + __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -298,7 +297,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) all_disabled = 1; } } else { // TxIrq - __HAL_UART_DISABLE_IT(huart, UART_IT_TC); + __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); // Check if RxIrq is disabled too if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) { all_disabled = 1; diff --git a/targets/TARGET_STM/TARGET_STM32L4/serial_device.c b/targets/TARGET_STM/TARGET_STM32L4/serial_device.c index bcb9bc5e74..cb3354530a 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/serial_device.c +++ b/targets/TARGET_STM/TARGET_STM32L4/serial_device.c @@ -217,10 +217,9 @@ static void uart_irq(int id) UART_HandleTypeDef * huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { - if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { - if (__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) { - irq_handler(serial_irq_ids[id], TxIrq); - __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); + 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) { @@ -330,7 +329,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) if (irq == RxIrq) { __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); } else { // TxIrq - __HAL_UART_ENABLE_IT(huart, UART_IT_TC); + __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -344,7 +343,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) all_disabled = 1; } } else { // TxIrq - __HAL_UART_DISABLE_IT(huart, UART_IT_TC); + __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); // Check if RxIrq is disabled too if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) { all_disabled = 1;