app.c 30 KB

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