cancel
Showing results for 
Search instead for 
Did you mean: 

Bug in ST HAL CAN: pTxMsg, pRxMsg and pRx1Msg never initialized

Micah Richert
Associate
Posted on July 04, 2017 at 01:19

CanTxMsgTypeDef*  pTxMsg, pRxMsg and pRx1Msg in CAN_HandleTypeDef are never initialized.

I am convinced that they shouldn't be pointers and should just be defined as part of the structure directly, as in:

typedef struct

{

  CAN_TypeDef                 *Instance;  /*!< Register base address          */

  CAN_InitTypeDef             Init;       /*!< CAN required parameters        */

  CanTxMsgTypeDef             TxMsg;     /*!< Rransmit structure  */

  CanRxMsgTypeDef             RxMsg;     /*!< Reception structure for RX FIFO0 msg */

  CanRxMsgTypeDef             Rx1Msg;    /*!< Reception structure for RX FIFO1 msg */

  __IO HAL_CAN_StateTypeDef   State;      /*!< CAN communication state        */

  HAL_LockTypeDef             Lock;       /*!< CAN locking object             */

  __IO uint32_t               ErrorCode;  /*!< CAN Error code                 */

}CAN_HandleTypeDef;

See attached diff file for the complete fix.  I am using STM32Cube_FW_F4_V1.16.0 but I am pretty sure this applies to many older versions.

#bug #can #hal
1 REPLY 1
davewalker9
Associate
Posted on October 12, 2017 at 13:05

pTxMsg is a pointer to a message buffer structure.

You need to define you own message buffer of type CanTxMsgTypeDef and then point pTxMsg at it.

void CAN_Tx(CAN_HandleTypeDef* canHandle)

{

   if (canHandle->Instance==CAN)

   {

    / /* Create message buffer

   CanTxMsgTypeDef TxMessage;                           <<- Buffer to hold transmit message

   //* Configure message

   TxMessage.StdId = 0x123;

   TxMessage.RTR = CAN_RTR_DATA;

   TxMessage.IDE = CAN_ID_STD;

   TxMessage.DLC = 8;

   TxMessage.Data[0] = 0x01;

   TxMessage.Data[1] = 0x23;

   TxMessage.Data[2] = 0x45;

   TxMessage.Data[3] = 0x67;

   TxMessage.Data[4] = 0x89;

   TxMessage.Data[5] = 0xAB;

   TxMessage.Data[6] = 0xCD;

   TxMessage.Data[7] = 0xEF;

   //* Point to message

   canHandle->pTxMsg = &TxMessage;      //* <--- Point to newly created message buffer

   do

   {

      TransmitMailbox = HAL_CAN_Transmit(canHandle, 10);

   } while( TransmitMailbox == CAN_TXSTATUS_NOMAILBOX );

  }

}