From 5a7cee9f422afca50d67ef707c292ff7db80428f Mon Sep 17 00:00:00 2001 From: 94xhn <87560781+94xhn@users.noreply.github.com> Date: Sat, 11 Jul 2026 05:30:32 +0800 Subject: [PATCH] fix(UART_ReceptionToIdle_CircularDMA): wrap old_pos to fix silent data loss HAL_UARTEx_RxEventCallback() tracks the last-processed position in the circular DMA Rx buffer via old_pos, and compares it against Size to detect whether new data has arrived (`if (Size != old_pos)`). On a Transfer Complete event (a full circular buffer), the HAL reports Size == huart->RxXferSize == RX_BUFFER_SIZE, not a wrapped 0 (confirmed in stm32f4xx_hal_uart.c's UART_DMAReceiveCplt(), which calls HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize) for HAL_UART_RECEPTION_TOIDLE mode). Since old_pos was previously set to `old_pos = Size;` unwrapped, after one full-buffer cycle old_pos is left at RX_BUFFER_SIZE. On every subsequent full-buffer Transfer Complete event, Size == old_pos == RX_BUFFER_SIZE, so the `Size != old_pos` check spuriously evaluates false and the entire buffer's worth of newly received data is silently dropped without calling UserDataTreatment(). Wrap old_pos with `% RX_BUFFER_SIZE` so it stays a valid index into aRXBufferUser, matching the actual wrapped position. Fixes #196 Signed-off-by: 94xhn <87560781+94xhn@users.noreply.github.com> --- .../UART/UART_ReceptionToIdle_CircularDMA/Src/main.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Projects/STM32446E-Nucleo/Examples/UART/UART_ReceptionToIdle_CircularDMA/Src/main.c b/Projects/STM32446E-Nucleo/Examples/UART/UART_ReceptionToIdle_CircularDMA/Src/main.c index 3cb27163e2..3092b9d34d 100644 --- a/Projects/STM32446E-Nucleo/Examples/UART/UART_ReceptionToIdle_CircularDMA/Src/main.c +++ b/Projects/STM32446E-Nucleo/Examples/UART/UART_ReceptionToIdle_CircularDMA/Src/main.c @@ -392,8 +392,11 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) pBufferReadyForReception = ptemp; } /* Update old_pos as new reference of position in User Rx buffer that - indicates position to which data have been processed */ - old_pos = Size; + indicates position to which data have been processed. + Size can be equal to RX_BUFFER_SIZE (not wrapped to 0) when the + Transfer Complete event reports a full circular buffer, so it must be + wrapped here to stay a valid index into aRXBufferUser. */ + old_pos = Size % RX_BUFFER_SIZE; }