app.c 32 KB

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