当前位置: 首页 > news >正文

rt-thread H7 使用fdcan没有外接设备时或发送错误时线程被挂起的解决方案

一、问题查找
         使用的开发版是硬石的H7芯片型号STM32H743IIT6,测试时发现如果外面没有连接CAN设备,程序调用CAN发送时会一直等待发送反馈,导致相关线程挂起。
在线仿真时发现是卡在can.c文件的168行_can_int_tx函数:rt_completion_wait(&(tx_tosnd->completion), RT_WAITING_FOREVER);
代码如下:

rt_inline int _can_int_tx(struct rt_can_device *can, const struct rt_can_msg *data, int msgs)
{int size;struct rt_can_tx_fifo *tx_fifo;RT_ASSERT(can != RT_NULL);size = msgs;tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;RT_ASSERT(tx_fifo != RT_NULL);while (msgs){rt_base_t level;rt_uint32_t no;rt_uint32_t result;struct rt_can_sndbxinx_list tx_tosnd = RT_NULL;rt_sem_take(&(tx_fifo->sem), RT_WAITING_FOREVER);level = rt_hw_interrupt_disable();tx_tosnd = rt_list_entry(tx_fifo->freelist.next, struct rt_can_sndbxinx_list, list);RT_ASSERT(tx_tosnd != RT_NULL);rt_list_remove(&tx_tosnd->list);rt_hw_interrupt_enable(level);no = ((rt_uint32_t)tx_tosnd - (rt_uint32_t)tx_fifo->buffer) / sizeof(struct rt_can_sndbxinx_list);tx_tosnd->result = RT_CAN_SND_RESULT_WAIT;if (can->ops->sendmsg(can, data, no) != RT_EOK){/ send failed. */level = rt_hw_interrupt_disable();rt_list_insert_after(&tx_fifo->freelist, &tx_tosnd->list);rt_hw_interrupt_enable(level);rt_sem_release(&(tx_fifo->sem));continue;}can->status.sndchange = 1;rt_completion_wait(&(tx_tosnd->completion), RT_WAITING_FOREVER);

说明一直在等待完成信号,而发送完成信号的地方在can.c文件的900行rt_hw_can_isr函数:rt_completion_done(&(tx_fifo->buffer[no].completion));
源码如下:

case RT_CAN_EVENT_TX_DONE:
case RT_CAN_EVENT_TX_FAIL:
{struct rt_can_tx_fifo *tx_fifo;rt_uint32_t no;no = event > > 8;tx_fifo = (struct rt_can_tx_fifo *) can- >can_tx;RT_ASSERT(tx_fifo != RT_NULL);if ((event & 0xff) == RT_CAN_EVENT_TX_DONE){tx_fifo- >buffer[no].result = RT_CAN_SND_RESULT_OK;}else{tx_fifo- >buffer[no].result = RT_CAN_SND_RESULT_ERR;}rt_completion_done(&(tx_fifo- >buffer[no].completion));break;
}

二、解决方案
         看完can的驱动源码后想到如果can总线没有其他设备,CAN发送报文应该属于出错的情况,查看drv_fdcan.c文件中关于几种中断的处理,发现故障后的回调函数里没有调用rt_hw_can_isr。于是参考HAL_FDCAN_TxBufferCompleteCallback函数的处理方式,对HAL_FDCAN_ErrorCallback进行了如下处理。
源码如下:

void HAL_FDCAN_ErrorCallback(FDCAN_HandleTypeDef *hfdcan)
{rt_uint32_t tmp_u32Errcount;rt_uint32_t tmp_u32status;uint32_t ret = HAL_FDCAN_GetError(hfdcan);if(hfdcan->Instance == FDCAN1){#ifdef BSP_USING_FDCAN1//can1if( (ret & FDCAN_IT_ARB_PROTOCOL_ERROR) &&(hfdcan->Instance->CCCR & FDCAN_CCCR_INIT_Msk)){//hfdcan->Instance->CCCR |= FDCAN_CCCR_CCE_Msk;hfdcan->Instance->CCCR &= ~FDCAN_CCCR_INIT_Msk;st_DrvCan1.device.status.errcode = 0xff;}else{tmp_u32Errcount = st_DrvCan1.fdcanHandle.Instance->ECR;tmp_u32status = st_DrvCan1.fdcanHandle.Instance->PSR;st_DrvCan1.device.status.rcverrcnt = (tmp_u32Errcount>>8)&0x000000ff;st_DrvCan1.device.status.snderrcnt = (tmp_u32Errcount)&0x000000ff;st_DrvCan1.device.status.lasterrtype = tmp_u32status&0x000000007;rt_hw_can_isr(&st_DrvCan1.device, RT_CAN_EVENT_TX_FAIL);}#endif / BSP_USING_FDCAN1 /}else{#ifdef BSP_USING_FDCAN2if( (ret & FDCAN_IT_ARB_PROTOCOL_ERROR) &&(hfdcan->Instance->CCCR & FDCAN_CCCR_INIT_Msk)){//hfdcan->Instance->CCCR |= FDCAN_CCCR_CCE_Msk;hfdcan->Instance->CCCR &= ~FDCAN_CCCR_INIT_Msk;st_DrvCan2.device.status.errcode = 0xff;}else{//can2tmp_u32Errcount = st_DrvCan2.fdcanHandle.Instance->ECR;tmp_u32status = st_DrvCan2.fdcanHandle.Instance->PSR;st_DrvCan2.device.status.rcverrcnt = (tmp_u32Errcount>>8)&0x000000ff;st_DrvCan2.device.status.snderrcnt = (tmp_u32Errcount)&0x000000ff;st_DrvCan2.device.status.lasterrtype = tmp_u32status&0x000000007;rt_hw_can_isr(&st_DrvCan2.device, RT_CAN_EVENT_TX_FAIL);}#endif / BSP_USING_FDCAN2 /}
}

经修改后测试发现一切正常,发送错误后就会进入错误处理,不会卡死发送线程。

http://www.lryc.cn/news/416390.html

相关文章:

  • exptern “C“的作用,在 C 和 CPP 中分别调用 openblas 中的 gemm 为例
  • 如何提前预防网络威胁
  • ProviderRpc发送服务二将远程调用来的信息反序列化后调用服务方的方法,并将服务方的结果返回给发送方
  • Io 35
  • java基础概念11-方法
  • 大模型应用中的思维树(Tree of Thought)是什么?
  • 学习记录(11):训练图片分类的算法
  • 上网防泄密,这些雷区不要碰!九招教你如何防泄密
  • 数据库篇--八股文学习第十五天| 一条SQL查询语句是如何执行的?,事务的四大特性有哪些?,数据库的事务隔离级别有哪些?
  • elk + filebeat + kafka实验和RSync同步
  • 子类到底能继承父类中的哪些内容?
  • 【超详细公式】曝光值(EV)、光圈(AV)、快门(TV)、感光度(SV)、照度(Lux)
  • 【Java】增强for遍历集合。
  • 【Qt】管理创建子项目
  • 力扣——238.移动零
  • 编程的魅力
  • 想提升跨境电商运营?浏览器多开为你助力!
  • 使用QML的ListView自制树形结构图TreeView
  • 2.MySQL面试题之索引
  • 复制CodeIgniter新版的array_group_by辅助函数
  • 合并两个 ES (Elasticsearch) 的数据
  • Linux网络协议.之 tcp,udp,socket网络编程(四).之网络转换函数htonl,ntohs等介绍
  • LXC和udev知识点
  • 基于springboot+vue+uniapp的智慧校园管理系统小程序
  • 论文辅导 | 基于概率密度估计与时序Transformer网络的风功率日前区间预测
  • 金蝶云星空单据体数量汇总-分组列信息
  • 树状数组基础知识以及相关习题
  • 2023大数据-架构师案例(八)
  • 【Python】Python:探索未来科技的风向标
  • Java语言程序设计——篇十一(6)