芯片型號(hào)STM32H743IIT6,測(cè)試時(shí)發(fā)現(xiàn)如果外面沒有連接CAN設(shè)備,程序調(diào)用CAN發(fā)送時(shí)會(huì)一直等待發(fā)送反饋,導(dǎo)致相關(guān)線程掛起。
分析發(fā)現(xiàn)是卡在can.c文件的168行_can_int_tx函數(shù):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);
說明一直在等待完成信號(hào),而發(fā)送完成信號(hào)的地方在can.c文件的900行rt_hw_can_isr函數(shù):
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總線沒有其他設(shè)備,CAN發(fā)送報(bào)文應(yīng)該屬于出錯(cuò)的情況,查看drv_fdcan.c文件中關(guān)于幾種中斷的處理,發(fā)現(xiàn)故障后的回調(diào)函數(shù)里沒有調(diào)用rt_hw_can_isr。
于是參考HAL_FDCAN_TxBufferCompleteCallback函數(shù)的處理方式,對(duì)HAL_FDCAN_ErrorCallback進(jìn)行了如下處理。
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 /
}
}
經(jīng)過測(cè)試發(fā)現(xiàn)即使CAN總線上沒有別的設(shè)備,調(diào)用發(fā)送函數(shù)也不會(huì)一直等待,而是返回發(fā)送失敗。
-
CAN總線
+關(guān)注
關(guān)注
145文章
1946瀏覽量
130726 -
中斷處理
+關(guān)注
關(guān)注
0文章
94瀏覽量
10967 -
FIFO存儲(chǔ)
+關(guān)注
關(guān)注
0文章
103瀏覽量
5968 -
STM32H743
+關(guān)注
關(guān)注
0文章
24瀏覽量
1708
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論