Skip to main content
Visitor II
June 29, 2020
Solved

F042K6 - stuck in CAN receive interrupt

  • June 29, 2020
  • 3 replies
  • 4624 views

Hello,

I am trying to set up CAN communication on my F042K6 using the HAL library. I have followed the stm guide (attached). I got most of the functionality working, however if i try to use the Rx interrupt using "void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef*hcan)" the MCU gets stuck in this interrupt when it receives a message. I do not know what i am doing wrong and why I cannot leave the interrupt. I think it might have something to do with the NVIC interrupt priorities maybe? If someone could help me out it would be greatly appreciated. Please see attached my code, NVIC settings and the guide i followed.

Thank you:)

    This topic has been closed for replies.
    Best answer by retro

    For anyone that runs into a simillar issue:

    I found the problem, it was just me being an idiot. I wrote: HAL_CAN_GetRxMessage(&hcan,CAN_RX_FIFO0,&RxHeader,RxData); instead of HAL_CAN_GetRxMessage(hcan,CAN_RX_FIFO0,&RxHeader,RxData); so had the extra &. The GetRxMessage also clears the message from the buffer so the interrupt kept cycling as it was seeing a message in the buffer as per Cortex-M mechanics

    3 replies

    Graduate II
    June 29, 2020

    The mechanics of the Cortex-M parts is that the interrupts will tail-chain endlessly if you fail to service the source completely. See Interrupt Storm

    Dump registers of offending peripheral and NVIC, understand what is being left pending/flagging.

    The FIFO can contain more than one item, if the servicing gets diverted into other work more data may arrive in the mean time.

    If you have no new data from a TXE interrupt, disable it until you do.

    retroAuthor
    Visitor II
    June 29, 2020

    I am only sending one CAN message so the Rx FIFO should only have that message in it.

    Visitor II
    June 29, 2020

    Hi,

    I propose remove from interrupt function HAL_CAN_RxFifo0MsgPendingCallback call to the HAL_Delay(1000).

    void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef*hcan)
    {
    	/*add error handler here*/
    	HAL_CAN_GetRxMessage(&hcan,CAN_RX_FIFO0,&RxHeader,RxData);
    	HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
    	/*HAL_CAN_MspDeInit(hcan);*/
    }

    retroAuthor
    Visitor II
    June 29, 2020

    I tried that already but it has no effect.​

    Visitor II
    June 29, 2020

    Hi,

    Please remove from function "config_CAN_interrupt" interrupt "CAN_IT_TX_MAILBOX_EMPTY" and check again.

    static void config_CAN_interrupt(void)
    {
     
    	if(HAL_CAN_Start(&hcan)!=HAL_OK) /*start CAN*/
    	 {
    		 Error_Handler();
    	 }
     
    	 if(HAL_CAN_ActivateNotification(&hcan,CAN_IT_RX_FIFO0_MSG_PENDING) !=HAL_OK) /*configure CAN interrupt*/
    	 {
    		 Error_Handler();
    	 }
    }

    retroAuthorAnswer
    Visitor II
    June 30, 2020

    For anyone that runs into a simillar issue:

    I found the problem, it was just me being an idiot. I wrote: HAL_CAN_GetRxMessage(&hcan,CAN_RX_FIFO0,&RxHeader,RxData); instead of HAL_CAN_GetRxMessage(hcan,CAN_RX_FIFO0,&RxHeader,RxData); so had the extra &. The GetRxMessage also clears the message from the buffer so the interrupt kept cycling as it was seeing a message in the buffer as per Cortex-M mechanics