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?