Question
STM32F205VE CAN Bus-Off recovery problem
Posted on June 08, 2016 at 11:32
Hi,
I have a problem with CAN after a bus-off condition. When CAN state changes to bus-off, I set the CAN operation mode to SLEEP mode. After a few 100 ms I try to bring the CAN bus into NORMAL mode again and then the Interrupts are activated again. But it seems like the ESR register isn't cleard before CAN starts working and so I get a bus-off interrupt immediately after a restart... this is a never ending cycle. 1) Bus-Off Interrupt occurs 2) Disable CAN-Interrupts 3) Set Sleep-Mode (clear INRQ, set SLEEP in MCR Register) 4) Wait for SLAK bit set 5) Reset TX and RX Mailboxes/FIFOs, Clear all pending Interrupts 6) Wait for a few 100 ms 7) Enter Init-Mode (clear SLEEP, set INRQ, in MCR Register) 8) Wait for INAK Bit set 9) Enter Normal-Mode (clear SLEEP, clear INRQ in MCR Register) 10) Wait for INAK Bit cleared 11) Enable CAN-Interrupts 12) ---> SCE Interrupt --> BOFF Flag set --> Back to 1) When I put an ASSERT((can->ESR & BOFF_BIT) == 0) immediately after 10), the assertion fails. But if put a breakpoint at the assertion and I check the register, all bits are cleared. And when I resume, it starts working as expected. The Code for changing the CAN-Mode:
static tCanStatus SetCanMode(tCAN * can, tCanMode mode)
{
uint32_t timeout = TIMEOUT_VALUE;
if (mode == CAN_MODE_INIT)
{
can->MCR = (uint32_t)((can->MCR & (uint32_t)(~(uint32_t)CAN_MCR_SLEEP)) | CAN_MCR_INRQ);
while (((can->MSR & CAN_MODE_MASK) != CAN_MSR_INAK) && (timeout != 0))
timeout--;
if ((can->MSR & CAN_MODE_MASK) != CAN_MSR_INAK)
return CAN_STATUS_ERROR;
}
else if (mode == CAN_MODE_NORMAL)
{
can->MCR &= (uint32_t)(~(CAN_MCR_SLEEP | CAN_MCR_INRQ));
while (((can->MSR & CAN_MODE_MASK) != 0) && (timeout != 0))
timeout--;
if ((can->MSR & CAN_MODE_MASK) != 0)
return CAN_STATUS_ERROR;
configASSERT((can->ESR & 0x04) == 0); // Fails after a bus-off
}
else if (mode == CAN_MODE_SLEEP)
{
can->MCR = (uint32_t)((can->MCR & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
while (((can->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK) && (timeout != 0))
timeout--;
if ((can->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK)
return CAN_STATUS_ERROR;
}
return CAN_STATUS_OK;
}
#canbus #can #stm32