/* * @Author: chenjie * @Date: 2022-06-06 * @LastEditTime: 2022-10-27 * @LastEditors: chenjie * @Description: * @FilePath: \S32K146_4G\code\app\AppTaskCan.c * Copyright (c) 2022 by chenjie, All Rights Reserved. */ #include "AppTaskCan.h" static uint8 BattSendFlg = 0; static void UDSAnsFunc(uint8 *rawData); static void UdsAns(bool PosFlg,uint8 UdsService,uint8 UdsSubService,uint8 Idx,uint8 *AnsData,uint8 AnsDataLen); static void BattSendFunc(void); static void vCanRxCallback(TimerHandle_t pxTimer) { uint32 ulTimerID; ulTimerID = (uint32)pvTimerGetTimerID(pxTimer); if(ulTimerID==0) { Can_MainFunction_Read(); } } void CanTask(void *pvParameters) { uint32 timerCounterNow = 0; (void)pvParameters; CanIf_bRxFlag = false; Can_Msg_Type_Data CanRxMsg; CanRecvQueueHandle = xQueueCreate(100, sizeof(Can_Msg_Type_Data)); TimerHandle_t monitorTimer1ms; monitorTimer1ms = xTimerCreate("monitor1ms",1,pdTRUE,(void *)0,vCanRxCallback); xTimerStart(monitorTimer1ms,0); BaseType_t ret = pdFALSE; GsensorInit(); while(1) { do { memset(&CanRxMsg, 0, sizeof(CanRxMsg)); ret = xQueueReceive(CanRecvQueueHandle, &CanRxMsg, 1); if (ret == pdPASS) { if(IsJumptoBootloader(CanRxMsg.id, CanRxMsg.data)) { DoRoutinePositiveAnswer(); RequestEnterBootloader(); SystemSoftwareReset(); break; } else { BcuDecodeFunction(CanRxMsg.id,CanRxMsg.data);//can协议解析 } } }while(ret == pdPASS); SL_SC7A20_Read_XYZ_Data(xyzData); if(BattSendFlg) { BattSendFunc(); } if((TimerCounter - timerCounterNow)>=100) { timerCounterNow = TimerCounter; //温度采集 ADC_ReadValue(); } } } //注意解码超限问题 void BcuDecodeFunction(uint32 ID, uint8 *rawData) { switch(ID) { case 0x1C00EDEA: { UDSAnsFunc(rawData); break; } } } static void UDSAnsFunc(uint8 *rawData) { static uint8 UdsStates = 1; uint8 Data[8] = {0}; memcpy(Data,rawData,8); uint8 UdsService = 0; uint8 UdsSubService = 0; UdsService = Data[0]; UdsSubService = Data[1]; uint8 AnsData[24]={0}; static uint8 deviceSnRecvFlg = 0; switch(UdsService) { case 0x10://模式切换 { if(UdsSubService==0x01) { UdsStates = 0x01;//关闭UDS诊断 UdsAns(true,UdsService,UdsSubService,0x00,NULL,0); } else if(UdsSubService==0x02) { if(UdsStates==0x01||UdsStates==0x02) { UdsStates = 0x02;//打开UDS诊断 UdsAns(true,UdsService,UdsSubService,0x00,NULL,0); } else { UdsAns(false,UdsService,UdsSubService,0x00,NULL,0); } } break; } case 0x11://控制 { if(UdsStates==0x02) { switch(UdsSubService) { case 0x01://重启 { AnsData[0] = AppSwVersion>>24; AnsData[1] = AppSwVersion>>16; AnsData[2] = AppSwVersion>>8; AnsData[3] = AppSwVersion>>0; UdsAns(true,UdsService,UdsSubService,0x00,AnsData,4); AppConfigInfo.appSaveFlg = true; vTaskDelay(pdMS_TO_TICKS(1000)); SystemSoftwareReset(); break; } default: { UdsAns(false,UdsService,UdsSubService,0x00,NULL,0); break; } } } else { UdsAns(false,UdsService,UdsSubService,0x00,NULL,0); } break; } case 0x22://查询 { if(UdsStates==0x02) { switch(UdsSubService) { case 0x01://版本号查询 { AnsData[0] = AppSwVersion>>24; AnsData[1] = AppSwVersion>>16; AnsData[2] = AppSwVersion>>8; AnsData[3] = AppSwVersion>>0; UdsAns(true,UdsService,UdsSubService,0x00,AnsData,4); break; } case 0x02://SN号码查询 { memcpy(AnsData,AppConfigInfo.deviceSn,17); for(uint8 i=0;i<5;i++) { UdsAns(true,UdsService,UdsSubService,i,&AnsData[i*4],4); } break; } case 0x03://ICCID查询 { memcpy(AnsData,IccidNum,20); for(uint8 i=0;i<5;i++) { UdsAns(true,UdsService,UdsSubService,i,&AnsData[i*4],4); } break; } case 0x04://IMEI查询 { memcpy(AnsData,ImeiNum,15); for(uint8 i=0;i<4;i++) { UdsAns(true,UdsService,UdsSubService,i,&AnsData[i*4],4); } break; } case 0x05://驻网状态查询 { AnsData[0]= RegChkRet; UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1); break; } case 0x06://联网状态查询 { AnsData[0]= SocketId; UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1); break; } case 0x07://定位信息查询 { GPSInfo GpsRecvData; if (xQueuePeek(GpsDataQueueHandle, &GpsRecvData, 0) == pdPASS) { AnsData[0] = GpsRecvData.locateMark; AnsData[4] = GpsRecvData.longitude[0]; AnsData[5] = GpsRecvData.longitude[1]; AnsData[6] = GpsRecvData.longitude[2]; AnsData[7] = GpsRecvData.longitude[3]; AnsData[8] = GpsRecvData.latitude[0]; AnsData[9] = GpsRecvData.latitude[1]; AnsData[10] = GpsRecvData.latitude[2]; AnsData[11] = GpsRecvData.latitude[3]; AnsData[12] = GpsRecvData.altitude[0]; AnsData[13] = GpsRecvData.altitude[1]; AnsData[14] = GpsRecvData.satelliteNum; for(uint8 i=0;i<4;i++) { UdsAns(true,UdsService,UdsSubService,i,&AnsData[i*4],4); } break; } else { AnsData[0]=0xFF; UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1); } break; } case 0x08://温度检测查询 { UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1); break; } case 0x09://电池信息查询 { UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1); BattSendFlg = 1; break; } case 0x0A://三轴数据查询 { AnsData[0] = xyzData[0]>>8; AnsData[1] = xyzData[0]>>0; AnsData[2] = xyzData[1]>>8; AnsData[3] = xyzData[1]>>0; AnsData[4] = xyzData[2]>>8; AnsData[5] = xyzData[2]>>0; for(uint8 i=0;i<2;i++) { UdsAns(true,UdsService,UdsSubService,i,&AnsData[i*4],4); } break; } }//switch }//if else { UdsAns(false,UdsService,UdsSubService,0x00,NULL,0); } break; } case 0x2E://写入 { if(UdsStates==0x02) { switch(UdsSubService) { case 0x01: { setbit(deviceSnRecvFlg,Data[3]); if(Data[3]<0x04) { memcpy(&AppConfigInfo.deviceSn[Data[3]*4],&Data[4],4); UdsAns(true,UdsService,UdsSubService,0x00,&Data[3],1); } else if(Data[3]==0x04) { memcpy(&AppConfigInfo.deviceSn[Data[3]*4],&Data[4],1); UdsAns(true,UdsService,UdsSubService,0x00,&Data[3],1); } if(deviceSnRecvFlg==0x1F) { deviceSnRecvFlg = 0; AppConfigInfo.appSaveFlg = true; AppConfigInfo.eolFlg=true; } break; } } } else { UdsAns(false,UdsService,UdsSubService,0x00,NULL,0); } break; } case 0x31://控制 { if(UdsStates==0x02) { } else { UdsAns(false,UdsService,UdsSubService,0x00,NULL,0); } break; } default: break; } return; } static void UdsAns(bool PosFlg,uint8 UdsService,uint8 UdsSubService,uint8 Idx,uint8 *AnsData,uint8 AnsDataLen) { Can_Msg_Type CanTxMsg; uint32 CanAnsId = 0x1C00EAED; uint8 SendData[8] = {0}; if(PosFlg) { SendData[0] = 0x78; SendData[1] = UdsService; SendData[2] = UdsSubService; SendData[3] = Idx; if(AnsData!=NULL&&AnsDataLen!=0x00) { memcpy(&SendData[4],AnsData,AnsDataLen); } } else { SendData[0] = 0x7F; SendData[1] = UdsService; SendData[2] = UdsSubService; SendData[3] = 0x00; } CanTxMsg.id = CanAnsId; CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE; CanTxMsg.sdu = SendData; CanTxMsg.length = 8; CanIf_SendMessage(CAN1, CanTxMsg); } static void BattSendFunc(void) { Can_Msg_Type CanTxMsg; uint32 CanAnsId = 0; uint8 SendData[8] = {0}; static uint8 SendStep = 0; static uint8 SendCounter = 0; switch(SendStep) { case 0x00: { SendStep++; break; } case 0x01: { CanAnsId = 0x1C01EAED; SendData[0] = battHeatState; SendData[1] = battFanState; SendData[2] = battRelayState; SendData[3] = battConverState; SendStep++; break; } case 0x02: { uint16 ErrorTemp ; ErrorTemp = GetErrorNum(ErrorArray,sizeof(ErrorArray)/2); CanAnsId = 0x1C09EAED; SendData[0] = battWorkState; SendData[1] = ErrorTemp>>8; SendData[2] = ErrorTemp; SendData[3] = 0; SendData[4] = 0; SendData[5] = 0; SendData[6] = 0; SendData[7] = 0; SendStep++; break; } case 0x03: { CanAnsId = 0x1C0AEAED; uint16 SocTemp = battSOC*10; SendData[0] = battI>>8; SendData[1] = battI; SendData[2] = battPackVol>>8; SendData[3] = battPackVol; SendData[4] = SocTemp>>8; SendData[5] = SocTemp; SendData[6] = battSOH; SendData[7] = battMOSSwitchState; SendStep++; break; } case 0x04: { CanAnsId = 0x1C10EAED + (SendCounter<<16); if((SendCounter*8 + 7)>BMS_TEMPNUM) { SendCounter = 0; SendStep++; break; } else { SendData[0] = battCellTemp[SendCounter*8 + 0]; SendData[1] = battCellTemp[SendCounter*8 + 1]; SendData[2] = battCellTemp[SendCounter*8 + 2]; SendData[3] = battCellTemp[SendCounter*8 + 3]; SendData[4] = battCellTemp[SendCounter*8 + 4]; SendData[5] = battCellTemp[SendCounter*8 + 5]; SendData[6] = battCellTemp[SendCounter*8 + 6]; SendData[7] = battCellTemp[SendCounter*8 + 7]; } break; } case 0x05: { CanAnsId = 0x1CA0EAED + (SendCounter<<16); if((SendCounter*4 + 3)>BMS_CELLNUM) { SendCounter = 0; SendStep++; break; } else { SendData[0] = battCellU[SendCounter*4 + 0]>>8; SendData[1] = battCellU[SendCounter*4 + 0]; SendData[2] = battCellU[SendCounter*4 + 1]>>8; SendData[3] = battCellU[SendCounter*4 + 1]; SendData[4] = battCellU[SendCounter*4 + 2]>>8; SendData[5] = battCellU[SendCounter*4 + 2]; SendData[6] = battCellU[SendCounter*4 + 3]>>8; SendData[7] = battCellU[SendCounter*4 + 3]; } break; } default: { SendStep = 0; BattSendFlg = 0; break; } } CanTxMsg.id = CanAnsId; CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE; CanTxMsg.sdu = SendData; CanTxMsg.length = 8; CanIf_SendMessage(CAN1, CanTxMsg); }