From 700ba6a4add565a95ef37530ac90f6dbd8117037 Mon Sep 17 00:00:00 2001 From: Fredrik Thulin Date: Thu, 23 Mar 2017 16:38:01 +0100 Subject: Fix UART RX communication errors resulting in byte swapping. Sometimes, the Half-Transfer Complete and Transfer Complete events execute in the wrong order. This happens when both the TC and HT bits get set before HAL_DMA_IRQHandler() runs. Don't know what causes that, but this mitigates the problem with a separate read index for the uart_rx buffer. --- projects/hsm/hsm.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/projects/hsm/hsm.c b/projects/hsm/hsm.c index 60e35fc..9a314ff 100644 --- a/projects/hsm/hsm.c +++ b/projects/hsm/hsm.c @@ -118,6 +118,7 @@ void hal_ks_unlock(void) { osMutexRelease(ks_mutex); } #endif static uint8_t uart_rx[2]; /* current character received from UART */ +static uint32_t uart_rx_idx = 0; /* Callback for HAL_UART_Receive_DMA(). * With multiple worker threads, we can't do a blocking receive, because @@ -157,12 +158,14 @@ static void RxCallback(uint8_t c) void HAL_UART2_RxHalfCpltCallback(UART_HandleTypeDef *huart) { - RxCallback(uart_rx[0]); + RxCallback(uart_rx[uart_rx_idx]); + uart_rx_idx ^= 1; } void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart) { - RxCallback(uart_rx[1]); + RxCallback(uart_rx[uart_rx_idx]); + uart_rx_idx ^= 1; } void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) -- cgit v1.2.3