芯片:
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_FilterAc
tivation = 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);//³õʼ»¯CANʹÓõÄGPIOʱÖÓ
APBxPeriphClockCmd(CAN_BUS,pCanDevice->APBPeriphCan, ENABLE);//³õʼ»¯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; //À©Õ¹Ö¡
canMsg.RTR = CAN_RTR_DATA; //Êý¾ÝÖ¡
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);
/* µÈ´ý·¢ËÍÍê³É */
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;
}
一周热门 更多>