Search code examples
cfilteringstm32can-bus

I need to configure CAN filters in such a way that only ID 0x80 and IDs between 0x200 and 0x300 passes all others are not allowed to be passed


I tried in this way but the problem is only 200-300 can pass but 80h is not passed. if I use if-else then only 80h is passed not 200-300h. So, the problem is I can pass either 80h or 200h-300h. Not both 80h and 200-300h.

#define Filter_ID1  0x80

#define Filter_ID2  0x200


#if Filter_ID1

sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

sFilterConfig.FilterBank = 0;

sFilterConfig.FilterIdHigh=0x80<<5;

sFilterConfig.FilterMaskIdHigh=0xff<<5;

sFilterConfig.FilterIdLow = 0x0000;

sFilterConfig.FilterMaskIdLow = 0x0000;

sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;

sFilterConfig.FilterActivation = ENABLE;

#endif


#if Filter_ID2

sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

sFilterConfig.FilterBank = 1;

sFilterConfig.FilterIdHigh=0x200<<5;

sFilterConfig.FilterMaskIdHigh=0x300<<5;

sFilterConfig.FilterIdLow = 0x0000;

sFilterConfig.FilterMaskIdLow = 0x0000;

sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;

sFilterConfig.FilterActivation = ENABLE;

#endif


Solution

  • You need to configure two filter banks:

    For [0x200, 0x300) range (0x300 excluded), you need Identifier Mask mode. In this case:

    • Mask (FxR2) = 0x300 (check bits 8 & 9, ignore others)
    • ID (FxR1) = 0x200 (Bits 8 & 9 needs to be 0b10)

    For 0x80, you need Identifier List mode. For 32-bit list, a filter bank contains 2 items. In order to accept only 0x80, you need to fill them with the same value (0x80 twice)

    • ID (FxR1) = 0x80
    • ID (FxR2) = 0x80

    More than one filter bank can be active at the same time.

    FiRx is the naming convention used by the reference manual, where i = 0..27 (filter bank number) and x = 1,2 (register number). I don't know how the library you're using calls them.

    Of course, the values I mentioned are just the logical values. You need to shift/modify them to fit your HW registers. I'm not sure left shifting by 5 is correct. It seems << 3 is the correct value to me. See Filter bank scale configuration - register organization figure in your reference manual