Skip to main content
Visitor II
February 26, 2025
Solved

CAN Bus Error Handling and Classification

  • February 26, 2025
  • 4 replies
  • 1856 views

Hi all,

I am currently using the STM32F0 MCU. I would like to understand how errors are handled when the CAN bus triggers errors. Specifically, how is the error handling process for each error category implemented? Additionally, how can the system recover to normal operation mode?

Are there any related example codes or documentation that you can provide for reference? Thank you very much!

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    From the reference manual:

    mALLEm_0-1740558944563.png

    For the BusOff recovery you need to activate the automatic bus off recovery by hardware.

    To detect CAN errors, activate CAN_SCE interrupt:

     HAL_NVIC_EnableIRQ(CANx_SCE_IRQn);	
     HAL_CAN_ActivateNotification(hcanx, CAN_IT_ERROR_WARNING | CAN_IT_ERROR_PASSIVE | CAN_IT_BUSOFF | CAN_IT_LAST_ERROR_CODE | CAN_IT_ERROR);

    There is no ready to use example for CAN error management.

    4 replies

    Graduate II
    February 26, 2025

    Wouldn't you know it, it does work

    CAN Bus Error Handling and Classification 

    mƎALLEmAnswer
    Technical Moderator
    February 26, 2025

    Hello,

    From the reference manual:

    mALLEm_0-1740558944563.png

    For the BusOff recovery you need to activate the automatic bus off recovery by hardware.

    To detect CAN errors, activate CAN_SCE interrupt:

     HAL_NVIC_EnableIRQ(CANx_SCE_IRQn);	
     HAL_CAN_ActivateNotification(hcanx, CAN_IT_ERROR_WARNING | CAN_IT_ERROR_PASSIVE | CAN_IT_BUSOFF | CAN_IT_LAST_ERROR_CODE | CAN_IT_ERROR);

    There is no ready to use example for CAN error management.

    Super User
    February 26, 2025

    @1123 wrote:

    the STM32F0 MCU.


    "STM32F0" is an entire family of MCUs - so which one, specifically, are you using? Please give the full part number.

    As @mƎALLEm suggests, the Reference Manual for your particular chip will fully describe its CAN features ...

     

    1123Author
    Visitor II
    February 26, 2025

    Thank you for the information provided. I would like to ask another question.

    When the receive error counter (CANREC) or transmit error counter (CANTEC) of the CAN module reaches 128, it automatically enters the "Bus-off" state, meaning the bus goes offline. Currently, the method to handle this situation is to use can_deinit to reinitialize the CAN module and clear the error state.

    My question is, is it not possible to handle specific errors individually (e.g., bit stuffing error or form error)? And is using can_deinit the most direct solution?

    Technical Moderator
    February 26, 2025

    As I said previously, and the most important thing is you need to enable the automatic busoff to recover from it automatically by HW. You don't need to intervene to recover from the busoff situation. The recovery depends on the bus situation and the occurrence of 128*11 consecutive recessive bits.

    The other errors are indicative, and I don't think you can intervene by software to recover from that specific error.