2025-02-25 5:18 PM - last edited on 2025-02-26 12:34 AM by mƎALLEm
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!
2025-02-25 10:52 PM
Wouldn't you know it, it does work
CAN Bus Error Handling and Classification
2025-02-26 12:46 AM
Hello,
From the reference manual:
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.
2025-02-26 1:26 AM
2025-02-26 1:36 AM
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?
2025-02-26 1:40 AM - edited 2025-02-26 1:58 AM
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.
2025-02-26 1:49 AM
As mentioned, the error handling is covered by the CAN bus specification, to which STM32 CAN peripherals adhere to. AFAIK through licencing a Bosch CAN peripheral IP (the silicon block), which handles this errors by itself.
> 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.
No.
First, the limits for those errors are user-configurable and can be set in the respective CAN config registers.
The value 128 is the default for "error passive", which is not "bus off". In "error passive", a node only stops transmissions until the error counter falls below the threshold. The "bus off" limit is >255.
Check out something like this, for instance : https://www.csselectronics.com/pages/can-bus-errors-intro-tutorial