本帖最后由 luckyren 于 2023-1-13 09:04 编辑
我在调试HC32F460的CAN通讯时发现如果同时收到了两帧数据,第二帧数据会保存下来,导致下面接收的数据是上一帧的数据,然后后面的接收都错位了,中断里也清标志位了,有人指导一下吗?我的初始化及中断接收处理如下:static void CanInitConfig(uint16_t CAN_Baud_Rate)
{
stc_can_init_config_t stcCanInitCfg;
stc_can_filter_t astcFilters[CAN_FILTERS_COUNT] = \
{
{0x00000000ul, 0x111ul, CanFilterSel1, CanAllFrames}
};
//<< Enable can peripheral clock and buffer(ram)
PWC_RamOpMdConfig(HighSpeedMd);
PWC_RamPwrdownCmd(PWC_RAMPWRDOWN_CAN, Enable);
PWC_Fcg1PeriphClockCmd(PWC_FCG1_PERIPH_CAN, Enable);
//<< CAN GPIO config
PORT_SetFunc(PortC, Pin06, Func_Can1_Rx, Disable);
PORT_SetFunc(PortC, Pin07, Func_Can1_Tx, Disable);
MEM_ZERO_STRUCT(stcCanInitCfg);
//<< Can bit time config
switch(CAN_Baud_Rate)
{
case 1000: //1M
stcCanInitCfg.stcCanBt.PRESC = 1u-1u;
break;
case 500: //500K
stcCanInitCfg.stcCanBt.PRESC = 2u-1u;
break;
case 250: //250K
stcCanInitCfg.stcCanBt.PRESC = 4u-1u;
break;
case 125: //125K
stcCanInitCfg.stcCanBt.PRESC = 8u-1u;
break;
case 100: //100K
stcCanInitCfg.stcCanBt.PRESC = 10u-1u;
break;
default:
stcCanInitCfg.stcCanBt.PRESC = 2u-1u;
break;
}
stcCanInitCfg.stcCanBt.SEG_1 = 5u-2u;
stcCanInitCfg.stcCanBt.SEG_2 = 3u-1u;
stcCanInitCfg.stcCanBt.SJW = 3u-1u;
stcCanInitCfg.stcWarningLimit.CanErrorWarningLimitVal = 10u;
stcCanInitCfg.stcWarningLimit.CanWarningLimitVal = 16u-1u;
stcCanInitCfg.enCanRxBufAll = CanRxNormal;
stcCanInitCfg.enCanRxBufMode = CanRxBufNotStored;
stcCanInitCfg.enCanSAck = CanSelfAckDisable;
stcCanInitCfg.enCanSTBMode = CanSTBFifoMode;
stcCanInitCfg.pstcFilter = astcFilters;
stcCanInitCfg.u8FilterCount = CAN_FILTERS_COUNT;
CAN_Init(&stcCanInitCfg);
stc_irq_regi_conf_t stcIrqRegiConf;
stcIrqRegiConf.enIRQn = Int000_IRQn;
stcIrqRegiConf.enIntSrc = INT_CAN_INT;
stcIrqRegiConf.pfnCallback = &CAN_RxIrqCallBack;
enIrqRegistration(&stcIrqRegiConf);
NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn);
NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);
}
static void CAN_RxIrqCallBack(void)
{
if(true == CAN_Irq**Get(CanRxIrq**))
{
CAN_Irq**Clr(CanRxIrq**);
CAN_IrqCmd(CanRxIrqEn, Disable);
CAN_Receive(&stcRxFrame);
// rt_kprintf("stcRxFrame.Cst.Control_f.DLC = %d\r\n",stcRxFrame.Cst.Control_f.DLC);
// rt_kprintf("stcRxFrame.Cst.Control_f.IDE = %d\r\n",stcRxFrame.Cst.Control_f.IDE);
can_RX_flag = 1;
}
if(true == CAN_Irq**Get(CanTxPrimaryIrq**))
{
CAN_Irq**Clr(CanTxPrimaryIrq**);
CAN_IrqCmd(CanRxIrqEn, Enable);
}
//总线忙碌中断
if(true == CAN_Irq**Get(CanBusErrorIrq**))
{
CAN_Irq**Clr(CanBusErrorIrq**);
}
//仲裁失败中断
if(true == CAN_Irq**Get(CanArbiLostIrq**))
{
CAN_Irq**Clr(CanArbiLostIrq**);
}
if(true == CAN_Irq**Get(CanRxOverIrq**))
{
CAN_Irq**Clr(CanRxOverIrq**);
// for (size_t i = 0; i < 10; i++)
// {
// CAN_Receive(&stcRxFrame);
// rt_kprintf("stcRxFrame.Cst.Control_f.DLC = %d\r\n",stcRxFrame.Cst.Control_f.DLC);
// rt_kprintf("stcRxFrame.Cst.Control_f.IDE = %d\r\n",stcRxFrame.Cst.Control_f.IDE);
// }
}
if(true == CAN_Irq**Get(CanErrorIrq**))
{
//gData.ResetCanF=1;
CAN_Irq**Clr(CanErrorIrq**);
}
if(true == CAN_Irq**Get(CanErrorWarningIrq**))
{
//gData.ResetCanF=1;
CAN_Irq**Clr(CanErrorWarningIrq**);
}
if(true == CAN_Irq**Get(CanErrorPassivenodeIrq**))
{
//gData.ResetCanF=1;
CAN_Irq**Clr(CanErrorPassivenodeIrq**);
}
if(true == CAN_Irq**Get(CanTxBufFullIrq**))
{
CAN_Irq**Clr(CanTxBufFullIrq**);
}
if(true == CAN_Irq**Get(CanRxBufFullIrq**))
{
CAN_Irq**Clr(CanRxBufFullIrq**);
//CAN_Receive(&stcRxFrame);
// memset(rxCanBuff,0,sizeof(rxCanBuff));
// memset(txCanBuff,0,sizeof(txCanBuff));
// memset(&stcRxFrame,0,sizeof(stcRxFrame));
// putCan_Data(txCanBuff,8,rxCanBuff[2]);
}
if(true == CAN_Irq**Get(CanRxBufAlmostFullIrq**))
{
CAN_Irq**Clr(CanRxBufAlmostFullIrq**);
}
if(true == CAN_Irq**Get(CanTxSecondaryIrq**))
{
CAN_Irq**Clr(CanTxSecondaryIrq**);
}
if(true == CAN_Irq**Get(CanAbortIrq**))
{
CAN_Irq**Clr(CanAbortIrq**);
}
if(true == CAN_Irq**Get(CanErrorPassiveIrq**))
{
CAN_Irq**Clr(CanErrorPassiveIrq**);
}
}
|