jurij jurij - 9 months ago 72
C Question

STM32F4Discovery: CAN filter configuration

I am using the STM32F4Discovery kit to build a simple CAN interface. I managed to configure it so I can transmit CAN messages, however, I am stuck at receiving them. As far so I know, to receive a CAN message, it has to pass acceptance filters. I would like to set the filter to accept all (standard, 2.0A) messages from ID 0x700 to 0x7FF. How do I do this?

I've read the reference manual, but I have no idea how to actually configure the filtering.

void CAN_FilterConfiguration(void) {
CAN_FilterInitTypeDef CAN_FilterInitStructure;

/* CAN2 filter configuration */
CAN_FilterInitStructure.CAN_FilterNumber = 1; // Filter number = 1 (0<=x<=13)
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; // FIFO = 0
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;

Answer Source

Filters are working in List or Mask mode. They can be 32 bit or 16 bit. You can have 32 filters, but 0-13 are assigned to CAN1 and 14+ are for CAN2.

In one filter there is one 32-bit filter or two 16-bit filters

  • One 32-bit filter for the STDID[10:0], EXTID[17:0], IDE and RTR bits.
  • Two 16-bit filters for the STDID[10:0], RTR, IDE and EXTID[17:15] bits.

This is the way to make a tested word. In mask mode the CAN accepts when TESTED & MASK == ID. In list mode CAN accepts when TESTED == ID or MASK == ID. There are two filters in 16 bit and one filter in 32 bit - when ID = IDHigh << 16 | IDLow (and same way to join mask).

More information on DM0090 link