需要另外配置全局过滤器以拒绝所有不匹配的 ID。
全局过滤器
调用此函数配置全局过滤器。
/**
* @brief Configure the FDCAN global filter.
* @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
* the configuration information for the specified FDCAN.
* @param NonMatchingStd Defines how received messages with 11-bit IDs that
* do not match any element of the filter list are treated.
* This parameter can be a value of @arg FDCAN_Non_Matching_Frames.
* @param NonMatchingExt Defines how received messages with 29-bit IDs that
* do not match any element of the filter list are treated.
* This parameter can be a value of @arg FDCAN_Non_Matching_Frames.
* @param RejectRemoteStd Filter or reject all the remote 11-bit IDs frames.
* This parameter can be a value of @arg FDCAN_Reject_Remote_Frames.
* @param RejectRemoteExt Filter or reject all the remote 29-bit IDs frames.
* This parameter can be a value of @arg FDCAN_Reject_Remote_Frames.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_FDCAN_ConfigGlobalFilter(FDCAN_HandleTypeDef *hfdcan,
uint32_t NonMatchingStd,
uint32_t NonMatchingExt,
uint32_t RejectRemoteStd,
uint32_t RejectRemoteExt)
接收过滤器
标准 ID,掩码模式,只接收 ID 为 0xFF 的消息。
void FDCAN1_Filter_Config(void)
{
FDCAN_FilterTypeDef sFilterConfig0;
sFilterConfig0.IdType = FDCAN_STANDARD_ID;
sFilterConfig0.FilterIndex = 0;
sFilterConfig0.FilterType = FDCAN_FILTER_MASK;
sFilterConfig0.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig0.FilterID1 = 0xFF;
sFilterConfig0.FilterID2 = 0x7FF;
if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig0) != HAL_OK)
{
Error_Handler();
}
HAL_FDCAN_ConfigGlobalFilter(&hfdcan1, FDCAN_REJECT, FDCAN_REJECT, DISABLE, DISABLE);
}