CAN初始化代码
/*
设置采样点在81.25%
tseg1 = (S_SEG1 +2);
tseg2 = (S_SEG2+1);
BandRate = (48M/(S_PRESC +1) / ((S_SEG1+2)+(S_SEG2+1))))
SamplePoint = (tseg1 /(tseg1 + tseg2));
*/
void SocCanPerhInit(void)
{
CAN_ConfigType canConfig;
CAN_BitrateConfigType canBandrateConfig;
memset((void *)&canConfig, 0, (uint32_t)sizeof(CAN_ConfigType));
memset((void *)&canBandrateConfig, 0, sizeof(CAN_BitrateConfigType));
GPIO_SetFunc(SPORT_GPIOCANTX,SPIN_GPIOCANTX, GPIO_FUN1);
GPIO_SetFunc(SPORT_GPIOCANRX,SPIN_GPIOCANRX, GPIO_FUN1);
GPIO_SetFunc(CAN0_STB, GPIO_FUN0);
GPIO_SetDir(CAN0_STB, GPIO_OUT);
CAN0_TRASCVER_NML;
GPIO_SetPinLevel(CAN0_STB, GPIO_LEVEL_LOW);
canBandrateConfig.PRESC = 5;
canBandrateConfig.SEG_1 = 11;
canBandrateConfig.SEG_2 = 2;
canBandrateConfig.SJW = 2;
canConfig.interruptEn = ENABLE;
canConfig.interruptMask = CAN_IRQ_ALL_ENABLE_MSK;
canConfig.canMode = CAN_MODE_NORMAL;
canConfig.clockSrc = CAN_CLKSRC_AHB;
canConfig.tpss = ENABLE;
canConfig.tsss = ENABLE;
canConfig.filterNum = 3;
canConfig.filterList = canFilterControl; //过滤器设置
canConfig.callback = CAN0_IRQnCallBack;
canConfig.normalBitrate = &canBandrateConfig;
g_sendCANMsgInfo.DATA = (uint8_t *)sendDataBuff;
g_recvCANMsgInfo.DATA = (uint8_t *)recvDataBuff;
CAN_Init(CAN0, &canConfig);
}
//CAN 报文发送函数
uint8_t SocCANSendMsg(uint32_t ID,uint8_t IDE,uint8_t RTR,uint8_t *pDataBuf,uint8_t dataLen)
{
uint8_t ret = 0;
g_sendCANMsgInfo.ID = ID;
g_sendCANMsgInfo.IDE = IDE;
g_sendCANMsgInfo.DLC = dataLen;
memcpy(g_sendCANMsgInfo.DATA, pDataBuf, dataLen);
ret = CAN_TransmitMessage(CAN0, &g_sendCANMsgInfo, CAN_TRANSMIT_SECONDARY);//·¢?íêy?Y
return ret;
}
|