cancel
Showing results for 
Search instead for 
Did you mean: 

Setting up CAN filter for STM32F446

MG.5
Associate II

Below is my piece of code for CAN filter. I wish to accept only frames with ID of 0x102XXXXX (hexadecimal value) where X can be any number.

uint32_t filter_id =   0x10200000;
  uint32_t filter_mask = 0x1FF00000;
  sFilterConfig.FilterBank = 14;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;//CAN_FILTERMODE_IDLIST;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = ((filter_id << 5)  | (filter_id >> (32 - 5))) & 0xFFFF; // STID[10:0] & EXTID[17:13];
  sFilterConfig.FilterIdLow = (filter_id >> (11 - 3)) & 0xFFF8; // EXID[12:5] & 3 Reserved bits
  sFilterConfig.FilterMaskIdHigh = ((filter_mask << 5)  | (filter_mask >> (32 - 5))) & 0xFFFF;
  sFilterConfig.FilterMaskIdLow = (filter_mask >> (11 - 3)) & 0xFFF8;
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;
 
  if (HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig) != HAL_OK)
  {
    /* Filter configuration Error */
    Error_Handler();
  }

As per this page https://schulz-m.github.io/2017/03/23/stm32-can-id-filter/ it should work as I expected i.e. discarding other IDs except those starting with 0x102XXXXX.

But it is blocking the desired Ids also. Please tell me where it is going wrong?

2 REPLIES 2
KnarfB
Principal III

The bits set in filter_mask determine the bits in filter_id which must match. So, for matching 0x102XXXXX the filter_mask should be 0x1FF00000.

Hi, thanks. I have changed it as per your suggestion.

But it is now accepting IDs 0x18FF00 and 0x48FF00 also.

How to reject those messages?