[Cryptech-Commits] [sw/stm32] 01/02: Fix UART RX communication errors resulting in byte swapping.

git at cryptech.is git at cryptech.is
Thu Mar 23 22:03:24 UTC 2017


This is an automated email from the git hooks/post-receive script.

fredrik at thulin.net pushed a commit to branch ft-crc32
in repository sw/stm32.

commit 700ba6a4add565a95ef37530ac90f6dbd8117037
Author: Fredrik Thulin <fredrik at thulin.net>
AuthorDate: Thu Mar 23 16:38:01 2017 +0100

    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)



More information about the Commits mailing list