cancel
Showing results for 
Search instead for 
Did you mean: 

About CAN filter

Ukazu
Associate III

Hi

Many CAN messages are flowing on the CAN bus.
I would like to receive only ID 0x5C0 among these.

Please let me know the suitable settings to receive only 0x5C0 as a hardware filter without any software intervention, without any load on the MCU.

I wrote the code below. Naturally, I think ID mask mode is better.
I don't understand the settings for sFilterConfig.FilterMaskIdHigh and sFilterConfig.FilterMaskIdLow.

Is this correct?

 

void    CanFilterConfig ( uint8_t filterNum, uint8_t fifoNum, uint32_t canId )
{
    CAN_FilterTypeDef sFilterConfig;

    sFilterConfig.FilterBank           = filterNum;
    sFilterConfig.FilterMode           = CAN_FILTERMODE_IDMASK;
    sFilterConfig.FilterScale          = CAN_FILTERSCALE_32BIT;
    sFilterConfig.FilterIdHigh         = canId << 5;
    sFilterConfig.FilterIdLow          = 0x00000000U;
    sFilterConfig.FilterMaskIdHigh     = 0x0000FFFFU;
    sFilterConfig.FilterMaskIdLow      = 0x0000FFFFU;
    sFilterConfig.FilterFIFOAssignment = fifoNum;
    sFilterConfig.FilterActivation     = ENABLE;
    sFilterConfig.SlaveStartFilterBank = 14;

    (void)HAL_CAN_ConfigFilter( &hcan, &sFilterConfig );
}
1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello @Ukazu ,

In your case use ID list instead of ID Mask config.

Try the following:

  sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT;	
  sFilterConfig.FilterIdHigh = canId <<5;       
  sFilterConfig.FilterIdLow = canId <<5;        
  sFilterConfig.FilterMaskIdHigh = canId <<5;   
  sFilterConfig.FilterMaskIdLow = canId <<5;	  	
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
SofLit
ST Employee

Hello @Ukazu ,

In your case use ID list instead of ID Mask config.

Try the following:

  sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT;	
  sFilterConfig.FilterIdHigh = canId <<5;       
  sFilterConfig.FilterIdLow = canId <<5;        
  sFilterConfig.FilterMaskIdHigh = canId <<5;   
  sFilterConfig.FilterMaskIdLow = canId <<5;	  	
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.
void    CanFilterConfig ( uint8_t filterNum, uint8_t fifoNum, uint32_t canId )
{
    CAN_FilterTypeDef sFilterConfig;

    sFilterConfig.FilterBank           = filterNum;
    sFilterConfig.FilterMode           = CAN_FILTERMODE_IDMASK;
    sFilterConfig.FilterScale          = CAN_FILTERSCALE_32BIT;
    sFilterConfig.FilterIdHigh         = canId << 5; // Comparator
    sFilterConfig.FilterIdLow          = 0x00000000U;
    sFilterConfig.FilterMaskIdHigh     = 0x7FFU << 5; // Mask 11-bit
    sFilterConfig.FilterMaskIdLow      = 0;
    sFilterConfig.FilterFIFOAssignment = fifoNum;
    sFilterConfig.FilterActivation     = ENABLE;
    sFilterConfig.SlaveStartFilterBank = 14; // CAN1 / CAN2 split for FilterBank

    (void)HAL_CAN_ConfigFilter( &hcan, &sFilterConfig );
}
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@SofLit 

Thank you.

An ID list is used to receive one CAN message (0x5C0) on a bus where many CAN messages flow. I've been wrong until now.

Should I use an ID list instead of an ID mask?
I received only 0x5C0

The example you advised is CAN_FILTERSCALE_16BIT, and canId <<5 is entered in FilterIdHigh and FilterIdLow.

FilterMaskIdHigh and FilterMaskIdLow are also filled with canId <<5.

What does this mean?
0x5C0 not received into two buffers?

 

@Tesla DeLorean 

Do you think it is best practice to use ID masks or ID lists?

@SofLit 

When the filter scale register (CAN_FS1R) is set to 16bit in ID list mode, there are CAN_FiR1 and CAN_FiR2, so if you want to receive only one CAN message, set 0x5C0 to the upper and lower of CAN_FiR1 and the upper and lower of CAN_FiR2. That means setting it!

If 0x5C0 is set only in the upper part of CAN_FiR1, and nothing is set in the lower part of CAN_FiR1 and the upper and lower parts of CAN_FiR2, what will happen to the movement of CAN resources? Are you trying to receive 0x000?