app.c 31 KB

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