123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- /*
- * @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);
- }
|