有关CAN多节点发送数据冲突的问题!
时间:10-02
整理:3721RD
点击:
芯片:stm32 F103E芯片,一个电脑上安装周立功CAN卡,总线上连接12个设备CAN。调试时出现总线冲突。大家帮忙看看代码是否有问题。
int InitFilterCan(u8 canAddr, u32 filterMaskCode)
{
CAN_FilterInitTypeDef CAN_FilterStructure;
CAN_ITConfig((&CANDevList[canAddr - 1])->ADDR, CAN_IT_FMP0, DISABLE);
CAN_FilterStructure.CAN_FilterNumber = 0;
CAN_FilterStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterStructure.CAN_FilterIdHigh = (u16)(filterMaskCode& 0xE0)>>5;
CAN_FilterStructure.CAN_FilterIdLow = (filterMaskCode << 11);//6.20
CAN_FilterStructure.CAN_FilterMaskIdHigh = 0x0007;
CAN_FilterStructure.CAN_FilterMaskIdLow = (u16)((u16)0xFF << 11);//6.20
CAN_FilterStructure.CAN_FilterFIFOAssignment = CAN_FIFO0;
CAN_FilterStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterStructure);
#define CAN_FILTER_2
#ifdef CAN_FILTER_2
CAN_FilterStructure.CAN_FilterNumber = 2;
CAN_FilterStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterStructure.CAN_FilterIdHigh = 0x0007;
CAN_FilterStructure.CAN_FilterIdLow = (u16)((u16)0x00FF << 11);
CAN_FilterStructure.CAN_FilterMaskIdHigh = 0x0007;
CAN_FilterStructure.CAN_FilterMaskIdLow = (u16)((u16)0x00FF << 11);
CAN_FilterStructure.CAN_FilterFIFOAssignment = CAN_FIFO0;
CAN_FilterStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterStructure);
#endif
CAN_ITConfig((&CANDevList[canAddr - 1])->ADDR, CAN_IT_FMP0, ENABLE);
return 1;
}
int InitCan(pCANDev pCanDevice, u32 filterMaskCode, u32 baudType)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
//CAN_FilterInitTypeDef CAN_FilterStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//CanTxMsg CAN_TxStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = pCanDevice->RxIRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pCanDevice->PreemptionPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = pCanDevice->SubPriority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = pCanDevice->TxIRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pCanDevice->PreemptionPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = pCanDevice->SubPriority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
BSP_IntVectSet(pCanDevice->IntId,pCanDevice->RxIRQHandler);
APBxPeriphClockCmd(GPIO_BUS,pCanDevice->APBPeriphGpio , ENABLE);//3?ê??ˉCANê1ó?μ?GPIOê±?ó
APBxPeriphClockCmd(CAN_BUS,pCanDevice->APBPeriphCan, ENABLE);//3?ê??ˉCANê±?ó
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = pCanDevice->CAN_RX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(pCanDevice->CPOIx, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = pCanDevice->CAN_TX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(pCanDevice->CPOIx, &GPIO_InitStructure);
/* CAN register init */
CAN_DeInit(pCanDevice->ADDR);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = ENABLE;//ENABLE;//;
CAN_InitStructure.CAN_AWUM = ENABLE;
CAN_InitStructure.CAN_NART = DISABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
CAN_InitStructure.CAN_Prescaler = 36000/baudType/6; //OK //36M/(36000/baudType/6)/(2+3+1)
CAN_Init(pCanDevice->ADDR, &CAN_InitStructure);
return 1;
}
int CanOpen(u8 canAddr, u32 filterMaskCode, u32 baudType)
{
if(canAddr == 1 )
{
InitCan(&CANDevList[canAddr - 1], filterMaskCode, baudType);
InitFilterCan(canAddr, filterMaskCode);
}
return 1;
}
uint32_t CreateExtId(u8 PF)
{
uint32_t extId = 0;
extId |= PF;
extId <<= 8;
extId |= 0;
extId <<= 8;
extId |= _g_DC_Id;
return extId;
}
CanTxMsg __createCanTxMsgHeader(u8 PF)
{
CanTxMsg canMsg;
memset(&canMsg, 0, sizeof(CanTxMsg));
canMsg.StdId = 0;
canMsg.ExtId = CreateExtId(PF);
canMsg.IDE = CAN_ID_EXT; //à??1??
canMsg.RTR = CAN_RTR_DATA; //êy?Y??
return canMsg;
}
int CanSend(u8 canAddr, CanTxMsg CAN_TxStructure)
{
u8 state;
// CAN_Transmit(CANDevList[canAddr - 1].ADDR, &CAN_TxStructure);
// return 1;
u8 TransmitMailbox = 0;
u32 t=0;
TransmitMailbox = CAN_Transmit(CANDevList[canAddr - 1].ADDR, &CAN_TxStructure);
/* μè′y·¢?ííê3é */
state = CAN_TransmitStatus(CANDevList[canAddr - 1].ADDR, TransmitMailbox);
while((state != CANTXOK) && (t < 0xffff))
{
if(state == CANTXFAILED)
return _Can_Transmit_Fail;
else if(state == CAN_NO_MB)
return _Can_No_MailBox;
t++;
state = CAN_TransmitStatus(CANDevList[canAddr - 1].ADDR, TransmitMailbox);
}
if(t == 0XFFFF)
return _Can_Transmit_timeout;
return _Can_Success;
}
int InitFilterCan(u8 canAddr, u32 filterMaskCode)
{
CAN_FilterInitTypeDef CAN_FilterStructure;
CAN_ITConfig((&CANDevList[canAddr - 1])->ADDR, CAN_IT_FMP0, DISABLE);
CAN_FilterStructure.CAN_FilterNumber = 0;
CAN_FilterStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterStructure.CAN_FilterIdHigh = (u16)(filterMaskCode& 0xE0)>>5;
CAN_FilterStructure.CAN_FilterIdLow = (filterMaskCode << 11);//6.20
CAN_FilterStructure.CAN_FilterMaskIdHigh = 0x0007;
CAN_FilterStructure.CAN_FilterMaskIdLow = (u16)((u16)0xFF << 11);//6.20
CAN_FilterStructure.CAN_FilterFIFOAssignment = CAN_FIFO0;
CAN_FilterStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterStructure);
#define CAN_FILTER_2
#ifdef CAN_FILTER_2
CAN_FilterStructure.CAN_FilterNumber = 2;
CAN_FilterStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterStructure.CAN_FilterIdHigh = 0x0007;
CAN_FilterStructure.CAN_FilterIdLow = (u16)((u16)0x00FF << 11);
CAN_FilterStructure.CAN_FilterMaskIdHigh = 0x0007;
CAN_FilterStructure.CAN_FilterMaskIdLow = (u16)((u16)0x00FF << 11);
CAN_FilterStructure.CAN_FilterFIFOAssignment = CAN_FIFO0;
CAN_FilterStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterStructure);
#endif
CAN_ITConfig((&CANDevList[canAddr - 1])->ADDR, CAN_IT_FMP0, ENABLE);
return 1;
}
int InitCan(pCANDev pCanDevice, u32 filterMaskCode, u32 baudType)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
//CAN_FilterInitTypeDef CAN_FilterStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//CanTxMsg CAN_TxStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = pCanDevice->RxIRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pCanDevice->PreemptionPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = pCanDevice->SubPriority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = pCanDevice->TxIRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pCanDevice->PreemptionPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = pCanDevice->SubPriority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
BSP_IntVectSet(pCanDevice->IntId,pCanDevice->RxIRQHandler);
APBxPeriphClockCmd(GPIO_BUS,pCanDevice->APBPeriphGpio , ENABLE);//3?ê??ˉCANê1ó?μ?GPIOê±?ó
APBxPeriphClockCmd(CAN_BUS,pCanDevice->APBPeriphCan, ENABLE);//3?ê??ˉCANê±?ó
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = pCanDevice->CAN_RX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(pCanDevice->CPOIx, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = pCanDevice->CAN_TX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(pCanDevice->CPOIx, &GPIO_InitStructure);
/* CAN register init */
CAN_DeInit(pCanDevice->ADDR);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = ENABLE;//ENABLE;//;
CAN_InitStructure.CAN_AWUM = ENABLE;
CAN_InitStructure.CAN_NART = DISABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
CAN_InitStructure.CAN_Prescaler = 36000/baudType/6; //OK //36M/(36000/baudType/6)/(2+3+1)
CAN_Init(pCanDevice->ADDR, &CAN_InitStructure);
return 1;
}
int CanOpen(u8 canAddr, u32 filterMaskCode, u32 baudType)
{
if(canAddr == 1 )
{
InitCan(&CANDevList[canAddr - 1], filterMaskCode, baudType);
InitFilterCan(canAddr, filterMaskCode);
}
return 1;
}
uint32_t CreateExtId(u8 PF)
{
uint32_t extId = 0;
extId |= PF;
extId <<= 8;
extId |= 0;
extId <<= 8;
extId |= _g_DC_Id;
return extId;
}
CanTxMsg __createCanTxMsgHeader(u8 PF)
{
CanTxMsg canMsg;
memset(&canMsg, 0, sizeof(CanTxMsg));
canMsg.StdId = 0;
canMsg.ExtId = CreateExtId(PF);
canMsg.IDE = CAN_ID_EXT; //à??1??
canMsg.RTR = CAN_RTR_DATA; //êy?Y??
return canMsg;
}
int CanSend(u8 canAddr, CanTxMsg CAN_TxStructure)
{
u8 state;
// CAN_Transmit(CANDevList[canAddr - 1].ADDR, &CAN_TxStructure);
// return 1;
u8 TransmitMailbox = 0;
u32 t=0;
TransmitMailbox = CAN_Transmit(CANDevList[canAddr - 1].ADDR, &CAN_TxStructure);
/* μè′y·¢?ííê3é */
state = CAN_TransmitStatus(CANDevList[canAddr - 1].ADDR, TransmitMailbox);
while((state != CANTXOK) && (t < 0xffff))
{
if(state == CANTXFAILED)
return _Can_Transmit_Fail;
else if(state == CAN_NO_MB)
return _Can_No_MailBox;
t++;
state = CAN_TransmitStatus(CANDevList[canAddr - 1].ADDR, TransmitMailbox);
}
if(t == 0XFFFF)
return _Can_Transmit_timeout;
return _Can_Success;
}
12个设备同时上电,用示波器抓出来显示,似乎有电平叠加在一起了。高高低低,很乱,请高手帮忙解决!
正在学习中。