aboutsummaryrefslogtreecommitdiff
path: root/projects
diff options
context:
space:
mode:
authorFredrik Thulin <fredrik@thulin.net>2017-03-23 16:38:01 +0100
committerFredrik Thulin <fredrik@thulin.net>2017-03-23 19:25:32 +0100
commit700ba6a4add565a95ef37530ac90f6dbd8117037 (patch)
treebae83f7d5b0a112039e35fc97effe795bed80d5e /projects
parent7a8a2564c64894026e6e79eb116f5b8b358d622c (diff)
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.
Diffstat (limited to 'projects')
-rw-r--r--projects/hsm/hsm.c7
1 files 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)