2018-06-13 12:06 PM
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.2018-06-13 04:06 PM
2018-06-14 06:42 PM
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
2018-06-15 04:29 AM
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.
2018-06-15 11:53 AM
m_can is the CAN_HandleTypeDef in the C++ wrapper I have.
2018-06-15 12:25 PM
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;
2018-06-15 06:39 PM
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.
2018-06-15 06:44 PM
if the receiving node has a different bit-rate, the Tx will not satisfy and will remain full.
2018-06-15 06:54 PM
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
2018-06-15 06:57 PM
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;