From a74df885670cc58a16a8b0183d90d05618b90984 Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Thu, 14 Sep 2023 14:04:48 +0200 Subject: [PATCH] [SERIAL] Fix STM HAL DMA received size computation datarace in case of DATA RX during computation. --- network/serial_network/HAL/STM32F0/serial_network_hal.c | 3 +++ network/serial_network/HAL/STM32F4/serial_network_hal.c | 3 +++ network/serial_network/HAL/STM32G431/serial_network_hal.c | 3 +++ network/serial_network/HAL/STM32G474/serial_network_hal.c | 3 +++ network/serial_network/HAL/STM32L4/serial_network_hal.c | 3 +++ 5 files changed, 15 insertions(+) diff --git a/network/serial_network/HAL/STM32F0/serial_network_hal.c b/network/serial_network/HAL/STM32F0/serial_network_hal.c index 34f66a071..705d6a573 100644 --- a/network/serial_network/HAL/STM32F0/serial_network_hal.c +++ b/network/serial_network/HAL/STM32F0/serial_network_hal.c @@ -139,6 +139,9 @@ void SerialHAL_Loop(void) { if (SERIAL_RX_DMA_TC(SERIAL_RX_DMA) != RESET) // DMA buffer overflow { + // Recompute the RX_PointerPosition to be sure to get the DMA buffer after the overflow. + // If the RX_PointerPosition was taken just before the overflow we may compute this with a wrong value. + RX_PointerPosition = rx_buffer_size - LL_DMA_GetDataLength(SERIAL_RX_DMA, SERIAL_RX_DMA_CHANNEL); SERIAL_RX_DMA_CLEAR_TC(SERIAL_RX_DMA); size = (rx_buffer_size - RX_PrevPointerPosition) + RX_PointerPosition; } diff --git a/network/serial_network/HAL/STM32F4/serial_network_hal.c b/network/serial_network/HAL/STM32F4/serial_network_hal.c index 4b21d53af..cb38b3706 100644 --- a/network/serial_network/HAL/STM32F4/serial_network_hal.c +++ b/network/serial_network/HAL/STM32F4/serial_network_hal.c @@ -142,6 +142,9 @@ void SerialHAL_Loop(void) { if (SERIAL_RX_DMA_TC(SERIAL_RX_DMA) != RESET) // DMA buffer overflow { + // Recompute the RX_PointerPosition to be sure to get the DMA buffer after the overflow. + // If the RX_PointerPosition was taken just before the overflow we may compute this with a wrong value. + RX_PointerPosition = rx_buffer_size - LL_DMA_GetDataLength(SERIAL_RX_DMA, SERIAL_RX_DMA_CHANNEL); SERIAL_RX_DMA_CLEAR_TC(SERIAL_RX_DMA); size = (rx_buffer_size - RX_PrevPointerPosition) + RX_PointerPosition; } diff --git a/network/serial_network/HAL/STM32G431/serial_network_hal.c b/network/serial_network/HAL/STM32G431/serial_network_hal.c index 05b93944a..035565c34 100644 --- a/network/serial_network/HAL/STM32G431/serial_network_hal.c +++ b/network/serial_network/HAL/STM32G431/serial_network_hal.c @@ -142,6 +142,9 @@ void SerialHAL_Loop(void) { if (SERIAL_RX_DMA_TC(SERIAL_RX_DMA) != RESET) // DMA buffer overflow { + // Recompute the RX_PointerPosition to be sure to get the DMA buffer after the overflow. + // If the RX_PointerPosition was taken just before the overflow we may compute this with a wrong value. + RX_PointerPosition = rx_buffer_size - LL_DMA_GetDataLength(SERIAL_RX_DMA, SERIAL_RX_DMA_CHANNEL); SERIAL_RX_DMA_CLEAR_TC(SERIAL_RX_DMA); size = (rx_buffer_size - RX_PrevPointerPosition) + RX_PointerPosition; } diff --git a/network/serial_network/HAL/STM32G474/serial_network_hal.c b/network/serial_network/HAL/STM32G474/serial_network_hal.c index 726eceb51..589048861 100644 --- a/network/serial_network/HAL/STM32G474/serial_network_hal.c +++ b/network/serial_network/HAL/STM32G474/serial_network_hal.c @@ -142,6 +142,9 @@ void SerialHAL_Loop(void) { if (SERIAL_RX_DMA_TC(SERIAL_RX_DMA) != RESET) // DMA buffer overflow { + // Recompute the RX_PointerPosition to be sure to get the DMA buffer after the overflow. + // If the RX_PointerPosition was taken just before the overflow we may compute this with a wrong value. + RX_PointerPosition = rx_buffer_size - LL_DMA_GetDataLength(SERIAL_RX_DMA, SERIAL_RX_DMA_CHANNEL); SERIAL_RX_DMA_CLEAR_TC(SERIAL_RX_DMA); size = (rx_buffer_size - RX_PrevPointerPosition) + RX_PointerPosition; } diff --git a/network/serial_network/HAL/STM32L4/serial_network_hal.c b/network/serial_network/HAL/STM32L4/serial_network_hal.c index 71ae31a07..60ac5ac66 100644 --- a/network/serial_network/HAL/STM32L4/serial_network_hal.c +++ b/network/serial_network/HAL/STM32L4/serial_network_hal.c @@ -141,6 +141,9 @@ void SerialHAL_Loop(void) { if (SERIAL_RX_DMA_TC(SERIAL_RX_DMA) != RESET) // DMA buffer overflow { + // Recompute the RX_PointerPosition to be sure to get the DMA buffer after the overflow. + // If the RX_PointerPosition was taken just before the overflow we may compute this with a wrong value. + RX_PointerPosition = rx_buffer_size - LL_DMA_GetDataLength(SERIAL_RX_DMA, SERIAL_RX_DMA_CHANNEL); SERIAL_RX_DMA_CLEAR_TC(SERIAL_RX_DMA); size = (rx_buffer_size - RX_PrevPointerPosition) + RX_PointerPosition; }