新手上路
- 积分
- 23
- 金钱
- 23
- 注册时间
- 2017-7-6
- 在线时间
- 7 小时
|
1金钱
在做CAN纵行线的项目,汽车OBD的数据读取,出现一个很奇怪的问题,CAN总线连接(此时发送连接指令可以接收到回应数据,能进入can接收中断函数)成功后,开始读取数据,数据读取两次后就再也没法读取成功,CAN接收中断也没法进入CAN初始化代码如下:
[mw_shl_code=c,true]void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void CAN_GPIOConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APBxPeriph_CAN_IO | RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
GPIO_InitStructure.GPIO_Pin = CAN_RXD;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CAN_IO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CAN_TXD;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CAN_IO, &GPIO_InitStructure);
if (CAN_PinRemap == ENABLE)
{
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);
}
}
void CAN_Config(u8 velocity)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_GPIOConfig();
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
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_11tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = velocity;
CAN_Init(CAN1, &CAN_InitStructure);
}
void CAN1_Config32BitFilter(u32 id1, u32 id2, u32 mid1, u32 mid2)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000; //id1>>13;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000; //(id1<<3)|4;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000; //mid1>>13;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000; //(mid1<<3)|4;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
// CAN_FilterInitStructure.CAN_FilterNumber = 1;
// CAN_FilterInitStructure.CAN_FilterIdHigh = id2>>13;
// CAN_FilterInitStructure.CAN_FilterIdLow = (id2<<3)|4;
// CAN_FilterInitStructure.CAN_FilterMaskIdHigh = mid2>>13;
// CAN_FilterInitStructure.CAN_FilterMaskIdLow = (mid2<<3)|4;
// CAN_FilterInit(&CAN_FilterInitStructure);
NVIC_Config();
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
void CAN1_Config16BitFilter(u16 id1, u16 id2, u16 mid1, u16 mid2)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CAN_FilterInitStructure.CAN_FilterNumber=1;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_16bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=id1<<5;
CAN_FilterInitStructure.CAN_FilterIdLow=id2<<5;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=mid1<<5;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=mid2<<5;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
NVIC_Config();
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
uint8_t RX_BUF[100];
u8* Send_CANFrame(u8 cmd)
{
BaseType_t rxerr;
// u8 TransmitMailbox;
CAN_TX_Msg.Data[0] = ISO15765_4_Cmd[cmd][0];
CAN_TX_Msg.Data[1] = ISO15765_4_Cmd[cmd][1];
CAN_TX_Msg.Data[2] = ISO15765_4_Cmd[cmd][2];
printf ("TX_StdId %d\r\n",CAN_TX_Msg.StdId);
printf ("TX_ExtId %d\r\n",CAN_TX_Msg.ExtId);
printf ("TX_StdId %d\r\n",CAN_TX_Msg.Data[0]);
printf ("TX_StdId %d\r\n",CAN_TX_Msg.Data[1]);
printf ("TX_StdId %d\r\n",CAN_TX_Msg.Data[2]);
CAN_Transmit(CAN1, &CAN_TX_Msg);
// while(CAN_TransmitStatus(CAN1,TransmitMailbox) != CANTXOK);
rxerr=CAN_RX_DATA.QueueReceiveDel(RX_BUF,1500);
if(rxerr==pdFAIL){
return NULL;
}
return RX_BUF;
}[/mw_shl_code]
使用部分代码如下:
[mw_shl_code=c,true]uint8_t ISO15765_4_link(u8 CANStype,u8 velocity)
{
uint8_t * p;
CAN_TX_Msg.IDE = CANStype;
ISO15765_4_Config(CAN_TX_Msg.IDE,velocity);
printf("LINKING\r\n");
p = Send_CANFrame(ENTERCMD);
if (p == NULL){
printf("[ OBD ] Link Fail\r\n");
return FAL;
}
printf("[ OBD ] Link OK\r\n");
printf("\r\n");
return OK;
}[/mw_shl_code]
[mw_shl_code=c,true]void canbus_func(void *pvParameters)
{
uint8_t * p;
while(1)
{
p = Send_CANFrame(ECT);
if(p == NULL){
printf ("[ OBD ] can not get speed data\r\n");
}
else{
OBDprint(p,8);
printf ("\r\n");
}
vTaskDelay(1000);
}
}[/mw_shl_code]
CAN中断函数:(代码逻辑是。每次发送完成后,等待消息队列,等待时间为1.5S,CAN中断接收到数据后,发送到消息队列)
[mw_shl_code=c,true]void USB_LP_CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
SaveData(&RxMessage);
}
[/mw_shl_code]
[mw_shl_code=c,true]void SaveData(CanRxMsg* RxMessage)
{
u8 i,j,TransmitMailbox;
CanTxMsg Cmd30H = {0x7DF,0x18DB33F1,CAN_ID_STD,CAN_RTR_DATA,8,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
if (RxMessage->Data[0] == 0x10)
{
Cmd30H.IDE = RxMessage->IDE;
TransmitMailbox = CAN_Transmit(CAN1, &Cmd30H);
while(CAN_TransmitStatus(CAN1,TransmitMailbox) != CANTXOK);
if ((RxMessage->Data[1]+1)%7 > 0)
{
FLCAN = (RxMessage->Data[1]+1)/7 + 1;
}
else
{
FLCAN = (RxMessage->Data[1]+1)/7;
}
FI = 0;
}
for(i = 0; i < 8; i++)
{
RxRAM[i+FI*8] = RxMessage->Data;
}
if (FLCAN != 0)
{
FI++;
if (FLCAN <= FI)
{
FI = 0;
for (j = 0; j < FLCAN; j++)
{
for (i = 0; i < 7; i++)
{
RxRAM[i+7*j] = RxRAM[i+1+7*j+j];
}
}
// RxFlay = SUCCESS;
FLCAN = 0;
Can1_RX0_Callback(RxRAM);
}
}
else
{
// RxFlay = SUCCESS;
Can1_RX0_Callback(RxRAM);
}
}
[/mw_shl_code]
苦恼的不知道为何连接读取几次后就没反应了:
|
|