app.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /****************************************************************************
  2. *
  3. * Copy right: 2020-, Copyrigths of QIXIANG TECH Ltd.
  4. * File name: app.c
  5. * Description: QX app source file
  6. * History: Rev1.0 2020-10-16
  7. * Athuor: chenjie
  8. *
  9. ****************************************************************************/
  10. //include
  11. #include "bsp.h"
  12. #include "bsp_custom.h"
  13. #include "osasys.h"
  14. #include "ostask.h"
  15. #include "queue.h"
  16. #include "ps_event_callback.h"
  17. #include "app.h"
  18. #include "cmisim.h"
  19. #include "cmimm.h"
  20. #include "cmips.h"
  21. #include "sockets.h"
  22. #include "psifevent.h"
  23. #include "ps_lib_api.h"
  24. #include "lwip/netdb.h"
  25. #include <cis_def.h>
  26. #include "debug_log.h"
  27. #include "slpman_ec616.h"
  28. #include "plat_config.h"
  29. //define
  30. // app task static stack and control block
  31. #define PROC_TASK_STACK_SIZE (1024)
  32. //uart def
  33. #define Uart_Send_LEN (8)
  34. #define Uart_Rece_LEN (40)//串口读取的最大数量,40个字节,能满足一次性读取17个单体
  35. #define RTE_UART_RX_IO_MODE RTE_UART1_RX_IO_MODE
  36. //statement variable
  37. extern ARM_DRIVER_USART Driver_USART1;
  38. static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
  39. /** \brief receive timeout flag */
  40. volatile bool isRecvTimeout = false;
  41. /** \brief receive complete flag */
  42. volatile bool isRecvComplete = false;
  43. uint8_t process0SlpHandler = 0xff;
  44. uint8_t process1SlpHandler = 0xff;
  45. uint8_t process2SlpHandler = 0xff;
  46. uint8_t process3SlpHandler = 0xff;
  47. uint8_t Can_Rece_buffer[8];
  48. uint8_t Batt_Cell_Num = 14;//默认数值14、17
  49. uint8_t Batt_Cell_Num_2 ;//默认数值
  50. uint8_t Batt_Temp_Num = 5;//默认数值5、7
  51. int16_t Uart_Rece_BattI=0x0000;
  52. uint8_t battbuffer[73];//电池数据都存在此数组中————电压14,温度5
  53. /**
  54. * 存放规则如下:
  55. * 位置: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  56. * 数据: 年 月 日 时 分 秒 信息体标志 年 月 日 时 分 秒 网络信号 故障等级 故障代码高 故障代码低
  57. *
  58. * 17 18 19 20 21 22 23 24 25 26 27 28 29 30 30+1 .... 30+X*2 31+X*2 31+1...31+X*2+N
  59. * 电流H 电流L Link电压H Link电压L Pack电压H Pack电压L 开关状态 SOC SOH 均衡状态 单体个数X 单体v1...单体vX 温度个数N 温度1..温度N
  60. * 32+X*2+N 33+X*2+N
  61. * 电池状态 是否加热
  62. * */
  63. uint16_t data_index = 0x0000;
  64. typedef enum
  65. {
  66. PROCESS_STATE_IDLE = 0,
  67. PROCESS_STATE_WORK,
  68. PROCESS_STATE_SLEEP
  69. }processSM;
  70. typedef enum
  71. {
  72. PROCESS_Uart_STATE_IDLE = 0,
  73. PROCESS_Uart_STATE_WORK,
  74. PROCESS_Uart_STATE_CHECK,
  75. PROCESS_Uart_STATE_SLEEP
  76. }process_Uart;
  77. typedef enum
  78. {
  79. PROCESS_NB_STATE_IDLE = 0,
  80. PROCESS_NB_STATE_WORK,
  81. PROCESS_NB_STATE_CONNECT,
  82. PROCESS_NB_STATE_SLEEP
  83. }process_NB;
  84. static StaticTask_t gProcessTask0;
  85. static UINT8 gProcessTaskStack0[PROC_TASK_STACK_SIZE];
  86. static StaticTask_t gProcessTask1;
  87. static UINT8 gProcessTaskStack1[PROC_TASK_STACK_SIZE];
  88. static StaticTask_t gProcessTask2;
  89. static UINT8 gProcessTaskStack2[PROC_TASK_STACK_SIZE];
  90. static StaticTask_t gProcessTask3;
  91. static UINT8 gProcessTaskStack3[PROC_TASK_STACK_SIZE];
  92. processSM gProc0State = PROCESS_STATE_IDLE;
  93. process_Uart gProc1State = PROCESS_Uart_STATE_IDLE;
  94. processSM gProc2State = PROCESS_STATE_IDLE;
  95. process_NB gProc3State = PROCESS_NB_STATE_IDLE;
  96. #define PROC_Task_STATE_SWITCH(a) (gProc0State = a) //任务调度切换
  97. #define PROC_Uart_STATE_SWITCH(a) (gProc1State = a) //uart 状态切换
  98. #define PROC_Can_STATE_SWITCH(a) (gProc2State = a) //can 状态切换
  99. #define PROC_NB_STATE_SWITCH(a) (gProc3State = a) //NB状态切换
  100. uint8_t deepslpTimerID = 7;
  101. //-------------------------------------------------------------------------------------------------------------------------------------------------------
  102. unsigned int crc_chk(uint8_t* data, uint8_t length)
  103. {
  104. int j;
  105. uint16_t reg_crc=0xFFFF;
  106. while(length--)
  107. {
  108. reg_crc ^= *data++;
  109. for(j=0;j<8;j++)
  110. {
  111. if(reg_crc & 0x01)
  112. {
  113. reg_crc=(reg_crc>>1) ^ 0xA001;
  114. }
  115. else
  116. {
  117. reg_crc=reg_crc >>1;
  118. }
  119. }
  120. }
  121. return reg_crc;
  122. }
  123. uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t *Uart_Rece_buffer)
  124. {
  125. uint16_t CRC_Rece_buffer;
  126. uint16_t CRC_chk_buffer;
  127. uint8_t Uart_Send_buffer[8];
  128. uint8_t Rece_Data_Len;
  129. Uart_Send_buffer[0] = Uart_Receive_Msg.Bms_Address;
  130. Uart_Send_buffer[1] = Uart_Receive_Msg.Bms_Read_Funcode;
  131. Uart_Send_buffer[2] = Uart_Receive_Msg.Reg_Begin_H;
  132. Uart_Send_buffer[3] = Uart_Receive_Msg.Reg_Begin_L;
  133. Uart_Send_buffer[4] = Uart_Receive_Msg.Reg_Num_H;
  134. Uart_Send_buffer[5] = Uart_Receive_Msg.Reg_Num_L;
  135. CRC_chk_buffer = crc_chk(Uart_Send_buffer,6);
  136. Uart_Send_buffer[6] = CRC_chk_buffer;
  137. Uart_Send_buffer[7] = CRC_chk_buffer>>8;
  138. uint32_t timeout=0;
  139. USARTdrv->Send(Uart_Send_buffer,8);
  140. Rece_Data_Len = Uart_Receive_Msg.Reg_Num_L<<1;//读取几个寄存器的值,数据长度乘以二
  141. USARTdrv->Receive(Uart_Rece_buffer,Rece_Data_Len+5);
  142. while((isRecvTimeout == false) && (isRecvComplete == false))// 未收到数据不叫时间超时,收到数据但是不全叫时间超时
  143. {
  144. timeout++;
  145. if (timeout>7000000)
  146. {
  147. timeout =0;
  148. isRecvTimeout = true;
  149. break;
  150. }
  151. }
  152. if (isRecvComplete == true)
  153. {
  154. Rece_Data_Len = *(Uart_Rece_buffer+2);
  155. isRecvComplete = false;
  156. CRC_Rece_buffer =*(Uart_Rece_buffer+Rece_Data_Len+4)<<8|*(Uart_Rece_buffer+Rece_Data_Len+3);
  157. CRC_chk_buffer = crc_chk(Uart_Rece_buffer,Rece_Data_Len+3);
  158. // #ifdef USING_PRINTF
  159. // // printf("Uart_Send_buffer: ");
  160. // // for(int i=0;i<8;i++)
  161. // // {
  162. // // printf("%x ",Uart_Send_buffer[i]);
  163. // // }
  164. // // printf("\n");
  165. // printf("Uart_Rece_buffer: ");
  166. // for(int i=0;i<Rece_Data_Len+5;i++)
  167. // {
  168. // printf("%x ",*(Uart_Rece_buffer+i));
  169. // }
  170. // printf("crcchk:%x,%x ",CRC_chk_buffer,CRC_Rece_buffer);
  171. // #endif
  172. if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
  173. {
  174. return Uart_Rece_buffer+3;
  175. }
  176. else //接收数据的校验不过暂时屏蔽
  177. {
  178. memset(Uart_Rece_buffer,0xff,Uart_Rece_LEN);
  179. return Uart_Rece_buffer;
  180. }
  181. }
  182. if (isRecvTimeout == true)//没收到数据,全部为空值
  183. {
  184. memset(Uart_Rece_buffer,0x00,Uart_Rece_LEN);
  185. isRecvTimeout = false;
  186. osDelay(1000);
  187. return Uart_Rece_buffer;
  188. }
  189. return Uart_Rece_buffer;
  190. }
  191. void USART_callback(uint32_t event)
  192. {
  193. if(event & ARM_USART_EVENT_RX_TIMEOUT)
  194. {
  195. isRecvTimeout = true;
  196. }
  197. if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
  198. {
  199. isRecvComplete = true;
  200. }
  201. }
  202. static void process0AppTask(void* arg)//任务调度程序
  203. {
  204. UINT16 Can_index = 0;
  205. UINT16 Uart_index = 0;
  206. UINT16 NB_index = 0;
  207. uint32_t sleep_index = 0;
  208. PROC_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
  209. NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
  210. NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
  211. NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
  212. NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
  213. FaultDisplay(LED_TURN_OFF);
  214. NVIC_EnableIRQ(PadWakeup1_IRQn);
  215. slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
  216. slpManApplyPlatVoteHandle("process0slp",&process0SlpHandler);
  217. slpManPlatVoteDisableSleep(process0SlpHandler, SLP_SLP2_STATE);
  218. slpManSlpState_t slpstate = slpManGetLastSlpState();
  219. #ifdef USING_PRINTF
  220. printf("slpstate:%d \n",slpstate);
  221. #endif
  222. if((slpstate == SLP_SLP2_STATE) || (slpstate == SLP_HIB_STATE))
  223. {
  224. PROC_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
  225. }
  226. else
  227. {
  228. PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
  229. }
  230. while(1)
  231. {
  232. switch(gProc0State)
  233. {
  234. case PROCESS_STATE_IDLE:
  235. {
  236. #ifdef USING_PRINTF
  237. printf("wake up 2s \n");
  238. #endif
  239. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_CHECK);
  240. NetSocDisplay(LED_SOC_0,LED_TURN_ON);
  241. osDelay(100);//delay 100ms
  242. if(Uart_Rece_BattI!=0x0000)
  243. {
  244. PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
  245. }
  246. sleep_index++;
  247. if(sleep_index>=50)
  248. {
  249. NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
  250. sleep_index=0;
  251. PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
  252. }
  253. break;
  254. }
  255. case PROCESS_STATE_WORK:
  256. {
  257. osDelay(10);//10ms
  258. Can_index++;
  259. Uart_index++;
  260. NB_index++;
  261. if (Uart_index >10)//Uart 100ms 调用一次
  262. {
  263. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_WORK);
  264. Uart_index = 0;
  265. }
  266. if (Can_index >=100)//Can 1000ms 调用一次
  267. {
  268. PROC_Can_STATE_SWITCH(PROCESS_STATE_WORK);
  269. Can_index = 0;
  270. }
  271. if (NB_index >=1000)//NB 10s 调用一次
  272. {
  273. PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_CONNECT);
  274. NB_index = 0;
  275. }
  276. if((Uart_Rece_BattI==0x0000)&&(Can_Rece_buffer[0]==0xff))
  277. {
  278. sleep_index++;
  279. }
  280. else
  281. {
  282. sleep_index = 0;
  283. }
  284. if (sleep_index >=6000)
  285. {
  286. PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
  287. sleep_index = 0;
  288. }
  289. break;
  290. }
  291. case PROCESS_STATE_SLEEP:
  292. {
  293. PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_SLEEP);
  294. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_SLEEP);
  295. PROC_Can_STATE_SWITCH(PROCESS_STATE_SLEEP);
  296. slpManPlatVoteEnableSleep(process0SlpHandler,SLP_SLP2_STATE);
  297. slpManPlatVoteDisableSleep(process0SlpHandler, SLP_HIB_STATE);
  298. #ifdef USING_PRINTF
  299. printf("ready to sleep \n");
  300. #endif
  301. slpManDeepSlpTimerStart(deepslpTimerID,60000); // create a 60s timer, DeepSleep Timer is always oneshoot
  302. while(1) // now app can enter hib, but ps and phy maybe not, so wait here
  303. {
  304. osDelay(3000);
  305. }
  306. }
  307. }
  308. }
  309. }
  310. static void process1AppTask(void* arg)
  311. {
  312. USARTdrv->Initialize(USART_callback);
  313. USARTdrv->PowerControl(ARM_POWER_FULL);
  314. USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
  315. ARM_USART_DATA_BITS_8 |
  316. ARM_USART_PARITY_NONE |
  317. ARM_USART_STOP_BITS_1 |
  318. ARM_USART_FLOW_CONTROL_NONE, 9600);
  319. int Rece_index = 0;
  320. uint8_t *Uart_Reve_Point = NULL;
  321. Uart_Receive_Type Uart_Receive_Msg;
  322. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
  323. slpManApplyPlatVoteHandle("process1slp",&process1SlpHandler);
  324. Uart_Receive_Msg.Bms_Address = 0x01;
  325. Uart_Receive_Msg.Bms_Read_Funcode = 0x03;
  326. uint8_t *Uart_Rece_buffer;
  327. Batt_Cell_Num_2 = Batt_Cell_Num<<1;
  328. while(1)
  329. {
  330. switch(gProc1State)
  331. {
  332. case PROCESS_Uart_STATE_IDLE:
  333. {
  334. NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
  335. Rece_index = 0;
  336. break;
  337. }
  338. case PROCESS_Uart_STATE_CHECK://检查电流数值
  339. {
  340. Uart_Rece_buffer = (uint8_t *)malloc(16);
  341. memset(Uart_Rece_buffer,0xff,8);
  342. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  343. Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num;
  344. Uart_Receive_Msg.Reg_Num_H = 0x00;
  345. Uart_Receive_Msg.Reg_Num_L = 0x01;
  346. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
  347. Uart_Rece_BattI = *(Uart_Reve_Point+0)<<8 |*(Uart_Reve_Point+1);
  348. #ifdef USING_PRINTF
  349. printf("Check_Current!");
  350. #endif
  351. free(Uart_Rece_buffer);
  352. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
  353. break;
  354. }
  355. case PROCESS_Uart_STATE_WORK:
  356. {
  357. NetSocDisplay(LED_SOC_1,LED_TURN_ON);
  358. Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN);
  359. memset(Uart_Rece_buffer,0xff,Uart_Rece_LEN);
  360. switch(Rece_index)
  361. {
  362. case 0://读取电流
  363. {
  364. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  365. Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num;
  366. Uart_Receive_Msg.Reg_Num_H = 0x00;
  367. Uart_Receive_Msg.Reg_Num_L = 0x01;
  368. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
  369. Uart_Rece_BattI = *(Uart_Reve_Point+0)<<8 |*(Uart_Reve_Point+1);
  370. break;
  371. }
  372. case 1://读取单体电压
  373. {
  374. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  375. Uart_Receive_Msg.Reg_Begin_L = 0x02;
  376. Uart_Receive_Msg.Reg_Num_H = Batt_Cell_Num>>8;
  377. Uart_Receive_Msg.Reg_Num_L = Batt_Cell_Num;
  378. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
  379. // #ifdef USING_PRINTF
  380. // printf("BattCellV: ");
  381. // for (size_t i = 0; i < Batt_Cell_Num_2; i++)
  382. // {
  383. // printf("%x ",*(Uart_Reve_Point+i));
  384. // }
  385. // printf("\n");
  386. // #endif
  387. battbuffer[30] = Batt_Cell_Num;
  388. memcpy(&battbuffer[31],Uart_Reve_Point,Batt_Cell_Num_2);
  389. break;
  390. }
  391. case 2://读取温度
  392. {
  393. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  394. Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num;
  395. Uart_Receive_Msg.Reg_Num_H = Batt_Temp_Num>>8;
  396. Uart_Receive_Msg.Reg_Num_L = Batt_Temp_Num;
  397. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
  398. battbuffer[31+Batt_Cell_Num_2] = Batt_Temp_Num;
  399. for (int i = 0; i < Batt_Temp_Num; i++)
  400. {
  401. battbuffer[32+Batt_Cell_Num_2+i] = *(Uart_Reve_Point+2*i+1);
  402. }
  403. // #ifdef USING_PRINTF
  404. // printf("BattCellT: ");
  405. // for (size_t i = 0; i < Batt_Temp_Num; i++)
  406. // {
  407. // printf("%x ",*(Uart_Reve_Point+2*i+1));
  408. // }
  409. // printf("\n");
  410. // #endif
  411. break;
  412. }
  413. case 3://读取总电压,目前保护板只有一个电压
  414. {
  415. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  416. Uart_Receive_Msg.Reg_Begin_L = 0x18+Batt_Cell_Num+Batt_Temp_Num;
  417. Uart_Receive_Msg.Reg_Num_H = 0x00;
  418. Uart_Receive_Msg.Reg_Num_L = 0x01;
  419. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
  420. battbuffer[19] = *(Uart_Reve_Point+0);//Link U
  421. battbuffer[20] = *(Uart_Reve_Point+1);
  422. battbuffer[21] = *(Uart_Reve_Point+0);//Pack U
  423. battbuffer[22] = *(Uart_Reve_Point+1);
  424. break;
  425. }
  426. case 4://读取状态及SOC
  427. {
  428. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  429. Uart_Receive_Msg.Reg_Begin_L = 0x09+Batt_Cell_Num+Batt_Temp_Num;
  430. Uart_Receive_Msg.Reg_Num_H = 0x00;
  431. Uart_Receive_Msg.Reg_Num_L = 0x04;
  432. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
  433. battbuffer[23] = *(Uart_Reve_Point+0)>>1;//mos状态
  434. battbuffer[24] = *(Uart_Reve_Point+5);//SOC
  435. battbuffer[25] = *(Uart_Reve_Point+7);//SOH
  436. break;
  437. }
  438. case 5://读取均衡状态
  439. {
  440. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  441. Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num+Batt_Temp_Num;
  442. Uart_Receive_Msg.Reg_Num_H = 0x00;
  443. Uart_Receive_Msg.Reg_Num_L = 0x02;
  444. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
  445. memcpy(&battbuffer[26],Uart_Reve_Point,4);
  446. break;
  447. }
  448. default:
  449. {
  450. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
  451. break;
  452. }
  453. }
  454. Rece_index++;
  455. free(Uart_Rece_buffer);
  456. break;
  457. }
  458. case PROCESS_Uart_STATE_SLEEP:
  459. {
  460. while(1)
  461. {
  462. osDelay(3000);
  463. }
  464. //此处休眠
  465. }
  466. }
  467. }
  468. }
  469. static void process2AppTask(void* arg)
  470. {
  471. PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
  472. slpManApplyPlatVoteHandle("process2slp",&process2SlpHandler);
  473. uint32_t Can_ID;
  474. Can_InitType param;
  475. Can_TxMsgType Can_TxMsg;
  476. param.baudrate = CAN_500Kbps;
  477. param.mode = REQOP_NORMAL;
  478. param.TxStdIDH = 0x00;
  479. param.TxStdIDL = 0x00;
  480. param.RxStdIDH[0] = 0x00;
  481. param.RxStdIDL[0] = 0x00;
  482. /*stdid 0000 0000 001x*/
  483. param.RxStdIDH[1] = 0x00;
  484. param.RxStdIDL[1] = 0x20;
  485. /*stdid 0000 0000 010x */
  486. param.RxStdIDH[2] = 0x00;
  487. param.RxStdIDL[2] = 0x40;
  488. /*stdid 0000 0000 011x*/
  489. param.RxStdIDH[3] = 0x00;
  490. param.RxStdIDL[3] =0x60;
  491. /*stdid 0000 0000 100x */
  492. param.RxStdIDH[4] = 0x00;
  493. param.RxStdIDL[4] = 0x80;
  494. /*stdid 0000 0000 101x*/
  495. param.RxStdIDH[5] = 0x00;
  496. param.RxStdIDL[5] =0xa0;
  497. param.packType = STD_PACK;
  498. HAL_Can_Init(param);
  499. int send_index = 0;
  500. while(1)
  501. {
  502. switch(gProc2State)
  503. {
  504. case PROCESS_STATE_IDLE:
  505. {
  506. HAL_Can_Receive(Can_Rece_buffer);
  507. send_index = 0;
  508. break;
  509. }
  510. case PROCESS_STATE_WORK:
  511. {
  512. switch(send_index)
  513. {
  514. case 0:
  515. {
  516. Can_ID = 0x001;
  517. for (int i = 0; i < 8; i++)
  518. {
  519. Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
  520. }
  521. Can_TxMsg.stdIDH = Can_ID>>3;
  522. Can_TxMsg.stdIDL = Can_ID<<5;
  523. Can_TxMsg.DLC = 8;
  524. HAL_Can_Transmit(Can_TxMsg);
  525. break;
  526. }
  527. case 1:
  528. {
  529. Can_ID = 0x013;
  530. for (int i = 0; i < 8; i++)
  531. {
  532. Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
  533. }
  534. Can_TxMsg.stdIDH = Can_ID>>3;
  535. Can_TxMsg.stdIDL = Can_ID<<5;
  536. Can_TxMsg.DLC = 8;
  537. HAL_Can_Transmit(Can_TxMsg);
  538. break;
  539. }
  540. case 2:
  541. {
  542. Can_ID = 0x021;
  543. for (int i = 0; i < 8; i++)
  544. {
  545. Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
  546. }
  547. Can_TxMsg.stdIDH = Can_ID>>3;
  548. Can_TxMsg.stdIDL = Can_ID<<5;
  549. Can_TxMsg.DLC = 8;
  550. HAL_Can_Transmit(Can_TxMsg);
  551. break;
  552. }
  553. case 3:
  554. {
  555. Can_ID = 0x031;
  556. for (int i = 0; i < 4; i++)
  557. {
  558. Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
  559. }
  560. Can_TxMsg.Data[4] = 0x00;
  561. Can_TxMsg.Data[5] = 0x00;
  562. Can_TxMsg.Data[6] = 0x00;
  563. Can_TxMsg.Data[7] = 0x00;
  564. Can_TxMsg.stdIDH = Can_ID>>3;
  565. Can_TxMsg.stdIDL = Can_ID<<5;
  566. Can_TxMsg.DLC = 8;
  567. HAL_Can_Transmit(Can_TxMsg);
  568. break;
  569. }
  570. case 4:
  571. {
  572. Can_ID = 0x101;
  573. for (int i = 0; i < 4; i++)
  574. {
  575. Can_TxMsg.Data[2*i] = 0x00;
  576. Can_TxMsg.Data[2*i+1] = battbuffer[i+32+Batt_Cell_Num_2];
  577. }
  578. Can_TxMsg.stdIDH = Can_ID>>3;
  579. Can_TxMsg.stdIDL = Can_ID<<5;
  580. Can_TxMsg.DLC = 8;
  581. HAL_Can_Transmit(Can_TxMsg);
  582. break;
  583. }
  584. default:
  585. {
  586. PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
  587. Can_Rece_buffer[0]=0xff;
  588. break;
  589. }
  590. }
  591. // #ifdef USING_PRINTF
  592. // printf("CANID:%#x Msg: ",Can_ID);
  593. // for (size_t i = 0; i < 8; i++)
  594. // {
  595. // printf("%x ",Can_TxMsg.Data[i]);
  596. // }
  597. // printf("\n");
  598. // #endif
  599. // #ifdef USING_PRINTF
  600. // printf("CANsend :%x\n ",Can_ID);
  601. // #endif
  602. send_index ++;
  603. break;
  604. }
  605. case PROCESS_STATE_SLEEP:
  606. {
  607. while(1)
  608. {
  609. osDelay(3000);
  610. }
  611. }
  612. }
  613. }
  614. }
  615. uint8_t bcc_chk(uint8_t* data, uint8_t length)
  616. {
  617. uint8_t bcc_chk_return = 0x00;
  618. uint8_t count = 0;
  619. while (count<length)
  620. {
  621. bcc_chk_return^=data[count];
  622. count++;
  623. }
  624. return bcc_chk_return;
  625. }
  626. void TcpCallBack(void)
  627. {
  628. #ifdef USING_PRINTF
  629. printf("[%d]TcpCallBacl\n",__LINE__);
  630. #endif
  631. }
  632. void Tcp_Data_Assemble(uint8_t datatype)
  633. {
  634. int16_t Batt_current;
  635. Batt_current = Uart_Rece_BattI;
  636. OsaUtcTimeTValue timestracture;
  637. appGetSystemTimeUtcSync(&timestracture);
  638. battbuffer[0] = timestracture.UTCtimer1>>16;
  639. battbuffer[0] = battbuffer[0] - 0x07D0;
  640. battbuffer[1] = timestracture.UTCtimer1>>8;
  641. battbuffer[2] = timestracture.UTCtimer1;
  642. battbuffer[3] = timestracture.UTCtimer2>>24;
  643. battbuffer[4] = timestracture.UTCtimer2>>16;
  644. battbuffer[5] = timestracture.UTCtimer2>>8;
  645. switch (datatype)
  646. {
  647. case 0x80:
  648. {
  649. battbuffer[6] = 0x80;//信息体标志,此处为电池信息
  650. battbuffer[7] = battbuffer[0];//年
  651. battbuffer[8] = battbuffer[1];//月
  652. battbuffer[9] = battbuffer[2];//日
  653. battbuffer[10] = battbuffer[3];//时 0时区时间
  654. battbuffer[11] = battbuffer[4];//分
  655. battbuffer[12] = battbuffer[5];//秒
  656. battbuffer[13] = 0x1A;// 网络信号
  657. battbuffer[14] = 0x00;//故障等级
  658. battbuffer[15] = 0x00;//故障代码高
  659. battbuffer[16] = 0x00;//故障代码低
  660. //电流适应性更改,从int转换到uint,加1000的偏移量,100mA的单位
  661. if (Batt_current>0x8000)
  662. {
  663. Batt_current = Batt_current|0x7fff;
  664. Batt_current = 0x2710 - Batt_current;
  665. Batt_current = Batt_current;
  666. }
  667. else
  668. {
  669. Batt_current = Batt_current+0x2710;
  670. Batt_current = Batt_current;
  671. }
  672. battbuffer[17] = Batt_current>>8;
  673. battbuffer[18] = Batt_current;
  674. data_index = 32+Batt_Cell_Num_2+Batt_Temp_Num;
  675. battbuffer[data_index] = 0x00;//电池状态
  676. data_index++;
  677. battbuffer[data_index] = 0x00;//是否加热
  678. data_index++;
  679. break;
  680. }
  681. default:
  682. break;
  683. }
  684. return;
  685. }
  686. static void process3AppTask(void* arg)
  687. {
  688. CHAR SN[] = "GYTEST00000000003";
  689. CHAR serverip[] = "47.97.127.222";
  690. UINT16 serverport = 8712;
  691. int TcpConnectID = -1;
  692. int TcpSendID = -1;
  693. int NB_send_len=59+Batt_Cell_Num_2+Batt_Temp_Num;//设定网络发送最大数值
  694. slpManApplyPlatVoteHandle("process3slp",&process3SlpHandler);
  695. PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_IDLE);
  696. while(1)
  697. {
  698. switch(gProc3State)
  699. {
  700. case PROCESS_NB_STATE_IDLE:
  701. {
  702. osDelay(100);
  703. break;
  704. }
  705. case PROCESS_NB_STATE_CONNECT:
  706. {
  707. while(TcpConnectID<0)
  708. {
  709. TcpConnectID = tcpipConnectionCreate(1,PNULL,PNULL,serverip,serverport,TcpCallBack);
  710. osDelay(100);
  711. #ifdef USING_PRINTF
  712. printf("ConnectID:%d\n ",TcpConnectID);
  713. #endif
  714. }
  715. PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_WORK);
  716. break;
  717. }
  718. case PROCESS_NB_STATE_WORK:
  719. {
  720. uint8_t* TcpSendBuffer;
  721. TcpSendBuffer = (uint8_t *)malloc(NB_send_len);//申请发送的数据内存
  722. if (!TcpSendBuffer)
  723. {
  724. #ifdef USING_PRINTF
  725. printf("[%d]malloc error! \r\n",__LINE__);
  726. #endif
  727. }
  728. memset(TcpSendBuffer,0x00,92);
  729. *(TcpSendBuffer+0) = 0x23;
  730. *(TcpSendBuffer+1) = 0x23;
  731. *(TcpSendBuffer+2) = 0x02;
  732. *(TcpSendBuffer+3) = 0xfe;
  733. memcpy(TcpSendBuffer+4,SN,17);
  734. *(TcpSendBuffer+21) = 0x01;//不加密
  735. Tcp_Data_Assemble(0x80);
  736. *(TcpSendBuffer+22) = data_index>>8;//数据长度
  737. *(TcpSendBuffer+23) = data_index;//数据长度
  738. memcpy(TcpSendBuffer+24,battbuffer,data_index);
  739. #ifdef USING_PRINTF
  740. printf("battbuffer:");
  741. for (int i = 0; i < data_index; i++)
  742. {
  743. printf("%x ",battbuffer[i]);
  744. }
  745. printf("\n ");
  746. #endif
  747. *(TcpSendBuffer+NB_send_len-1) = bcc_chk(TcpSendBuffer,NB_send_len-1);
  748. // #ifdef USING_PRINTF
  749. // printf("[%d]sizeof:%d \r\n",__LINE__,sizeof(TcpSendBuffer)-1);
  750. // #endif
  751. // #ifdef USING_PRINTF
  752. // printf("[%d]Tcpchk:%#X \r\n",__LINE__,*(TcpSendBuffer+91));
  753. // #endif
  754. TcpSendID = tcpipConnectionSend(TcpConnectID,TcpSendBuffer,NB_send_len,PNULL,PNULL,PNULL);
  755. #ifdef USING_PRINTF
  756. printf("ConnectID:%d,TcpSend:%d,data length:%d,Data: ",TcpConnectID,TcpSendID,NB_send_len);
  757. for (int i = 0; i < NB_send_len; i++)
  758. {
  759. printf("%x ",*(TcpSendBuffer+i));
  760. }
  761. printf("\n ");
  762. #endif
  763. free(TcpSendBuffer);
  764. PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_IDLE);
  765. break;
  766. }
  767. case PROCESS_NB_STATE_SLEEP:
  768. {
  769. while(1)
  770. {
  771. osDelay(3000);
  772. }
  773. }
  774. }
  775. }
  776. }
  777. /**
  778. \fn process0Init(void)
  779. \brief process0Init function.
  780. \return
  781. */
  782. void process0Init(void)
  783. {
  784. osThreadAttr_t task_attr;
  785. #ifndef USING_PRINTF
  786. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  787. {
  788. HAL_UART_RecvFlowControl(false);
  789. }
  790. #endif
  791. memset(&task_attr,0,sizeof(task_attr));
  792. memset(gProcessTaskStack0, 0xA5,PROC_TASK_STACK_SIZE);
  793. task_attr.name = "Process0AppTask";
  794. task_attr.stack_mem = gProcessTaskStack0;
  795. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  796. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  797. task_attr.cb_mem = &gProcessTask0;//task control block
  798. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  799. osThreadNew(process0AppTask, NULL, &task_attr);
  800. }
  801. /**
  802. \fn process1Init(void)
  803. \brief process1Init function.
  804. \return
  805. */
  806. void process1Init(void)
  807. {
  808. osThreadAttr_t task_attr;
  809. #ifndef USING_PRINTF
  810. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  811. {
  812. HAL_UART_RecvFlowControl(false);
  813. }
  814. #endif
  815. memset(&task_attr,0,sizeof(task_attr));
  816. memset(gProcessTaskStack1, 0xA5,PROC_TASK_STACK_SIZE);
  817. task_attr.name = "Process1AppTask";
  818. task_attr.stack_mem = gProcessTaskStack1;
  819. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  820. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  821. task_attr.cb_mem = &gProcessTask1;//task control block
  822. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  823. osThreadNew(process1AppTask, NULL, &task_attr);
  824. }
  825. /**
  826. \fn process2Init(void)
  827. \brief process2Init function.
  828. \return
  829. */
  830. void process2Init(void)
  831. {
  832. osThreadAttr_t task_attr;
  833. memset(&task_attr,0,sizeof(task_attr));
  834. memset(gProcessTaskStack2, 0xA5,PROC_TASK_STACK_SIZE);
  835. task_attr.name = "Process2AppTask";
  836. task_attr.stack_mem = gProcessTaskStack2;
  837. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  838. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  839. task_attr.cb_mem = &gProcessTask2;//task control block
  840. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  841. osThreadNew(process2AppTask, NULL, &task_attr);
  842. }
  843. void process3Init(void)
  844. {
  845. osThreadAttr_t task_attr;
  846. memset(&task_attr,0,sizeof(task_attr));
  847. memset(gProcessTaskStack3, 0xA5,PROC_TASK_STACK_SIZE);
  848. task_attr.name = "Process3AppTask";
  849. task_attr.stack_mem = gProcessTaskStack3;
  850. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  851. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  852. task_attr.cb_mem = &gProcessTask3;//task control block
  853. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  854. osThreadNew(process3AppTask, NULL, &task_attr);
  855. }
  856. /**
  857. \fn appInit(void)
  858. \brief appInit function.
  859. \return
  860. */
  861. void appInit(void *arg)
  862. {
  863. process0Init();//任务调度和检测程序
  864. process1Init();//Uart程序
  865. process2Init();//Can程序
  866. process3Init();//NB程序
  867. }
  868. /**
  869. \fn int main_entry(void)
  870. \brief main entry function.
  871. \return
  872. */
  873. void main_entry(void) {
  874. BSP_CommonInit();
  875. osKernelInitialize();
  876. registerAppEntry(appInit, NULL);
  877. if (osKernelGetState() == osKernelReady)
  878. {
  879. osKernelStart();
  880. }
  881. while(1);
  882. }