2025-01-10 04:23 AM - last edited on 2025-01-10 04:59 AM by SofLit
Hi,
I've been implementing CAN1 on NUCLEO-412ZG board.
CAN bus is able to transmit and receive CAN packets. However, the problem is when board receives CAN packet from PC over CanAnalyser tool, it transmits the same packet on Tx line automatically. As we can see in following code, I'm not using HAL_CAN_AddTxMessage() expect in initialization. CAN is configured in Normal mode with 250kbps.
Here are the settings:
static void MX_CAN1_Init(void)
{
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 24;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_6TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = DISABLE;
hcan1.Init.AutoWakeUp = DISABLE;
hcan1.Init.AutoRetransmission = ENABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CAN1_Init 2 */
/*##-2- Configure the CAN Filter ###########################################*/
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if(HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
HAL_CAN_Start(&hcan1);
HAL_CAN_ActivateNotification(&hcan1,CAN_IT_RX_FIFO0_MSG_PENDING);
}
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
/* Get RX message */
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
{
/* Reception Error */
Error_Handler();
}
CAN1Flag= 1;
}
void CAN1_InitMessage(void)
{
TxHeader.StdId = 0x11;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.IDE = CAN_ID_STD;
TxHeader.DLC = 7;
TxHeader.TransmitGlobalTime = DISABLE;
TxData[0] = 'W';
TxData[1] = 'a';
TxData[2] = 'i';
TxData[3] = 't';
TxData[4] = ' ';
TxData[5] = 'R';
TxData[6] = 'x';
/* Request transmission */
if(HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox) != HAL_OK)
{
/* Transmission request Error */
Error_Handler();
}
/* Wait transmission complete */
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) != 3) {}
}
void main()
.. Transmit CAN message once via CAN1_InitMessage() here.
while(1)
{
if (CAN1Flag)
{
... Toggle led. This working.
}
}
2025-01-10 04:57 AM
Hello,
Not sure I've understood the issue.
You said:
@stm32f412 wrote:
CAN bus is able to transmit and receive CAN packets.
Then you said:
@stm32f412 wrote:
However, the problem is when board receives CAN packet from PC over CanAnalyser tool, it transmits the same packet on Tx line automatically.
1- In the first phrase you said that transmit and receive are OK: how you did test that (please in details)?
2- Then what was the modification you applied to get the issue?
3- The issue is not clear. Are you sending a message from CAN analyzer to the board, then the board is re-transmitting it again to the CAN analyzer each time you receive a message as you have an "echo"? if yes, how you did confirm that?
2025-01-12 07:25 PM - edited 2025-01-12 07:26 PM
Hello,
1 - Transmit: The board transmits expected CAN packet that I can see in CAN analyzer tool. Receive: The board receives expected CAN packet from CAN analyzer tool which was confirmed by toggling board LED.
2 - There was no modification applied.
3 - Yes, I'm sending a message from CAN analyzer to the board, then the board (toggles LED and) re-transmits it to the CAN analyzer even I've not added any code to "echo" the incoming CAN message. This is the issue. How can board re-transmit CAN message by itself (without HAL_CAN_AddTxMessage())?
2025-01-13 12:13 AM
@stm32f412 wrote:
How can board re-transmit CAN message by itself (without HAL_CAN_AddTxMessage())?
At this stage I can't tell nothing. Remove the transmission on STM32 side and probe CAN_Tx using a logic analyzer or oscilloscope when you transmit a frame from the CAN analyzer and check if you really have that retransmitted frame.
2025-01-13 01:56 AM
I checked CAN_Tx with oscilloscope. It has retransmitted frame.
2025-01-13 01:56 AM
I checked CAN_Tx with oscilloscope. It has retransmitted frame.
2025-01-13 02:08 AM
It has no sense as you didn't re-send it again.
Could you please share your complete project?
2025-01-13 03:06 AM - edited 2025-01-13 03:31 AM
Please check below comment.
2025-01-13 03:19 AM
2025-01-13 03:31 AM
Did you remove something in your code? I don't see the Rxcallback.
Please be sure to attach a project that reproduces the behavior without a modification from my side. So test it before attaching it.
Thank you.