Skip to main content
1123
Associate II
February 26, 2025
Solved

CAN Bus Error Handling and Classification

  • February 26, 2025
  • 4 replies
  • 1861 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!

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

Karl Yamashita
Principal
February 26, 2025

Wouldn't you know it, it does work

CAN Bus Error Handling and Classification 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
mƎALLEm
mƎALLEmBest answer
ST 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.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Andrew Neil
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 ...

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
1123
1123Author
Associate 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?

mƎALLEm
ST 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.

 

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.