查看: 1075|回复: 2

[分享] 解决LPC54608can使用中断处理接收数据时漏包问题

[复制链接]
  • TA的每日心情
    开心
    2023-2-28 15:37
  • 签到天数: 42 天

    [LV.5]常住居民I

    42

    主题

    500

    帖子

    0

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    1369

    热心会员

    最后登录
    2024-2-23
    发表于 2022-6-28 09:05:53 | 显示全部楼层 |阅读模式
    解决LPC54608can使用中断处理接收数据时漏包问题
    接上一篇说吧,今天还是针对can这块做个简单的分享。


    1、要先明确什么会导致漏包?
    can发送到速度过快。中断接收到数据,但是数据被覆盖,应用层处理不过来。


    1个can包16Byte(8Byte数据,8Byte其他参数)
    假设can 速率250kbps。则1s中大能最传输多少包?
    250000bps / (16*8) = 1953个包。


    2、解决方法:
    使用ringbuff增加缓存数据区,增加一段buffer保存数据。
    1. mcan_rx_buffer_frame_t rxFrame[64];
    2. volatile uint16_t rdIndex=0; /* Index of the data to send out. */
    3. volatile uint16_t wrIndex=0; /* Index of the memory to save new arrived data. */
    4. void CAN0_IRQ0_IRQHandler(void)
    5. {
    6.     MCAN_ClearStatusFlag(EXAMPLE_MCAN, CAN_IR_RF0N_MASK);
    7.        
    8.     MCAN_ReadRxFifo(EXAMPLE_MCAN, 0, &rxFrame[wrIndex]);
    9.           wrIndex = ((++wrIndex) & (64-1));
    10.           if (wrIndex == rdIndex) {
    11.                         rdIndex = ((++rdIndex) & (64-1));
    12.                 }
    13.     /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
    14.       exception return operation might vector to incorrect interrupt */
    15. #if defined __CORTEX_M && (__CORTEX_M == 4U)
    16.     __DSB();
    17. #endif
    18. }
    19.         read_data()
    20.     {
    21.                 ... ...
    22.                 while (rdIndex == wrIndex) {
    23.                 }
    24.                 memcpy(rx_buf, rxFrame[rdIndex].data, rxFrame[rdIndex].size);
    25.                 can_id = rxFrame[rdIndex].id;
    26.                 //rdIndex = ((rdIndex++) & (64-1));

    27.                 rdIndex = ((++rdIndex) & (64-1));
    28.                 return (can_id/* >> STDID_OFFSET*/);
    29.     }
    复制代码


    签到签到
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-26 15:16
  • 签到天数: 266 天

    [LV.8]以坛为家I

    3299

    主题

    6546

    帖子

    0

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    32024
    最后登录
    2024-4-25
    发表于 2022-6-28 09:15:55 | 显示全部楼层
    感谢分享
    签到签到
    回复

    使用道具 举报

    该用户从未签到

    26

    主题

    97

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    386
    最后登录
    2023-9-12
    发表于 2023-5-19 15:22:45 | 显示全部楼层
    牛鼻了我的神啦
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2024-4-26 01:56 , Processed in 0.114375 second(s), 20 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表