Skip to main content
Visitor II
December 18, 2023
Solved

STM32F103 CAN filter range of ID's

  • December 18, 2023
  • 2 replies
  • 10135 views

Hello, I have a set of extended CAN ID's I need to pas the bxCAN filter. I looked at some examples where one ID is passed the filter, but could not figure out how to allow a set of ID's. If my understanding is correct the mask is used to determine which bits of the ID are important. How can I configure the filter so that I can pass a set of different ID's?

These are the ID's:

// 0x0CF00400
// 0x0CFD9200
// 0x18FEE500
// 0x18FEEE00
// 0x18FEEF00
// 0x18FEF200
// 0x18FEF700
// 0x18FEFC00

 

This is what my filter config looks like:

	can_fil.FilterBank = 0;
	can_fil.FilterMode = CAN_FILTERMODE_IDLIST;
	can_fil.FilterFIFOAssignment = CAN_RX_FIFO0;
	can_fil.FilterIdHigh = 0;
	can_fil.FilterIdLow = 0;
	can_fil.FilterMaskIdHigh = 0;
	can_fil.FilterMaskIdLow = 0;
	can_fil.FilterScale = CAN_FILTERSCALE_32BIT;
	can_fil.FilterActivation = ENABLE;
	can_fil.SlaveStartFilterBank = 13;

 

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    I think for your case, ID mask is not suitable as there are many differences in your ID list mainly the first ones. But here you need to use ID list but you need more than one filter to configure. Each filter can set 2 IDs. So you need four 32-bit filters in your case.

    This is an example of a filter configuring your first two IDs:

    #define REMOTE_FRAME 0 /* If = 1 the frame should be a remote frame. If = 0 the frame will be a data frame */
    #define EXTID 1 /* If = 0 the frame should be a frame with standard ID. If = 1 the frame should be a frame with extended ID */
    
    #define ID1 0x0CF00400
    #define ID2 0x0CFD9200
    
    #define FILTER_ID ((ID1 << 3) | (REMOTE_FRAME<<1) | (EXTID <<2)) // ID1 = 0x0CF00400 (FxFR1[31:0])
    #define FILTER_MASK ((ID2 << 3) | (REMOTE_FRAME<<1) | (EXTID <<2)) // ID2 = 0x0CFD9200 (FxFR2[31:0])
     
     sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
     sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
     sFilterConfig.FilterIdHigh = (FILTER_ID >> 16); // Filter ID2 LSB (FxFR2[0:15])
     sFilterConfig.FilterIdLow = (FILTER_ID & 0xFFFF); // Filter ID1 LSB (FxFR1[0:15])
     sFilterConfig.FilterMaskIdHigh = (FILTER_MASK >> 16); // Filter ID2 MSB (FxFR2[16:31])
     sFilterConfig.FilterMaskIdLow = (FILTER_MASK & 0xFFFF); // Filter ID1 MSB (FxFR1[16:31])

     

    2 replies

    mƎALLEmAnswer
    Technical Moderator
    December 18, 2023

    Hello,

    I think for your case, ID mask is not suitable as there are many differences in your ID list mainly the first ones. But here you need to use ID list but you need more than one filter to configure. Each filter can set 2 IDs. So you need four 32-bit filters in your case.

    This is an example of a filter configuring your first two IDs:

    #define REMOTE_FRAME 0 /* If = 1 the frame should be a remote frame. If = 0 the frame will be a data frame */
    #define EXTID 1 /* If = 0 the frame should be a frame with standard ID. If = 1 the frame should be a frame with extended ID */
    
    #define ID1 0x0CF00400
    #define ID2 0x0CFD9200
    
    #define FILTER_ID ((ID1 << 3) | (REMOTE_FRAME<<1) | (EXTID <<2)) // ID1 = 0x0CF00400 (FxFR1[31:0])
    #define FILTER_MASK ((ID2 << 3) | (REMOTE_FRAME<<1) | (EXTID <<2)) // ID2 = 0x0CFD9200 (FxFR2[31:0])
     
     sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
     sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
     sFilterConfig.FilterIdHigh = (FILTER_ID >> 16); // Filter ID2 LSB (FxFR2[0:15])
     sFilterConfig.FilterIdLow = (FILTER_ID & 0xFFFF); // Filter ID1 LSB (FxFR1[0:15])
     sFilterConfig.FilterMaskIdHigh = (FILTER_MASK >> 16); // Filter ID2 MSB (FxFR2[16:31])
     sFilterConfig.FilterMaskIdLow = (FILTER_MASK & 0xFFFF); // Filter ID1 MSB (FxFR1[16:31])

     

    SmdeintAuthor
    Visitor II
    December 19, 2023

    And how can I configure more filters at once? 

    Technical Moderator
    December 19, 2023

    As I said using several filters.

    For each filter (new filter configuration) you need to increment .FilterBank:

    sFilterConfig.FilterBank = 0; -> filter number 0

    sFilterConfig.FilterBank = 1; -> filter number 1

    sFilterConfig.FilterBank = 2; -> filter number 2 etc ..

     

    Technical Moderator
    December 19, 2023

    Nice!