一、问题查找
使用的开发版是硬石的H7芯片型号STM32H743IIT6,测试时发现如果外面没有连接CAN设备,程序调用CAN发送时会一直等待发送反馈,导致相关线程挂起。
在线仿真时发现是卡在can.c文件的168行_can_int_tx函数:rt_completion_wait(&(tx_tosnd->completion), RT_WAITING_FOREVER);
代码如下:
说明一直在等待完成信号,而发送完成信号的地方在can.c文件的900行rt_hw_can_isr函数:rt_completion_done(&(tx_fifo->buffer[no].completion));
源码如下:
二、解决方案
看完can的驱动源码后想到如果can总线没有其他设备,CAN发送报文应该属于出错的情况,查看drv_fdcan.c文件中关于几种中断的处理,发现故障后的回调函数里没有调用rt_hw_can_isr。于是参考HAL_FDCAN_TxBufferCompleteCallback函数的处理方式,对HAL_FDCAN_ErrorCallback进行了如下处理。
源码如下:
经修改后测试发现一切正常,发送错误后就会进入错误处理,不会卡死发送线程。