cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure CAN filters to use CAN1 and CAN2 parallelly.

SPeri.1
Associate II

Both CAN buses are transmitting correctly. But when I initialize CAN1 and CAN2 only CAN2 receives messages, when only CAN2 is initialized it receives. same for the CAN1. Here I attached my initializing function.

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 = 16;

 hcan1.Init.Mode = CAN_MODE_NORMAL;

 hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan1.Init.TimeSeg1 = CAN_BS1_12TQ;

 hcan1.Init.TimeSeg2 = CAN_BS2_5TQ;

 hcan1.Init.TimeTriggeredMode = DISABLE;

 hcan1.Init.AutoBusOff = DISABLE;

 hcan1.Init.AutoWakeUp = DISABLE;

 hcan1.Init.AutoRetransmission = DISABLE;

 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 can_filter_config1;

 can_filter_config1.FilterActivation = CAN_FILTER_ENABLE;

 can_filter_config1.FilterBank = 10;

 can_filter_config1.FilterFIFOAssignment = CAN_FILTER_FIFO0;

 can_filter_config1.FilterIdHigh = 0;

 can_filter_config1.FilterIdLow = 0x0000;

 can_filter_config1.FilterMaskIdHigh = 0;

 can_filter_config1.FilterMaskIdLow = 0x0000;

 can_filter_config1.FilterMode = CAN_FILTERMODE_IDMASK;

 can_filter_config1.FilterScale = CAN_FILTERSCALE_32BIT;

 can_filter_config1.SlaveStartFilterBank = 0;

 HAL_CAN_ConfigFilter(&hcan1, &can_filter_config1);

 /* 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 = 16;

 hcan2.Init.Mode = CAN_MODE_NORMAL;

 hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan2.Init.TimeSeg1 = CAN_BS1_12TQ;

 hcan2.Init.TimeSeg2 = CAN_BS2_5TQ;

 hcan2.Init.TimeTriggeredMode = DISABLE;

 hcan2.Init.AutoBusOff = DISABLE;

 hcan2.Init.AutoWakeUp = DISABLE;

 hcan2.Init.AutoRetransmission = DISABLE;

 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 can_filter_config2;

 can_filter_config2.FilterActivation = CAN_FILTER_ENABLE;

 can_filter_config2.FilterBank = 1;

 can_filter_config2.FilterFIFOAssignment = CAN_FILTER_FIFO1;

 can_filter_config2.FilterIdHigh = 0;

 can_filter_config2.FilterIdLow = 0x0000;

 can_filter_config2.FilterMaskIdHigh = 0;

 can_filter_config2.FilterMaskIdLow = 0x0000;

 can_filter_config2.FilterMode = CAN_FILTERMODE_IDMASK;

 can_filter_config2.FilterScale = CAN_FILTERSCALE_32BIT;

 can_filter_config2.SlaveStartFilterBank = 1;

 HAL_CAN_ConfigFilter(&hcan2, &can_filter_config2);

 /* USER CODE END CAN2_Init 2 */

}

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello,

You have an issue with filter config:

.SlaveStartFilterBank parameter you have to configure it once and it determines the filter allocation for both CAN1 and CAN2. Assimilate it as a cursor.

.FilterBank should be configured according to .SlaveStartFilterBank.

I'm attaching a text file that explains the principal.

Hope it helps

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

5 REPLIES 5
Foued_KH
ST Employee

Hello @SPeri.1​ ,

which board do you use ?

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Foued_KH
ST Employee

Hello @SPeri.1​ ,

  • FilterActivation specifies if we want to enable Filters or not. Obviously we have to enable them.
  • SlaveStartFilterBank specifies How many Filter Banks do we want to assign to CAN1. Basically the controllers with dual CAN peripheral have 28 Filter Banks, which can be distributed between these 2 CAN. Here I am assigning 20 Filter Banks to the CAN1, and Rest to the CAN 2.
    • This parameter is useless for the controllers with single CAN peripheral. And these Controllers have 14 Filter Banks ( 0 to 13)
  • FilterBank specifies which Filter Bank do we want to use for the filter Process. Here I have assigned 20 Banks for CAN 1, and I can only choose Out of these 20 Banks. So I am choosing Bank number 18.
    • In case of Single CAN Peripheral, you can choose any value between 0 to 13.

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

SofLit
ST Employee

Hello,

You have an issue with filter config:

.SlaveStartFilterBank parameter you have to configure it once and it determines the filter allocation for both CAN1 and CAN2. Assimilate it as a cursor.

.FilterBank should be configured according to .SlaveStartFilterBank.

I'm attaching a text file that explains the principal.

Hope it helps

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

STM32F105RCT6

Now i get it. Thank you so much.