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-13 03:34 AM - edited 2025-01-13 03:35 AM
It's the same project. There is following Rxcallback in main.c. I'm reattaching the copy of project.
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
//HAL_CAN_DeactivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
/* Get RX message */
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
{
/* Reception Error */
Error_Handler();
}
SetCANTxFlag = 1;
//HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
}
2025-01-13 03:48 AM - edited 2025-01-13 03:55 AM
Just make a test with these CAN timing parameters:
Also is that possible to use CAN2 instead of the CAN analyzer (remove the CAN analyzer) and transmit CAN frames to CAN1 and see if you receive frames on CAN2 ("echoed" by CAN1)?
2025-01-14 08:02 PM
I changed CAN timing but there is no change in behavior. The board is still retransmitting. CAN analyzer is mandatory and can't be removed.
Any suggestions? Is it possible for you to share any Nucleo-F412ZG CAN example?
2025-01-15 12:19 AM - edited 2025-01-15 12:19 AM
Why the analyzer is mandatory? the analyzer is just an analyzer. What is mandatory is to have at least two nodes on the CAN bus. What I suggested is to replace the CAN analyzer by a second CAN node which is a second CAN instance (CAN2) to discard something wrong on the CAN analyzer.