【ST NUCLEO-U5A5ZJ-Q开发板测评】-7-FDCAN接收数据测试
<div class='showpostmsg'> 本帖最后由 慕容雪花 于 2024-3-5 16:09 编辑<p>U5上一篇了解FDCAN的测评:【ST NUCLEO-U5A5ZJ-Q开发板测评】-6-FDCAN体验 <a href="https://bbs.eeworld.com.cn/thread-1273353-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1273353-1-1.html</a></p>
<p> </p>
<p>本次实验来学习FDCAN的接收上位机发来的Can报文,了解报文的ID过滤器等相关机制以及在cubeide中配置的地方。</p>
<p> </p>
<p>在配置接收过滤器的时候,需要指定标准帧过滤器和扩展帧过滤器的个数:</p>
<p> </p>
<p>生成代码:</p>
<p> </p>
<p> </p>
<p>接下来是过滤器的具体工作模式:</p>
<pre>
<code class="language-cpp"> 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();
}</code></pre>
<p>其中,标准帧过滤器选择的是RANGE即范围模式,FilterID1=0x100, FilterID2=0x200,即接收报文ID在两者之间的会被接收到RXFIFO0.</p>
<p>在主函数中来检查FIFO里是否有待读取的报文:</p>
<pre>
<code class="language-cpp"> /*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);
}
printf("\n");
}</code></pre>
<p>测试:</p>
<p></p>
<p>上图所示为上位机发送到ID为0x168的标准数据帧,在串口助手中可以看到U5已经正确接收相关数据了。</p>
<p>接下来又发送了ID为0x200的报文,可以正常被U5接收,但是ID为0x201的报文就被拒绝了。说明过滤器干活了!</p>
<p> </p>
<p> </p>
<p><strong><span style="font-size:16px;">参考文章:</span></strong></p>
<p>【STM32】STM32 CANFD外设的几种消息接收过滤模式 <a href="https://blog.csdn.net/qq_42820594/article/details/134388925" target="_blank">https://blog.csdn.net/qq_42820594/article/details/134388925</a></p>
<p>STM32H7 FDCAN FILTER <a href="https://community.st.com/t5/stm32-mcus-products/stm32h7-fdcan-filter/m-p/406487" target="_blank">https://community.st.com/t5/stm32-mcus-products/stm32h7-fdcan-filter/m-p/406487</a></p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]