本帖最后由 慕容雪花 于 2024-3-5 16:09 编辑
U5上一篇了解FDCAN的测评:【ST NUCLEO-U5A5ZJ-Q开发板测评】-6-FDCAN体验 https://bbs.eeworld.com.cn/thread-1273353-1-1.html
本次实验来学习FDCAN的接收上位机发来的Can报文,了解报文的ID过滤器等相关机制以及在cubeide中配置的地方。
在配置接收过滤器的时候,需要指定标准帧过滤器和扩展帧过滤器的个数:
生成代码:
接下来是过滤器的具体工作模式:
sFilterConfig.IdType = FDCAN_STANDARD_ID;
sFilterConfig.FilterIndex = 0;
sFilterConfig.FilterType = FDCAN_FILTER_RANGE;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = 0x100;
sFilterConfig.FilterID2 = 0x200;
if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK)
{
Error_Handler();
}
/* Configure extended ID reception filter to Rx FIFO 1 */
sFilterConfig.IdType = FDCAN_EXTENDED_ID;
sFilterConfig.FilterIndex = 0;
sFilterConfig.FilterType = FDCAN_FILTER_RANGE_NO_EIDM;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO1;
sFilterConfig.FilterID1 = 0x1111111;
sFilterConfig.FilterID2 = 0x2222222;
if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK)
{
Error_Handler();
}
其中,标准帧过滤器选择的是RANGE即范围模式,FilterID1=0x100, FilterID2=0x200,即接收报文ID在两者之间的会被接收到RXFIFO0.
在主函数中来检查FIFO里是否有待读取的报文:
/*Check Rec Msg Info*/
if(HAL_FDCAN_GetRxFifoFillLevel(&hfdcan1, FDCAN_RX_FIFO0) >= 1){
printf("\r\nNew Can Msg Received!\n");
/* Retrieve message from Rx FIFO 0 */
if (HAL_FDCAN_GetRxMessage(&hfdcan1, FDCAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
{
Error_Handler();
}else{
printf("\r\nReceived CAN message: ID:0x%02X, DLC:%d, Data:", RxHeader.Identifier, RxHeader.DataLength);
}
/* Print the received payload */
for(uint8_t i = 0; i < RxHeader.DataLength; i++)
{
printf("%02X ", RxData[i]);
}
printf("\n");
}
测试:
上图所示为上位机发送到ID为0x168的标准数据帧,在串口助手中可以看到U5已经正确接收相关数据了。
接下来又发送了ID为0x200的报文,可以正常被U5接收,但是ID为0x201的报文就被拒绝了。说明过滤器干活了!
参考文章:
【STM32】STM32 CANFD外设的几种消息接收过滤模式 https://blog.csdn.net/qq_42820594/article/details/134388925
STM32H7 FDCAN FILTER https://community.st.com/t5/stm32-mcus-products/stm32h7-fdcan-filter/m-p/406487