app_1_6.c 24 KB

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