2023-10-04 06:29 AM
Hi,
I am trying to set up a project with 2 can buses (CAN1 and CAN2),
First goal was to check if both CAN1 and CAN2 would be able to receive and send messages.
I read a bit about people struggling to make both can work because of the "master-slave" can filter configuration that was not clearly explained in the reference manual especially when setting the can filterbank "cursor". Therefore I took care of CAN's filter configuration and my MX_CAN1_Init and MX_CAN2_Init looks like this:
/**
* @brief CAN1 Initialization Function
* @PAram None
* @retval None
*/
static void MX_CAN1_Init(void)
{
/* USER CODE BEGIN CAN1_Init 0 */
/* USER CODE END CAN1_Init 0 */
/* USER CODE BEGIN CAN1_Init 1 */
/* USER CODE END CAN1_Init 1 */
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 4;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_2TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
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 */
CAN_FilterTypeDef sFilterConfig;
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);
if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
{
/* Notification Error */
Error_Handler();
}
/* USER CODE END CAN1_Init 2 */
}
/**
* @brief CAN2 Initialization Function
* @PAram None
* @retval None
*/
static void MX_CAN2_Init(void)
{
/* USER CODE BEGIN CAN2_Init 0 */
/* USER CODE END CAN2_Init 0 */
/* USER CODE BEGIN CAN2_Init 1 */
/* USER CODE END CAN2_Init 1 */
hcan2.Instance = CAN2;
hcan2.Init.Prescaler = 4;
hcan2.Init.Mode = CAN_MODE_NORMAL;
hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan2.Init.TimeSeg1 = CAN_BS1_2TQ;
hcan2.Init.TimeSeg2 = CAN_BS2_2TQ;
hcan2.Init.TimeTriggeredMode = DISABLE;
hcan2.Init.AutoBusOff = DISABLE;
hcan2.Init.AutoWakeUp = DISABLE;
hcan2.Init.AutoRetransmission = ENABLE;
hcan2.Init.ReceiveFifoLocked = DISABLE;
hcan2.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CAN2_Init 2 */
CAN_FilterTypeDef sFilterConfig;
sFilterConfig.FilterBank = 14;
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(&hcan2, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
HAL_CAN_Start(&hcan2);
/*##-4- Activate CAN RX notification #######################################*/
if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
{
/* Notification Error */
Error_Handler();
}
/* USER CODE END CAN2_Init 2 */
}
I've also activated both can RX0 interrupt and declared the HAL_CAN_RxFifo0MsgPendingCallback inside my main.c file.
Both can buses are connected to a PEAK CAN-USB interface in order to check traffic on each bus.
First test was done with CAN1 in normal mode and CAN2 in loopback silentmode. This test went well as I saw in debugging mode that CAN2 messages were successfully sent and that the CAN_RxfifoMsgPendingCallback was fired.
Second test was done with both CAN1 and CAN2 in normal mode. I was expecting seeing a CAN2 message coming out using my can-usb monitor. However, no messages came out, the CAN2 mailboxes were filled until 3d level and then an error occured due to full CAN2 transmit mailboxes (HAL_CAN_ERROR_PARAM)
I've tried to check both CAN2 pins on an oscilloscope and I got no signal..
Same problem occured with the CAN2 reception while sending messages using the CAN-USB to send a message towards CAN2.
To sum up, while I have no problem with CAN1 transmission and reception, I got serious difficulties making CAN2 work without this strange behavior.
Solved! Go to Solution.
2023-10-04 07:47 AM
Also Check the state of the transceiver connected to CAN2.
Some transceivers have standby mode/enable pin. So if the transceiver is disabled you cannot establish CAN communication
2023-10-04 07:10 AM - edited 2023-10-04 07:44 AM
Hello,
First check the following:
- Pin used for CAN2_Tx and CAN2_Rx .
- Their configs
- Check their connections to the transceiver
I suspect something in the pins selection (are they the ones connected to the CAN transceiver?) or their config
2023-10-04 07:47 AM
Also Check the state of the transceiver connected to CAN2.
Some transceivers have standby mode/enable pin. So if the transceiver is disabled you cannot establish CAN communication
2023-10-05 07:48 AM
Thanks for you prompt response.
Actually Cmy transceiver does not have standby mode/enable pin. Anyway, I've checked my transceiver datasheet and saw that a red jumper was used to activate the "end line" 120 ohm resistor. As my differential line was ridiculously short (from the transceiver output to the CAN-USB input), I removed the jumper and that's when I started seing can correct messages coming out of CAN2.
Communication established !
Thanks for your support,