AppTaskCan.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * @Author: chenjie
  3. * @Date: 2022-06-06
  4. * @LastEditTime: 2022-10-27
  5. * @LastEditors: chenjie
  6. * @Description:
  7. * @FilePath: \S32K146_4G\code\app\AppTaskCan.c
  8. * Copyright (c) 2022 by chenjie, All Rights Reserved.
  9. */
  10. #include "AppTaskCan.h"
  11. static uint8 BattSendFlg = 0;
  12. static void UDSAnsFunc(uint8 *rawData);
  13. static void UdsAns(bool PosFlg,uint8 UdsService,uint8 UdsSubService,uint8 Idx,uint8 *AnsData,uint8 AnsDataLen);
  14. static void BattSendFunc(void);
  15. static void vCanRxCallback(TimerHandle_t pxTimer)
  16. {
  17. uint32 ulTimerID;
  18. ulTimerID = (uint32)pvTimerGetTimerID(pxTimer);
  19. if(ulTimerID==0)
  20. {
  21. Can_MainFunction_Read();
  22. }
  23. }
  24. void CanTask(void *pvParameters)
  25. {
  26. uint32 timerCounterNow = 0;
  27. (void)pvParameters;
  28. CanIf_bRxFlag = false;
  29. Can_Msg_Type_Data CanRxMsg;
  30. CanRecvQueueHandle = xQueueCreate(100, sizeof(Can_Msg_Type_Data));
  31. TimerHandle_t monitorTimer1ms;
  32. monitorTimer1ms = xTimerCreate("monitor1ms",1,pdTRUE,(void *)0,vCanRxCallback);
  33. xTimerStart(monitorTimer1ms,0);
  34. BaseType_t ret = pdFALSE;
  35. GsensorInit();
  36. while(1)
  37. {
  38. do
  39. {
  40. memset(&CanRxMsg, 0, sizeof(CanRxMsg));
  41. ret = xQueueReceive(CanRecvQueueHandle, &CanRxMsg, 1);
  42. if (ret == pdPASS)
  43. {
  44. if(IsJumptoBootloader(CanRxMsg.id, CanRxMsg.data))
  45. {
  46. DoRoutinePositiveAnswer();
  47. RequestEnterBootloader();
  48. SystemSoftwareReset();
  49. break;
  50. }
  51. else
  52. {
  53. BcuDecodeFunction(CanRxMsg.id,CanRxMsg.data);//can协议解析
  54. }
  55. }
  56. }while(ret == pdPASS);
  57. SL_SC7A20_Read_XYZ_Data(xyzData);
  58. if(BattSendFlg)
  59. {
  60. BattSendFunc();
  61. }
  62. if((TimerCounter - timerCounterNow)>=100)
  63. {
  64. timerCounterNow = TimerCounter;
  65. //温度采集
  66. ADC_ReadValue();
  67. }
  68. }
  69. }
  70. //注意解码超限问题
  71. void BcuDecodeFunction(uint32 ID, uint8 *rawData)
  72. {
  73. switch(ID)
  74. {
  75. case 0x1C00EDEA:
  76. {
  77. UDSAnsFunc(rawData);
  78. break;
  79. }
  80. }
  81. }
  82. static void UDSAnsFunc(uint8 *rawData)
  83. {
  84. static uint8 UdsStates = 1;
  85. uint8 Data[8] = {0};
  86. memcpy(Data,rawData,8);
  87. uint8 UdsService = 0;
  88. uint8 UdsSubService = 0;
  89. UdsService = Data[0];
  90. UdsSubService = Data[1];
  91. uint8 AnsData[24]={0};
  92. static uint8 deviceSnRecvFlg = 0;
  93. switch(UdsService)
  94. {
  95. case 0x10://模式切换
  96. {
  97. if(UdsSubService==0x01)
  98. {
  99. UdsStates = 0x01;//关闭UDS诊断
  100. UdsAns(true,UdsService,UdsSubService,0x00,NULL,0);
  101. }
  102. else if(UdsSubService==0x02)
  103. {
  104. if(UdsStates==0x01||UdsStates==0x02)
  105. {
  106. UdsStates = 0x02;//打开UDS诊断
  107. UdsAns(true,UdsService,UdsSubService,0x00,NULL,0);
  108. }
  109. else
  110. {
  111. UdsAns(false,UdsService,UdsSubService,0x00,NULL,0);
  112. }
  113. }
  114. break;
  115. }
  116. case 0x11://控制
  117. {
  118. if(UdsStates==0x02)
  119. {
  120. switch(UdsSubService)
  121. {
  122. case 0x01://重启
  123. {
  124. AnsData[0] = AppSwVersion>>24;
  125. AnsData[1] = AppSwVersion>>16;
  126. AnsData[2] = AppSwVersion>>8;
  127. AnsData[3] = AppSwVersion>>0;
  128. UdsAns(true,UdsService,UdsSubService,0x00,AnsData,4);
  129. AppConfigInfo.appSaveFlg = true;
  130. vTaskDelay(pdMS_TO_TICKS(1000));
  131. SystemSoftwareReset();
  132. break;
  133. }
  134. default:
  135. {
  136. UdsAns(false,UdsService,UdsSubService,0x00,NULL,0);
  137. break;
  138. }
  139. }
  140. }
  141. else
  142. {
  143. UdsAns(false,UdsService,UdsSubService,0x00,NULL,0);
  144. }
  145. break;
  146. }
  147. case 0x22://查询
  148. {
  149. if(UdsStates==0x02)
  150. {
  151. switch(UdsSubService)
  152. {
  153. case 0x01://版本号查询
  154. {
  155. AnsData[0] = AppSwVersion>>24;
  156. AnsData[1] = AppSwVersion>>16;
  157. AnsData[2] = AppSwVersion>>8;
  158. AnsData[3] = AppSwVersion>>0;
  159. UdsAns(true,UdsService,UdsSubService,0x00,AnsData,4);
  160. break;
  161. }
  162. case 0x02://SN号码查询
  163. {
  164. memcpy(AnsData,AppConfigInfo.deviceSn,17);
  165. for(uint8 i=0;i<5;i++)
  166. {
  167. UdsAns(true,UdsService,UdsSubService,i,&AnsData[i*4],4);
  168. }
  169. break;
  170. }
  171. case 0x03://ICCID查询
  172. {
  173. memcpy(AnsData,IccidNum,20);
  174. for(uint8 i=0;i<5;i++)
  175. {
  176. UdsAns(true,UdsService,UdsSubService,i,&AnsData[i*4],4);
  177. }
  178. break;
  179. }
  180. case 0x04://IMEI查询
  181. {
  182. memcpy(AnsData,ImeiNum,15);
  183. for(uint8 i=0;i<4;i++)
  184. {
  185. UdsAns(true,UdsService,UdsSubService,i,&AnsData[i*4],4);
  186. }
  187. break;
  188. }
  189. case 0x05://驻网状态查询
  190. {
  191. AnsData[0]= RegChkRet;
  192. UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1);
  193. break;
  194. }
  195. case 0x06://联网状态查询
  196. {
  197. AnsData[0]= SocketId;
  198. UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1);
  199. break;
  200. }
  201. case 0x07://定位信息查询
  202. {
  203. GPSInfo GpsRecvData;
  204. if (xQueuePeek(GpsDataQueueHandle, &GpsRecvData, 0) == pdPASS)
  205. {
  206. AnsData[0] = GpsRecvData.locateMark;
  207. AnsData[4] = GpsRecvData.longitude[0];
  208. AnsData[5] = GpsRecvData.longitude[1];
  209. AnsData[6] = GpsRecvData.longitude[2];
  210. AnsData[7] = GpsRecvData.longitude[3];
  211. AnsData[8] = GpsRecvData.latitude[0];
  212. AnsData[9] = GpsRecvData.latitude[1];
  213. AnsData[10] = GpsRecvData.latitude[2];
  214. AnsData[11] = GpsRecvData.latitude[3];
  215. AnsData[12] = GpsRecvData.altitude[0];
  216. AnsData[13] = GpsRecvData.altitude[1];
  217. AnsData[14] = GpsRecvData.satelliteNum;
  218. for(uint8 i=0;i<4;i++)
  219. {
  220. UdsAns(true,UdsService,UdsSubService,i,&AnsData[i*4],4);
  221. }
  222. break;
  223. }
  224. else
  225. {
  226. AnsData[0]=0xFF;
  227. UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1);
  228. }
  229. break;
  230. }
  231. case 0x08://温度检测查询
  232. {
  233. UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1);
  234. break;
  235. }
  236. case 0x09://电池信息查询
  237. {
  238. UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1);
  239. BattSendFlg = 1;
  240. break;
  241. }
  242. case 0x0A://三轴数据查询
  243. {
  244. AnsData[0] = xyzData[0]>>8;
  245. AnsData[1] = xyzData[0]>>0;
  246. AnsData[2] = xyzData[1]>>8;
  247. AnsData[3] = xyzData[1]>>0;
  248. AnsData[4] = xyzData[2]>>8;
  249. AnsData[5] = xyzData[2]>>0;
  250. for(uint8 i=0;i<2;i++)
  251. {
  252. UdsAns(true,UdsService,UdsSubService,i,&AnsData[i*4],4);
  253. }
  254. break;
  255. }
  256. }//switch
  257. }//if
  258. else
  259. {
  260. UdsAns(false,UdsService,UdsSubService,0x00,NULL,0);
  261. }
  262. break;
  263. }
  264. case 0x2E://写入
  265. {
  266. if(UdsStates==0x02)
  267. {
  268. switch(UdsSubService)
  269. {
  270. case 0x01:
  271. {
  272. setbit(deviceSnRecvFlg,Data[3]);
  273. if(Data[3]<0x04)
  274. {
  275. memcpy(&AppConfigInfo.deviceSn[Data[3]*4],&Data[4],4);
  276. UdsAns(true,UdsService,UdsSubService,0x00,&Data[3],1);
  277. }
  278. else if(Data[3]==0x04)
  279. {
  280. memcpy(&AppConfigInfo.deviceSn[Data[3]*4],&Data[4],1);
  281. UdsAns(true,UdsService,UdsSubService,0x00,&Data[3],1);
  282. }
  283. if(deviceSnRecvFlg==0x1F)
  284. {
  285. deviceSnRecvFlg = 0;
  286. AppConfigInfo.appSaveFlg = true;
  287. AppConfigInfo.eolFlg=true;
  288. }
  289. break;
  290. }
  291. }
  292. }
  293. else
  294. {
  295. UdsAns(false,UdsService,UdsSubService,0x00,NULL,0);
  296. }
  297. break;
  298. }
  299. case 0x31://控制
  300. {
  301. if(UdsStates==0x02)
  302. {
  303. }
  304. else
  305. {
  306. UdsAns(false,UdsService,UdsSubService,0x00,NULL,0);
  307. }
  308. break;
  309. }
  310. default:
  311. break;
  312. }
  313. return;
  314. }
  315. static void UdsAns(bool PosFlg,uint8 UdsService,uint8 UdsSubService,uint8 Idx,uint8 *AnsData,uint8 AnsDataLen)
  316. {
  317. Can_Msg_Type CanTxMsg;
  318. uint32 CanAnsId = 0x1C00EAED;
  319. uint8 SendData[8] = {0};
  320. if(PosFlg)
  321. {
  322. SendData[0] = 0x78;
  323. SendData[1] = UdsService;
  324. SendData[2] = UdsSubService;
  325. SendData[3] = Idx;
  326. if(AnsData!=NULL&&AnsDataLen!=0x00)
  327. {
  328. memcpy(&SendData[4],AnsData,AnsDataLen);
  329. }
  330. }
  331. else
  332. {
  333. SendData[0] = 0x7F;
  334. SendData[1] = UdsService;
  335. SendData[2] = UdsSubService;
  336. SendData[3] = 0x00;
  337. }
  338. CanTxMsg.id = CanAnsId;
  339. CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
  340. CanTxMsg.sdu = SendData;
  341. CanTxMsg.length = 8;
  342. CanIf_SendMessage(CAN1, CanTxMsg);
  343. }
  344. static void BattSendFunc(void)
  345. {
  346. Can_Msg_Type CanTxMsg;
  347. uint32 CanAnsId = 0;
  348. uint8 SendData[8] = {0};
  349. static uint8 SendStep = 0;
  350. static uint8 SendCounter = 0;
  351. switch(SendStep)
  352. {
  353. case 0x00:
  354. {
  355. SendStep++;
  356. break;
  357. }
  358. case 0x01:
  359. {
  360. CanAnsId = 0x1C01EAED;
  361. SendData[0] = battHeatState;
  362. SendData[1] = battFanState;
  363. SendData[2] = battRelayState;
  364. SendData[3] = battConverState;
  365. SendStep++;
  366. break;
  367. }
  368. case 0x02:
  369. {
  370. uint16 ErrorTemp ;
  371. ErrorTemp = GetErrorNum(ErrorArray,sizeof(ErrorArray)/2);
  372. CanAnsId = 0x1C09EAED;
  373. SendData[0] = battWorkState;
  374. SendData[1] = ErrorTemp>>8;
  375. SendData[2] = ErrorTemp;
  376. SendData[3] = 0;
  377. SendData[4] = 0;
  378. SendData[5] = 0;
  379. SendData[6] = 0;
  380. SendData[7] = 0;
  381. SendStep++;
  382. break;
  383. }
  384. case 0x03:
  385. {
  386. CanAnsId = 0x1C0AEAED;
  387. uint16 SocTemp = battSOC*10;
  388. SendData[0] = battI>>8;
  389. SendData[1] = battI;
  390. SendData[2] = battPackVol>>8;
  391. SendData[3] = battPackVol;
  392. SendData[4] = SocTemp>>8;
  393. SendData[5] = SocTemp;
  394. SendData[6] = battSOH;
  395. SendData[7] = battMOSSwitchState;
  396. SendStep++;
  397. break;
  398. }
  399. case 0x04:
  400. {
  401. CanAnsId = 0x1C10EAED + (SendCounter<<16);
  402. if((SendCounter*8 + 7)>BMS_TEMPNUM)
  403. {
  404. SendCounter = 0;
  405. SendStep++;
  406. break;
  407. }
  408. else
  409. {
  410. SendData[0] = battCellTemp[SendCounter*8 + 0];
  411. SendData[1] = battCellTemp[SendCounter*8 + 1];
  412. SendData[2] = battCellTemp[SendCounter*8 + 2];
  413. SendData[3] = battCellTemp[SendCounter*8 + 3];
  414. SendData[4] = battCellTemp[SendCounter*8 + 4];
  415. SendData[5] = battCellTemp[SendCounter*8 + 5];
  416. SendData[6] = battCellTemp[SendCounter*8 + 6];
  417. SendData[7] = battCellTemp[SendCounter*8 + 7];
  418. }
  419. break;
  420. }
  421. case 0x05:
  422. {
  423. CanAnsId = 0x1CA0EAED + (SendCounter<<16);
  424. if((SendCounter*4 + 3)>BMS_CELLNUM)
  425. {
  426. SendCounter = 0;
  427. SendStep++;
  428. break;
  429. }
  430. else
  431. {
  432. SendData[0] = battCellU[SendCounter*4 + 0]>>8;
  433. SendData[1] = battCellU[SendCounter*4 + 0];
  434. SendData[2] = battCellU[SendCounter*4 + 1]>>8;
  435. SendData[3] = battCellU[SendCounter*4 + 1];
  436. SendData[4] = battCellU[SendCounter*4 + 2]>>8;
  437. SendData[5] = battCellU[SendCounter*4 + 2];
  438. SendData[6] = battCellU[SendCounter*4 + 3]>>8;
  439. SendData[7] = battCellU[SendCounter*4 + 3];
  440. }
  441. break;
  442. }
  443. default:
  444. {
  445. SendStep = 0;
  446. BattSendFlg = 0;
  447. break;
  448. }
  449. }
  450. CanTxMsg.id = CanAnsId;
  451. CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
  452. CanTxMsg.sdu = SendData;
  453. CanTxMsg.length = 8;
  454. CanIf_SendMessage(CAN1, CanTxMsg);
  455. }