Skip to content

Commit 60a5d3c

Browse files
committed
[HAL][FDCAN] Fix HAL_FDCAN_GetRxMessage GetIndex computation overflow in overwrite mode
1 parent 1363c24 commit 60a5d3c

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

Src/stm32h7xx_hal_fdcan.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2973,19 +2973,20 @@ HAL_StatusTypeDef HAL_FDCAN_GetRxMessage(FDCAN_HandleTypeDef *hfdcan, uint32_t R
29732973
}
29742974
else
29752975
{
2976+
/* Calculate Rx FIFO 0 element index */
2977+
GetIndex = ((hfdcan->Instance->RXF0S & FDCAN_RXF0S_F0GI) >> FDCAN_RXF0S_F0GI_Pos);
2978+
29762979
/* Check that the Rx FIFO 0 is full & overwrite mode is on */
29772980
if (((hfdcan->Instance->RXF0S & FDCAN_RXF0S_F0F) >> FDCAN_RXF0S_F0F_Pos) == 1U)
29782981
{
29792982
if (((hfdcan->Instance->RXF0C & FDCAN_RXF0C_F0OM) >> FDCAN_RXF0C_F0OM_Pos) == FDCAN_RX_FIFO_OVERWRITE)
29802983
{
29812984
/* When overwrite status is on discard first message in FIFO */
2982-
GetIndex = 1U;
2985+
/* GetIndex is incremented by one and wraps to 0 in case it overflows the FIFO size */
2986+
GetIndex = (GetIndex + 1U) & ((hfdcan->Instance->RXF0C & FDCAN_RXF0C_F0S) >> FDCAN_RXF0C_F0S_Pos);
29832987
}
29842988
}
29852989

2986-
/* Calculate Rx FIFO 0 element index */
2987-
GetIndex += ((hfdcan->Instance->RXF0S & FDCAN_RXF0S_F0GI) >> FDCAN_RXF0S_F0GI_Pos);
2988-
29892990
/* Calculate Rx FIFO 0 element address */
29902991
RxAddress = (uint32_t *)(hfdcan->msgRam.RxFIFO0SA + (GetIndex * hfdcan->Init.RxFifo0ElmtSize * 4U));
29912992
}
@@ -3011,19 +3012,20 @@ HAL_StatusTypeDef HAL_FDCAN_GetRxMessage(FDCAN_HandleTypeDef *hfdcan, uint32_t R
30113012
}
30123013
else
30133014
{
3015+
/* Calculate Rx FIFO 1 element index */
3016+
GetIndex = ((hfdcan->Instance->RXF1S & FDCAN_RXF1S_F1GI) >> FDCAN_RXF1S_F1GI_Pos);
3017+
30143018
/* Check that the Rx FIFO 1 is full & overwrite mode is on */
30153019
if (((hfdcan->Instance->RXF1S & FDCAN_RXF1S_F1F) >> FDCAN_RXF1S_F1F_Pos) == 1U)
30163020
{
30173021
if (((hfdcan->Instance->RXF1C & FDCAN_RXF1C_F1OM) >> FDCAN_RXF1C_F1OM_Pos) == FDCAN_RX_FIFO_OVERWRITE)
30183022
{
30193023
/* When overwrite status is on discard first message in FIFO */
3020-
GetIndex = 1U;
3024+
/* GetIndex is incremented by one and wraps to 0 in case it overflows the FIFO size */
3025+
GetIndex = (GetIndex + 1U) & ((hfdcan->Instance->RXF1C & FDCAN_RXF1C_F1S) >> FDCAN_RXF1C_F1S_Pos);
30213026
}
30223027
}
30233028

3024-
/* Calculate Rx FIFO 1 element index */
3025-
GetIndex += ((hfdcan->Instance->RXF1S & FDCAN_RXF1S_F1GI) >> FDCAN_RXF1S_F1GI_Pos);
3026-
30273029
/* Calculate Rx FIFO 1 element address */
30283030
RxAddress = (uint32_t *)(hfdcan->msgRam.RxFIFO1SA + (GetIndex * hfdcan->Init.RxFifo1ElmtSize * 4U));
30293031
}

0 commit comments

Comments
 (0)