cancel
Showing results for 
Search instead for 
Did you mean: 

How to stop HAL_CAN_ErrorCallback

Mark Shoe
Senior
Posted on April 25, 2017 at 20:27

My hardware has to disconnect the CAN bus sometimes. When i do the callback HAL_CAN_ErrorCallback will pop up. My CAN bus has to continue work so i clear all flags that i found:

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_EWG);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_EPV);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_BOF);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_TSR_RQCP0);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_TSR_RQCP1);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_TSR_RQCP2);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_TXOK0);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_TXOK1);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_TXOK2);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_TME0);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_TME1);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_TME2);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_FF0);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_FOV0);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_FF1);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_FOV1);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_WKU);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_SLAKI);

__HAL_CAN_CLEAR_FLAG(hcan,CAN_FLAG_BOF);

The CAN bus does work after that however every receive message the ErrorCallback comes back, what do i need to clear?

13 REPLIES 13
Posted on January 05, 2018 at 23:08

I don't get any errors any more. I never reinitialise any part of the network.

it sounds like the network your attaching to, is contentious, is it a preexisting network ? or still on the bench ?

did you look at the canbus wires, on a scope ?

is it possible that your clock source is drifting ? 

2 boards, both with a drifting clocks ?

if the canbus frequency between boards, drifts by 5%, you would likely see these errors.

did you check the frequency on a scope ?

Mark Shoe
Senior
Posted on January 06, 2018 at 10:48

It is an existing (6 year) network with 12 microchip 18F modules on it. Length 30 meter 125kb. 120E on both sides. And a third party CAN-USB translator for debugging. The CAN on the microchip was working within a few minutes. It does not have so may error conditions so never stops receiving or reaches the state off bus.

So my STM32 CAN works prefect for hours or sometimes days. Then a problem happens. A CAN cable hardware disconnect or whatever. 

I need to recover from the 

CAN_ESR error state. The reference manual or HAL manual does not tell how.

Posted on January 06, 2018 at 22:01

I am not sure I can help you,

I had to power cycle many times before I fixed my issues,

Now, I don't see any errors anymore.

the trick was to make sure that every messageID was unique in the network.

my nodes all have a separate 16 'ID's'  some channels are TX and some are Rx. much like endpoints

no two nodes can transmit the same ID.

Did you set the ABOM bit ?

(the automatic Bus Off repair mechanism )

T J
Lead
Posted on January 06, 2018 at 23:49

I checked my '767 code, I dont enable most interrupts.. maybe that's why I don't see any issues ??

void initCairoCan (void) {

            CAN_Config();

            canRxpointerIN = 0;

            canRxMsgIN  =0;

            canRxMsgOUT =0;

            canRxMsgTableEMPTY = true;

            canRxMsgTableFULL = false;

            for(int i = 0; i<16;i++)

                IOCanMsgFlag[i] =false;

//    // Receive Interrupts //

////    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_FF0);        //!< FIFO 0 full interrupt            //

////    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_FOV0);       //!< FIFO 0 overrun interrupt         //

////    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_FF1);        //!< FIFO 1 full interrupt            //

////    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_FOV1);       //!< FIFO 1 overrun interrupt         //

////    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_FMP0);       //!< FIFO 0 message pending interrupt //

////    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_FMP1);       //!< FIFO 1 message pending interrupt //

//

//    // Operating Mode Interrupts //

////    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_WKU);        //!< Wake-up interrupt           //

//    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_SLK);        //!< Sleep acknowledge interrupt //

//

////    // Error Interrupts //

//    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_EWG);        //!< Error warning interrupt   //

//    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_EPV);        //!< Error passive interrupt   //

//    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_BOF);        //!< Bus-off interrupt         //

//    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_LEC);        //!< Last error code interrupt //

//    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_ERR);        //!< Error Interrupt           //

//    

    

    /* Enable Transmit mailbox empty Interrupt */

    __HAL_CAN_ENABLE_IT (&hcan1, CAN_IT_TME);       //!< Transmit mailbox empty interrupt //

    __HAL_CAN_ENABLE_IT (&hcan1, CAN_IT_FMP0);       //!< FIFO 0 message pending interrupt //

    __HAL_CAN_ENABLE_IT (&hcan1, CAN_IT_FMP1);       //!< FIFO 1 message pending interrupt //

      

                canTxMsgIN  =0;

                canTxMsgOUT =0;

                canTxMsgTableEMPTY         = true;

                canTxMsgTableFULL         = false;

                canTxMsgTableOverflow = false;

                canTxMsgOverrun = false;

                blockCanTx = false;

}