cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Bus Error Handling and Classification

1123
Associate II

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!

6 REPLIES 6
Karl Yamashita
Principal

Wouldn't you know it, it does work

CAN Bus Error Handling and Classification 

Don't worry, I won't byte.
TimerCallback tutorial! | UART and DMA Idle tutorial!

If you find my solution useful, please click the Accept as Solution so others see the solution.
mƎALLEm
ST Employee

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 on "Accept as Solution" on the reply which solved your issue or answered your question.

@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 ...

 

1123
Associate II

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?

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 on "Accept as Solution" on the reply which solved your issue or answered your question.

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