用于 STM32F446 的 CAN 过滤器

CAN filter for STM32F446

下面是我的 CAN 过滤器代码。我希望只接受 ID 为 0x102XXXXX(十六进制值)的帧,其中 X 可以是任何数字。

  uint32_t filter_id =   0x10200000;
  uint32_t filter_mask = 0x1fffffff;
  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();
  }

根据此页面 https://schulz-m.github.io/2017/03/23/stm32-can-id-filter/ 它应该按我的预期工作,即丢弃除以 0x102XXXXX 开头的 ID 之外的其他 ID。
但它也阻止了所需的 ID。请告诉我哪里出错了?

问题依旧。问题解决了。更新了下面的代码。我只使用 CAN Ext ID。

  uint32_t filter_id =   0x1020;
  uint32_t filter_mask = 0x1ff0;
  sFilterConfig.FilterBank = 14;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = filter_id << 3;
  sFilterConfig.FilterIdLow = 0;
  sFilterConfig.FilterMaskIdHigh = filter_mask << 3;
  sFilterConfig.FilterMaskIdLow = 0;
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;

  if (HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig) != HAL_OK)
  {
    /* Filter configuration Error */
    Error_Handler();
  }

此过滤器(仅)接受与 ID 序列 0x102XXXXX 匹配的邮件,其中 X 可以是任何数字。