cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_CAN_AddTxMessage() issue

Dick Lin
Senior
Posted on June 13, 2018 at 21:06

Hi,

I am using Nucleo-144 connecting CAN RX/TX PD0/PD1 to external CAN transceiver, PEAK.

The function is pretty straight forward. Prepare data then call

HAL_CAN_AddTxMessage() to transmit.

CAN_TxHeaderTypeDef TxHeader;

TxHeader.StdId = 0x321;

TxHeader.ExtId = 0x01;

TxHeader.RTR = CAN_RTR_DATA;

TxHeader.IDE = CAN_ID_STD;

TxHeader.DLC = 2;

uint8_t txData[8] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08} ;

HAL_StatusTypeDef status = HAL_CAN_AddTxMessage(m_can, &TxHeader, txData, (uint32_t *)CAN_TX_MAILBOX0);

The

HAL_CAN_AddTxMessage() function bailed out on the CAN_TSR TME0/1/2 are all RESET. From the manual, seems all 3 mailboxes are full.

Why it's always full from the very beginning? How do I control this? __HAL_CAN_CLEAR_FLAG() doesn't have this flag.

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

if (((hcan->Instance->TSR & CAN_TSR_TME0) != RESET) ||

((hcan->Instance->TSR & CAN_TSR_TME1) != RESET) ||

((hcan->Instance->TSR & CAN_TSR_TME2) != RESET))

{

/* Return function status */

return HAL_OK;

}

else

{

/* Update error code */

hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;

return HAL_ERROR;

}

Bit 28 TME2: Transmit mailbox 2 empty

This bit is set by hardware when no transmit request is pending for mailbox 2.

Bit 27 TME1: Transmit mailbox 1 empty

This bit is set by hardware when no transmit request is pending for mailbox 1.

Bit 26 TME0: Transmit mailbox 0 empty

This bit is set by hardware when no transmit request is pending for mailbox 0.

Note: this post was migrated and contained many threaded conversations, some content may be missing.
14 REPLIES 14
T J
Lead
Posted on June 14, 2018 at 01:06

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6sj&d=%2Fa%2F0X0000000bxB%2FJ6v0XJnNmJpRrg8O18bARfmL8_H0LggvVNKKVr8dw0E&asPdf=false
Posted on June 15, 2018 at 01:42

My problem now became I can't tx/rx using HAL_CAN_AddTxMessage/HAL_CAN_GetRxMessage even I can see CAN message from PEAK.

The issue seems both RX/TX FIFO are empty.

I thought the flags should control by STM32 CAN master.

Thx

Posted on June 15, 2018 at 11:29

i think your issue may be here

what is m_can ?  I don't use this line, maybe it is your problem...

HAL_StatusTypeDef status = HAL_CAN_AddTxMessage(m_can, &TxHeader, txData, (uint32_t *)CAN_TX_MAILBOX0);

these lines look ok, but they are not working for you,.maybe there is a syntax issue

/* Check that any of the Tx mailboxes are empty */   /* check if any one is ready to send*/  fixed comment

if (((hcan->Instance->TSR & CAN_TSR_TME0) != RESET) ||

((hcan->Instance->TSR & CAN_TSR_TME1) != RESET) ||

((hcan->Instance->TSR & CAN_TSR_TME2) != RESET))

{

/* Return function status */

return HAL_OK;

}

else

{

/* Update error code */

hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;

return HAL_ERROR;

}

check the differences in my code, its almost the same, just syntax differences

if ((CAN->TSR & CAN_TSR_TME0) == CAN_TSR_TME0) // ready to transmit in this mailbox.

Posted on June 15, 2018 at 18:53

m_can is the CAN_HandleTypeDef in the C++ wrapper I have.

Posted on June 15, 2018 at 19:25

I debugged a little bit more. My TX is in a loop, it do run the code to

transmit for 3 times. The code below get transmitmailbox value 0, 1, 2.

After that, it starting fail.

From the analyzer, never seen the data out. I am guessing it's in mailbox

and never sent out.

/* Select an empty transmit mailbox */

transmitmailbox = (hcan->Instance->TSR & CAN_TSR_CODE) >>

CAN_TSR_CODE_Pos;

Posted on June 16, 2018 at 01:39

are you using a line driver ?

do you have a terminator?

do you have a other device on the wire ?

then the part will be able to transmit and the CanBus protocol can be satisfied.

Posted on June 16, 2018 at 01:44

if the receiving node has a different bit-rate, the Tx will not satisfy and will remain full.

Posted on June 16, 2018 at 01:54

My config is simple, connect Nucleo-144 CAN_RX/TX to a CAN bus transceiver

(TI SN65HVD230 chip). The transceiver with 120 Ohms termination.

From the transceiver, connecting CANH/CANL to PEAK D type pin 7/2. I did

modify the PEAK to have termination enabled.

So the termination is not an issue. And only 2 devices on the bus.

Thx

Posted on June 16, 2018 at 01:57

I did setup all to have 500Kbps. I believe the setting is correct from the

website - http://www.bittiming.can-wiki.info/

hcan1.Instance = CAN1;

hcan1.Init.Prescaler = 6;

hcan1.Init.Mode = CAN_MODE_NORMAL;

hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

hcan1.Init.TimeSeg1 = CAN_BS1_13TQ;

hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;

hcan1.Init.TimeTriggeredMode = DISABLE;

hcan1.Init.AutoBusOff = ENABLE;

hcan1.Init.AutoWakeUp = DISABLE;

hcan1.Init.AutoRetransmission = ENABLE;

hcan1.Init.ReceiveFifoLocked = DISABLE;

hcan1.Init.TransmitFifoPriority = DISABLE;