Skip to content

Commit 5d1d233

Browse files
committed
[HAL][UART] Provide accurate position in RxEventCallback when ReceptionToIdle mode is used with DMA, when UART and DMA interrupts process is delayed
1 parent a2d2cf5 commit 5d1d233

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

Src/stm32c0xx_hal_uart.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3739,12 +3739,24 @@ static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
37393739
If Reception till IDLE event has been selected : use Rx Event callback */
37403740
if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
37413741
{
3742+
huart->RxXferCount = 0;
3743+
3744+
/* Check current nb of data still to be received on DMA side.
3745+
DMA Normal mode, remaining nb of data will be 0
3746+
DMA Circular mode, remaining nb of data is reset to RxXferSize */
3747+
uint16_t nb_remaining_rx_data = (uint16_t) __HAL_DMA_GET_COUNTER(hdma);
3748+
if (nb_remaining_rx_data < huart->RxXferSize)
3749+
{
3750+
/* Update nb of remaining data */
3751+
huart->RxXferCount = nb_remaining_rx_data;
3752+
}
3753+
37423754
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
37433755
/*Call registered Rx Event callback*/
3744-
huart->RxEventCallback(huart, huart->RxXferSize);
3756+
huart->RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
37453757
#else
37463758
/*Call legacy weak Rx Event callback*/
3747-
HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
3759+
HAL_UARTEx_RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
37483760
#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
37493761
}
37503762
else
@@ -3777,12 +3789,22 @@ static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
37773789
If Reception till IDLE event has been selected : use Rx Event callback */
37783790
if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
37793791
{
3792+
huart->RxXferCount = huart->RxXferSize / 2U;
3793+
3794+
/* Check current nb of data still to be received on DMA side. */
3795+
uint16_t nb_remaining_rx_data = (uint16_t) __HAL_DMA_GET_COUNTER(hdma);
3796+
if (nb_remaining_rx_data <= huart->RxXferSize)
3797+
{
3798+
/* Update nb of remaining data */
3799+
huart->RxXferCount = nb_remaining_rx_data;
3800+
}
3801+
37803802
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
37813803
/*Call registered Rx Event callback*/
3782-
huart->RxEventCallback(huart, huart->RxXferSize / 2U);
3804+
huart->RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
37833805
#else
37843806
/*Call legacy weak Rx Event callback*/
3785-
HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize / 2U);
3807+
HAL_UARTEx_RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
37863808
#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
37873809
}
37883810
else

0 commit comments

Comments
 (0)