cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F427 CAN Error Status

john239955_st
Associate III
Posted on September 15, 2016 at 17:40

STM32CubeMX 4.16  STM32Cube FW_F4 V1.13.0

I have CAN working OK and need to get the Error Status, I have the following Code fragment but do not receive an Error Code when a CAN Error occurs i.e. disconnected.

              hcan1.pTxMsg->StdId = Out_ID;

                        

              hcan1.pTxMsg->DLC = 8;

              hcan1.pTxMsg->RTR = CAN_RTR_DATA;

              hcan1.pTxMsg->IDE = CAN_ID_STD;

    

              hcan1.pTxMsg->Data[0] = byte1;                                         

              hcan1.pTxMsg->Data[1] = byte2;                             

              hcan1.pTxMsg->Data[2] = byte3;                             

              hcan1.pTxMsg->Data[3] = byte4;

              hcan1.pTxMsg->Data[4] = byte5;                            

              hcan1.pTxMsg->Data[5] = byte6;                                        

              hcan1.pTxMsg->Data[6] = byte7;                        

              hcan1.pTxMsg->Data[7] = byte8;

            

                        

                if(HAL_CAN_Transmit_IT(&hcan1) != HAL_OK)

                {

                            

                 CAN1_Error_Code = hcan1.ErrorCode;

                 LED1(ON);

                            

                 // HAL_CAN_ERROR_NONE      0x00U    /*!< No error             */

                  // HAL_CAN_ERROR_EWG       0x01U    /*!< EWG error            */

                  // HAL_CAN_ERROR_EPV       0x02U    /*!< EPV error            */

                  // HAL_CAN_ERROR_BOF       0x04U    /*!< BOF error            */

                  // HAL_CAN_ERROR_STF       0x08U    /*!< Stuff error          */

                  // HAL_CAN_ERROR_FOR       0x10U    /*!< Form error           */

                  // HAL_CAN_ERROR_ACK       0x20U    /*!< Acknowledgment error */

                  // HAL_CAN_ERROR_BR        0x40U    /*!< Bit recessive        */

                  // HAL_CAN_ERROR_BD        0x80U    /*!< LEC dominant         */

                  // HAL_CAN_ERROR_CRC       0x100U   /*!< LEC transfer error   */

                    

                   // Display value ... CAN1_Error_Code;

                            

                   }

Am I thinking about this too simply or should hcan1.ErrorCode; return a non zero value when an error occurs

My LED comes on when CAN1 is disconnected but the ErrorCode is 0.

Any advise would be of great help.   KR John W

                        

#no-hablo-hal #no-hablo-hal
8 REPLIES 8
Posted on September 15, 2016 at 18:28

What does HAL_CAN_Transmit_IT() actually return when it is not HAL_OK ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
john239955_st
Associate III
Posted on September 15, 2016 at 19:02

Thanks Clive...

The Value is 0;

I would of expected a 0x04 BUS OFF.

Posted on September 15, 2016 at 19:29

The code returned by the *function* not the error code in the structure.

I would set hcan1.ErrorCode to 0x1234 before entry, not clear if all exit paths would set it.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
john239955_st
Associate III
Posted on September 15, 2016 at 20:00

Thanks ...

Adding hcan1.ErrorCode = 0x1234; prior to Transmit_IT, this value is retained when the when != HAL_OK, i.e. CAN1 disconnected.

If Connected and OK, ErrorCode gets reset to 0x0000.

john239955_st
Associate III
Posted on September 16, 2016 at 10:19

Hi,

Also tried as follows:-

void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)

{

        uint32_t can1_error, can2_error;

 

        if(hcan == &hcan1)   {

                                            can1_error = hcan1.ErrorCoder;

                                            // Display error

                                            }

         if(hcan == &hcan2    {

                                             can2_error = hcan2.ErrorCode;

                                             // Display Error

                                            }

}

This also appears to return 0x00 when CAN disconnected.

KR John W

                                            

john239955_st
Associate III
Posted on September 16, 2016 at 15:10

With regard to HAL_CAN_Transmit_IT   I have checked out the return value from the function and is a non-zero i.e.

After a 4  Message Send, disconnected the Return Value goes to a 1;

If I use Polling, HAL_CAN_Transmit, the return value is 3 after the first Transmission,

on the 4th Transmission the return value goes to 1, although this does not explain why hcan1.ErrorCode alway returns 0 when in Polling or Interrupt.

KR John W

Posted on September 16, 2016 at 16:34

HAL_OK = 0x00, HAL_ERROR = 0x01, HAL_BUSY = 0x02, HAL_TIMEOUT = 0x03

I have a rather dim view of the quality of the HAL code, these problems with exit paths and codes are systemic issues. I've elected not to use this library for commercial products, and continue to use the SPL, along with register level solutions where appropriate. The support for HAL needs to come from ST, which seems to have a near endless task of bug fixes and code releases.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
john239955_st
Associate III
Posted on September 16, 2016 at 17:03

Thanks Clive,

I wnder if ST will comment ...  I guess this Function should send Data, If not, advise the User of any errors.  hcan1.ErrorCode appears not to do so.

K.R. John W