can-busstm32f4

STM32F4xx CAN Filter Configuration


I am writing in reference to the information in the reference manual (bxCAN).

I am trying to understand how CAN filter configuration works and need your help to check whether I understand it correctly or not.

Especially the configuration of the filter ID and the filter mask ID.

After I take a look of the stdPeriphLib and the ref. manual, I think that in understand what happens but I´m not sure.

FilterIdHigh/Low:

Is FilterIdHigh/Low the comparative value for CAN Controller after the binary AND with the FilterIdMask?

e.g: CAN Controller receives a message --> CAN_Rx_

CAN Controller makes a binary AND with the FilterIdMask --> 
    CAN_Rx_ArbitrationField & FilterIdMask = Result

The CAN Controller compares the Result with the FilterId.

If there is a match CAN Controller puts the CAN_Rx_ message into the assigned FIFO otherwise it will discard the message.

Isn´t it?

Thank´s in advantage.


Solution

  • First received ID is ANDed with Mask to remove bits which are not required and then compared with ID. If they match, then only message is accepted.

    if((CAN_RX_ID & CAN_FILTER_MASK) == (CAN_FILTER_ID & CAN_FILTER_MASK))
    {
      Copy Data in Buffer
    }
    else
    {
      Discard the message
    }
    

    Note that, only those bits which are set in Mask are compared.

    Say, you want to accept only one frame with ID 0x18EBFAB0. In that case, you will set Filter ID as 0x18EBFAB0 and Mask as 0x1FFFFFFF. When message with any other ID arrives, it will not satisfy required condition and it will be ignored. If message with ID 0x18EBF9B0 is received,

    (0x18EBF9B0 & 0x1FFFFFFF) != (0x18EBFAB0 & 0x1FFFFFFF)
    

    Message will be ignored

    If you want to accept any message between ID 0x120 to 0x127. In that case, set Mask as 0x1F0 and Filter ID as 0x120. With this, Last 4 bits of the ID will be ignored as they are set to 0. When Message with ID 0x123 is received,

    (0x123 & 0x1F0) == (0x120 & 0x1F0)
    

    Message will be accepted.