Skip to main content
Ukazu
Associate III
November 17, 2023
Solved

About CAN filter

  • November 17, 2023
  • 5 replies
  • 1935 views

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 );
}
This topic has been closed for replies.
Best answer by mƎALLEm

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;	 	

5 replies

mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
November 17, 2023

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 "Best answer" on the reply which solved your issue or answered your question.
Ukazu
UkazuAuthor
Associate III
November 19, 2023

@mƎALLEm 

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
Guru
November 17, 2023
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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Ukazu
UkazuAuthor
Associate III
November 19, 2023

@Tesla DeLorean 

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