cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_CAN_AddTxMessage() returning error

snehadakhare01
Associate II

Hii st-term

 

Can you please tell what is issue in this code it is going in Error Handler

HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, const CAN_TxHeaderTypeDef *pHeader,

const uint8_t aData[], uint32_t *pTxMailbox)

{

uint32_t transmitmailbox;

HAL_CAN_StateTypeDef state = hcan->State;

uint32_t tsr = READ_REG(hcan->Instance->TSR);



/* Check the parameters */

assert_param(IS_CAN_IDTYPE(pHeader->IDE));

assert_param(IS_CAN_RTR(pHeader->RTR));

assert_param(IS_CAN_DLC(pHeader->DLC));

if (pHeader->IDE == CAN_ID_STD)

{

assert_param(IS_CAN_STDID(pHeader->StdId));

}

else

{

assert_param(IS_CAN_EXTID(pHeader->ExtId));

}

assert_param(IS_FUNCTIONAL_STATE(pHeader->TransmitGlobalTime));



if ((state == HAL_CAN_STATE_READY) ||

(state == HAL_CAN_STATE_LISTENING))

{

/* Check that all the Tx mailboxes are not full */

if (((tsr & CAN_TSR_TME0) != 0U) ||

((tsr & CAN_TSR_TME1) != 0U) ||

((tsr & CAN_TSR_TME2) != 0U))

{

/* Select an empty transmit mailbox */

transmitmailbox = (tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos;



/* Store the Tx mailbox */

*pTxMailbox = (uint32_t)1 << transmitmailbox;



/* Set up the Id */

if (pHeader->IDE == CAN_ID_STD)

{

hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->StdId << CAN_TI0R_STID_Pos) |

pHeader->RTR);

}

else

{

hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->ExtId << CAN_TI0R_EXID_Pos) |

pHeader->IDE |

pHeader->RTR);

}



/* Set up the DLC */

hcan->Instance->sTxMailBox[transmitmailbox].TDTR = (pHeader->DLC);



/* Set up the Transmit Global Time mode */
if (pHeader->TransmitGlobalTime == ENABLE)
{
SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TDTR, CAN_TDT0R_TGT);
}
/* Set up the data field */
WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDHR,
((uint32_t)aData[7] << CAN_TDH0R_DATA7_Pos) |
((uint32_t)aData[6] << CAN_TDH0R_DATA6_Pos) |
((uint32_t)aData[5] << CAN_TDH0R_DATA5_Pos) |
((uint32_t)aData[4] << CAN_TDH0R_DATA4_Pos));

WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDLR,
((uint32_t)aData[3] << CAN_TDL0R_DATA3_Pos) |
((uint32_t)aData[2] << CAN_TDL0R_DATA2_Pos) |
((uint32_t)aData[1] << CAN_TDL0R_DATA1_Pos) |
((uint32_t)aData[0] << CAN_TDL0R_DATA0_Pos));
/* Request transmission */
SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TIR, CAN_TI0R_TXRQ);
/* Return function status */
return HAL_OK;
}
else
{
/* Update error code */
hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
return HAL_ERROR;
}
}
else
{
/* Update error code */

hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
return HAL_ERROR;
}
}

3 REPLIES 3
TDK
Guru

Do you know how to debug and step through the code? Do so, step through the function, figure out where exactly it goes to error handler and you will find the cause.

Or, when it's in the error handler, examine the call stack to figure out how it ended up there. These are fairly basic debugging operations.

It's unlikely to be a problem with HAL_CAN_AddTxMessage itself. More likely to be a problem with how you're using it.

If you feel a post has answered your question, please click "Accept as Solution".
LCE
Principal

At least put the name of the peripheral in your header for the thread. About "Firmware" is actually almost everything here. 😉

And please use the </> button to paste code.

At least ST has heard us and put this button into the first row of the reply window options.

You can edit both.

SofLit
ST Employee

Hello @snehadakhare01 ,

You need to be more specific and provide more details. 

Which device you are using?

You can put break points in different "return HAL_ERROR" and find which block of code is causing the issue.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.