app.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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*2)
  26. #define PROC_UART_TASK_STACK_SIZE (1024)
  27. #define PROC_CAN_TASK_STACK_SIZE (1024)
  28. #define PROC_TCP_TASK_STACK_SIZE (2048)
  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 battbuffer_len (110)
  38. #define RTE_UART_RX_IO_MODE RTE_UART1_RX_IO_MODE
  39. extern ARM_DRIVER_USART Driver_USART1;
  40. static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
  41. volatile bool isRecvTimeout = false;
  42. volatile bool isRecvComplete = false;
  43. QueueHandle_t gpsMsgQueue = NULL;
  44. GPS_INFO Gps_Data;
  45. static volatile UINT32 Event;
  46. static QueueHandle_t psEventQueueHandle;
  47. static UINT8 gImsi[16] = {0};
  48. static INT32 sockfd = -1;
  49. static UINT32 gCellID = 0;
  50. int TcpconnectID = -1;
  51. volatile uint8_t Tcp_statuts = 0;
  52. //slpManTimerID_e Timer_ID;
  53. //#define BAT6060
  54. #define BAT4830
  55. #ifdef BAT4830
  56. uint8_t Batt_Cell_Num = 14;
  57. uint8_t Batt_Temp_Num = 5;
  58. #else
  59. uint8_t Batt_Cell_Num = 17;
  60. uint8_t Batt_Temp_Num = 7;
  61. #endif
  62. CHAR SN[] = "GY483000000000001";//SN应写到osfile里面
  63. uint8_t Batt_Cell_Num_2 ;//默认数值
  64. int16_t Uart_Rece_BattI=0x0000;
  65. uint8_t Can_Rece_buffer[8];
  66. volatile bool Can_Enable = false;
  67. uint16_t data_index = 0x0000;
  68. volatile bool Sleep_Flag = false;
  69. volatile bool Uart_Sleep_Flag = false;
  70. volatile bool Can_Sleep_Flag = false;
  71. volatile bool Tcp_Sleep_Flag = false;
  72. uint8_t battbuffer[battbuffer_len];//电池数据都存在此数组中————电压14,温度5
  73. /**
  74. * 存放规则如下:
  75. * 位置: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  76. * 数据: 年 月 日 时 分 秒 信息体标志 年 月 日 时 分 秒 网络信号 故障等级 故障代码高 故障代码低
  77. *
  78. * 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
  79. * 电流H 电流L Link电压H Link电压L Pack电压H Pack电压L 开关状态 SOC SOH 均衡状态 单体个数X 单体v1...单体vX 温度个数N 温度1..温度N
  80. * 32+X*2+N 33+X*2+N 34+x*2+N 35 +X*2+N 36+X*2 +N 37+X*2+N
  81. * 电池状态 是否加热 最高单体H 最高单体L 最低单体H 最低单体L
  82. * */
  83. //GPS信息
  84. uint8_t gpsbuffer[30];//定位都存在此数组中
  85. /**
  86. * 存放规则如下:
  87. * 位置: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  88. * 数据: 年 月 日 时 分 秒 信息体标志 年 月 日 时 分 秒 定位标志 卫星数目 航向H 航向L
  89. *
  90. * 17 18 19 20 21 22 23 24 25 26 27 28
  91. * 速度H 速度L 海拔H 海拔L 纬度HH 纬度HL 纬度LH 纬度LL 经度HH 经度HL 经度LH 经度LL
  92. *
  93. * */
  94. //GPS信息
  95. //状态机定义
  96. typedef enum
  97. {
  98. PROCESS_STATE_IDLE = 0,
  99. PROCESS_STATE_WORK,
  100. PROCESS_STATE_SLEEP
  101. }process_Main;
  102. static process_Main gProcess_Main_Task = PROCESS_STATE_IDLE;
  103. #define PROC_MAIN_STATE_SWITCH(a) (gProcess_Main_Task = a)
  104. typedef enum
  105. {
  106. PROCESS_UART_STATE_IDLE = 0,
  107. PROCESS_UART_STATE_CHECK,
  108. PROCESS_UART_STATE_WORK,
  109. PROCESS_UART_STATE_SLEEP
  110. }process_Uart;
  111. static process_Uart gProcess_Uart_Task = PROCESS_UART_STATE_IDLE;
  112. #define PROC_UART_STATE_SWITCH(a) (gProcess_Uart_Task = a)
  113. typedef enum
  114. {
  115. PROCESS_CAN_STATE_IDLE = 0,
  116. PROCESS_CAN_STATE_RECV,
  117. PROCESS_CAN_STATE_SEND,
  118. PROCESS_CAN_STATE_SLEEP
  119. }process_CAN;
  120. static process_CAN gProcess_Can_Task = PROCESS_CAN_STATE_IDLE;
  121. #define PROC_CAN_STATE_SWITCH(a) (gProcess_Can_Task = a)
  122. typedef enum
  123. {
  124. PROCESS_TCP_STATE_IDLE = 0,
  125. PROCESS_TCP_STATE_LINK,
  126. PROCESS_TCP_STATE_WORK,
  127. PROCESS_TCP_STATE_SLEEP
  128. }process_TCP;
  129. static process_TCP gProcess_Tcp_Task = PROCESS_TCP_STATE_IDLE;
  130. #define PROC_TCP_STATE_SWITCH(a) (gProcess_Tcp_Task = a)
  131. //堆栈申请
  132. static StaticTask_t gProcess_Main_Task_t;
  133. static UINT8 gProcess_Main_TaskStack[PROC_MAIN_TASK_STACK_SIZE];
  134. static StaticTask_t gProcess_Uart_Task_t;
  135. static UINT8 gProcess_Uart_TaskStack[PROC_UART_TASK_STACK_SIZE];
  136. static StaticTask_t gProcess_Can_Task_t;
  137. static UINT8 gProcess_Can_TaskStack[PROC_CAN_TASK_STACK_SIZE];
  138. static StaticTask_t gProcess_Tcp_Task_t;
  139. static UINT8 gProcess_Tcp_TaskStack[PROC_TCP_TASK_STACK_SIZE];
  140. #ifdef BL_FILE_LOG
  141. uint8_t lockoutState=0;
  142. #endif
  143. //睡眠进出函数
  144. static void appBeforeHib(void *pdata, slpManLpState state)
  145. {
  146. uint32_t *p_param = (uint32_t *)pdata;
  147. #ifdef USING_PRINTF
  148. printf("Before Hibernate:%d \n",state);
  149. #endif
  150. slpManAONIOLatchEn(AonIOLatch_Enable);
  151. }
  152. static void appAfterHib(void *pdata, slpManLpState state)
  153. {
  154. #ifdef USING_PRINTF
  155. printf("Try Hibernate Failed:%d \n",state);
  156. #endif
  157. }
  158. static void appBeforeSlp1(void *pdata, slpManLpState state)
  159. {
  160. #ifdef USING_PRINTF
  161. printf("Before Sleep1:%d \n",state);
  162. #endif
  163. slpManAONIOLatchEn(AonIOLatch_Enable);
  164. }
  165. static void appAfterSlp1(void *pdata, slpManLpState state)
  166. {
  167. #ifdef USING_PRINTF
  168. printf("After Sleep1:%d \n",state);
  169. #endif
  170. }
  171. static void appBeforeSlp2(void *pdata, slpManLpState state)
  172. {
  173. #ifdef USING_PRINTF
  174. printf("before sleep2:%d \n",state);
  175. #endif
  176. slpManAONIOLatchEn(AonIOLatch_Enable);
  177. }
  178. static void appAfterSlp2(void *pdata, slpManLpState state)
  179. {
  180. #ifdef USING_PRINTF
  181. printf("sleep2 failed:%d \n",state);
  182. #endif
  183. }
  184. // 主任务线程
  185. static void Main_Task(void* arg)
  186. {
  187. UINT16 Can_index = 0;
  188. UINT16 Uart_index = 0;
  189. UINT16 Tcp_index = 0;
  190. uint32_t sleep_index = 0;
  191. uint32_t sleep_time_flag;
  192. int32_t inParam = 0xAABBCCDD;
  193. uint32_t Chrgend_Work_time = 180000;
  194. uint32_t Wakeup_Work_time = 5*60*100;
  195. uint32_t Sleep_Time = 5*60*1000;
  196. //上述参数应写入文件里
  197. slpManWakeSrc_e Wakeup_source;
  198. PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
  199. NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
  200. NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
  201. NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
  202. NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
  203. FaultDisplay(LED_TURN_OFF);
  204. NVIC_EnableIRQ(PadWakeup1_IRQn);
  205. slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
  206. slpManApplyPlatVoteHandle("MainSlp",&MainSlpHandler);
  207. slpManPlatVoteDisableSleep(MainSlpHandler, SLP_SLP2_STATE);
  208. slpManRegisterUsrdefinedBackupCb(appBeforeHib,&inParam,SLPMAN_HIBERNATE_STATE);
  209. slpManRegisterUsrdefinedRestoreCb(appAfterHib,NULL,SLPMAN_HIBERNATE_STATE);
  210. slpManRegisterUsrdefinedBackupCb(appBeforeSlp1,NULL,SLPMAN_SLEEP1_STATE);
  211. slpManRegisterUsrdefinedRestoreCb(appAfterSlp1,NULL,SLPMAN_SLEEP1_STATE);
  212. slpManRegisterUsrdefinedBackupCb(appBeforeSlp2,NULL,SLPMAN_SLEEP2_STATE);
  213. slpManRegisterUsrdefinedRestoreCb(appAfterSlp2,NULL,SLPMAN_SLEEP2_STATE);
  214. #ifdef BL_FILE_LOG
  215. if(lockoutState)
  216. bluejoy_del_logfile();
  217. bluejoy_read_logfile();
  218. bluejoy_printf(BL_LEVEL3,"processAppTask\n");
  219. #endif
  220. slpManSlpState_t slpstate = slpManGetLastSlpState();
  221. #ifdef USING_PRINTF
  222. printf("slpstate:%d \n",slpstate);
  223. #endif
  224. #ifdef BL_FILE_LOG
  225. bluejoy_printf(BL_LEVEL3,"Last slpstate:%d \n",slpstate);
  226. #endif
  227. Wakeup_source = slpManGetWakeupSrc();//获取唤醒源
  228. #ifdef USING_PRINTF
  229. printf("Wakeup_source:%d \n",Wakeup_source);
  230. #endif
  231. #ifdef BL_FILE_LOG
  232. bluejoy_printf(BL_LEVEL3,"Wakeup_source:%d \n",Wakeup_source);
  233. #endif
  234. if (Wakeup_source==0)
  235. {
  236. sleep_time_flag = Wakeup_Work_time;
  237. }
  238. else if (Wakeup_source==1)
  239. {
  240. sleep_time_flag = Wakeup_Work_time;
  241. }
  242. else
  243. {
  244. sleep_time_flag = Chrgend_Work_time;
  245. }
  246. if((slpstate == SLP_SLP2_STATE) || (slpstate == SLP_HIB_STATE))
  247. {
  248. PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK);
  249. }
  250. else
  251. {
  252. PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK);
  253. }
  254. // if (Timer_ID!=7)
  255. // {
  256. // PROC_MAIN_STATE_SWITCH(PROCESS_STATE_SLEEP);
  257. // }
  258. // else
  259. // {
  260. // PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
  261. // }
  262. //线程初始化完成
  263. while (true)
  264. {
  265. switch(gProcess_Main_Task)
  266. {
  267. case PROCESS_STATE_IDLE:
  268. {
  269. // 暂时不区分唤醒
  270. PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK);
  271. break;
  272. }
  273. case PROCESS_STATE_WORK:
  274. {
  275. osDelay(10/portTICK_PERIOD_MS);
  276. Can_index++;
  277. Uart_index++;
  278. Tcp_index++;
  279. if (Uart_index >100)//Uart 1s 调用一次
  280. {
  281. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WORK);
  282. Uart_index = 0;
  283. }
  284. if (Can_index >=100)//Can 1s 调用一次
  285. {
  286. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_RECV);
  287. Can_index = 0;
  288. }
  289. if (Tcp_index >=500)//Tcp 5s 调用一次
  290. {
  291. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_LINK);
  292. Tcp_index = 0;
  293. }
  294. if((!Can_Enable)&&((Uart_Rece_BattI==0x0000)||(Uart_Rece_BattI==0xffff)))
  295. {
  296. sleep_index++;
  297. }
  298. else
  299. {
  300. sleep_index = 0;
  301. }
  302. if ((Uart_Rece_BattI!=0x0000)&&(Uart_Rece_BattI<=0x8000))
  303. {
  304. sleep_time_flag = Chrgend_Work_time;
  305. }
  306. if (sleep_index >=sleep_time_flag)
  307. {
  308. PROC_MAIN_STATE_SWITCH(PROCESS_STATE_SLEEP);
  309. sleep_index = 0;
  310. }
  311. break;
  312. }
  313. case PROCESS_STATE_SLEEP:
  314. {
  315. while(true)
  316. {
  317. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
  318. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SLEEP);
  319. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_SLEEP);
  320. Sleep_Flag = TRUE;
  321. if((Uart_Sleep_Flag)&&(Can_Sleep_Flag)&&(Tcp_Sleep_Flag))
  322. break;
  323. }
  324. #ifdef BL_FILE_LOG
  325. bluejoy_printf(BL_LEVEL3,"sleep\n");
  326. #endif
  327. osDelay(10000);
  328. #ifdef USING_PRINTF
  329. printf("Ms_sleep:uart_%d,can_%d,tcp_%d\r\n",gProcess_Uart_Task,gProcess_Can_Task,gProcess_Tcp_Task);
  330. #endif
  331. slpManSlpState_t State;
  332. uint8_t cnt;
  333. if(slpManCheckVoteState(MainSlpHandler, &State, &cnt)==RET_TRUE)
  334. {
  335. #ifdef USING_PRINTF
  336. printf("[%d]We Can Check Vote Main State, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
  337. #endif
  338. }
  339. slpManPlatVoteForceEnableSleep(MainSlpHandler, SLP_SLP2_STATE); //增加强制投票
  340. if(slpManCheckVoteState(MainSlpHandler, &State, &cnt)==RET_TRUE)
  341. {
  342. #ifdef USING_PRINTF
  343. printf("[%d]We Can Check Vote Main State Again, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
  344. #endif
  345. }
  346. #ifdef USING_PRINTF
  347. printf("[%d]Waiting sleep\r\n",__LINE__);
  348. #endif
  349. FaultDisplay(LED_TURN_ON);
  350. slpManSlpState_t slpstate = slpManPlatGetSlpState();
  351. #ifdef USING_PRINTF
  352. printf("which slpstate can go now :%d \n",slpstate);
  353. #endif
  354. slpManDeepSlpTimerStart(deepslpTimerID, Sleep_Time);
  355. while(1)
  356. {
  357. if(!slpManDeepSlpTimerIsRunning(deepslpTimerID))
  358. {
  359. slpManDeepSlpTimerDel(deepslpTimerID);
  360. }
  361. slpManDeepSlpTimerStart(deepslpTimerID, Sleep_Time);
  362. osDelay(60000/portTICK_PERIOD_MS);
  363. }
  364. break;
  365. }
  366. }
  367. }
  368. }
  369. //Uart校验程序
  370. unsigned int crc_chk(uint8_t* data, uint8_t length)
  371. {
  372. int j;
  373. uint16_t reg_crc=0xFFFF;
  374. while(length--)
  375. {
  376. reg_crc ^= *data++;
  377. for(j=0;j<8;j++)
  378. {
  379. if(reg_crc & 0x01)
  380. {
  381. reg_crc=(reg_crc>>1) ^ 0xA001;
  382. }
  383. else
  384. {
  385. reg_crc=reg_crc >>1;
  386. }
  387. }
  388. }
  389. return reg_crc;
  390. }
  391. //Uart回调程序
  392. void USART_callback(uint32_t event)
  393. {
  394. if(event & ARM_USART_EVENT_RX_TIMEOUT)
  395. {
  396. isRecvTimeout = true;
  397. }
  398. if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
  399. {
  400. isRecvComplete = true;
  401. }
  402. }
  403. //Uart发送接收函数
  404. uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t* Uart_Rece_buffer,uint8_t Data_Len)
  405. {
  406. uint16_t CRC_Rece_buffer;
  407. uint16_t CRC_chk_buffer;
  408. uint8_t Uart_Send_buffer[8];
  409. Uart_Send_buffer[0] = Uart_Receive_Msg.Bms_Address;
  410. Uart_Send_buffer[1] = Uart_Receive_Msg.Bms_Read_Funcode;
  411. Uart_Send_buffer[2] = Uart_Receive_Msg.Reg_Begin_H;
  412. Uart_Send_buffer[3] = Uart_Receive_Msg.Reg_Begin_L;
  413. Uart_Send_buffer[4] = Uart_Receive_Msg.Reg_Num_H;
  414. Uart_Send_buffer[5] = Uart_Receive_Msg.Reg_Num_L;
  415. CRC_chk_buffer = crc_chk(Uart_Send_buffer,6);
  416. Uart_Send_buffer[6] = CRC_chk_buffer;
  417. Uart_Send_buffer[7] = CRC_chk_buffer>>8;
  418. uint32_t timeout = 0x00000000;
  419. USARTdrv->Send(Uart_Send_buffer,8);
  420. // #ifdef USING_PRINTF
  421. // printf("Uart_Send_buffer: ");
  422. // for(int i=0;i<8;i++)
  423. // {
  424. // printf("%x ",Uart_Send_buffer[i]);
  425. // }
  426. // printf("\n");
  427. // #endif
  428. USARTdrv->Receive(Uart_Rece_buffer,Data_Len);
  429. while((isRecvTimeout == false) && (isRecvComplete == false))
  430. {
  431. timeout++;
  432. osDelay(100);
  433. if (timeout>=10)
  434. {
  435. timeout =0;
  436. isRecvTimeout = true;
  437. break;
  438. }
  439. }
  440. // #ifdef USING_PRINTF
  441. // printf("Uart_Rece_buffer1: ");
  442. // for(int i=0;i<Data_Len;i++)
  443. // {
  444. // printf("%x ",*(Uart_Rece_buffer+i));
  445. // }
  446. // #endif
  447. if (isRecvComplete == true)
  448. {
  449. isRecvComplete = false;
  450. CRC_Rece_buffer =*(Uart_Rece_buffer+Data_Len-1)<<8|*(Uart_Rece_buffer+Data_Len-2);
  451. CRC_chk_buffer = crc_chk(Uart_Rece_buffer,Data_Len-2);
  452. // #ifdef USING_PRINTF
  453. // printf("Uart_Rece_buffer2: ");
  454. // for(int i=0;i<Data_Len;i++)
  455. // {
  456. // printf("%x ",*(Uart_Rece_buffer+i));
  457. // }
  458. // printf("\ncrcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
  459. // #endif
  460. if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
  461. {
  462. return Uart_Rece_buffer;//此处指针移位出现重启问题
  463. }
  464. else //接收数据的校验不过
  465. {
  466. USARTdrv->Uninitialize();
  467. osDelay(1000);
  468. USARTdrv->Initialize(USART_callback);
  469. USARTdrv->PowerControl(ARM_POWER_FULL);
  470. USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
  471. ARM_USART_DATA_BITS_8 |
  472. ARM_USART_PARITY_NONE |
  473. ARM_USART_STOP_BITS_1 |
  474. ARM_USART_FLOW_CONTROL_NONE, 9600);
  475. memset(Uart_Rece_buffer,0xff,Data_Len);
  476. return Uart_Rece_buffer;
  477. }
  478. }
  479. else
  480. {
  481. osDelay(100);
  482. memset(Uart_Rece_buffer,0x00,Data_Len);
  483. isRecvTimeout = false;
  484. return Uart_Rece_buffer;
  485. }
  486. }
  487. void GGACallBack(UINT8 res, UINT8 * params)
  488. {
  489. #ifdef USING_PRINTF
  490. printf("GPS:len=%d,data=%s\r\n",res,params);
  491. #else
  492. ECOMM_STRING(UNILOG_PLA_STRING, GPS_DATA, P_INFO, "gps:%s", params);
  493. #endif
  494. }
  495. //Uart线程
  496. static void Uart_Task(void* arg)
  497. {
  498. USARTdrv->Initialize(USART_callback);
  499. USARTdrv->PowerControl(ARM_POWER_FULL);
  500. USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
  501. ARM_USART_DATA_BITS_8 |
  502. ARM_USART_PARITY_NONE |
  503. ARM_USART_STOP_BITS_1 |
  504. ARM_USART_FLOW_CONTROL_NONE, 9600);
  505. int Rece_index = 0;
  506. volatile uint8_t Data_Len;
  507. Uart_Receive_Type Uart_Receive_Msg;
  508. memset(&battbuffer[0],0x00,battbuffer_len);
  509. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  510. slpManApplyPlatVoteHandle("UARTSLP",&UartSlpHandler);
  511. slpManPlatVoteDisableSleep(UartSlpHandler, SLP_SLP2_STATE);
  512. Uart_Receive_Msg.Bms_Address = 0x01;
  513. Uart_Receive_Msg.Bms_Read_Funcode = 0x03;
  514. uint8_t *Uart_Rece_buffer = NULL;
  515. volatile bool Uart_task = false;
  516. Batt_Cell_Num_2 = Batt_Cell_Num<<1;
  517. while (true)
  518. {
  519. switch (gProcess_Uart_Task)
  520. {
  521. case PROCESS_UART_STATE_IDLE:
  522. {
  523. Rece_index = 0;
  524. break;
  525. }
  526. case PROCESS_UART_STATE_CHECK:
  527. {
  528. Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN);
  529. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  530. Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num;
  531. Uart_Receive_Msg.Reg_Num_H = 0x00;
  532. Uart_Receive_Msg.Reg_Num_L = 0x01;
  533. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  534. memset(Uart_Rece_buffer,0x00,Data_Len);
  535. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  536. Uart_Rece_BattI = *(Uart_Rece_buffer+3+0)<<8 |*(Uart_Rece_buffer+3+1);
  537. #ifdef USING_PRINTF
  538. printf("Check_Current!\n");
  539. #endif
  540. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  541. free(Uart_Rece_buffer);
  542. break;
  543. }
  544. case PROCESS_UART_STATE_WORK:
  545. {
  546. #ifdef USING_PRINTF
  547. //printf("UART_STATE_WORK!\n");
  548. #endif
  549. Uart_task = false;
  550. Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN);
  551. while(!Uart_task)
  552. {
  553. switch (Rece_index)
  554. {
  555. case 0://current
  556. {
  557. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  558. Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num;
  559. Uart_Receive_Msg.Reg_Num_H = 0x00;
  560. Uart_Receive_Msg.Reg_Num_L = 0x01;
  561. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  562. memset(Uart_Rece_buffer,0x00,Data_Len);
  563. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  564. Uart_Rece_BattI = *(Uart_Rece_buffer+3+0)<<8 |*(Uart_Rece_buffer+3+1);
  565. break;
  566. }
  567. case 1://cell votage
  568. {
  569. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  570. Uart_Receive_Msg.Reg_Begin_L = 0x02;
  571. Uart_Receive_Msg.Reg_Num_H = Batt_Cell_Num>>8;
  572. Uart_Receive_Msg.Reg_Num_L = Batt_Cell_Num;
  573. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  574. memset(Uart_Rece_buffer,0x00,Data_Len);
  575. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  576. battbuffer[30] = Batt_Cell_Num;
  577. memcpy(&battbuffer[31],Uart_Rece_buffer+3,Batt_Cell_Num_2);
  578. break;
  579. }
  580. case 2://temprature
  581. {
  582. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  583. Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num;
  584. Uart_Receive_Msg.Reg_Num_H = Batt_Temp_Num>>8;
  585. Uart_Receive_Msg.Reg_Num_L = Batt_Temp_Num;
  586. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  587. memset(Uart_Rece_buffer,0x00,Data_Len);
  588. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  589. battbuffer[31+Batt_Cell_Num_2] = Batt_Temp_Num;
  590. for (int i = 0; i < Batt_Temp_Num; i++)
  591. {
  592. battbuffer[32+Batt_Cell_Num_2+i] = *(Uart_Rece_buffer+3+2*i+1);
  593. }
  594. break;
  595. }
  596. case 3://batt votage
  597. {
  598. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  599. Uart_Receive_Msg.Reg_Begin_L = 0x18+Batt_Cell_Num+Batt_Temp_Num;
  600. Uart_Receive_Msg.Reg_Num_H = 0x00;
  601. Uart_Receive_Msg.Reg_Num_L = 0x01;
  602. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  603. memset(Uart_Rece_buffer,0x00,Data_Len);
  604. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  605. battbuffer[19] = *(Uart_Rece_buffer+3+0);//Link U
  606. battbuffer[20] = *(Uart_Rece_buffer+3+1);
  607. battbuffer[21] = *(Uart_Rece_buffer+3+0);//Pack U
  608. battbuffer[22] = *(Uart_Rece_buffer+3+1);
  609. break;
  610. }
  611. case 4://soc,soh
  612. {
  613. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  614. Uart_Receive_Msg.Reg_Begin_L = 0x09+Batt_Cell_Num+Batt_Temp_Num;
  615. Uart_Receive_Msg.Reg_Num_H = 0x00;
  616. Uart_Receive_Msg.Reg_Num_L = 0x04;
  617. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  618. memset(Uart_Rece_buffer,0x00,Data_Len);
  619. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  620. battbuffer[23] = *(Uart_Rece_buffer+3+0)>>1;//mos状态
  621. battbuffer[24] = *(Uart_Rece_buffer+3+5);//SOC
  622. battbuffer[25] = *(Uart_Rece_buffer+3+7);//SOH
  623. break;
  624. }
  625. case 5://均衡
  626. {
  627. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  628. Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num+Batt_Temp_Num;
  629. Uart_Receive_Msg.Reg_Num_H = 0x00;
  630. Uart_Receive_Msg.Reg_Num_L = 0x02;
  631. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  632. memset(Uart_Rece_buffer,0x00,Data_Len);
  633. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  634. memcpy(&battbuffer[26],Uart_Rece_buffer+3,4);
  635. break;
  636. }
  637. case 6://最高最低温度和加热和充放电MOS控制
  638. {
  639. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  640. Uart_Receive_Msg.Reg_Begin_L = 0x19+Batt_Cell_Num+Batt_Temp_Num;
  641. Uart_Receive_Msg.Reg_Num_H = 0x00;
  642. Uart_Receive_Msg.Reg_Num_L = 0x04;
  643. Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
  644. memset(Uart_Rece_buffer,0x00,Data_Len);
  645. Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
  646. memcpy(&battbuffer[34+Batt_Cell_Num_2+Batt_Temp_Num],Uart_Rece_buffer+3,4);
  647. memcpy(&battbuffer[32+Batt_Cell_Num_2+Batt_Temp_Num],Uart_Rece_buffer+3+6,2);
  648. break;
  649. }
  650. default:
  651. {
  652. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  653. Uart_task = true;
  654. break;
  655. }
  656. }
  657. Rece_index++;
  658. }
  659. free(Uart_Rece_buffer);
  660. break;
  661. }
  662. case PROCESS_UART_STATE_SLEEP:
  663. {
  664. USARTdrv->PowerControl(ARM_POWER_LOW);
  665. slpManPlatVoteEnableSleep(UartSlpHandler, SLP_SLP2_STATE);
  666. #ifdef USING_PRINTF
  667. printf("UART_STATE_SLEEP\n");
  668. #endif
  669. Uart_Sleep_Flag = true;
  670. while(1)
  671. {
  672. osDelay(60000/portTICK_PERIOD_MS);
  673. }
  674. }
  675. }
  676. }
  677. }
  678. //Can-线程任务
  679. static void Can_Task(void* arg)
  680. {
  681. uint32_t Can_ID;
  682. Can_InitType param;
  683. Can_TxMsgType Can_TxMsg;
  684. param.baudrate = CAN_500Kbps;
  685. param.mode = REQOP_NORMAL;
  686. //过滤ID配置
  687. param.TxStdIDH = 0x00;
  688. param.TxStdIDL = 0x00;
  689. param.RxStdIDH[0] = 0x00;
  690. param.RxStdIDL[0] = 0x00;
  691. /*stdid 0000 0000 001x*/
  692. param.RxStdIDH[1] = 0x00;
  693. param.RxStdIDL[1] = 0x20;
  694. /*stdid 0000 0000 010x */
  695. param.RxStdIDH[2] = 0x00;
  696. param.RxStdIDL[2] = 0x40;
  697. /*stdid 0000 0000 011x*/
  698. param.RxStdIDH[3] = 0x00;
  699. param.RxStdIDL[3] =0x60;
  700. /*stdid 0000 0000 100x */
  701. param.RxStdIDH[4] = 0x00;
  702. param.RxStdIDL[4] = 0x80;
  703. /*stdid 0000 0000 101x*/
  704. param.RxStdIDH[5] = 0x00;
  705. param.RxStdIDL[5] =0xa0;
  706. //配置完毕
  707. param.packType = STD_PACK;
  708. HAL_Can_Init(param);
  709. int send_index = 0;
  710. uint16_t Batt_Cell_addU = 0x0000;
  711. slpManApplyPlatVoteHandle("CanSlp",&CanSlpHandler);
  712. slpManPlatVoteDisableSleep(CanSlpHandler, SLP_SLP2_STATE);
  713. volatile bool Can_Flag=false;
  714. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE);
  715. posGGAServiceStart(GGACallBack);
  716. posGGAReset();
  717. while(1)
  718. {
  719. switch(gProcess_Can_Task)
  720. {
  721. case PROCESS_CAN_STATE_IDLE:
  722. {
  723. Can_Rece_buffer[0]=0xff;
  724. send_index = 0;
  725. break;
  726. }
  727. case PROCESS_CAN_STATE_RECV:
  728. {
  729. //加入GPS启动程序
  730. if(gpsMsgQueue == NULL)
  731. {
  732. gpsMsgQueue = osMessageQueueNew(1,sizeof(GPS_INFO), NULL);
  733. }
  734. osMessageQueueGet(gpsMsgQueue, &Gps_Data, 0, 1000);
  735. #ifdef USING_PRINTF
  736. printf("Gpsmsgqueue: %d,%d,%d,%d\r\n",Gps_Data.latitude,Gps_Data.longitude,Gps_Data.speed,Gps_Data.direction);
  737. #endif
  738. //can采用先接收后发送的策略
  739. HAL_Can_Receive(Can_Rece_buffer);
  740. if (Can_Rece_buffer[0]!=0xff)//满足can发送使能
  741. {
  742. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SEND);
  743. Can_Enable = true;
  744. }
  745. else
  746. {
  747. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE);
  748. Can_Enable = false;
  749. }
  750. break;
  751. }
  752. case PROCESS_CAN_STATE_SEND:
  753. {
  754. #ifdef USING_PRINTF
  755. printf("CAN_STATE_SEND!\n");
  756. #endif
  757. Can_Flag=false;
  758. while(!Can_Flag)
  759. {
  760. switch(send_index)
  761. {
  762. case 0:
  763. {
  764. Can_ID = 0x001;
  765. for (int i = 0; i < 8; i++)
  766. {
  767. Can_TxMsg.Data[i] = battbuffer[i+31+0*8];
  768. }
  769. Can_TxMsg.stdIDH = Can_ID>>3;
  770. Can_TxMsg.stdIDL = Can_ID<<5;
  771. Can_TxMsg.DLC = 8;
  772. HAL_Can_Transmit(Can_TxMsg);
  773. break;
  774. }
  775. case 1:
  776. {
  777. Can_ID = 0x011;
  778. for (int i = 0; i < 8; i++)
  779. {
  780. Can_TxMsg.Data[i] = battbuffer[i+31+1*8];
  781. }
  782. Can_TxMsg.stdIDH = Can_ID>>3;
  783. Can_TxMsg.stdIDL = Can_ID<<5;
  784. Can_TxMsg.DLC = 8;
  785. HAL_Can_Transmit(Can_TxMsg);
  786. break;
  787. }
  788. case 2:
  789. {
  790. Can_ID = 0x020;
  791. for (int i = 0; i < 8; i++)
  792. {
  793. Can_TxMsg.Data[i] = battbuffer[i+31+2*8];
  794. }
  795. Can_TxMsg.stdIDH = Can_ID>>3;
  796. Can_TxMsg.stdIDL = Can_ID<<5;
  797. Can_TxMsg.DLC = 8;
  798. HAL_Can_Transmit(Can_TxMsg);
  799. break;
  800. }
  801. case 3:
  802. {
  803. #ifdef BAT4830
  804. Can_ID = 0x031;
  805. for (int i = 0; i < 4; i++)
  806. {
  807. Can_TxMsg.Data[i] = battbuffer[i+31+3*8];
  808. }
  809. Can_TxMsg.stdIDH = Can_ID>>3;
  810. Can_TxMsg.stdIDL = Can_ID<<5;
  811. Can_TxMsg.DLC = 8;
  812. HAL_Can_Transmit(Can_TxMsg);
  813. #else
  814. Can_ID = 0x031;
  815. for (int i = 0; i < 8; i++)
  816. {
  817. Can_TxMsg.Data[i] = battbuffer[i+31+3*8];
  818. }
  819. Can_TxMsg.stdIDH = Can_ID>>3;
  820. Can_TxMsg.stdIDL = Can_ID<<5;
  821. Can_TxMsg.DLC = 8;
  822. HAL_Can_Transmit(Can_TxMsg);
  823. Can_ID = 0x041;
  824. for (int i = 0; i < 2; i++)
  825. {
  826. Can_TxMsg.Data[i] = battbuffer[i+31+4*8];
  827. }
  828. for (int i = 0; i < 6; i++)
  829. {
  830. Can_TxMsg.Data[i+2] = 0x00;
  831. }
  832. Can_TxMsg.stdIDH = Can_ID>>3;
  833. Can_TxMsg.stdIDL = Can_ID<<5;
  834. Can_TxMsg.DLC = 8;
  835. HAL_Can_Transmit(Can_TxMsg);
  836. #endif
  837. break;
  838. }
  839. case 4:
  840. {
  841. Can_ID = 0x101;
  842. for (int i = 0; i < Batt_Temp_Num; i++)
  843. {
  844. Can_TxMsg.Data[i] = battbuffer[i+32+Batt_Cell_Num_2];
  845. }
  846. Can_TxMsg.stdIDH = Can_ID>>3;
  847. Can_TxMsg.stdIDL = Can_ID<<5;
  848. Can_TxMsg.DLC = 8;
  849. HAL_Can_Transmit(Can_TxMsg);
  850. break;
  851. }
  852. case 5:
  853. {
  854. Can_ID = 0x200;
  855. Batt_Cell_addU = 0x0000;
  856. for (int i = 0; i < Batt_Cell_Num; i++)
  857. {
  858. Batt_Cell_addU = Batt_Cell_addU + (battbuffer[31+i*2]<<8|battbuffer[31+i*2+1])/10;
  859. }
  860. Can_TxMsg.Data[0] = battbuffer[19];
  861. Can_TxMsg.Data[1] = battbuffer[20];
  862. Can_TxMsg.Data[2] = battbuffer[21];
  863. Can_TxMsg.Data[3] = battbuffer[22];//外电压
  864. Can_TxMsg.Data[4] = Batt_Cell_addU>>8;
  865. Can_TxMsg.Data[5] = Batt_Cell_addU;//累加电压
  866. Can_TxMsg.Data[6] = Uart_Rece_BattI>>8;
  867. Can_TxMsg.Data[7] = Uart_Rece_BattI;
  868. Can_TxMsg.stdIDH = Can_ID>>3;
  869. Can_TxMsg.stdIDL = Can_ID<<5;
  870. Can_TxMsg.DLC = 8;
  871. HAL_Can_Transmit(Can_TxMsg);
  872. break;
  873. }
  874. case 6:
  875. {
  876. Can_ID = 0x211;
  877. Can_TxMsg.Data[0] = battbuffer[34+Batt_Cell_Num_2+Batt_Temp_Num];
  878. Can_TxMsg.Data[1] = battbuffer[35+Batt_Cell_Num_2+Batt_Temp_Num];
  879. Can_TxMsg.Data[2] = battbuffer[36+Batt_Cell_Num_2+Batt_Temp_Num];
  880. Can_TxMsg.Data[3] = battbuffer[37+Batt_Cell_Num_2+Batt_Temp_Num];
  881. Can_TxMsg.Data[4] = 0x00;
  882. Can_TxMsg.Data[5] = 0x00;
  883. Can_TxMsg.Data[6] = battbuffer[24];
  884. Can_TxMsg.Data[7] = Batt_Temp_Num<<4|0;
  885. Can_TxMsg.stdIDH = Can_ID>>3;
  886. Can_TxMsg.stdIDL = Can_ID<<5;
  887. Can_TxMsg.DLC = 8;
  888. HAL_Can_Transmit(Can_TxMsg);
  889. break;
  890. }
  891. case 7:
  892. {
  893. Can_ID = 0x221;
  894. Can_TxMsg.Data[0] = battbuffer[25];//SOH
  895. Can_TxMsg.Data[1] = 0x00;
  896. Can_TxMsg.Data[2] = 0x00;
  897. Can_TxMsg.Data[3] = 0x00;
  898. Can_TxMsg.Data[4] = 0x00;
  899. Can_TxMsg.Data[5] = 0x00;
  900. Can_TxMsg.Data[6] = 0x00;
  901. Can_TxMsg.Data[7] = 0x00;
  902. Can_TxMsg.stdIDH = Can_ID>>3;
  903. Can_TxMsg.stdIDL = Can_ID<<5;
  904. Can_TxMsg.DLC = 8;
  905. HAL_Can_Transmit(Can_TxMsg);
  906. break;
  907. }
  908. default:
  909. {
  910. Can_Flag=true;
  911. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE);
  912. break;
  913. }
  914. }
  915. send_index ++;
  916. }
  917. break;
  918. }
  919. case PROCESS_CAN_STATE_SLEEP:
  920. {
  921. posGGAServiceStop();
  922. HAL_Can_Sleep();
  923. slpManSlpState_t State;
  924. uint8_t cnt;
  925. if(slpManCheckVoteState(CanSlpHandler, &State, &cnt)==RET_TRUE)
  926. {
  927. #ifdef USING_PRINTF
  928. printf("[%d]We Can Check Vote Can State, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
  929. #endif
  930. }
  931. slpManPlatVoteEnableSleep(CanSlpHandler, SLP_SLP2_STATE);
  932. if(slpManCheckVoteState(CanSlpHandler, &State, &cnt)==RET_TRUE)
  933. {
  934. #ifdef USING_PRINTF
  935. printf("[%d]We Can Check Vote can State Again, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
  936. #endif
  937. }
  938. Can_Sleep_Flag = true;
  939. while(true)
  940. {
  941. osDelay(60000/portTICK_PERIOD_MS);
  942. }
  943. }
  944. }
  945. }
  946. }
  947. static void sendQueueMsg(UINT32 msgId, UINT32 xTickstoWait)
  948. {
  949. eventCallbackMessage_t *queueMsg = NULL;
  950. queueMsg = malloc(sizeof(eventCallbackMessage_t));
  951. queueMsg->messageId = msgId;
  952. if (psEventQueueHandle)
  953. {
  954. if (pdTRUE != xQueueSend(psEventQueueHandle, &queueMsg, xTickstoWait))
  955. {
  956. ECOMM_TRACE(UNILOG_PLA_APP, sendQueueMsg_1, P_ERROR, 0, "xQueueSend error");
  957. }
  958. }
  959. }
  960. // void slpManTimerCb(slpManTimerID_e ID)
  961. // {
  962. // Timer_ID = ID;
  963. // }
  964. static INT32 socketRegisterPSUrcCallback(urcID_t eventID, void *param, UINT32 paramLen)
  965. {
  966. CmiSimImsiStr *imsi = NULL;
  967. CmiPsCeregInd *cereg = NULL;
  968. UINT8 rssi = 0, index = 0;
  969. NmAtiNetifInfo *netif = NULL;
  970. switch(eventID)
  971. {
  972. case NB_URC_ID_SIM_READY:
  973. {
  974. imsi = (CmiSimImsiStr *)param;
  975. memcpy(gImsi, imsi->contents, imsi->length);
  976. #ifdef USING_PRINTF
  977. printf("SIM ready(imsi=%s)\n",(UINT8 *)imsi->contents);
  978. #endif
  979. break;
  980. }
  981. case NB_URC_ID_MM_SIGQ:
  982. {
  983. rssi = *(UINT8 *)param;
  984. #ifdef USING_PRINTF
  985. printf("RSSI signal=%d\n",rssi);
  986. #endif
  987. ECOMM_TRACE(UNILOG_PLA_APP, socketRegisterPSUrcCallback_1, P_INFO, 1, "RSSI signal=%d", rssi);
  988. break;
  989. }
  990. case NB_URC_ID_PS_BEARER_ACTED:
  991. {
  992. #ifdef USING_PRINTF
  993. printf("Default bearer activated\n");
  994. #endif
  995. break;
  996. }
  997. case NB_URC_ID_PS_BEARER_DEACTED:
  998. {
  999. #ifdef USING_PRINTF
  1000. printf("Default bearer Deactivated\n");
  1001. #endif
  1002. break;
  1003. }
  1004. case NB_URC_ID_PS_CEREG_CHANGED:
  1005. {
  1006. cereg = (CmiPsCeregInd *)param;
  1007. gCellID = cereg->celId;
  1008. #ifdef USING_PRINTF
  1009. printf("URCCallBack:CEREG changed act:%d celId:%d locPresent:%d tac:%d\n", cereg->act, cereg->celId, cereg->locPresent, cereg->tac);
  1010. #endif
  1011. break;
  1012. }
  1013. case NB_URC_ID_PS_NETINFO:
  1014. {
  1015. netif = (NmAtiNetifInfo *)param;
  1016. if (netif->netStatus == NM_NETIF_ACTIVATED)
  1017. //sendQueueMsg(QMSG_ID_NW_IP_READY, 0);
  1018. Tcp_statuts = 1;
  1019. break;
  1020. }
  1021. }
  1022. return 0;
  1023. }
  1024. uint8_t bcc_chk(uint8_t* data, uint8_t length)//发送bcc校验函数
  1025. {
  1026. uint8_t bcc_chk_return = 0x00;
  1027. uint8_t count = 0;
  1028. while (count<length)
  1029. {
  1030. bcc_chk_return^=data[count];
  1031. count++;
  1032. }
  1033. return bcc_chk_return;
  1034. }
  1035. //电池数据组装函数
  1036. void Tcp_Batt_Data_Assemble(void)
  1037. {
  1038. int16_t Batt_current;
  1039. uint16_t BattU = 0x00;
  1040. uint8_t csq=0;
  1041. int8_t snr=0;
  1042. int8_t rsnr=0;
  1043. Batt_current = Uart_Rece_BattI;
  1044. OsaUtcTimeTValue timestracture;
  1045. appGetSystemTimeUtcSync(&timestracture);
  1046. battbuffer[0] = timestracture.UTCtimer1>>16;
  1047. battbuffer[0] = battbuffer[0] - 0x07D0;
  1048. battbuffer[1] = timestracture.UTCtimer1>>8;
  1049. battbuffer[2] = timestracture.UTCtimer1;
  1050. battbuffer[3] = timestracture.UTCtimer2>>24;
  1051. battbuffer[4] = timestracture.UTCtimer2>>16;
  1052. battbuffer[5] = timestracture.UTCtimer2>>8;
  1053. battbuffer[6] = 0x80;//信息体标志,此处为电池信息
  1054. battbuffer[7] = battbuffer[0];//年
  1055. battbuffer[8] = battbuffer[1];//月
  1056. battbuffer[9] = battbuffer[2];//日
  1057. battbuffer[10] = battbuffer[3];//时 0时区时间
  1058. battbuffer[11] = battbuffer[4];//分
  1059. battbuffer[12] = battbuffer[5];//秒
  1060. appGetSignalInfoSync(&csq,&snr,&rsnr);//获取信号质量
  1061. battbuffer[13] = csq;// 网络信号
  1062. battbuffer[14] = 0x00;//故障等级
  1063. battbuffer[15] = 0x00;//故障代码高
  1064. battbuffer[16] = 0x00;//故障代码低
  1065. //电流适应性更改,从int转换到uint,加10000的偏移量,100mA的单位
  1066. if (Batt_current>0x8000)
  1067. {
  1068. Batt_current = Batt_current|0x7fff;
  1069. Batt_current = Batt_current/10;
  1070. Batt_current = 0x2710 - Batt_current;
  1071. Batt_current = Batt_current;
  1072. }
  1073. else
  1074. {
  1075. Batt_current = Batt_current/10;
  1076. Batt_current = Batt_current+0x2710;
  1077. Batt_current = Batt_current;
  1078. }
  1079. battbuffer[17] = Batt_current>>8;
  1080. battbuffer[18] = Batt_current;
  1081. BattU =( battbuffer[19]<<8|battbuffer[20])/10;
  1082. battbuffer[19] = BattU>>8;
  1083. battbuffer[20] = BattU;
  1084. battbuffer[21] = BattU>>8;
  1085. battbuffer[22] = BattU;//外电压
  1086. data_index = 32+Batt_Cell_Num_2+Batt_Temp_Num;
  1087. if(Batt_current==10000)
  1088. {
  1089. battbuffer[data_index] = 0x00;//电池状态
  1090. }
  1091. else if(Batt_current>10000)
  1092. {
  1093. battbuffer[data_index] = 0x02;//电池状态
  1094. }
  1095. else if(Batt_current<10000)
  1096. {
  1097. battbuffer[data_index] = 0x01;//电池状态
  1098. }
  1099. data_index++;
  1100. //battbuffer[data_index] = 0x00;//是否加热
  1101. data_index++;
  1102. }
  1103. //Gps 数据组装函数
  1104. void Tcp_Gps_Data_Assemble(void)
  1105. {
  1106. OsaUtcTimeTValue timestracture;
  1107. appGetSystemTimeUtcSync(&timestracture);
  1108. gpsbuffer[0] = timestracture.UTCtimer1>>16;
  1109. gpsbuffer[0] = gpsbuffer[0] - 0x07D0;
  1110. gpsbuffer[1] = timestracture.UTCtimer1>>8;
  1111. gpsbuffer[2] = timestracture.UTCtimer1;
  1112. gpsbuffer[3] = timestracture.UTCtimer2>>24;
  1113. gpsbuffer[4] = timestracture.UTCtimer2>>16;
  1114. gpsbuffer[5] = timestracture.UTCtimer2>>8;
  1115. gpsbuffer[6] = 0x82;//信息体标志,此处为GPS信息
  1116. gpsbuffer[7] = gpsbuffer[0];//年
  1117. gpsbuffer[8] = gpsbuffer[1];//月
  1118. gpsbuffer[9] = gpsbuffer[2];//日
  1119. gpsbuffer[10] = gpsbuffer[3];//时 0时区时间
  1120. gpsbuffer[11] = gpsbuffer[4];//分
  1121. gpsbuffer[12] = gpsbuffer[5];//秒
  1122. gpsbuffer[13] = Gps_Data.status;
  1123. gpsbuffer[14] = Gps_Data.satellite_num;
  1124. gpsbuffer[15] = Gps_Data.direction>>8;
  1125. gpsbuffer[16] = Gps_Data.direction;
  1126. gpsbuffer[17] = Gps_Data.speed>>8;
  1127. gpsbuffer[18] = Gps_Data.speed;
  1128. gpsbuffer[19] = Gps_Data.altitude>>8;
  1129. gpsbuffer[20] = Gps_Data.altitude;
  1130. gpsbuffer[21] = Gps_Data.latitude>>24;
  1131. gpsbuffer[22] = Gps_Data.latitude>>16;
  1132. gpsbuffer[23] = Gps_Data.latitude>>8;
  1133. gpsbuffer[24] = Gps_Data.latitude;
  1134. gpsbuffer[25] = Gps_Data.longitude>>24;
  1135. gpsbuffer[26] = Gps_Data.longitude>>16;
  1136. gpsbuffer[27] = Gps_Data.longitude>>8;
  1137. gpsbuffer[28] = Gps_Data.longitude;
  1138. }
  1139. //Tcp线程
  1140. static void Tcp_Task(void* arg)
  1141. {
  1142. CHAR serverip[] = "47.97.127.222";
  1143. CHAR serverport[] = "8712";
  1144. struct addrinfo hints, *server_res;
  1145. int TcpsendID = -1;
  1146. memset( &hints, 0, sizeof( hints ) );
  1147. hints.ai_socktype = SOCK_STREAM;
  1148. hints.ai_protocol = IPPROTO_TCP;
  1149. psEventQueueHandle = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(eventCallbackMessage_t*));
  1150. slpManApplyPlatVoteHandle("TcpSlp",&TcpSlpHandler);
  1151. slpManPlatVoteDisableSleep(TcpSlpHandler, SLP_SLP2_STATE);
  1152. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
  1153. uint8_t Tcp_Index = 0;
  1154. uint8_t* TcpSendBuffer=NULL;
  1155. volatile bool Tcp_Flag=false;
  1156. eventCallbackMessage_t *queueItem = NULL;
  1157. uint8_t pmode;
  1158. uint32_t tau;
  1159. uint32_t act;
  1160. uint8_t NB_send_len;
  1161. uint8_t Reconnect_Num = 0;
  1162. appSetEDRXSettingSync(0,5,1800000);
  1163. appSetPSMSettingSync(1,3*60*60,10);
  1164. slpManWakeSrc_e Wakeup_source;
  1165. Wakeup_source = slpManGetWakeupSrc();//获取唤醒源
  1166. if(Wakeup_source==0)
  1167. {
  1168. Tcp_statuts = 0;
  1169. }
  1170. else
  1171. {
  1172. Tcp_statuts = 1;
  1173. }
  1174. NetSocDisplay(LED_SOC_2,LED_TURN_ON);
  1175. appSetCFUN(1);
  1176. while(true)
  1177. {
  1178. switch(gProcess_Tcp_Task)
  1179. {
  1180. case PROCESS_TCP_STATE_IDLE:
  1181. {
  1182. osDelay(100);
  1183. break;
  1184. }
  1185. case PROCESS_TCP_STATE_LINK:
  1186. {
  1187. #ifdef USING_PRINTF
  1188. printf("TCP_statuts:%d\n",Tcp_statuts);
  1189. #endif
  1190. switch(Tcp_statuts)
  1191. {
  1192. case 0://0代表网络为驻网前
  1193. {
  1194. #ifdef USING_PRINTF
  1195. printf("TCP Not Ready\n");
  1196. #endif
  1197. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
  1198. break;
  1199. }
  1200. case 1://1代表网络驻网
  1201. {
  1202. if (getaddrinfo( serverip, serverport , &hints, &server_res ) != 0 )
  1203. {
  1204. #ifdef USING_PRINTF
  1205. printf("TCP connect unresolved dns\n");
  1206. #endif
  1207. #ifdef BL_FILE_LOG
  1208. bluejoy_printf(BL_LEVEL3,"TCP connect unresolved dns\n");
  1209. #endif
  1210. }
  1211. Tcp_statuts=2;
  1212. break;
  1213. }
  1214. case 2://2代表尝试建立连接
  1215. {
  1216. #ifdef USING_PRINTF
  1217. printf("Connecting...!%d\n",TcpconnectID);
  1218. #endif
  1219. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  1220. if(sockfd<0)
  1221. {
  1222. #ifdef USING_PRINTF
  1223. printf("socket create error\n");
  1224. #endif
  1225. #ifdef BL_FILE_LOG
  1226. bluejoy_printf(BL_LEVEL3,"socket create error\n");
  1227. #endif
  1228. Tcp_statuts=1;
  1229. }
  1230. else
  1231. {
  1232. TcpconnectID = connect(sockfd, (struct sockaddr *) server_res->ai_addr, server_res->ai_addrlen);
  1233. if(TcpconnectID<0 && errno != EINPROGRESS)
  1234. {
  1235. #ifdef USING_PRINTF
  1236. printf("Connect failed!%d,Reconnect:%d\n",TcpconnectID,Reconnect_Num);
  1237. #endif
  1238. close(sockfd);
  1239. Tcp_statuts=2;
  1240. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
  1241. Reconnect_Num++;
  1242. NetSocDisplay(LED_SOC_0,LED_TURN_ON);
  1243. }
  1244. else
  1245. {
  1246. NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
  1247. Tcp_statuts=3;
  1248. Reconnect_Num = 0;
  1249. }
  1250. if(Reconnect_Num>=20)
  1251. {
  1252. #ifdef BL_FILE_LOG
  1253. bluejoy_printf(BL_LEVEL3,"20 Connect failed!%d,Reconnect:%d\n",TcpconnectID,Reconnect_Num);
  1254. #endif
  1255. appSetCFUN(0);
  1256. osDelay(10);
  1257. appSetCFUN(1);
  1258. PSInitEventCallbackTable();
  1259. Tcp_statuts=1;
  1260. Reconnect_Num = 0;
  1261. }
  1262. }
  1263. break;
  1264. }
  1265. case 3://3代表连接建立成功
  1266. {
  1267. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK);
  1268. break;
  1269. }
  1270. case 4://4代表连接断开
  1271. {
  1272. Tcp_statuts=2;
  1273. break;
  1274. }
  1275. }
  1276. break;
  1277. }
  1278. case PROCESS_TCP_STATE_WORK:
  1279. {
  1280. #ifdef USING_PRINTF
  1281. printf("TCP_STATE_WORK!\n");
  1282. #endif
  1283. appGetPSMSettingSync(&pmode,&tau,&act);
  1284. #ifdef USING_PRINTF
  1285. printf("psm:pmode-%d,tau-%d,act-%d!\n",pmode,tau,act);
  1286. #endif
  1287. Tcp_Flag = false;
  1288. while (!Tcp_Flag)
  1289. {
  1290. switch(Tcp_Index)
  1291. {
  1292. case 0://发送登录信息
  1293. {
  1294. Tcp_Index=1;
  1295. break;
  1296. }
  1297. case 1://发送电池数据
  1298. {
  1299. NB_send_len=59+Batt_Cell_Num_2+Batt_Temp_Num;//电池数据长度
  1300. TcpSendBuffer = (uint8_t *)malloc(NB_send_len);
  1301. data_index = 0;
  1302. memset(TcpSendBuffer,0x00,NB_send_len);
  1303. *(TcpSendBuffer+0) = 0x23;
  1304. *(TcpSendBuffer+1) = 0x23;
  1305. *(TcpSendBuffer+2) = 0x02;
  1306. *(TcpSendBuffer+3) = 0xfe;
  1307. memcpy(TcpSendBuffer+4,SN,17);
  1308. *(TcpSendBuffer+21) = 0x01;//01表示不加密
  1309. Tcp_Batt_Data_Assemble();//数据组装函数
  1310. *(TcpSendBuffer+22) = data_index>>8;//数据长度
  1311. *(TcpSendBuffer+23) = data_index;//数据长度
  1312. memcpy(TcpSendBuffer+24,battbuffer,data_index);
  1313. *(TcpSendBuffer+NB_send_len-1) = bcc_chk(TcpSendBuffer,NB_send_len-1);
  1314. // #ifdef USING_PRINTF
  1315. // printf("battbuffer:");
  1316. // for (int i = 0; i < data_index; i++)
  1317. // {
  1318. // printf("%x ",battbuffer[i]);
  1319. // }
  1320. // printf("\n");
  1321. //#endif
  1322. TcpsendID = send(sockfd, TcpSendBuffer, NB_send_len, 0 );
  1323. //发送失败
  1324. // #ifdef USING_PRINTF
  1325. // printf("ConnectID:%d,TcpSend:%d,data length:%d,Data: ",sockfd,TcpsendID,NB_send_len);
  1326. // for (int i = 0; i < NB_send_len; i++)
  1327. // {
  1328. // printf("%x ",*(TcpSendBuffer+i));
  1329. // }
  1330. // printf("\n");
  1331. // #endif
  1332. free(TcpSendBuffer);
  1333. if (TcpsendID<0)
  1334. {
  1335. #ifdef BL_FILE_LOG
  1336. bluejoy_printf(BL_LEVEL3,"send failed,ConnectID:%d,TcpSend:%d,data length:%d\n",sockfd,TcpsendID,NB_send_len);
  1337. #endif
  1338. closesocket(sockfd);
  1339. TcpconnectID = -1;
  1340. Tcp_Index=-1;
  1341. Tcp_statuts = 4;
  1342. }
  1343. else
  1344. {
  1345. Tcp_Index=2;
  1346. }
  1347. break;
  1348. }
  1349. case 2:
  1350. {
  1351. data_index = 29;
  1352. NB_send_len = 25+29;//数据总长度:25个固定+29个GPS长度
  1353. TcpSendBuffer = (uint8_t *)malloc(NB_send_len);
  1354. memset(TcpSendBuffer,0x00,NB_send_len);
  1355. *(TcpSendBuffer+0) = 0x23;
  1356. *(TcpSendBuffer+1) = 0x23;
  1357. *(TcpSendBuffer+2) = 0x02;
  1358. *(TcpSendBuffer+3) = 0xfe;
  1359. memcpy(TcpSendBuffer+4,SN,17);
  1360. *(TcpSendBuffer+21) = 0x01;//01表示不加密
  1361. Tcp_Gps_Data_Assemble();//Gps数据组装
  1362. *(TcpSendBuffer+22) = data_index>>8;//数据长度
  1363. *(TcpSendBuffer+23) = data_index;//数据长度
  1364. memcpy(TcpSendBuffer+24,gpsbuffer,data_index);
  1365. *(TcpSendBuffer+NB_send_len-1) = bcc_chk(TcpSendBuffer,NB_send_len-1);
  1366. // #ifdef USING_PRINTF
  1367. // printf("ConnectID:%d,TcpSend:%d,data length:%d,Data: ",sockfd,TcpsendID,NB_send_len);
  1368. // for (int i = 0; i < NB_send_len; i++)
  1369. // {
  1370. // printf("%x ",*(TcpSendBuffer+i));
  1371. // }
  1372. // printf("\n");
  1373. // #endif
  1374. TcpsendID = send(sockfd, TcpSendBuffer, NB_send_len, 0 );
  1375. free(TcpSendBuffer);
  1376. if (TcpsendID<0)
  1377. {
  1378. closesocket(sockfd);
  1379. TcpconnectID = -1;
  1380. Tcp_Index=-1;
  1381. Tcp_statuts = 4;
  1382. NetSocDisplay(LED_SOC_1,LED_TURN_ON);
  1383. }
  1384. else
  1385. {
  1386. NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
  1387. Tcp_Index=3;
  1388. }
  1389. break;
  1390. }
  1391. default:
  1392. {
  1393. PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
  1394. Tcp_Index = 0;
  1395. Tcp_Flag = true;
  1396. }
  1397. }
  1398. }
  1399. break;
  1400. }
  1401. case PROCESS_TCP_STATE_SLEEP:
  1402. {
  1403. slpManPlatVoteEnableSleep(TcpSlpHandler, SLP_SLP2_STATE);
  1404. closesocket(sockfd);
  1405. //deregisterPSEventCallback(socketRegisterPSUrcCallback);//注销NB网络事件函数
  1406. #ifdef USING_PRINTF
  1407. printf("TCP_STATE_SLEEP\n");
  1408. #endif
  1409. Tcp_Sleep_Flag = TRUE;
  1410. appSetCFUN(0);
  1411. while(1)
  1412. {
  1413. osDelay(30000/portTICK_PERIOD_MS);
  1414. }
  1415. }
  1416. }
  1417. }
  1418. }
  1419. //主任务线程初始化
  1420. void Main_Task_Init()
  1421. {
  1422. #ifndef USING_PRINTF
  1423. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  1424. {
  1425. HAL_UART_RecvFlowControl(false);
  1426. }
  1427. #endif
  1428. //slpManDeepSlpTimerRegisterExpCb(slpManTimerCb);
  1429. osThreadAttr_t task_attr;
  1430. memset(&task_attr,0,sizeof(task_attr));
  1431. memset(gProcess_Main_TaskStack, 0xA5, PROC_MAIN_TASK_STACK_SIZE);
  1432. task_attr.name = "Main_Task";
  1433. task_attr.stack_mem = gProcess_Main_TaskStack;
  1434. task_attr.stack_size = PROC_MAIN_TASK_STACK_SIZE;
  1435. task_attr.priority = osPriorityNormal;
  1436. task_attr.cb_mem = &gProcess_Main_Task_t;
  1437. task_attr.cb_size = sizeof(StaticTask_t);
  1438. osThreadNew(Main_Task, NULL, &task_attr);
  1439. }
  1440. //Uart读取线程初始化
  1441. void Uart_Task_Init()
  1442. {
  1443. osThreadAttr_t task_attr;
  1444. memset(&task_attr,0,sizeof(task_attr));
  1445. memset(gProcess_Uart_TaskStack, 0xA5, PROC_UART_TASK_STACK_SIZE);
  1446. task_attr.name = "Uart_Task";
  1447. task_attr.stack_mem = gProcess_Uart_TaskStack;
  1448. task_attr.stack_size = PROC_UART_TASK_STACK_SIZE;
  1449. task_attr.priority = osPriorityBelowNormal7;
  1450. task_attr.cb_mem = &gProcess_Uart_Task_t;
  1451. task_attr.cb_size = sizeof(StaticTask_t);
  1452. osThreadNew(Uart_Task, NULL, &task_attr);
  1453. }
  1454. //Can线程初始化
  1455. void Can_Task_Init()
  1456. {
  1457. osThreadAttr_t task_attr;
  1458. memset(&task_attr,0,sizeof(task_attr));
  1459. memset(gProcess_Can_TaskStack, 0xA5, PROC_CAN_TASK_STACK_SIZE);
  1460. task_attr.name = "Can_Task";
  1461. task_attr.stack_mem = gProcess_Can_TaskStack;
  1462. task_attr.stack_size = PROC_CAN_TASK_STACK_SIZE;
  1463. task_attr.priority = osPriorityBelowNormal7;
  1464. task_attr.cb_mem = &gProcess_Can_Task_t;
  1465. task_attr.cb_size = sizeof(StaticTask_t);
  1466. osThreadNew(Can_Task, NULL, &task_attr);
  1467. }
  1468. //Tcp线程初始化
  1469. void Tcp_Task_Init()
  1470. {
  1471. osThreadAttr_t task_attr;
  1472. registerPSEventCallback(NB_GROUP_ALL_MASK, socketRegisterPSUrcCallback);
  1473. memset(&task_attr,0,sizeof(task_attr));
  1474. memset(gProcess_Tcp_TaskStack, 0xA5, PROC_TCP_TASK_STACK_SIZE);
  1475. task_attr.name = "Tcp_Task";
  1476. task_attr.stack_mem = gProcess_Tcp_TaskStack;
  1477. task_attr.stack_size = PROC_TCP_TASK_STACK_SIZE;
  1478. task_attr.priority = osPriorityBelowNormal7;
  1479. task_attr.cb_mem = &gProcess_Tcp_Task_t;
  1480. task_attr.cb_size = sizeof(StaticTask_t);
  1481. osThreadNew(Tcp_Task, NULL, &task_attr);
  1482. }
  1483. void appInit(void *arg)
  1484. {
  1485. Main_Task_Init();
  1486. Uart_Task_Init();
  1487. Can_Task_Init();
  1488. GpsTaskInit();
  1489. Tcp_Task_Init();
  1490. }
  1491. //主函数入口
  1492. void main_entry(void) {
  1493. BSP_CommonInit();
  1494. osKernelInitialize();
  1495. registerAppEntry(appInit, NULL);
  1496. if (osKernelGetState() == osKernelReady)
  1497. {
  1498. osKernelStart();
  1499. }
  1500. while(1);
  1501. }