Accept exactly four CAN IDs with filter list mode
Accept exactly four CAN standard IDs on STM32 bxCAN with one filter bank in 16-bit identifier list mode, shifting each ID left by five.
One bxCAN filter bank in 16-bit list mode holds four 11-bit identifiers, and a frame passes only when its ID equals one of them. That is the cheapest way to listen to a handful of messages without receiving anything else. With the STM32F4 HAL, the four slots reuse the mask fields of the filter structure.
CAN_FilterTypeDef f = {0};
f.FilterBank = 0;
f.FilterMode = CAN_FILTERMODE_IDLIST;
f.FilterScale = CAN_FILTERSCALE_16BIT;
f.FilterIdHigh = 0x101 << 5; // slot 1
f.FilterIdLow = 0x102 << 5; // slot 2
f.FilterMaskIdHigh = 0x103 << 5; // slot 3 (holds an ID, not a mask)
f.FilterMaskIdLow = 0x104 << 5; // slot 4
f.FilterFIFOAssignment = CAN_RX_FIFO0;
f.FilterActivation = ENABLE;
f.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(&hcan1, &f) != HAL_OK) Error_Handler();Each 16-bit register holds STID[10:0] in bits 15 to 5, then RTR, IDE and three extended-ID bits below. Shifting by five puts the standard ID in place and leaves RTR and IDE at zero, so only standard data frames match. A remote frame for 0x101 is rejected unless you set the RTR bit in that slot.
The names are misleading in list mode. FilterMaskIdHigh and FilterMaskIdLow are not masks there but IDs three and four. If you need more than four IDs, use a second bank.