app.c 29 KB

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