STM32H743的FDCAN发送线程卡死的处理方法

发布者:美好的人生最新更新时间:2024-09-04 来源: elecfans关键字:STM32H743  FDCAN 手机看文章 扫描二维码
随时随地手机看文章

芯片型号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发送报文应该属于出错的情况,查看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

//can1

if( (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_FDCAN2

if( (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

{

//can2

tmp_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 /

}

}


经过测试发现即使CAN总线上没有别的设备,调用发送函数也不会一直等待,而是返回发送失败


关键字:STM32H743  FDCAN 引用地址:STM32H743的FDCAN发送线程卡死的处理方法

上一篇:STM32的RTC晶振不起振的可能原因?
下一篇:STM32F0系列MCU硬件电路外设设计

小广播
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved