AnsweredAssumed Answered

BUG: CAN race condition if HAL_CAN_Receive_IT is used

Question asked by Vladimir Romanov on Apr 15, 2015
Latest reply on Apr 17, 2015 by Burton.Mark
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]) {
        } else {
    /* 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