How to escape CAN error passive state on STM32F4 microcontroler?
Hi there!
I have trouble escaping CAN error passive state on STM32F417VG microcontroller.
The goal of my application is to automatically detect external CAN bus baud rate (either 250kHz or 500kHz) then send CAN messages on the bus at the right frequency.
To do so, I first initialize my CAN peripheral in silent mode with 250kHz frequency. I then wait a while and check if an error occurred in HAL_CAN_ErrorCallback (meaning real bus CAN frequency is not 250kHz).
I then follow the same process with 500kHz silent mode and check for error triggering (meaning real bus CAN frequency is not 500kHz).
From that point, I am able to know which is the CAN frequency used on my external bus CAN.
However, that method implies error triggering leading the "Receive error counter " CAN1->ESR->REC to reach its maximal value of 0xFF.
The reference manual says that "in case of an error during reception, this counter is incremented by 1 or by 8 depending on the error condition as defined by the CAN standard. After every successful reception the counter is decremented by 1 or reset to 120 if its value was higher than 128. When the counter value exceeds 127, the CAN controller enters the error passive state.".
In my case, the counter value exceeds 127. The CAN controller thus enters the error passive state and set CAN1->ESR->EWGF and CAN1->ESR->EPVF bits.
FYI :
- CAN1->ESR->EWGF: This bit is set by hardware when the warning limit has been reached (Receive Error Counter or Transmit Error Counter≥96).
- CAN1->ESR->EPVF: This bit is set by hardware when the Error Passive limit has been reached (Receive Error Counter or Transmit Error Counter>127).
As long as one of these two bits are set, I cannot send/receive CAN messages.
If other nodes send CAN messages on the bus, I can see CAN1->ESR->REC being decremented. At a certain point, CAN1->ESR->REC is small enough for CAN1->ESR->EWGF and CAN1->ESR->EPVF bits to be reset. I then can successfully send/receive CAN messages.
However, in my case, other nodes do not communicate very often. As a result, CAN1->ESR->REC decrement is very slow, and my application is blocked during a long time.
Does somebody have a solution to escape that CAN error passive state? FYI, CAN1->ESR->REC register and CAN1->ESR->EWGF and CAN1->ESR->EPVF bits are read only.
I found some other threads about similar issues, but I did not find any solution other than resetting the MCU.
Thank you for your help,
Have a nice day,
Aubin
