app.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*******************************
  2. *
  3. * 骑享程序测试
  4. *
  5. * ****************************/
  6. #include "bsp.h"
  7. #include "bsp_custom.h"
  8. #include "osasys.h"
  9. #include "ostask.h"
  10. #include "queue.h"
  11. #include "ps_event_callback.h"
  12. #include "app.h"
  13. #include "cmisim.h"
  14. #include "cmimm.h"
  15. #include "cmips.h"
  16. #include "sockets.h"
  17. #include "psifevent.h"
  18. #include "ps_lib_api.h"
  19. #include "lwip/netdb.h"
  20. #include <cis_def.h>
  21. #include "debug_log.h"
  22. #include "slpman_ec616.h"
  23. #include "plat_config.h"
  24. //空间及变量定义
  25. #define PROC_MAIN_TASK_STACK_SIZE (1024)
  26. #define PROC_UART_TASK_STACK_SIZE (1024)
  27. #define PROC_CAN_TASK_STACK_SIZE (1024)
  28. #define PROC_TCP_TASK_STACK_SIZE (1024)
  29. #define APP_EVENT_QUEUE_SIZE (10)
  30. uint8_t deepslpTimerID = DEEPSLP_TIMER_ID7;
  31. uint8_t MainSlpHandler = 0xff;
  32. uint8_t UartSlpHandler = 0xfe;
  33. uint8_t CanSlpHandler = 0xfd;
  34. uint8_t TcpSlpHandler = 0xfd;
  35. #define Uart_Send_LEN (8)
  36. #define Uart_Rece_LEN (40)
  37. #define RTE_UART_RX_IO_MODE RTE_UART1_RX_IO_MODE
  38. extern ARM_DRIVER_USART Driver_USART1;
  39. static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
  40. volatile bool isRecvTimeout = false;
  41. volatile bool isRecvComplete = false;
  42. static volatile UINT32 Event;
  43. static QueueHandle_t psEventQueueHandle;
  44. static UINT8 gImsi[16] = {0};
  45. static INT32 sockfd = -1;
  46. static UINT32 gCellID = 0;
  47. uint8_t Batt_Cell_Num = 14;//默认数值14、17
  48. uint8_t Batt_Cell_Num_2 ;//默认数值
  49. uint8_t Batt_Temp_Num = 5;//默认数值5、7
  50. int16_t Uart_Rece_BattI=0x0000;
  51. uint8_t Can_Rece_buffer[8];
  52. volatile bool Can_Enable = false;
  53. uint16_t data_index = 0x0000;
  54. uint8_t battbuffer[100];//电池数据都存在此数组中————电压14,温度5
  55. /**
  56. * 存放规则如下:
  57. * 位置: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  58. * 数据: 年 月 日 时 分 秒 信息体标志 年 月 日 时 分 秒 网络信号 故障等级 故障代码高 故障代码低
  59. *
  60. * 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
  61. * 电流H 电流L Link电压H Link电压L Pack电压H Pack电压L 开关状态 SOC SOH 均衡状态 单体个数X 单体v1...单体vX 温度个数N 温度1..温度N
  62. * 32+X*2+N 33+X*2+N 34+x*2+N 35 +X*2+N 36+X*2 +N 37+X*2+N
  63. * 电池状态 是否加热 最高单体H 最高单体L 最低单体H 最低单体L
  64. * */
  65. //状态机定义
  66. typedef enum
  67. {
  68. PROCESS_STATE_IDLE = 0,
  69. PROCESS_STATE_WORK,
  70. PROCESS_STATE_SLEEP
  71. }process_Main;
  72. static process_Main gProcess_Main_Task = PROCESS_STATE_IDLE;
  73. #define PROC_MAIN_STATE_SWITCH(a) (gProcess_Main_Task = a)
  74. typedef enum
  75. {
  76. PROCESS_UART_STATE_IDLE = 0,
  77. PROCESS_UART_STATE_CHECK,
  78. PROCESS_UART_STATE_WORK,
  79. PROCESS_UART_STATE_SLEEP
  80. }process_Uart;
  81. static process_Uart gProcess_Uart_Task = PROCESS_UART_STATE_IDLE;
  82. #define PROC_UART_STATE_SWITCH(a) (gProcess_Uart_Task = a)
  83. typedef enum
  84. {
  85. PROCESS_CAN_STATE_IDLE = 0,
  86. PROCESS_CAN_STATE_RECV,
  87. PROCESS_CAN_STATE_SEND,
  88. PROCESS_CAN_STATE_SLEEP
  89. }process_CAN;
  90. static process_CAN gProcess_Can_Task = PROCESS_CAN_STATE_IDLE;
  91. #define PROC_CAN_STATE_SWITCH(a) (gProcess_Can_Task = a)
  92. typedef enum
  93. {
  94. PROCESS_TCP_STATE_IDLE = 0,
  95. PROCESS_TCP_STATE_LINK,
  96. PROCESS_TCP_STATE_WORK,
  97. PROCESS_TCP_STATE_SLEEP
  98. }process_TCP;
  99. static process_TCP gProcess_Tcp_Task = PROCESS_TCP_STATE_IDLE;
  100. #define PROC_TCP_STATE_SWITCH(a) (gProcess_Tcp_Task = a)
  101. //堆栈申请
  102. static StaticTask_t gProcess_Main_Task_t;
  103. static UINT8 gProcess_Main_TaskStack[PROC_UART_TASK_STACK_SIZE];
  104. static StaticTask_t gProcess_Uart_Task_t;
  105. static UINT8 gProcess_Uart_TaskStack[PROC_UART_TASK_STACK_SIZE];
  106. static StaticTask_t gProcess_Can_Task_t;
  107. static UINT8 gProcess_Can_TaskStack[PROC_CAN_TASK_STACK_SIZE];
  108. static StaticTask_t gProcess_Tcp_Task_t;
  109. static UINT8 gProcess_Tcp_TaskStack[PROC_TCP_TASK_STACK_SIZE];
  110. //睡眠进出函数
  111. static void appBeforeHib(void *pdata, slpManLpState state)
  112. {
  113. uint32_t *p_param = (uint32_t *)pdata;
  114. #ifdef USING_PRINTF
  115. printf("Before Hibernate:%d \n",state);
  116. #endif
  117. slpManAONIOLatchEn(AonIOLatch_Enable);
  118. }
  119. static void appAfterHib(void *pdata, slpManLpState state)
  120. {
  121. #ifdef USING_PRINTF
  122. printf("Try Hibernate Failed:%d \n",state);
  123. #endif
  124. }
  125. static void appBeforeSlp1(void *pdata, slpManLpState state)
  126. {
  127. #ifdef USING_PRINTF
  128. printf("Before Sleep1:%d \n",state);
  129. #endif
  130. slpManAONIOLatchEn(AonIOLatch_Enable);
  131. }
  132. static void appAfterSlp1(void *pdata, slpManLpState state)
  133. {
  134. #ifdef USING_PRINTF
  135. printf("After Sleep1:%d \n",state);
  136. #endif
  137. }
  138. static void appBeforeSlp2(void *pdata, slpManLpState state)
  139. {
  140. #ifdef USING_PRINTF
  141. printf("before sleep2:%d \n",state);
  142. #endif
  143. slpManAONIOLatchEn(AonIOLatch_Enable);
  144. }
  145. static void appAfterSlp2(void *pdata, slpManLpState state)
  146. {
  147. #ifdef USING_PRINTF
  148. printf("sleep2 failed:%d \n",state);
  149. #endif
  150. }
  151. // 主任务线程
  152. static void Main_Task(void* arg)
  153. {
  154. UINT16 Can_index = 0;
  155. UINT16 Uart_index = 0;
  156. UINT16 Tcp_index = 0;
  157. uint32_t sleep_index = 0;
  158. int32_t inParam = 0xAABBCCDD;
  159. PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
  160. NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
  161. NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
  162. NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
  163. NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
  164. FaultDisplay(LED_TURN_OFF);
  165. NVIC_EnableIRQ(PadWakeup1_IRQn);
  166. slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
  167. slpManApplyPlatVoteHandle("MainSlp",&MainSlpHandler);
  168. slpManPlatVoteDisableSleep(MainSlpHandler, SLP_SLP2_STATE);
  169. slpManRegisterUsrdefinedBackupCb(appBeforeHib,&inParam,SLPMAN_HIBERNATE_STATE);
  170. slpManRegisterUsrdefinedRestoreCb(appAfterHib,NULL,SLPMAN_HIBERNATE_STATE);
  171. slpManRegisterUsrdefinedBackupCb(appBeforeSlp1,NULL,SLPMAN_SLEEP1_STATE);
  172. slpManRegisterUsrdefinedRestoreCb(appAfterSlp1,NULL,SLPMAN_SLEEP1_STATE);
  173. slpManRegisterUsrdefinedBackupCb(appBeforeSlp2,NULL,SLPMAN_SLEEP2_STATE);
  174. slpManRegisterUsrdefinedRestoreCb(appAfterSlp2,NULL,SLPMAN_SLEEP2_STATE);
  175. slpManSlpState_t slpstate = slpManGetLastSlpState();
  176. #ifdef USING_PRINTF
  177. printf("slpstate:%d \n",slpstate);
  178. #endif
  179. if((slpstate == SLP_SLP2_STATE) || (slpstate == SLP_HIB_STATE))
  180. {
  181. PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
  182. }
  183. else
  184. {
  185. PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK);
  186. }
  187. //线程初始化完成
  188. while (true)
  189. {
  190. switch(gProcess_Main_Task)
  191. {
  192. case PROCESS_STATE_IDLE:
  193. {
  194. // 暂时不区分唤醒
  195. PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK);
  196. break;
  197. }
  198. case PROCESS_STATE_WORK:
  199. {
  200. osDelay(10/portTICK_PERIOD_MS);
  201. Can_index++;
  202. Uart_index++;
  203. Tcp_index++;
  204. if (Uart_index >100)//Uart 1s 调用一次
  205. {
  206. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WORK);
  207. Uart_index = 0;
  208. }
  209. if (Can_index >=100)//Can 1s 调用一次
  210. {
  211. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_RECV);
  212. Can_index = 0;
  213. }
  214. if (Tcp_index >=100)//Tcp 1s 调用一次
  215. {
  216. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_LINK);
  217. Tcp_index = 0;
  218. }
  219. if((!Can_Enable)&&(Uart_Rece_BattI==0x0000))
  220. {
  221. sleep_index++;
  222. }
  223. else
  224. {
  225. sleep_index = 0;
  226. }
  227. if (sleep_index >=6000)
  228. {
  229. PROC_MAIN_STATE_SWITCH(PROCESS_STATE_SLEEP);
  230. sleep_index = 0;
  231. }
  232. break;
  233. }
  234. case PROCESS_STATE_SLEEP:
  235. {
  236. while((gProcess_Uart_Task!=PROCESS_UART_STATE_SLEEP)||(gProcess_Can_Task!=PROCESS_CAN_STATE_SLEEP)||(gProcess_Tcp_Task!=PROCESS_TCP_STATE_SLEEP))
  237. {
  238. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
  239. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SLEEP);
  240. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_SLEEP);
  241. }
  242. slpManSlpState_t State;
  243. uint8_t cnt;
  244. if(slpManCheckVoteState(MainSlpHandler, &State, &cnt)==RET_TRUE)
  245. {
  246. #ifdef USING_PRINTF
  247. printf("[%d]We Can Check Vote State, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
  248. #endif
  249. }
  250. slpManPlatVoteEnableSleep(MainSlpHandler, SLP_SLP2_STATE);
  251. if(slpManCheckVoteState(MainSlpHandler, &State, &cnt)==RET_TRUE)
  252. {
  253. #ifdef USING_PRINTF
  254. printf("[%d]We Can Check Vote State Again, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
  255. #endif
  256. }
  257. #ifdef USING_PRINTF
  258. printf("[%d]Waiting sleep\r\n",__LINE__);
  259. #endif
  260. #ifdef USING_PRINTF
  261. printf("Main_Sleep\n");
  262. #endif
  263. FaultDisplay(LED_TURN_ON);
  264. slpManDeepSlpTimerStart(deepslpTimerID,300000);
  265. while(1)
  266. {
  267. osDelay(30000/portTICK_PERIOD_MS);
  268. }
  269. break;
  270. }
  271. }
  272. }
  273. }
  274. //Uart校验程序
  275. unsigned int crc_chk(uint8_t* data, uint8_t length)
  276. {
  277. int j;
  278. uint16_t reg_crc=0xFFFF;
  279. while(length--)
  280. {
  281. reg_crc ^= *data++;
  282. for(j=0;j<8;j++)
  283. {
  284. if(reg_crc & 0x01)
  285. {
  286. reg_crc=(reg_crc>>1) ^ 0xA001;
  287. }
  288. else
  289. {
  290. reg_crc=reg_crc >>1;
  291. }
  292. }
  293. }
  294. return reg_crc;
  295. }
  296. //Uart回调程序
  297. void USART_callback(uint32_t event)
  298. {
  299. if(event & ARM_USART_EVENT_RX_TIMEOUT)
  300. {
  301. isRecvTimeout = true;
  302. }
  303. if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
  304. {
  305. isRecvComplete = true;
  306. }
  307. }
  308. //Uart发送接收函数
  309. uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t* Uart_Rece_buffer,uint8_t Data_Len)
  310. {
  311. uint16_t CRC_Rece_buffer;
  312. uint16_t CRC_chk_buffer;
  313. uint8_t Uart_Send_buffer[8];
  314. Uart_Send_buffer[0] = Uart_Receive_Msg.Bms_Address;
  315. Uart_Send_buffer[1] = Uart_Receive_Msg.Bms_Read_Funcode;
  316. Uart_Send_buffer[2] = Uart_Receive_Msg.Reg_Begin_H;
  317. Uart_Send_buffer[3] = Uart_Receive_Msg.Reg_Begin_L;
  318. Uart_Send_buffer[4] = Uart_Receive_Msg.Reg_Num_H;
  319. Uart_Send_buffer[5] = Uart_Receive_Msg.Reg_Num_L;
  320. CRC_chk_buffer = crc_chk(Uart_Send_buffer,6);
  321. Uart_Send_buffer[6] = CRC_chk_buffer;
  322. Uart_Send_buffer[7] = CRC_chk_buffer>>8;
  323. uint32_t timeout = 0x00000000;
  324. USARTdrv->Send(Uart_Send_buffer,8);
  325. // #ifdef USING_PRINTF
  326. // printf("Uart_Send_buffer: ");
  327. // for(int i=0;i<8;i++)
  328. // {
  329. // printf("%x ",Uart_Send_buffer[i]);
  330. // }
  331. // printf("\n");
  332. // #endif
  333. USARTdrv->Receive(Uart_Rece_buffer,Data_Len);
  334. while((isRecvTimeout == false) && (isRecvComplete == false))
  335. {
  336. timeout++;
  337. if (timeout>=7000000)
  338. {
  339. timeout =0;
  340. isRecvTimeout = true;
  341. break;
  342. }
  343. }
  344. if (isRecvComplete == true)
  345. {
  346. #ifdef USING_PRINTF
  347. #endif
  348. isRecvComplete = false;
  349. CRC_Rece_buffer =*(Uart_Rece_buffer+Data_Len-1)<<8|*(Uart_Rece_buffer+Data_Len-2);
  350. CRC_chk_buffer = crc_chk(Uart_Rece_buffer,Data_Len-2);
  351. if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
  352. {
  353. return Uart_Rece_buffer;//此处指针移位出现重启问题
  354. }
  355. else //接收数据的校验不过屏蔽
  356. {
  357. #ifdef USING_PRINTF
  358. printf("Uart_Rece_buffer: ");
  359. for(int i=0;i<Data_Len;i++)
  360. {
  361. printf("%x ",*(Uart_Rece_buffer+i));
  362. }
  363. printf("crcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
  364. #endif
  365. memset(Uart_Rece_buffer,0xff,Data_Len);
  366. return Uart_Rece_buffer;
  367. }
  368. }
  369. else
  370. {
  371. memset(Uart_Rece_buffer,0x00,Data_Len);
  372. isRecvTimeout = false;
  373. osDelay(100);
  374. return Uart_Rece_buffer;
  375. }
  376. }
  377. //Uart线程
  378. static void Uart_Task(void* arg)
  379. {
  380. USARTdrv->Initialize(USART_callback);
  381. USARTdrv->PowerControl(ARM_POWER_FULL);
  382. USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
  383. ARM_USART_DATA_BITS_8 |
  384. ARM_USART_PARITY_NONE |
  385. ARM_USART_STOP_BITS_1 |
  386. ARM_USART_FLOW_CONTROL_NONE, 9600);
  387. int Rece_index = 0;
  388. volatile uint8_t Data_Len;
  389. Uart_Receive_Type Uart_Receive_Msg;
  390. memset(&battbuffer[0],0x00,100);
  391. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  392. slpManApplyPlatVoteHandle("UARTSLP",&UartSlpHandler);
  393. slpManPlatVoteDisableSleep(UartSlpHandler, SLP_SLP2_STATE);
  394. Uart_Receive_Msg.Bms_Address = 0x01;
  395. Uart_Receive_Msg.Bms_Read_Funcode = 0x03;
  396. uint8_t *Uart_Rece_buffer = NULL;
  397. volatile bool Uart_task = false;
  398. Batt_Cell_Num_2 = Batt_Cell_Num<<1;
  399. while (true)
  400. {
  401. switch (gProcess_Uart_Task)
  402. {
  403. case PROCESS_UART_STATE_IDLE:
  404. {
  405. NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
  406. Rece_index = 0;
  407. break;
  408. }
  409. case PROCESS_UART_STATE_CHECK:
  410. {
  411. Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN);
  412. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  413. Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num;
  414. Uart_Receive_Msg.Reg_Num_H = 0x00;
  415. Uart_Receive_Msg.Reg_Num_L = 0x01;
  416. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  417. memset(Uart_Rece_buffer,0x00,Data_Len);
  418. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  419. Uart_Rece_BattI = *(Uart_Rece_buffer+3+0)<<8 |*(Uart_Rece_buffer+3+1);
  420. #ifdef USING_PRINTF
  421. printf("Check_Current!\n");
  422. #endif
  423. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  424. free(Uart_Rece_buffer);
  425. break;
  426. }
  427. case PROCESS_UART_STATE_WORK:
  428. {
  429. NetSocDisplay(LED_SOC_1,LED_TURN_ON);
  430. Uart_task = false;
  431. Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN);
  432. while(!Uart_task)
  433. {
  434. switch (Rece_index)
  435. {
  436. case 0://current
  437. {
  438. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  439. Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num;
  440. Uart_Receive_Msg.Reg_Num_H = 0x00;
  441. Uart_Receive_Msg.Reg_Num_L = 0x01;
  442. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  443. memset(Uart_Rece_buffer,0x00,Data_Len);
  444. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  445. Uart_Rece_BattI = *(Uart_Rece_buffer+3+0)<<8 |*(Uart_Rece_buffer+3+1);
  446. break;
  447. }
  448. case 1://cell votage
  449. {
  450. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  451. Uart_Receive_Msg.Reg_Begin_L = 0x02;
  452. Uart_Receive_Msg.Reg_Num_H = Batt_Cell_Num>>8;
  453. Uart_Receive_Msg.Reg_Num_L = Batt_Cell_Num;
  454. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  455. memset(Uart_Rece_buffer,0x00,Data_Len);
  456. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  457. battbuffer[30] = Batt_Cell_Num;
  458. memcpy(&battbuffer[31],Uart_Rece_buffer+3,Batt_Cell_Num_2);
  459. break;
  460. }
  461. case 2://temprature
  462. {
  463. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  464. Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num;
  465. Uart_Receive_Msg.Reg_Num_H = Batt_Temp_Num>>8;
  466. Uart_Receive_Msg.Reg_Num_L = Batt_Temp_Num;
  467. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  468. memset(Uart_Rece_buffer,0x00,Data_Len);
  469. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  470. battbuffer[31+Batt_Cell_Num_2] = Batt_Temp_Num;
  471. for (int i = 0; i < Batt_Temp_Num; i++)
  472. {
  473. battbuffer[32+Batt_Cell_Num_2+i] = *(Uart_Rece_buffer+3+2*i+1);
  474. }
  475. break;
  476. }
  477. case 3://batt votage
  478. {
  479. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  480. Uart_Receive_Msg.Reg_Begin_L = 0x18+Batt_Cell_Num+Batt_Temp_Num;
  481. Uart_Receive_Msg.Reg_Num_H = 0x00;
  482. Uart_Receive_Msg.Reg_Num_L = 0x01;
  483. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  484. memset(Uart_Rece_buffer,0x00,Data_Len);
  485. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  486. battbuffer[19] = *(Uart_Rece_buffer+3+0);//Link U
  487. battbuffer[20] = *(Uart_Rece_buffer+3+1);
  488. battbuffer[21] = *(Uart_Rece_buffer+3+0);//Pack U
  489. battbuffer[22] = *(Uart_Rece_buffer+3+1);
  490. break;
  491. }
  492. case 4://soc,soh
  493. {
  494. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  495. Uart_Receive_Msg.Reg_Begin_L = 0x09+Batt_Cell_Num+Batt_Temp_Num;
  496. Uart_Receive_Msg.Reg_Num_H = 0x00;
  497. Uart_Receive_Msg.Reg_Num_L = 0x04;
  498. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  499. memset(Uart_Rece_buffer,0x00,Data_Len);
  500. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  501. battbuffer[23] = *(Uart_Rece_buffer+3+0)>>1;//mos状态
  502. battbuffer[24] = *(Uart_Rece_buffer+3+5);//SOC
  503. battbuffer[25] = *(Uart_Rece_buffer+3+7);//SOH
  504. break;
  505. }
  506. case 5://均衡
  507. {
  508. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  509. Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num+Batt_Temp_Num;
  510. Uart_Receive_Msg.Reg_Num_H = 0x00;
  511. Uart_Receive_Msg.Reg_Num_L = 0x02;
  512. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  513. memset(Uart_Rece_buffer,0x00,Data_Len);
  514. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  515. memcpy(&battbuffer[26],Uart_Rece_buffer+3,4);
  516. break;
  517. }
  518. case 6:
  519. {
  520. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  521. Uart_Receive_Msg.Reg_Begin_L = 0x19+Batt_Cell_Num+Batt_Temp_Num;
  522. Uart_Receive_Msg.Reg_Num_H = 0x00;
  523. Uart_Receive_Msg.Reg_Num_L = 0x02;
  524. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  525. memset(Uart_Rece_buffer,0x00,Data_Len);
  526. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  527. memcpy(&battbuffer[34+Batt_Cell_Num_2+Batt_Temp_Num],Uart_Rece_buffer+3,4);
  528. break;
  529. }
  530. default:
  531. {
  532. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  533. Uart_task = true;
  534. break;
  535. }
  536. }
  537. Rece_index++;
  538. }
  539. free(Uart_Rece_buffer);
  540. break;
  541. }
  542. case PROCESS_UART_STATE_SLEEP:
  543. {
  544. slpManPlatVoteEnableSleep(UartSlpHandler, SLP_SLP2_STATE);
  545. while(1)
  546. {
  547. osDelay(30000/portTICK_PERIOD_MS);
  548. }
  549. }
  550. }
  551. }
  552. }
  553. //Can-线程任务
  554. static void Can_Task(void* arg)
  555. {
  556. uint32_t Can_ID;
  557. Can_InitType param;
  558. Can_TxMsgType Can_TxMsg;
  559. param.baudrate = CAN_500Kbps;
  560. param.mode = REQOP_NORMAL;
  561. //过滤ID配置
  562. param.TxStdIDH = 0x00;
  563. param.TxStdIDL = 0x00;
  564. param.RxStdIDH[0] = 0x00;
  565. param.RxStdIDL[0] = 0x00;
  566. /*stdid 0000 0000 001x*/
  567. param.RxStdIDH[1] = 0x00;
  568. param.RxStdIDL[1] = 0x20;
  569. /*stdid 0000 0000 010x */
  570. param.RxStdIDH[2] = 0x00;
  571. param.RxStdIDL[2] = 0x40;
  572. /*stdid 0000 0000 011x*/
  573. param.RxStdIDH[3] = 0x00;
  574. param.RxStdIDL[3] =0x60;
  575. /*stdid 0000 0000 100x */
  576. param.RxStdIDH[4] = 0x00;
  577. param.RxStdIDL[4] = 0x80;
  578. /*stdid 0000 0000 101x*/
  579. param.RxStdIDH[5] = 0x00;
  580. param.RxStdIDL[5] =0xa0;
  581. //配置完毕
  582. param.packType = STD_PACK;
  583. HAL_Can_Init(param);
  584. int send_index = 0;
  585. uint16_t Batt_Cell_addU = 0x0000;
  586. slpManApplyPlatVoteHandle("CanSlp",&CanSlpHandler);
  587. slpManPlatVoteDisableSleep(CanSlpHandler, SLP_SLP2_STATE);
  588. volatile bool Can_Flag=false;
  589. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE);
  590. while(1)
  591. {
  592. switch(gProcess_Can_Task)
  593. {
  594. case PROCESS_CAN_STATE_IDLE:
  595. {
  596. Can_Rece_buffer[0]=0xff;
  597. send_index = 0;
  598. break;
  599. }
  600. case PROCESS_CAN_STATE_RECV:
  601. {
  602. //can采用先接收后发送的策略
  603. HAL_Can_Receive(Can_Rece_buffer);
  604. if (Can_Rece_buffer[0]!=0xff)//满足can发送使能
  605. {
  606. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SEND);
  607. Can_Enable = true;
  608. }
  609. else
  610. {
  611. Can_Enable = false;
  612. }
  613. break;
  614. }
  615. case PROCESS_CAN_STATE_SEND:
  616. {
  617. Can_Flag=false;
  618. while(!Can_Flag)
  619. {
  620. #ifdef USING_PRINTF
  621. printf("Can_Send!\n");
  622. #endif
  623. switch(send_index)
  624. {
  625. case 0:
  626. {
  627. Can_ID = 0x001;
  628. for (int i = 0; i < 8; i++)
  629. {
  630. Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
  631. }
  632. Can_TxMsg.stdIDH = Can_ID>>3;
  633. Can_TxMsg.stdIDL = Can_ID<<5;
  634. Can_TxMsg.DLC = 8;
  635. HAL_Can_Transmit(Can_TxMsg);
  636. break;
  637. }
  638. case 1:
  639. {
  640. Can_ID = 0x011;
  641. for (int i = 0; i < 8; i++)
  642. {
  643. Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
  644. }
  645. Can_TxMsg.stdIDH = Can_ID>>3;
  646. Can_TxMsg.stdIDL = Can_ID<<5;
  647. Can_TxMsg.DLC = 8;
  648. HAL_Can_Transmit(Can_TxMsg);
  649. break;
  650. }
  651. case 2:
  652. {
  653. Can_ID = 0x020;
  654. for (int i = 0; i < 8; i++)
  655. {
  656. Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
  657. }
  658. Can_TxMsg.stdIDH = Can_ID>>3;
  659. Can_TxMsg.stdIDL = Can_ID<<5;
  660. Can_TxMsg.DLC = 8;
  661. HAL_Can_Transmit(Can_TxMsg);
  662. break;
  663. }
  664. case 3:
  665. {
  666. Can_ID = 0x031;
  667. for (int i = 0; i < 4; i++)
  668. {
  669. Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
  670. }
  671. Can_TxMsg.Data[4] = 0x00;
  672. Can_TxMsg.Data[5] = 0x00;
  673. Can_TxMsg.Data[6] = 0x00;
  674. Can_TxMsg.Data[7] = 0x00;
  675. Can_TxMsg.stdIDH = Can_ID>>3;
  676. Can_TxMsg.stdIDL = Can_ID<<5;
  677. Can_TxMsg.DLC = 8;
  678. HAL_Can_Transmit(Can_TxMsg);
  679. break;
  680. }
  681. case 4:
  682. {
  683. Can_ID = 0x101;
  684. for (int i = 0; i < Batt_Temp_Num; i++)
  685. {
  686. Can_TxMsg.Data[i] = battbuffer[i+32+Batt_Cell_Num_2];
  687. }
  688. Can_TxMsg.stdIDH = Can_ID>>3;
  689. Can_TxMsg.stdIDL = Can_ID<<5;
  690. Can_TxMsg.DLC = 8;
  691. HAL_Can_Transmit(Can_TxMsg);
  692. break;
  693. }
  694. case 5:
  695. {
  696. Batt_Cell_addU = 0x0000;
  697. for (int i = 0; i < Batt_Cell_Num; i++)
  698. {
  699. Batt_Cell_addU = Batt_Cell_addU + (battbuffer[31+i*2]<<8|battbuffer[31+i*2+1]);
  700. }
  701. Can_ID = 0x201;
  702. Can_TxMsg.Data[0] = battbuffer[19];
  703. Can_TxMsg.Data[1] = battbuffer[20];
  704. Can_TxMsg.Data[2] = battbuffer[21];
  705. Can_TxMsg.Data[3] = battbuffer[22];//外电压
  706. Can_TxMsg.Data[4] = Batt_Cell_addU>>8;
  707. Can_TxMsg.Data[5] = Batt_Cell_addU;//累加电压
  708. Can_TxMsg.Data[6] = Uart_Rece_BattI>>8;
  709. Can_TxMsg.Data[7] = Uart_Rece_BattI;
  710. Can_TxMsg.stdIDH = Can_ID>>3;
  711. Can_TxMsg.stdIDL = Can_ID<<5;
  712. Can_TxMsg.DLC = 8;
  713. HAL_Can_Transmit(Can_TxMsg);
  714. break;
  715. }
  716. case 6:
  717. {
  718. Can_ID = 0x211;
  719. Can_TxMsg.Data[0] = battbuffer[34+Batt_Cell_Num_2+Batt_Temp_Num];
  720. Can_TxMsg.Data[1] = battbuffer[35+Batt_Cell_Num_2+Batt_Temp_Num];
  721. Can_TxMsg.Data[2] = battbuffer[36+Batt_Cell_Num_2+Batt_Temp_Num];
  722. Can_TxMsg.Data[3] = battbuffer[37+Batt_Cell_Num_2+Batt_Temp_Num];
  723. Can_TxMsg.Data[4] = 0x00;
  724. Can_TxMsg.Data[5] = 0x00;
  725. Can_TxMsg.Data[6] = battbuffer[24];
  726. Can_TxMsg.Data[7] = Batt_Temp_Num<<4|0;
  727. Can_TxMsg.stdIDH = Can_ID>>3;
  728. Can_TxMsg.stdIDL = Can_ID<<5;
  729. Can_TxMsg.DLC = 8;
  730. HAL_Can_Transmit(Can_TxMsg);
  731. break;
  732. }
  733. case 7:
  734. {
  735. Can_ID = 0x221;
  736. Can_TxMsg.Data[0] = battbuffer[25];//SOH
  737. Can_TxMsg.Data[1] = 0x00;
  738. Can_TxMsg.Data[2] = 0x00;
  739. Can_TxMsg.Data[3] = 0x00;
  740. Can_TxMsg.Data[4] = 0x00;
  741. Can_TxMsg.Data[5] = 0x00;
  742. Can_TxMsg.Data[6] = 0x00;
  743. Can_TxMsg.Data[7] = 0x00;
  744. Can_TxMsg.stdIDH = Can_ID>>3;
  745. Can_TxMsg.stdIDL = Can_ID<<5;
  746. Can_TxMsg.DLC = 8;
  747. HAL_Can_Transmit(Can_TxMsg);
  748. break;
  749. }
  750. default:
  751. {
  752. Can_Flag=true;
  753. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE);
  754. break;
  755. }
  756. }
  757. send_index ++;
  758. }
  759. break;
  760. }
  761. case PROCESS_CAN_STATE_SLEEP:
  762. {
  763. slpManPlatVoteEnableSleep(CanSlpHandler, SLP_SLP2_STATE);
  764. while(1)
  765. {
  766. osDelay(30000/portTICK_PERIOD_MS);
  767. }
  768. }
  769. }
  770. }
  771. }
  772. static void sendQueueMsg(UINT32 msgId, UINT32 xTickstoWait)
  773. {
  774. eventCallbackMessage_t *queueMsg = NULL;
  775. queueMsg = malloc(sizeof(eventCallbackMessage_t));
  776. queueMsg->messageId = msgId;
  777. if (psEventQueueHandle)
  778. {
  779. if (pdTRUE != xQueueSend(psEventQueueHandle, &queueMsg, xTickstoWait))
  780. {
  781. ECOMM_TRACE(UNILOG_PLA_APP, sendQueueMsg_1, P_ERROR, 0, "xQueueSend error");
  782. }
  783. }
  784. }
  785. static INT32 socketRegisterPSUrcCallback(urcID_t eventID, void *param, UINT32 paramLen)
  786. {
  787. CmiSimImsiStr *imsi = NULL;
  788. CmiPsCeregInd *cereg = NULL;
  789. UINT8 rssi = 0, index = 0;
  790. NmAtiNetifInfo *netif = NULL;
  791. switch(eventID)
  792. {
  793. case NB_URC_ID_SIM_READY:
  794. {
  795. imsi = (CmiSimImsiStr *)param;
  796. memcpy(gImsi, imsi->contents, imsi->length);
  797. #ifdef USING_PRINTF
  798. printf("SIM ready(imsi=%s)\n",(UINT8 *)imsi->contents);
  799. #endif
  800. break;
  801. }
  802. case NB_URC_ID_MM_SIGQ:
  803. {
  804. rssi = *(UINT8 *)param;
  805. #ifdef USING_PRINTF
  806. printf("RSSI signal=%d\n",rssi);
  807. #endif
  808. ECOMM_TRACE(UNILOG_PLA_APP, socketRegisterPSUrcCallback_1, P_INFO, 1, "RSSI signal=%d", rssi);
  809. break;
  810. }
  811. case NB_URC_ID_PS_BEARER_ACTED:
  812. {
  813. #ifdef USING_PRINTF
  814. printf("Default bearer activated\n");
  815. #endif
  816. break;
  817. }
  818. case NB_URC_ID_PS_BEARER_DEACTED:
  819. {
  820. #ifdef USING_PRINTF
  821. printf("Default bearer Deactivated\n");
  822. #endif
  823. break;
  824. }
  825. case NB_URC_ID_PS_CEREG_CHANGED:
  826. {
  827. cereg = (CmiPsCeregInd *)param;
  828. gCellID = cereg->celId;
  829. #ifdef USING_PRINTF
  830. printf("URCCallBack:CEREG changed act:%d celId:%d locPresent:%d tac:%d\n", cereg->act, cereg->celId, cereg->locPresent, cereg->tac);
  831. #endif
  832. break;
  833. }
  834. case NB_URC_ID_PS_NETINFO:
  835. {
  836. netif = (NmAtiNetifInfo *)param;
  837. if (netif->netStatus == NM_NETIF_ACTIVATED)
  838. sendQueueMsg(QMSG_ID_NW_IP_READY, 0);
  839. break;
  840. }
  841. }
  842. return 0;
  843. }
  844. uint8_t bcc_chk(uint8_t* data, uint8_t length)//发送bcc校验函数
  845. {
  846. uint8_t bcc_chk_return = 0x00;
  847. uint8_t count = 0;
  848. while (count<length)
  849. {
  850. bcc_chk_return^=data[count];
  851. count++;
  852. }
  853. return bcc_chk_return;
  854. }
  855. //电池数据组装函数
  856. void Tcp_Batt_Data_Assemble(void)
  857. {
  858. int16_t Batt_current;
  859. uint8_t csq=0;
  860. int8_t snr=0;
  861. int8_t rsnr=0;
  862. Batt_current = Uart_Rece_BattI;
  863. OsaUtcTimeTValue timestracture;
  864. appGetSystemTimeUtcSync(&timestracture);
  865. battbuffer[0] = timestracture.UTCtimer1>>16;
  866. battbuffer[0] = battbuffer[0] - 0x07D0;
  867. battbuffer[1] = timestracture.UTCtimer1>>8;
  868. battbuffer[2] = timestracture.UTCtimer1;
  869. battbuffer[3] = timestracture.UTCtimer2>>24;
  870. battbuffer[4] = timestracture.UTCtimer2>>16;
  871. battbuffer[5] = timestracture.UTCtimer2>>8;
  872. battbuffer[6] = 0x80;//信息体标志,此处为电池信息
  873. battbuffer[7] = battbuffer[0];//年
  874. battbuffer[8] = battbuffer[1];//月
  875. battbuffer[9] = battbuffer[2];//日
  876. battbuffer[10] = battbuffer[3];//时 0时区时间
  877. battbuffer[11] = battbuffer[4];//分
  878. battbuffer[12] = battbuffer[5];//秒
  879. appGetSignalInfoSync(&csq,&snr,&rsnr);//获取信号质量
  880. battbuffer[13] = csq;// 网络信号
  881. battbuffer[14] = 0x00;//故障等级
  882. battbuffer[15] = 0x00;//故障代码高
  883. battbuffer[16] = 0x00;//故障代码低
  884. //电流适应性更改,从int转换到uint,加10000的偏移量,100mA的单位
  885. if (Batt_current>0x8000)
  886. {
  887. Batt_current = Batt_current|0x7fff;
  888. Batt_current = 0x2710 - Batt_current;
  889. Batt_current = Batt_current;
  890. }
  891. else
  892. {
  893. Batt_current = Batt_current+0x2710;
  894. Batt_current = Batt_current;
  895. }
  896. battbuffer[17] = Batt_current>>8;
  897. battbuffer[18] = Batt_current;
  898. data_index = 32+Batt_Cell_Num_2+Batt_Temp_Num;
  899. battbuffer[data_index] = 0x00;//电池状态
  900. data_index++;
  901. battbuffer[data_index] = 0x00;//是否加热
  902. data_index++;
  903. }
  904. //Tcp线程
  905. static void Tcp_Task(void* arg)
  906. {
  907. CHAR SN[] = "GYTEST00000000003";//SN应写到osfile里面
  908. CHAR serverip[] = "47.97.127.222";
  909. CHAR serverport[] = "8712";
  910. struct addrinfo hints, *server_res;
  911. int TcpsendID = -1;
  912. int TcpconnectID = -1;
  913. memset( &hints, 0, sizeof( hints ) );
  914. hints.ai_socktype = SOCK_STREAM;
  915. hints.ai_protocol = IPPROTO_TCP;
  916. psEventQueueHandle = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(eventCallbackMessage_t*));
  917. int NB_send_len=59+Batt_Cell_Num_2+Batt_Temp_Num;//设定tcp发送的最大数值,以电池数据为上线
  918. slpManApplyPlatVoteHandle("TcpSlp",&TcpSlpHandler);
  919. slpManPlatVoteDisableSleep(TcpSlpHandler, SLP_SLP2_STATE);
  920. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
  921. uint8_t Tcp_Index = 0;
  922. uint8_t* TcpSendBuffer=NULL;
  923. volatile bool Tcp_Flag=false;
  924. eventCallbackMessage_t *queueItem = NULL;
  925. while(true)
  926. {
  927. switch(gProcess_Tcp_Task)
  928. {
  929. case PROCESS_TCP_STATE_IDLE:
  930. {
  931. osDelay(100);
  932. break;
  933. }
  934. case PROCESS_TCP_STATE_LINK:
  935. {
  936. if(TcpconnectID<0)
  937. {
  938. if (xQueueReceive(psEventQueueHandle, &queueItem, portMAX_DELAY))
  939. {
  940. switch(queueItem->messageId)
  941. {
  942. case QMSG_ID_NW_IP_READY:
  943. {
  944. if (getaddrinfo( serverip, serverport , &hints, &server_res ) != 0 )
  945. {
  946. #ifdef USING_PRINTF
  947. printf("TCP connect unresolved dns\n");
  948. #endif
  949. }
  950. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  951. TcpconnectID = connect(sockfd, (struct sockaddr *) server_res->ai_addr, server_res->ai_addrlen);
  952. if(TcpconnectID<0)
  953. {
  954. #ifdef USING_PRINTF
  955. printf("Connect failed!%d\n",TcpconnectID);
  956. #endif
  957. close(sockfd);
  958. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
  959. break;
  960. }
  961. else
  962. {
  963. sendQueueMsg(QMSG_ID_SOCK_SENDPKG, 0);
  964. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK);
  965. }
  966. break;
  967. }
  968. }
  969. free(queueItem);
  970. }
  971. }
  972. else
  973. {
  974. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK);
  975. }
  976. break;
  977. }
  978. case PROCESS_TCP_STATE_WORK:
  979. {
  980. Tcp_Flag = false;
  981. TcpSendBuffer = (uint8_t *)malloc(NB_send_len);
  982. while (!Tcp_Flag)
  983. {
  984. switch(Tcp_Index)
  985. {
  986. case 0://发送登录信息
  987. {
  988. Tcp_Index=1;
  989. break;
  990. }
  991. case 1://发送电池数据
  992. {
  993. memset(TcpSendBuffer,0x00,NB_send_len);
  994. *(TcpSendBuffer+0) = 0x23;
  995. *(TcpSendBuffer+1) = 0x23;
  996. *(TcpSendBuffer+2) = 0x02;
  997. *(TcpSendBuffer+3) = 0xfe;
  998. memcpy(TcpSendBuffer+4,SN,17);
  999. *(TcpSendBuffer+21) = 0x01;//01表示不加密
  1000. Tcp_Batt_Data_Assemble();//数据组装函数
  1001. *(TcpSendBuffer+22) = data_index>>8;//数据长度
  1002. *(TcpSendBuffer+23) = data_index;//数据长度
  1003. memcpy(TcpSendBuffer+24,battbuffer,data_index);
  1004. *(TcpSendBuffer+NB_send_len-1) = bcc_chk(TcpSendBuffer,NB_send_len-1);
  1005. #ifdef USING_PRINTF
  1006. printf("battbuffer:");
  1007. for (int i = 0; i < data_index; i++)
  1008. {
  1009. printf("%x ",battbuffer[i]);
  1010. }
  1011. printf("\n ");
  1012. #endif
  1013. TcpsendID = send(sockfd, TcpSendBuffer, NB_send_len, 0 );
  1014. //发送失败暂时不写
  1015. #ifdef USING_PRINTF
  1016. printf("ConnectID:%d,TcpSend:%d,data length:%d,Data: ",sockfd,TcpsendID,NB_send_len);
  1017. for (int i = 0; i < NB_send_len; i++)
  1018. {
  1019. printf("%x ",*(TcpSendBuffer+i));
  1020. }
  1021. printf("\n ");
  1022. #endif
  1023. Tcp_Index=2;
  1024. break;
  1025. }
  1026. case 2:
  1027. {
  1028. Tcp_Index=3;
  1029. break;
  1030. }
  1031. default:
  1032. {
  1033. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
  1034. Tcp_Index = 0;
  1035. Tcp_Flag = true;
  1036. }
  1037. }
  1038. }
  1039. free(TcpSendBuffer);
  1040. break;
  1041. }
  1042. case PROCESS_TCP_STATE_SLEEP:
  1043. {
  1044. slpManPlatVoteEnableSleep(TcpSlpHandler, SLP_SLP2_STATE);
  1045. closesocket(sockfd);
  1046. deregisterPSEventCallback(socketRegisterPSUrcCallback);//注销NB网络事件函数
  1047. while(1)
  1048. {
  1049. osDelay(30000/portTICK_PERIOD_MS);
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }
  1055. //主任务线程初始化
  1056. void Main_Task_Init()
  1057. {
  1058. #ifndef USING_PRINTF
  1059. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  1060. {
  1061. HAL_UART_RecvFlowControl(false);
  1062. }
  1063. #endif
  1064. osThreadAttr_t task_attr;
  1065. memset(&task_attr,0,sizeof(task_attr));
  1066. memset(gProcess_Main_TaskStack, 0xA5, PROC_MAIN_TASK_STACK_SIZE);
  1067. task_attr.name = "Main_Task";
  1068. task_attr.stack_mem = gProcess_Main_TaskStack;
  1069. task_attr.stack_size = PROC_MAIN_TASK_STACK_SIZE;
  1070. task_attr.priority = osPriorityNormal;
  1071. task_attr.cb_mem = &gProcess_Main_Task_t;
  1072. task_attr.cb_size = sizeof(StaticTask_t);
  1073. osThreadNew(Main_Task, NULL, &task_attr);
  1074. }
  1075. //Uart读取线程初始化
  1076. void Uart_Task_Init()
  1077. {
  1078. osThreadAttr_t task_attr;
  1079. memset(&task_attr,0,sizeof(task_attr));
  1080. memset(gProcess_Uart_TaskStack, 0xA5, PROC_UART_TASK_STACK_SIZE);
  1081. task_attr.name = "Uart_Task";
  1082. task_attr.stack_mem = gProcess_Uart_TaskStack;
  1083. task_attr.stack_size = PROC_UART_TASK_STACK_SIZE;
  1084. task_attr.priority = osPriorityNormal;
  1085. task_attr.cb_mem = &gProcess_Uart_Task_t;
  1086. task_attr.cb_size = sizeof(StaticTask_t);
  1087. osThreadNew(Uart_Task, NULL, &task_attr);
  1088. }
  1089. //Can线程初始化
  1090. void Can_Task_Init()
  1091. {
  1092. osThreadAttr_t task_attr;
  1093. memset(&task_attr,0,sizeof(task_attr));
  1094. memset(gProcess_Can_TaskStack, 0xA5, PROC_CAN_TASK_STACK_SIZE);
  1095. task_attr.name = "Can_Task";
  1096. task_attr.stack_mem = gProcess_Can_TaskStack;
  1097. task_attr.stack_size = PROC_CAN_TASK_STACK_SIZE;
  1098. task_attr.priority = osPriorityNormal;
  1099. task_attr.cb_mem = &gProcess_Can_Task_t;
  1100. task_attr.cb_size = sizeof(StaticTask_t);
  1101. osThreadNew(Can_Task, NULL, &task_attr);
  1102. }
  1103. //Tcp线程初始化
  1104. void Tcp_Task_Init()
  1105. {
  1106. osThreadAttr_t task_attr;
  1107. registerPSEventCallback(NB_GROUP_ALL_MASK, socketRegisterPSUrcCallback);
  1108. memset(&task_attr,0,sizeof(task_attr));
  1109. memset(gProcess_Tcp_TaskStack, 0xA5, PROC_TCP_TASK_STACK_SIZE);
  1110. task_attr.name = "Tcp_Task";
  1111. task_attr.stack_mem = gProcess_Tcp_TaskStack;
  1112. task_attr.stack_size = PROC_TCP_TASK_STACK_SIZE;
  1113. task_attr.priority = osPriorityNormal;
  1114. task_attr.cb_mem = &gProcess_Tcp_Task_t;
  1115. task_attr.cb_size = sizeof(StaticTask_t);
  1116. osThreadNew(Tcp_Task, NULL, &task_attr);
  1117. }
  1118. void appInit(void *arg)
  1119. {
  1120. Main_Task_Init();
  1121. Uart_Task_Init();
  1122. Can_Task_Init();
  1123. Tcp_Task_Init();
  1124. }
  1125. //主函数入口
  1126. void main_entry(void) {
  1127. BSP_CommonInit();
  1128. osKernelInitialize();
  1129. registerAppEntry(appInit, NULL);
  1130. if (osKernelGetState() == osKernelReady)
  1131. {
  1132. osKernelStart();
  1133. }
  1134. while(1);
  1135. }