Skip to main content
Associate III
June 20, 2025
Solved

Stm32f407 Filter configuration for CAN

  • June 20, 2025
  • 1 reply
  • 506 views

HI

Can you tell me how to configure filter to accept only ID 202 to 205 .

CAN_FilterTypeDef sFilterConfig;

/* USER CODE BEGIN CAN1_Init 2 */
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)
{
 Error_Handler();
}

note:

What value should come ?

all I have configure as standard ID only


Edited to apply source code formatting - please see How to insert source code for future reference.

Best answer by mƎALLEm

Hello,

Try this filter configuration:

 sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
 sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT;
 sFilterConfig.FilterIdHigh = 0x0204<<5;
 sFilterConfig.FilterIdLow = 0x0202<<5;
 sFilterConfig.FilterMaskIdHigh = 0xFFE<<5;
 sFilterConfig.FilterMaskIdLow = 0xFFE<<5;

Hope that helps.

1 reply

mƎALLEm
mƎALLEmBest answer
Technical Moderator
June 20, 2025

Hello,

Try this filter configuration:

 sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
 sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT;
 sFilterConfig.FilterIdHigh = 0x0204<<5;
 sFilterConfig.FilterIdLow = 0x0202<<5;
 sFilterConfig.FilterMaskIdHigh = 0xFFE<<5;
 sFilterConfig.FilterMaskIdLow = 0xFFE<<5;

Hope that 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."
Ash1Author
Associate III
June 20, 2025

HI

Note: Can you suggest me with this configuration when filter scale is 32 bit " CAN_FILTERSCALE_32BIT"

I want my filter to accept CANID from 202 to 205

 

mƎALLEm
Technical Moderator
June 20, 2025

What kind of ID frame are you using in your application Extended or Standard?

"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."