[STM32] Several message reception filtering modes for STM32 CANFD peripherals

In Controller Area Network (CAN) systems, correctly configuring message filters is critical to effectively manage network communications. Especially when using the Flexible Data-rate Controller Area Network (FDCAN), choosing the appropriate filter type can greatly improve network efficiency and message processing speed.

The CAN peripheral of the STM32G47x chip has the following reception message filtering methods:

/** @defgroup FDCAN_filter_type FDCAN Filter Type
  * @{
  */
#define FDCAN_FILTER_RANGE ((uint32_t)0x00000000U) /*!< Range filter from FilterID1 to FilterID2 */
#define FDCAN_FILTER_DUAL ((uint32_t)0x00000001U) /*!< Dual ID filter for FilterID1 or FilterID2 */
#define FDCAN_FILTER_MASK ((uint32_t)0x00000002U) /*!< Classic filter: FilterID1 = filter, FilterID2 = mask */
#define FDCAN_FILTER_RANGE_NO_EIDM ((uint32_t)0x00000003U) /*!< Range filter from FilterID1 to FilterID2, EIDM mask not applied */

FDCAN_FILTER_RANGE (0x00000000U):

  • This is a range filtering mode.
  • In this mode, two ID values (FilterID1 and FilterID2) are used to define an accepted message ID range.
  • Any message ID within this range (including boundary values) will pass the filter.
  • For example: if FilterID1 is set to 100 and FilterID2 is set to 200, then any ID between 100 and 200 (Including 100 and 200) messages will be accepted.

FDCAN_FILTER_DUAL (0x00000001U):

  • This mode implements dual ID filtering.
  • It uses two specific IDs (FilterID1 and FilterID2) and any message matching either of these two IDs will be accepted.
  • For example: If FilterID1 is 123 and FilterID2 is 456, then any message with an ID of 123 or 456 will pass the filter.

FDCAN_FILTER_MASK (0x00000002U):

  • This is a classic filtering method called mask filtering.
  • In this mode, FilterID1 is the filter ID and FilterID2 is the mask.
  • The controller uses masks to determine which bits must match in the filter ID.
  • For example: If FilterID1 is set to 100 and FilterID2 (mask) is set to 0xF0, only those messages whose high 4 bits of ID match the high 4 bits of 100 will been accepted.
  • Meet the following conditions:

    F

    i

    l

    t

    e

    r

    I

    D

    2

    &

    R

    e

    c

    e

    i

    v

    e

    M

    s

    g

    I

    D

    =

    =

    F

    i

    l

    t

    e

    r

    I

    D

    2

    &

    F

    i

    l

    t

    e

    r

    I

    D

    1

    FilterID2 \ & amp; ReceiveMsgID == FilterID2 \ & amp; FilterID1

    FilterID2 &ReceiveMsgID==FilterID2 &FilterID1

FDCAN_FILTER_RANGE_NO_EIDM (0x00000003U):

  • This is also a range filtering mode, but in this mode EIDM (Extended ID Masking) will not be applied.
  • Similar to standard range filtering, it accepts all message IDs between FilterID1 and FilterID2.
  • But unlike standard range filtering, it does not use additional masks to further limit the accepted message IDs.
  • Each filtering mode has its specific purpose and application scenario, and choosing the appropriate filtering mode depends on the specific needs and message traffic in the CAN network.

Each FDCAN filter type has its unique application scenarios and advantages. When choosing an appropriate filtering strategy, it is important to consider your network needs and message traffic characteristics. By understanding how these filter types work, you can configure your CAN network more effectively to ensure efficient and accurate data transmission.

About EIDM

EIDM (Extended Identifier Mask) plays an important role in filtering messages in the FDCAN (Flexible Data-rate Controller Area Network) system. EIDM is mainly used to refine the matching of extended identifiers (Extended Identifier) in mask filtering. In CAN communication, identifiers are used to distinguish different messages, and extended identifiers allow more message IDs.

The purpose of EIDM is to provide an additional masking layer for extended identifiers so that filters can more accurately match specific message IDs. EIDM allows you to specify which bits must match for a message to pass the filter, providing greater flexibility and precise control.

example
Let’s say you have a message with extension ID 0x1F345678 and you want to receive only IDs with a specific pattern. You can achieve this using mask filters and EIDM:

Set filter ID: Suppose you set the filter ID to 0x1F345678.
Apply EIDM: You can set EIDM to 0xFFF00000. This mask specifies that only the upper 12 bits of the ID need to match, while the lower 20 bits can be any value.
In this case, any extended ID whose higher 12 bits match 0x1F3 will pass the filter. This means that IDs such as 0x1F300000, 0x1F3FFFFF, etc. will be accepted because they match the filter ID on the bits specified by the mask.

  • Meet the following conditions:

    E

    I

    D

    M

    &

    R

    e

    c

    e

    i

    v

    e

    M

    s

    g

    I

    D

    =

    =

    E

    I

    D

    M

    &

    S

    e

    t

    I

    D

    EIDM \ & ReceiveMsgID == EIDM\ &SetID

    EIDM &ReceiveMsgID==EIDM &SetID

