app.c 55 KB

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