Associate III
April 15, 2015
Question
BUG: CAN race condition if HAL_CAN_Receive_IT is used
- April 15, 2015
- 6 replies
- 1716 views
Posted on April 15, 2015 at 22:27
If you use HAL_CAN_Receive_IT for receive and send messages at same time you can get race condition. Typical callback looks like:
void
HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* CanHandle) {
if
((CanHandle->pRxMsg->StdId == 0x321) && (CanHandle->pRxMsg->IDE == CAN_ID_STD) && (CanHandle->pRxMsg->DLC == 2)) {
if
(CanHandle->pRxMsg->Data[0]) {
LED_GREEN_ON();
}
else
{
LED_GREEN_OFF();
}
}
/* Receive */
if
(HAL_CAN_Receive_IT(CanHandle, CAN_FIFO0) != HAL_OK) {
/* Reception Error */
Error_Handler(1, 20);
}
} If interrupt occurs when HAL_CAN_Transmit (or other) is called,
HAL_CAN_Receive_IT will return HAL_BUSY because can handle is locked. I can reproduce this error in 100% if loopback mode is used.
I attach sample code to reproduce this. my_main() must be called inside generated main.c
#cubemx #bug #bxcan #stm32f2