app_1_4.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
  191. slpManApplyPlatVoteHandle("process0slp",&process0SlpHandler);
  192. slpManSlpState_t slpstate = slpManGetLastSlpState();
  193. UINT8 Can_index = 0;
  194. UINT8 Uart_index = 0;
  195. uint32_t sleep_index = 0;
  196. PROC_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
  197. slpManPlatVoteDisableSleep(process0SlpHandler, SLP_SLP2_STATE);
  198. NetSocDisplay(0,LED_TURN_OFF);
  199. NetSocDisplay(1,LED_TURN_OFF);
  200. NetSocDisplay(2,LED_TURN_OFF);
  201. NetSocDisplay(3,LED_TURN_OFF);
  202. NetSocDisplay(4,LED_TURN_OFF);
  203. while(1)
  204. {
  205. switch(gProc0State)
  206. {
  207. case PROCESS_STATE_IDLE:
  208. {
  209. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_CHECK);
  210. NetSocDisplay(0,LED_TURN_ON);
  211. osDelay(100);//delay 100ms
  212. if(Uart_Rece_BattI!=0x0000)
  213. {
  214. PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
  215. }
  216. sleep_index++;
  217. if(sleep_index>=100)
  218. {
  219. NetSocDisplay(0,LED_TURN_OFF);
  220. sleep_index=0;
  221. PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
  222. }
  223. break;
  224. }
  225. case PROCESS_STATE_WORK:
  226. {
  227. osDelay(10);//10ms
  228. Can_index++;
  229. Uart_index++;
  230. if (Uart_index >10)//Uart 100ms 调用一次
  231. {
  232. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_WORK);
  233. Uart_index = 0;
  234. }
  235. if (Can_index >=100)//Can 1000ms 调用一次
  236. {
  237. PROC_Can_STATE_SWITCH(PROCESS_STATE_WORK);
  238. Can_index = 0;
  239. }
  240. if(Uart_Rece_BattI==0x0000)
  241. {
  242. sleep_index++;
  243. }
  244. else
  245. {
  246. sleep_index = 0;
  247. }
  248. if (sleep_index >=6000)
  249. {
  250. PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
  251. sleep_index = 0;
  252. }
  253. break;
  254. }
  255. case PROCESS_STATE_SLEEP:
  256. {
  257. slpManSlpState_t State;
  258. uint8_t cnt;
  259. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_SLEEP);
  260. PROC_Can_STATE_SWITCH(PROCESS_STATE_SLEEP);
  261. slpManPlatVoteEnableSleep(process0SlpHandler,SLP_SLP2_STATE);
  262. slpManPlatVoteDisableSleep(process0SlpHandler, SLP_HIB_STATE);
  263. slpManDeepSlpTimerStart(deepslpTimerID, 300000); // create a 10s timer, DeepSleep Timer is always oneshoot
  264. while(1) // now app can enter hib, but ps and phy maybe not, so wait here
  265. {
  266. osDelay(3000);
  267. }
  268. }
  269. }
  270. }
  271. }
  272. static void process1AppTask(void* arg)
  273. {
  274. USARTdrv->Initialize(USART_callback);
  275. USARTdrv->PowerControl(ARM_POWER_FULL);
  276. USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
  277. ARM_USART_DATA_BITS_8 |
  278. ARM_USART_PARITY_NONE |
  279. ARM_USART_STOP_BITS_1 |
  280. ARM_USART_FLOW_CONTROL_NONE, 9600);
  281. int Rece_index = 0;
  282. uint8_t *Uart_Reve_Point = NULL;
  283. Uart_Receive_Type Uart_Receive_Msg;
  284. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
  285. slpManPlatVoteDisableSleep(process2SlpHandler, SLP_SLP2_STATE);
  286. Uart_Receive_Msg.Bms_Address = 0x01;
  287. Uart_Receive_Msg.Bms_Read_Funcode = 0x03;
  288. while(1)
  289. {
  290. switch(gProc1State)
  291. {
  292. case PROCESS_Uart_STATE_IDLE:
  293. {
  294. NetSocDisplay(1,LED_TURN_ON);
  295. Rece_index = 0;
  296. break;
  297. }
  298. case PROCESS_Uart_STATE_CHECK:
  299. {
  300. NetSocDisplay(1,LED_TURN_OFF);
  301. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  302. Uart_Receive_Msg.Reg_Begin_L= 0x10;
  303. Uart_Receive_Msg.Reg_Num_H = 0x00;
  304. Uart_Receive_Msg.Reg_Num_L = 0x04;
  305. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  306. Uart_Rece_BattI = *(Uart_Reve_Point+0)<<8 | *(Uart_Reve_Point+1);
  307. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
  308. break;
  309. }
  310. case PROCESS_Uart_STATE_WORK:
  311. {
  312. switch(Rece_index)
  313. {
  314. case 0://读取电压1-4
  315. {
  316. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  317. Uart_Receive_Msg.Reg_Begin_L = 0x02;
  318. Uart_Receive_Msg.Reg_Num_H = 0x00;
  319. Uart_Receive_Msg.Reg_Num_L = 0x04;
  320. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  321. for(int i =0;i<8;i++)
  322. {
  323. Uart_Rece_BattCellU1_U4[i] = *(Uart_Reve_Point+i);
  324. }
  325. break;
  326. }
  327. case 1://读取电压5-8
  328. {
  329. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  330. Uart_Receive_Msg.Reg_Begin_L= 0x06;
  331. Uart_Receive_Msg.Reg_Num_H = 0x00;
  332. Uart_Receive_Msg.Reg_Num_L = 0x04;
  333. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  334. for(int i =0;i<8;i++)
  335. {
  336. Uart_Rece_BattCellU5_U8[i] = *(Uart_Reve_Point+i);
  337. }
  338. break;
  339. }
  340. case 2:
  341. {
  342. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  343. Uart_Receive_Msg.Reg_Begin_L= 0x0A;
  344. Uart_Receive_Msg.Reg_Num_H = 0x00;
  345. Uart_Receive_Msg.Reg_Num_L = 0x04;
  346. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  347. for(int i =0;i<8;i++)
  348. {
  349. Uart_Rece_BattCellU9_U12[i] = *(Uart_Reve_Point+i);
  350. }
  351. break;
  352. }
  353. case 3:
  354. {
  355. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  356. Uart_Receive_Msg.Reg_Begin_L= 0x0E;
  357. Uart_Receive_Msg.Reg_Num_H = 0x00;
  358. Uart_Receive_Msg.Reg_Num_L = 0x04;
  359. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  360. for(int i =0;i<8;i++)
  361. {
  362. Uart_Rece_BattCellU13_U14[i] = *(Uart_Reve_Point+i);
  363. }
  364. break;
  365. }
  366. case 4:
  367. {
  368. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  369. Uart_Receive_Msg.Reg_Begin_L= 0x14;
  370. Uart_Receive_Msg.Reg_Num_H = 0x00;
  371. Uart_Receive_Msg.Reg_Num_L = 0x04;
  372. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  373. for(int i =0;i<8;i++)
  374. {
  375. Uart_Rece_BattT[i] = *(Uart_Reve_Point+i);
  376. }
  377. break;
  378. }
  379. case 5:
  380. {
  381. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  382. Uart_Receive_Msg.Reg_Begin_L= 0x10;
  383. Uart_Receive_Msg.Reg_Num_H = 0x00;
  384. Uart_Receive_Msg.Reg_Num_L = 0x04;
  385. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  386. Uart_Rece_BattI = *(Uart_Reve_Point+0)<<8 | *(Uart_Reve_Point+1);
  387. for (int i = 0; i < 6; i++)
  388. {
  389. Uart_Rece_Batt_states[i] = *(Uart_Reve_Point+2+i);
  390. }
  391. break;
  392. }
  393. case 6:
  394. {
  395. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  396. Uart_Receive_Msg.Reg_Begin_L= 0x2B;
  397. Uart_Receive_Msg.Reg_Num_H = 0x00;
  398. Uart_Receive_Msg.Reg_Num_L = 0x04;
  399. Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg);
  400. Uart_Rece_BattU = *(Uart_Reve_Point+0)<<8 | *(Uart_Reve_Point+1);
  401. Uart_Rece_Batt_MaxcellU = *(Uart_Reve_Point+2)<<8 | *(Uart_Reve_Point+3);
  402. Uart_Rece_Batt_MincellU = *(Uart_Reve_Point+4)<<8 | *(Uart_Reve_Point+5);
  403. break;
  404. }
  405. default:
  406. {
  407. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
  408. break;
  409. }
  410. }
  411. Rece_index++;
  412. break;
  413. }
  414. case PROCESS_Uart_STATE_SLEEP:
  415. {
  416. slpManPlatVoteEnableSleep(process2SlpHandler, SLP_SLP2_STATE);
  417. slpManPlatVoteDisableSleep(process2SlpHandler, SLP_HIB_STATE);
  418. while(1)
  419. {
  420. osDelay(3000);
  421. }
  422. //此处休眠
  423. break;
  424. }
  425. }
  426. }
  427. }
  428. static void process2AppTask(void* arg)
  429. {
  430. PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
  431. uint32_t Can_ID;
  432. NVIC_EnableIRQ(PadWakeup1_IRQn);
  433. Can_InitType param;
  434. Can_TxMsgType Can_TxMsg;
  435. param.baudrate = CAN_500Kbps;
  436. param.mode = REQOP_NORMAL;
  437. param.TxStdIDH = 0x00;
  438. param.TxStdIDL = 0x00;
  439. param.RxStdIDH[0] = 0x00;
  440. param.RxStdIDL[0] = 0x00;
  441. /*stdid 0000 0000 001x*/
  442. param.RxStdIDH[1] = 0x00;
  443. param.RxStdIDL[1] = 0x20;
  444. /*stdid 0000 0000 010x */
  445. param.RxStdIDH[2] = 0x00;
  446. param.RxStdIDL[2] = 0x40;
  447. /*stdid 0000 0000 011x*/
  448. param.RxStdIDH[3] = 0x00;
  449. param.RxStdIDL[3] =0x60;
  450. /*stdid 0000 0000 100x */
  451. param.RxStdIDH[4] = 0x00;
  452. param.RxStdIDL[4] = 0x80;
  453. /*stdid 0000 0000 101x*/
  454. param.RxStdIDH[5] = 0x00;
  455. param.RxStdIDL[5] =0xa0;
  456. param.packType = STD_PACK;
  457. HAL_Can_Init(param);
  458. int send_index = 0;
  459. slpManPlatVoteDisableSleep(process1SlpHandler, SLP_SLP2_STATE);
  460. while(1)
  461. {
  462. switch(gProc2State)
  463. {
  464. case PROCESS_STATE_IDLE:
  465. {
  466. send_index = 0;
  467. break;
  468. }
  469. case PROCESS_STATE_WORK:
  470. {
  471. switch(send_index)
  472. {
  473. case 0:
  474. {
  475. Can_ID = 0x001;
  476. for (int i = 0; i < 8; i++)
  477. {
  478. Can_TxMsg.Data[i] = Uart_Rece_BattCellU1_U4[i];
  479. }
  480. Can_TxMsg.stdIDH = Can_ID>>3;
  481. Can_TxMsg.stdIDL = Can_ID<<5;
  482. Can_TxMsg.DLC = 8;
  483. HAL_Can_Transmit(Can_TxMsg);
  484. break;
  485. }
  486. case 1:
  487. {
  488. Can_ID = 0x013;
  489. for (int i = 0; i < 8; i++)
  490. {
  491. Can_TxMsg.Data[i] = Uart_Rece_BattCellU5_U8[i];
  492. }
  493. Can_TxMsg.stdIDH = Can_ID>>3;
  494. Can_TxMsg.stdIDL = Can_ID<<5;
  495. Can_TxMsg.DLC = 8;
  496. HAL_Can_Transmit(Can_TxMsg);
  497. break;
  498. }
  499. case 2:
  500. {
  501. Can_ID = 0x021;
  502. for (int i = 0; i < 8; i++)
  503. {
  504. Can_TxMsg.Data[i] = Uart_Rece_BattCellU9_U12[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 3:
  513. {
  514. Can_ID = 0x031;
  515. for (int i = 0; i < 4; i++)
  516. {
  517. Can_TxMsg.Data[i] = Uart_Rece_BattCellU13_U14[i];
  518. }
  519. Can_TxMsg.Data[4] = 0x00;
  520. Can_TxMsg.Data[5] = 0x00;
  521. Can_TxMsg.Data[6] = 0x00;
  522. Can_TxMsg.Data[7] = 0x00;
  523. Can_TxMsg.stdIDH = Can_ID>>3;
  524. Can_TxMsg.stdIDL = Can_ID<<5;
  525. Can_TxMsg.DLC = 8;
  526. HAL_Can_Transmit(Can_TxMsg);
  527. break;
  528. }
  529. case 4:
  530. {
  531. Can_ID = 0x101;
  532. for (int i = 0; i < 4; i++)
  533. {
  534. Can_TxMsg.Data[i] = Uart_Rece_BattT[i*2+1];
  535. }
  536. Can_TxMsg.stdIDH = Can_ID>>3;
  537. Can_TxMsg.stdIDL = Can_ID<<5;
  538. Can_TxMsg.DLC = 8;
  539. HAL_Can_Transmit(Can_TxMsg);
  540. break;
  541. }
  542. case 5:
  543. {
  544. Can_ID = 0x201;
  545. Can_TxMsg.Data[0] = Uart_Rece_BattU>>8;
  546. Can_TxMsg.Data[1] = Uart_Rece_BattU;
  547. Can_TxMsg.Data[2] = 0x00;
  548. Can_TxMsg.Data[3] = 0x00;//外电压
  549. Can_TxMsg.Data[4] = 0x00;
  550. Can_TxMsg.Data[5] = 0x00;//累加电压
  551. Can_TxMsg.Data[6] = Uart_Rece_BattI>>8;
  552. Can_TxMsg.Data[7] = Uart_Rece_BattI;
  553. Can_TxMsg.stdIDH = Can_ID>>3;
  554. Can_TxMsg.stdIDL = Can_ID<<5;
  555. Can_TxMsg.DLC = 8;
  556. HAL_Can_Transmit(Can_TxMsg);
  557. break;
  558. }
  559. default:
  560. {
  561. PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
  562. }
  563. }
  564. send_index ++;
  565. break;
  566. }
  567. case PROCESS_STATE_SLEEP:
  568. {
  569. slpManPlatVoteEnableSleep(process1SlpHandler, SLP_SLP2_STATE);
  570. slpManPlatVoteDisableSleep(process1SlpHandler, SLP_HIB_STATE);
  571. while(1)
  572. {
  573. osDelay(3000);
  574. }
  575. break;
  576. }
  577. }
  578. }
  579. }
  580. /**
  581. \fn process0Init(void)
  582. \brief process0Init function.
  583. \return
  584. */
  585. void process0Init(void)
  586. {
  587. osThreadAttr_t task_attr;
  588. #ifndef USING_PRINTF
  589. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  590. {
  591. HAL_UART_RecvFlowControl(false);
  592. }
  593. #endif
  594. memset(&task_attr,0,sizeof(task_attr));
  595. memset(gProcessTaskStack0, 0xA5,PROC_TASK_STACK_SIZE);
  596. task_attr.name = "Process0AppTask";
  597. task_attr.stack_mem = gProcessTaskStack0;
  598. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  599. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  600. task_attr.cb_mem = &gProcessTask0;//task control block
  601. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  602. osThreadNew(process0AppTask, NULL, &task_attr);
  603. }
  604. /**
  605. \fn process1Init(void)
  606. \brief process1Init function.
  607. \return
  608. */
  609. void process1Init(void)
  610. {
  611. osThreadAttr_t task_attr;
  612. #ifndef USING_PRINTF
  613. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  614. {
  615. HAL_UART_RecvFlowControl(false);
  616. }
  617. #endif
  618. memset(&task_attr,0,sizeof(task_attr));
  619. memset(gProcessTaskStack1, 0xA5,PROC_TASK_STACK_SIZE);
  620. task_attr.name = "Process1AppTask";
  621. task_attr.stack_mem = gProcessTaskStack1;
  622. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  623. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  624. task_attr.cb_mem = &gProcessTask1;//task control block
  625. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  626. osThreadNew(process1AppTask, NULL, &task_attr);
  627. }
  628. /**
  629. \fn process2Init(void)
  630. \brief process2Init function.
  631. \return
  632. */
  633. void process2Init(void)
  634. {
  635. osThreadAttr_t task_attr;
  636. memset(&task_attr,0,sizeof(task_attr));
  637. memset(gProcessTaskStack2, 0xA5,PROC_TASK_STACK_SIZE);
  638. task_attr.name = "Process2AppTask";
  639. task_attr.stack_mem = gProcessTaskStack2;
  640. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  641. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  642. task_attr.cb_mem = &gProcessTask2;//task control block
  643. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  644. osThreadNew(process2AppTask, NULL, &task_attr);
  645. }
  646. /**
  647. \fn appInit(void)
  648. \brief appInit function.
  649. \return
  650. */
  651. void appInit(void *arg)
  652. {
  653. process0Init();//任务调度和检测程序
  654. process1Init();//Uart程序
  655. process2Init();//Can程序
  656. }
  657. /**
  658. \fn int main_entry(void)
  659. \brief main entry function.
  660. \return
  661. */
  662. void main_entry(void) {
  663. BSP_CommonInit();
  664. osKernelInitialize();
  665. registerAppEntry(appInit, NULL);
  666. if (osKernelGetState() == osKernelReady)
  667. {
  668. osKernelStart();
  669. }
  670. while(1);
  671. }