STM32 HAL库配置CAN滤波代码分享

在STM32上只用CAN,一定离不开滤波器配置,现总结下各种场景下的配置方法及注意事项。

STM32 HAL库配置CAN滤波代码分享_第1张图片

1、一路CAN有14组滤波器,每组滤波器可关联FIFO0或FIFO1;

2、14组滤波器为或关系,CAN报文通过任一组滤波器即可进入FIFO;

3、每组滤波器可以配置为4种工作模式:

(1) 1个32位屏蔽位模式,用于扩展帧。

    //只接收ID匹配的扩展数据帧
    CAN_FilterTypeDef  stFilterConfig;
    stFilterConfig.FilterBank = 0;
    stFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
    stFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
    stFilterConfig.FilterIdHigh = (((uint32_t)Id<<3)&0xFFFF0000)>>16;
    stFilterConfig.FilterIdLow = (((uint32_t)Id<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
    stFilterConfig.FilterMaskIdHigh = (((uint32_t)Mask<<3)&0xFFFF0000)>>16; 
    stFilterConfig.FilterMaskIdLow = (((uint32_t)Mask<<3)|0x06)&0xFFFF;
    stFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
    stFilterConfig.FilterActivation = ENABLE;   //must ENABLE
    stFilterConfig.SlaveStartFilterBank = 14;

(2) 2个32位列表模式,用于扩展帧,2个之间为或关系,若只使用1个,2个可配置相同参数。

   //只接收ID匹配的扩展数据帧
    CAN_FilterTypeDef CAN_FilterType;
    CAN_FilterType.FilterBank = 0;
    CAN_FilterType.FilterIdHigh = (((uint32_t)Id1<<3)&0xFFFF0000)>>16;
    CAN_FilterType.FilterIdLow =  (((uint32_t)Id1<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
    CAN_FilterType.FilterMaskIdHigh = (((uint32_t)Id2<<3)&0xFFFF0000)>>16;
    CAN_FilterType.FilterMaskIdLow = (((uint32_t)Id2<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
    CAN_FilterType.FilterFIFOAssignment = CAN_RX_FIFO0;
    CAN_FilterType.FilterMode = CAN_FILTERMODE_IDLIST;
    CAN_FilterType.FilterScale = CAN_FILTERSCALE_32BIT;
    CAN_FilterType.FilterActivation = ENABLE;
    CAN_FilterType.SlaveStartFilterBank = 14;

(3) 2个16位屏蔽位模式,用于标准帧,2个之间为或关系,若只使用一个,两个可配置相同参数。

   //只接收ID匹配的标准数据帧
    CAN_FilterTypeDef  stFilterConfig;
    stFilterConfig.FilterBank = 0;
    stFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
    stFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT;
    stFilterConfig.FilterIdHigh =(((uint32_t)Id1<<5)|((CAN_ID_STD|CAN_RTR_DATA)<<2))&0xFFFF;
    stFilterConfig.FilterIdLow = (((uint32_t)Id2<<5)|((CAN_ID_STD|CAN_RTR_DATA)<<2))&0xFFFF;
    stFilterConfig.FilterMaskIdHigh = (((uint32_t)Mask1<<5)|(0x06<< 2))&0xFFFF;
    stFilterConfig.FilterMaskIdLow = (((uint32_t)Mask2<<5)|(0x06<<2))&0xFFFF;
    stFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
    stFilterConfig.FilterActivation = ENABLE;   //must ENABLE
    stFilterConfig.SlaveStartFilterBank = 14;

(4) 4个16位列表模式,用于标准帧,4个之间为或关系,若只使用一个,四个可配置相同参数。

    CAN_FilterTypeDef CAN_FilterType;
    CAN_FilterType.FilterBank = 0;
    CAN_FilterType.FilterIdHigh = (Id1 << 5)|(CAN_ID_STD<<1)|(CAN_RTR_DATA << 3);
    CAN_FilterType.FilterIdLow =  (Id2 << 5)|(CAN_ID_STD<<1)|(CAN_RTR_REMOTE << 3);
    CAN_FilterType.FilterMaskIdHigh = (CAN_ID_EXT<<1);//也可以这样配置禁用该组
    CAN_FilterType.FilterMaskIdLow = (CAN_ID_EXT<<1);//也可以这样配置禁用该组
    CAN_FilterType.FilterFIFOAssignment = CAN_RX_FIFO0;
    CAN_FilterType.FilterMode = CAN_FILTERMODE_IDLIST;
    CAN_FilterType.FilterScale = CAN_FILTERSCALE_16BIT;
    CAN_FilterType.FilterActivation = ENABLE;
    CAN_FilterType.SlaveStartFilterBank = 14;

4、即使接收所有CAN报文不需要滤波,也要配置至少一组滤波器;

    CAN_FilterTypeDef CAN_FilterType;
    CAN_FilterType.FilterBank = 0;
    CAN_FilterType.FilterIdHigh = 0;
    CAN_FilterType.FilterIdLow =  0;
    CAN_FilterType.FilterMaskIdHigh = 0;
    CAN_FilterType.FilterMaskIdLow = 0;
    CAN_FilterType.FilterFIFOAssignment = CAN_RX_FIFO0;
    CAN_FilterType.FilterMode = CAN_FILTERMODE_IDMASK;
    CAN_FilterType.FilterScale = CAN_FILTERSCALE_32BIT;
    CAN_FilterType.FilterActivation = ENABLE;
    CAN_FilterType.SlaveStartFilterBank = 14;

##以上代码在STM32F103C8T6上亲测有效,欢迎大家参考反馈;

你可能感兴趣的:(STM32,stm32,can,过滤器)