EIDM is very useful when precise control of a large number of messages is required, especially in complex CAN network environments, such as automotive or industrial automation systems. This precise message filtering can ensure that only relevant and necessary information is processed.

FDCAN_RxHeaderTypeDef RxHeader;
uint8_t RxData[8];
void FDCAN1_Config(void)
{<!-- -->
  FDCAN_FilterTypeDef sFilterConfig;
  /* Configure Rx filter */
  sFilterConfig1.IdType = FDCAN_EXTENDED_ID; /* Set standard ID or extended ID */
  sFilterConfig1.FilterIndex = 0; /* Used to filter the index, if it is a standard ID, the range is 0 to 127. If it is an extended ID, the range is 0 to 64. The number of this is configured when configuring the CAN filter peripheral. */
  sFilterConfig1.FilterType = FDCAN_FILTER_MASK; /* Filter sampling mask bit pattern */
  sFilterConfig1.FilterConfig = FDCAN_FILTER_TO_RXFIFO0; /* If the filter matches, save the data to Rx FIFO 0 */
  sFilterConfig1.FilterID1 = 0x111; /* In mask bit mode, FilterID1 is the message ID */
  sFilterConfig1.FilterID2 = 0x7FF; /* In mask bit mode, FilterID2 is the message mask bit */

  if (HAL_FDCAN_ConfigFilter( & amp;hfdcan1, & amp;sFilterConfig1) != HAL_OK) /* Configure filter */
  {<!-- -->
    Error_Handler();
  }


  /* Configure global filter:
     Filter all remote frames with STD and EXT ID
     Reject non matching frames with STD ID and EXT ID */
  if (HAL_FDCAN_ConfigGlobalFilter( & amp;hfdcan1, FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE) != HAL_OK)
  {<!-- -->
    Error_Handler();
  }

  if (HAL_FDCAN_ActivateNotification( & amp;hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK)
  {<!-- -->
    Error_Handler();
  }

  /* Start the FDCAN module */
  if (HAL_FDCAN_Start( & amp;hfdcan1) != HAL_OK)
  {<!-- -->
    Error_Handler();
  }
}

void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
{<!-- -->
  if((RxFifo0ITs & amp; FDCAN_IT_RX_FIFO0_NEW_MESSAGE) != RESET)
  {<!-- -->
    /* Retrieve Rx messages from RX FIFO0 */
    if (HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, & amp;RxHeader, RxData) != HAL_OK)
    {<!-- -->
      Error_Handler();
    }
  }
}

HAL_FDCAN_ConfigGlobalFilter
@brief Configure FDCAN global filter.
@param hfdcan Pointer to the FDCAN_HandleTypeDef structure containing the specified FDCAN configuration information.
@param NonMatchingStd defines how to handle received messages with an 11-digit ID that do not match any element in the filter list.
This parameter can take the value @arg FDCAN_Non_Matching_Frames.
@param NonMatchingExt defines how to handle received messages with a 29-bit ID that do not match any element in the filter list.
This parameter can take the value @arg FDCAN_Non_Matching_Frames.
@param RejectRemoteStd Filters or rejects all remote 11-bit ID frames.
This parameter can take the value @arg FDCAN_Reject_Remote_Frames.
@param RejectRemoteExt Filter or reject all remote 29-bit ID frames.
This parameter can take the value @arg FDCAN_Reject_Remote_Frames.
@retval HAL status

/** @defgroup FDCAN_Non_Matching_Frames FDCAN non-matching frames

  • @{
    /
    #define FDCAN_ACCEPT_IN_RX_FIFO0 ((uint32_t)0x00000000U) /
    !< Accept in Rx FIFO 0 /
    #define FDCAN_ACCEPT_IN_RX_FIFO1 ((uint32_t)0x00000001U) /
    !< Accept in Rx FIFO 1 /
    #define FDCAN_REJECT ((uint32_t)0x00000002U) /
    !< Reject /
    /
    *
  • @}
    */

/** @defgroup FDCAN_Reject_Remote_Frames FDCAN reject remote frames

  • @{
    /
    #define FDCAN_FILTER_REMOTE ((uint32_t)0x00000000U) /
    !< Filter remote frames /
    #define FDCAN_REJECT_REMOTE ((uint32_t)0x00000001U) /
    !< Reject all remote frames */