app_1_5.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. typedef enum
  62. {
  63. PROCESS_STATE_IDLE = 0,
  64. PROCESS_STATE_WORK,
  65. PROCESS_STATE_SLEEP
  66. }processSM;
  67. typedef enum
  68. {
  69. PROCESS_Uart_STATE_IDLE = 0,
  70. PROCESS_Uart_STATE_WORK,
  71. PROCESS_Uart_STATE_CHECK,
  72. PROCESS_Uart_STATE_SLEEP
  73. }process_Uart;
  74. static StaticTask_t gProcessTask0;
  75. static UINT8 gProcessTaskStack0[PROC_TASK_STACK_SIZE];
  76. static StaticTask_t gProcessTask1;
  77. static UINT8 gProcessTaskStack1[PROC_TASK_STACK_SIZE];
  78. static StaticTask_t gProcessTask2;
  79. static UINT8 gProcessTaskStack2[PROC_TASK_STACK_SIZE];
  80. processSM gProc0State = PROCESS_STATE_IDLE;
  81. process_Uart gProc1State = PROCESS_Uart_STATE_IDLE;
  82. processSM gProc2State = PROCESS_STATE_IDLE;
  83. #define PROC_Task_STATE_SWITCH(a) (gProc0State = a) //任务调度切换
  84. #define PROC_Uart_STATE_SWITCH(a) (gProc1State = a) //uart 状态切换
  85. #define PROC_Can_STATE_SWITCH(a) (gProc2State = a) //can 状态切换
  86. unsigned int crc_chk(uint8_t* data, uint8_t length)
  87. {
  88. int j;
  89. uint16_t reg_crc=0xFFFF;
  90. while(length--)
  91. {
  92. reg_crc ^= *data++;
  93. for(j=0;j<8;j++)
  94. {
  95. if(reg_crc & 0x01)
  96. {
  97. reg_crc=(reg_crc>>1) ^ 0xA001;
  98. }
  99. else
  100. {
  101. reg_crc=reg_crc >>1;
  102. }
  103. }
  104. }
  105. return reg_crc;
  106. }
  107. uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg)
  108. {
  109. uint8_t Uart_Rece_buffer[Uart_Rece_LEN];
  110. uint16_t CRC_Reve_buffer;
  111. uint16_t CRC_chk_buffer;
  112. uint8_t Uart_Send_buffer[8];
  113. uint8_t Rece_Data_Len;
  114. Uart_Send_buffer[0] = Uart_Receive_Msg.Bms_Address;
  115. Uart_Send_buffer[1] = Uart_Receive_Msg.Bms_Read_Funcode;
  116. Uart_Send_buffer[2] = Uart_Receive_Msg.Reg_Begin_H;
  117. Uart_Send_buffer[3] = Uart_Receive_Msg.Reg_Begin_L;
  118. Uart_Send_buffer[4] = Uart_Receive_Msg.Reg_Num_H;
  119. Uart_Send_buffer[5] = Uart_Receive_Msg.Reg_Num_L;
  120. CRC_chk_buffer = crc_chk(Uart_Send_buffer,6);
  121. Uart_Send_buffer[6] = CRC_chk_buffer;
  122. Uart_Send_buffer[7] = CRC_chk_buffer>>8;
  123. Uart_Rece_buffer[0]=0xfe;
  124. uint32_t timeout=0;
  125. USARTdrv->Send(Uart_Send_buffer,8);
  126. USARTdrv->Receive(Uart_Rece_buffer,13);
  127. Rece_Data_Len = 11;
  128. while((isRecvTimeout == false) && (isRecvComplete == false))// 未收到数据不叫时间超时,收到数据但是不全叫时间超时
  129. {
  130. timeout++;
  131. if (timeout>7000000)
  132. {
  133. timeout =0;
  134. isRecvTimeout = true;
  135. break;
  136. }
  137. }
  138. if (isRecvComplete == true)
  139. {
  140. isRecvComplete = false;
  141. CRC_chk_buffer =Uart_Rece_buffer[12]<<8|Uart_Rece_buffer[11];
  142. CRC_Reve_buffer = crc_chk(Uart_Rece_buffer,11);
  143. if (CRC_Reve_buffer == CRC_chk_buffer)//满足校验
  144. {
  145. for (uint8_t i = 0; i < 8; i++)
  146. {
  147. Uart_Data_buffer[i]=Uart_Rece_buffer[i+3];
  148. }
  149. return Uart_Data_buffer;
  150. }
  151. else //接收数据的校验不过暂时屏蔽
  152. {
  153. for (uint8_t i = 0; i < 8; i++)
  154. {
  155. Uart_Data_buffer[i]=0xff;
  156. }
  157. return Uart_Data_buffer;
  158. }
  159. }
  160. if (isRecvTimeout == true)//没收到数据,全部为空值
  161. {
  162. Uart_Data_buffer[0] = 0x00;
  163. Uart_Data_buffer[1] = 0x00;
  164. Uart_Data_buffer[2] = 0x00;
  165. Uart_Data_buffer[3] = 0x00;
  166. Uart_Data_buffer[4] = 0x00;
  167. Uart_Data_buffer[5] = 0x00;
  168. Uart_Data_buffer[6] = 0x00;
  169. Uart_Data_buffer[7] = 0xff;
  170. isRecvTimeout = false;
  171. osDelay(1000);
  172. return Uart_Data_buffer;
  173. }
  174. }
  175. void USART_callback(uint32_t event)
  176. {
  177. if(event & ARM_USART_EVENT_RX_TIMEOUT)
  178. {
  179. isRecvTimeout = true;
  180. }
  181. if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
  182. {
  183. isRecvComplete = true;
  184. }
  185. }
  186. static void process0AppTask(void* arg)
  187. {
  188. uint32_t inParam = 0xAABBCCDD;
  189. uint32_t cnt;
  190. UINT8 Can_index = 0;
  191. UINT8 Uart_index = 0;
  192. uint32_t sleep_index = 0;
  193. PROC_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
  194. NetSocDisplay(0,LED_TURN_OFF);
  195. NetSocDisplay(1,LED_TURN_OFF);
  196. NetSocDisplay(2,LED_TURN_OFF);
  197. NetSocDisplay(3,LED_TURN_OFF);
  198. NetSocDisplay(4,LED_TURN_OFF);
  199. slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
  200. slpManApplyPlatVoteHandle("process0slp",&process0SlpHandler);
  201. slpManPlatVoteDisableSleep(process0SlpHandler, SLP_SLP2_STATE);
  202. slpManSlpState_t slpstate = slpManGetLastSlpState();
  203. if((slpstate == SLP_SLP2_STATE) || (slpstate == SLP_HIB_STATE))
  204. {
  205. PROC_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
  206. }
  207. else
  208. {
  209. PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
  210. }
  211. while(1)
  212. {
  213. switch(gProc0State)
  214. {
  215. case PROCESS_STATE_IDLE:
  216. {
  217. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_CHECK);
  218. NetSocDisplay(0,LED_TURN_ON);
  219. osDelay(100);//delay 100ms
  220. if(Uart_Rece_BattI!=0x0000)
  221. {
  222. PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
  223. }
  224. sleep_index++;
  225. if(sleep_index>=100)
  226. {
  227. NetSocDisplay(0,LED_TURN_OFF);
  228. sleep_index=0;
  229. PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
  230. }
  231. break;
  232. }
  233. case PROCESS_STATE_WORK:
  234. {
  235. osDelay(10);//10ms
  236. Can_index++;
  237. Uart_index++;
  238. if (Uart_index >10)//Uart 100ms 调用一次
  239. {
  240. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_WORK);
  241. Uart_index = 0;
  242. }
  243. if (Can_index >=100)//Can 1000ms 调用一次
  244. {
  245. PROC_Can_STATE_SWITCH(PROCESS_STATE_WORK);
  246. Can_index = 0;
  247. }
  248. if(Uart_Rece_BattI==0x0000)
  249. {
  250. sleep_index++;
  251. }
  252. else
  253. {
  254. sleep_index = 0;
  255. }
  256. if (sleep_index >=6000)
  257. {
  258. PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
  259. sleep_index = 0;
  260. }
  261. break;
  262. }
  263. case PROCESS_STATE_SLEEP:
  264. {
  265. slpManSlpState_t State;
  266. uint8_t cnt;
  267. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_SLEEP);
  268. PROC_Can_STATE_SWITCH(PROCESS_STATE_SLEEP);
  269. slpManPlatVoteEnableSleep(process0SlpHandler,SLP_SLP2_STATE);
  270. slpManPlatVoteDisableSleep(process0SlpHandler, SLP_HIB_STATE);
  271. slpManDeepSlpTimerStart(deepslpTimerID, 300000); // create a 10s timer, DeepSleep Timer is always oneshoot
  272. while(1) // now app can enter hib, but ps and phy maybe not, so wait here
  273. {
  274. osDelay(3000);
  275. }
  276. }
  277. }
  278. }
  279. }
  280. static void process1AppTask(void* arg)
  281. {
  282. USARTdrv->Initialize(USART_callback);
  283. USARTdrv->PowerControl(ARM_POWER_FULL);
  284. USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
  285. ARM_USART_DATA_BITS_8 |
  286. ARM_USART_PARITY_NONE |
  287. ARM_USART_STOP_BITS_1 |
  288. ARM_USART_FLOW_CONTROL_NONE, 9600);
  289. int Rece_index = 0;
  290. uint8_t *Uart_Reve_Point = NULL;
  291. Uart_Receive_Type Uart_Receive_Msg;
  292. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
  293. slpManPlatVoteDisableSleep(process2SlpHandler, SLP_SLP2_STATE);
  294. Uart_Receive_Msg.Bms_Address = 0x01;
  295. Uart_Receive_Msg.Bms_Read_Funcode = 0x03;
  296. while(1)
  297. {
  298. switch(gProc1State)
  299. {
  300. case PROCESS_Uart_STATE_IDLE:
  301. {
  302. NetSocDisplay(1,LED_TURN_ON);
  303. Rece_index = 0;
  304. break;
  305. }
  306. case PROCESS_Uart_STATE_CHECK:
  307. {
  308. NetSocDisplay(1,LED_TURN_OFF);
  309. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  310. Uart_Receive_Msg.Reg_Begin_L= 0x10;
  311. Uart_Receive_Msg.Reg_Num_H = 0x00;
  312. Uart_Receive_Msg.Reg_Num_L = 0x04;
  313. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  314. Uart_Rece_BattI = *(Uart_Reve_Point+0)<<8 | *(Uart_Reve_Point+1);
  315. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
  316. break;
  317. }
  318. case PROCESS_Uart_STATE_WORK:
  319. {
  320. switch(Rece_index)
  321. {
  322. case 0://读取电压1-4
  323. {
  324. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  325. Uart_Receive_Msg.Reg_Begin_L = 0x02;
  326. Uart_Receive_Msg.Reg_Num_H = 0x00;
  327. Uart_Receive_Msg.Reg_Num_L = 0x04;
  328. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  329. for(int i =0;i<8;i++)
  330. {
  331. Uart_Rece_BattCellU1_U4[i] = *(Uart_Reve_Point+i);
  332. }
  333. break;
  334. }
  335. case 1://读取电压5-8
  336. {
  337. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  338. Uart_Receive_Msg.Reg_Begin_L= 0x06;
  339. Uart_Receive_Msg.Reg_Num_H = 0x00;
  340. Uart_Receive_Msg.Reg_Num_L = 0x04;
  341. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  342. for(int i =0;i<8;i++)
  343. {
  344. Uart_Rece_BattCellU5_U8[i] = *(Uart_Reve_Point+i);
  345. }
  346. break;
  347. }
  348. case 2:
  349. {
  350. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  351. Uart_Receive_Msg.Reg_Begin_L= 0x0A;
  352. Uart_Receive_Msg.Reg_Num_H = 0x00;
  353. Uart_Receive_Msg.Reg_Num_L = 0x04;
  354. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  355. for(int i =0;i<8;i++)
  356. {
  357. Uart_Rece_BattCellU9_U12[i] = *(Uart_Reve_Point+i);
  358. }
  359. break;
  360. }
  361. case 3:
  362. {
  363. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  364. Uart_Receive_Msg.Reg_Begin_L= 0x0E;
  365. Uart_Receive_Msg.Reg_Num_H = 0x00;
  366. Uart_Receive_Msg.Reg_Num_L = 0x04;
  367. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  368. for(int i =0;i<8;i++)
  369. {
  370. Uart_Rece_BattCellU13_U14[i] = *(Uart_Reve_Point+i);
  371. }
  372. break;
  373. }
  374. case 4:
  375. {
  376. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  377. Uart_Receive_Msg.Reg_Begin_L= 0x14;
  378. Uart_Receive_Msg.Reg_Num_H = 0x00;
  379. Uart_Receive_Msg.Reg_Num_L = 0x04;
  380. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  381. for(int i =0;i<8;i++)
  382. {
  383. Uart_Rece_BattT[i] = *(Uart_Reve_Point+i);
  384. }
  385. break;
  386. }
  387. case 5:
  388. {
  389. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  390. Uart_Receive_Msg.Reg_Begin_L= 0x10;
  391. Uart_Receive_Msg.Reg_Num_H = 0x00;
  392. Uart_Receive_Msg.Reg_Num_L = 0x04;
  393. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  394. Uart_Rece_BattI = *(Uart_Reve_Point+0)<<8 | *(Uart_Reve_Point+1);
  395. for (int i = 0; i < 6; i++)
  396. {
  397. Uart_Rece_Batt_states[i] = *(Uart_Reve_Point+2+i);
  398. }
  399. break;
  400. }
  401. case 6:
  402. {
  403. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  404. Uart_Receive_Msg.Reg_Begin_L= 0x2B;
  405. Uart_Receive_Msg.Reg_Num_H = 0x00;
  406. Uart_Receive_Msg.Reg_Num_L = 0x04;
  407. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  408. Uart_Rece_BattU = *(Uart_Reve_Point+0)<<8 | *(Uart_Reve_Point+1);
  409. Uart_Rece_Batt_MaxcellU = *(Uart_Reve_Point+2)<<8 | *(Uart_Reve_Point+3);
  410. Uart_Rece_Batt_MincellU = *(Uart_Reve_Point+4)<<8 | *(Uart_Reve_Point+5);
  411. break;
  412. }
  413. default:
  414. {
  415. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
  416. break;
  417. }
  418. }
  419. Rece_index++;
  420. break;
  421. }
  422. case PROCESS_Uart_STATE_SLEEP:
  423. {
  424. slpManPlatVoteEnableSleep(process2SlpHandler, SLP_SLP2_STATE);
  425. slpManPlatVoteDisableSleep(process2SlpHandler, SLP_HIB_STATE);
  426. while(1)
  427. {
  428. osDelay(3000);
  429. }
  430. //此处休眠
  431. break;
  432. }
  433. }
  434. }
  435. }
  436. static void process2AppTask(void* arg)
  437. {
  438. PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
  439. uint32_t Can_ID;
  440. NVIC_EnableIRQ(PadWakeup1_IRQn);
  441. Can_InitType param;
  442. Can_TxMsgType Can_TxMsg;
  443. param.baudrate = CAN_500Kbps;
  444. param.mode = REQOP_NORMAL;
  445. param.TxStdIDH = 0x00;
  446. param.TxStdIDL = 0x00;
  447. param.RxStdIDH[0] = 0x00;
  448. param.RxStdIDL[0] = 0x00;
  449. /*stdid 0000 0000 001x*/
  450. param.RxStdIDH[1] = 0x00;
  451. param.RxStdIDL[1] = 0x20;
  452. /*stdid 0000 0000 010x */
  453. param.RxStdIDH[2] = 0x00;
  454. param.RxStdIDL[2] = 0x40;
  455. /*stdid 0000 0000 011x*/
  456. param.RxStdIDH[3] = 0x00;
  457. param.RxStdIDL[3] =0x60;
  458. /*stdid 0000 0000 100x */
  459. param.RxStdIDH[4] = 0x00;
  460. param.RxStdIDL[4] = 0x80;
  461. /*stdid 0000 0000 101x*/
  462. param.RxStdIDH[5] = 0x00;
  463. param.RxStdIDL[5] =0xa0;
  464. param.packType = STD_PACK;
  465. HAL_Can_Init(param);
  466. int send_index = 0;
  467. slpManPlatVoteDisableSleep(process1SlpHandler, SLP_SLP2_STATE);
  468. while(1)
  469. {
  470. switch(gProc2State)
  471. {
  472. case PROCESS_STATE_IDLE:
  473. {
  474. send_index = 0;
  475. break;
  476. }
  477. case PROCESS_STATE_WORK:
  478. {
  479. switch(send_index)
  480. {
  481. case 0:
  482. {
  483. Can_ID = 0x001;
  484. for (int i = 0; i < 8; i++)
  485. {
  486. Can_TxMsg.Data[i] = Uart_Rece_BattCellU1_U4[i];
  487. }
  488. Can_TxMsg.stdIDH = Can_ID>>3;
  489. Can_TxMsg.stdIDL = Can_ID<<5;
  490. Can_TxMsg.DLC = 8;
  491. HAL_Can_Transmit(Can_TxMsg);
  492. break;
  493. }
  494. case 1:
  495. {
  496. Can_ID = 0x013;
  497. for (int i = 0; i < 8; i++)
  498. {
  499. Can_TxMsg.Data[i] = Uart_Rece_BattCellU5_U8[i];
  500. }
  501. Can_TxMsg.stdIDH = Can_ID>>3;
  502. Can_TxMsg.stdIDL = Can_ID<<5;
  503. Can_TxMsg.DLC = 8;
  504. HAL_Can_Transmit(Can_TxMsg);
  505. break;
  506. }
  507. case 2:
  508. {
  509. Can_ID = 0x021;
  510. for (int i = 0; i < 8; i++)
  511. {
  512. Can_TxMsg.Data[i] = Uart_Rece_BattCellU9_U12[i];
  513. }
  514. Can_TxMsg.stdIDH = Can_ID>>3;
  515. Can_TxMsg.stdIDL = Can_ID<<5;
  516. Can_TxMsg.DLC = 8;
  517. HAL_Can_Transmit(Can_TxMsg);
  518. break;
  519. }
  520. case 3:
  521. {
  522. Can_ID = 0x031;
  523. for (int i = 0; i < 4; i++)
  524. {
  525. Can_TxMsg.Data[i] = Uart_Rece_BattCellU13_U14[i];
  526. }
  527. Can_TxMsg.Data[4] = 0x00;
  528. Can_TxMsg.Data[5] = 0x00;
  529. Can_TxMsg.Data[6] = 0x00;
  530. Can_TxMsg.Data[7] = 0x00;
  531. Can_TxMsg.stdIDH = Can_ID>>3;
  532. Can_TxMsg.stdIDL = Can_ID<<5;
  533. Can_TxMsg.DLC = 8;
  534. HAL_Can_Transmit(Can_TxMsg);
  535. break;
  536. }
  537. case 4:
  538. {
  539. Can_ID = 0x101;
  540. for (int i = 0; i < 4; i++)
  541. {
  542. Can_TxMsg.Data[i] = Uart_Rece_BattT[i*2+1];
  543. }
  544. Can_TxMsg.stdIDH = Can_ID>>3;
  545. Can_TxMsg.stdIDL = Can_ID<<5;
  546. Can_TxMsg.DLC = 8;
  547. HAL_Can_Transmit(Can_TxMsg);
  548. break;
  549. }
  550. case 5:
  551. {
  552. Can_ID = 0x201;
  553. Can_TxMsg.Data[0] = Uart_Rece_BattU>>8;
  554. Can_TxMsg.Data[1] = Uart_Rece_BattU;
  555. Can_TxMsg.Data[2] = 0x00;
  556. Can_TxMsg.Data[3] = 0x00;//外电压
  557. Can_TxMsg.Data[4] = 0x00;
  558. Can_TxMsg.Data[5] = 0x00;//累加电压
  559. Can_TxMsg.Data[6] = Uart_Rece_BattI>>8;
  560. Can_TxMsg.Data[7] = Uart_Rece_BattI;
  561. Can_TxMsg.stdIDH = Can_ID>>3;
  562. Can_TxMsg.stdIDL = Can_ID<<5;
  563. Can_TxMsg.DLC = 8;
  564. HAL_Can_Transmit(Can_TxMsg);
  565. break;
  566. }
  567. default:
  568. {
  569. PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
  570. }
  571. }
  572. send_index ++;
  573. break;
  574. }
  575. case PROCESS_STATE_SLEEP:
  576. {
  577. slpManPlatVoteEnableSleep(process1SlpHandler, SLP_SLP2_STATE);
  578. slpManPlatVoteDisableSleep(process1SlpHandler, SLP_HIB_STATE);
  579. while(1)
  580. {
  581. osDelay(3000);
  582. }
  583. break;
  584. }
  585. }
  586. }
  587. }
  588. /**
  589. \fn process0Init(void)
  590. \brief process0Init function.
  591. \return
  592. */
  593. void process0Init(void)
  594. {
  595. osThreadAttr_t task_attr;
  596. #ifndef USING_PRINTF
  597. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  598. {
  599. HAL_UART_RecvFlowControl(false);
  600. }
  601. #endif
  602. memset(&task_attr,0,sizeof(task_attr));
  603. memset(gProcessTaskStack0, 0xA5,PROC_TASK_STACK_SIZE);
  604. task_attr.name = "Process0AppTask";
  605. task_attr.stack_mem = gProcessTaskStack0;
  606. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  607. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  608. task_attr.cb_mem = &gProcessTask0;//task control block
  609. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  610. osThreadNew(process0AppTask, NULL, &task_attr);
  611. }
  612. /**
  613. \fn process1Init(void)
  614. \brief process1Init function.
  615. \return
  616. */
  617. void process1Init(void)
  618. {
  619. osThreadAttr_t task_attr;
  620. #ifndef USING_PRINTF
  621. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  622. {
  623. HAL_UART_RecvFlowControl(false);
  624. }
  625. #endif
  626. memset(&task_attr,0,sizeof(task_attr));
  627. memset(gProcessTaskStack1, 0xA5,PROC_TASK_STACK_SIZE);
  628. task_attr.name = "Process1AppTask";
  629. task_attr.stack_mem = gProcessTaskStack1;
  630. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  631. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  632. task_attr.cb_mem = &gProcessTask1;//task control block
  633. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  634. osThreadNew(process1AppTask, NULL, &task_attr);
  635. }
  636. /**
  637. \fn process2Init(void)
  638. \brief process2Init function.
  639. \return
  640. */
  641. void process2Init(void)
  642. {
  643. osThreadAttr_t task_attr;
  644. memset(&task_attr,0,sizeof(task_attr));
  645. memset(gProcessTaskStack2, 0xA5,PROC_TASK_STACK_SIZE);
  646. task_attr.name = "Process2AppTask";
  647. task_attr.stack_mem = gProcessTaskStack2;
  648. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  649. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  650. task_attr.cb_mem = &gProcessTask2;//task control block
  651. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  652. osThreadNew(process2AppTask, NULL, &task_attr);
  653. }
  654. /**
  655. \fn appInit(void)
  656. \brief appInit function.
  657. \return
  658. */
  659. void appInit(void *arg)
  660. {
  661. process0Init();//任务调度和检测程序
  662. process1Init();//Uart程序
  663. process2Init();//Can程序
  664. }
  665. /**
  666. \fn int main_entry(void)
  667. \brief main entry function.
  668. \return
  669. */
  670. void main_entry(void) {
  671. BSP_CommonInit();
  672. osKernelInitialize();
  673. registerAppEntry(appInit, NULL);
  674. if (osKernelGetState() == osKernelReady)
  675. {
  676. osKernelStart();
  677. }
  678. while(1);
  679. }