app _0_4.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 RECV_BUFFER_LEN (16)
  34. #define SEND_BUFFER_LEN (16)
  35. #define Uart_Send_LEN (8)
  36. #define Uart_Rece_LEN (16)
  37. #define RTE_UART_RX_IO_MODE RTE_UART1_RX_IO_MODE
  38. //statement variable
  39. extern ARM_DRIVER_USART Driver_USART1;
  40. static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
  41. /** \brief usart receive buffer */
  42. uint8_t recBuffer[RECV_BUFFER_LEN];
  43. /** \brief usart send buffer */
  44. uint8_t sendBuffer[RECV_BUFFER_LEN];
  45. /** \brief receive timeout flag */
  46. volatile bool isRecvTimeout = false;
  47. /** \brief receive complete flag */
  48. volatile bool isRecvComplete = false;
  49. uint8_t Uart_Rece_buffer[Uart_Rece_LEN];
  50. uint8_t Uart_Data_buffer[8];
  51. /** \brief 电压传输 */
  52. UINT8 *Uart_Rece_BattCellU1_U4;
  53. UINT8 *Uart_Rece_BattCellU5_U8;
  54. UINT8 *Uart_Rece_BattCellU9_U12;
  55. UINT8 *Uart_Rece_BattCellU13_U14;
  56. UINT8 *Uart_Rece_BattT;
  57. volatile bool Can_Task_Flag = false;
  58. volatile bool Uart_Task_Flag = false;
  59. typedef enum
  60. {
  61. PROCESS1_STATE_IDLE = 0,
  62. PROCESS1_STATE_WORK,
  63. PROCESS1_STATE_SLEEP
  64. }process1SM;
  65. typedef enum
  66. {
  67. PROCESS2_STATE_IDLE = 0,
  68. PROCESS2_STATE_WORK,
  69. PROCESS2_STATE_SLEEP
  70. }process2SM;
  71. static StaticTask_t gProcessTask0;
  72. static UINT8 gProcessTaskStack0[PROC_TASK_STACK_SIZE];
  73. static StaticTask_t gProcessTask1;
  74. static UINT8 gProcessTaskStack1[PROC_TASK_STACK_SIZE];
  75. static StaticTask_t gProcessTask2;
  76. static UINT8 gProcessTaskStack2[PROC_TASK_STACK_SIZE];
  77. static process1SM gProc1State = PROCESS1_STATE_IDLE;
  78. static process2SM gProc2State = PROCESS2_STATE_IDLE;
  79. #define PROC1_STATE_SWITCH(a) (gProc1State = a)
  80. #define PROC2_STATE_SWITCH(a) (gProc2State = a)
  81. unsigned int crc_chk(unsigned char* data, unsigned char length)
  82. {
  83. int j;
  84. unsigned int reg_crc=0xFFFF;
  85. while(length--)
  86. {
  87. reg_crc ^= *data++;
  88. for(j=0;j<8;j++)
  89. {
  90. if(reg_crc & 0x01)
  91. {
  92. reg_crc=(reg_crc>>1) ^ 0xA001;
  93. }
  94. else
  95. {
  96. reg_crc=reg_crc >>1;
  97. }
  98. }
  99. }
  100. return reg_crc;
  101. }
  102. uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg)
  103. {
  104. uint16_t CRC_chk_buffer;
  105. uint8_t Uart_Send_buffer[Uart_Send_LEN];
  106. uint8_t Rece_Data_Len;
  107. Uart_Send_buffer[0] = Uart_Receive_Msg.Bms_Address;
  108. Uart_Send_buffer[1] = Uart_Receive_Msg.Bms_Read_Funcode;
  109. Uart_Send_buffer[2] = Uart_Receive_Msg.Reg_Begin_H;
  110. Uart_Send_buffer[3] = Uart_Receive_Msg.Reg_Begin_L;
  111. Uart_Send_buffer[4] = Uart_Receive_Msg.Reg_Num_H;
  112. Uart_Send_buffer[5] = Uart_Receive_Msg.Reg_Num_L;
  113. CRC_chk_buffer = crc_chk(Uart_Send_buffer,6);
  114. Uart_Send_buffer[6] = CRC_chk_buffer;
  115. Uart_Send_buffer[7] = CRC_chk_buffer>>8;
  116. USARTdrv->Send(Uart_Send_buffer,8);
  117. Uart_Rece_buffer[0]=0xfe;
  118. uint8_t timeout=0;
  119. while((timeout<10)&&(Uart_Rece_buffer[0]!=0x01))
  120. {
  121. USARTdrv->Receive(Uart_Rece_buffer,13);
  122. timeout++;
  123. osDelay(10);
  124. }
  125. Rece_Data_Len = 11;
  126. if (Uart_Rece_buffer[0]==0xfe)//没收到数据,全部为空值
  127. {
  128. Uart_Data_buffer[0] = 0x00;
  129. Uart_Data_buffer[1] = 0x00;
  130. Uart_Data_buffer[2] = 0x00;
  131. Uart_Data_buffer[3] = 0x00;
  132. Uart_Data_buffer[4] = 0x00;
  133. Uart_Data_buffer[5] = 0x00;
  134. Uart_Data_buffer[6] = 0x00;
  135. Uart_Data_buffer[7] = 0x00;
  136. return Uart_Data_buffer;
  137. }
  138. else
  139. {
  140. CRC_chk_buffer =Uart_Rece_buffer[12]<<8|Uart_Rece_buffer[11];
  141. if (CRC_chk_buffer == crc_chk(Uart_Rece_buffer,11))//满足校验
  142. {
  143. for (uint8_t i = 0; i < 8; i++)
  144. {
  145. Uart_Data_buffer[i]=Uart_Rece_buffer[i+3];
  146. }
  147. return Uart_Data_buffer;
  148. }
  149. else //接收数据的校验不过
  150. {
  151. Uart_Data_buffer[0] = 0xff;
  152. Uart_Data_buffer[1] = 0xff;
  153. Uart_Data_buffer[2] = 0xff;
  154. Uart_Data_buffer[3] = 0xff;
  155. Uart_Data_buffer[4] = 0xff;
  156. Uart_Data_buffer[5] = 0xff;
  157. Uart_Data_buffer[6] = 0xff;
  158. Uart_Data_buffer[7] = 0xff;
  159. return Uart_Data_buffer;
  160. }
  161. }
  162. }
  163. void USART_callback(uint32_t event)
  164. {
  165. if(event & ARM_USART_EVENT_RX_TIMEOUT)
  166. {
  167. isRecvTimeout = true;
  168. }
  169. if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
  170. {
  171. isRecvComplete = true;
  172. }
  173. }
  174. static void process0AppTask(void* arg)
  175. {
  176. UINT8 Can_index = 0;
  177. UINT8 Uart_index = 0;
  178. Can_Task_Flag = false;
  179. Uart_Task_Flag = false;
  180. while(1)
  181. {
  182. osDelay(10);//10ms
  183. Can_index++;
  184. Uart_index++;
  185. if (Uart_index >=10)//Uart 100ms 调用一次
  186. {
  187. Uart_Task_Flag = true;
  188. Uart_index = 0;
  189. }
  190. if (Can_index >=10)//Can 100ms 调用一次
  191. {
  192. Can_Task_Flag = true;
  193. Can_index = 0;
  194. }
  195. }
  196. }
  197. static void process1AppTask(void* arg)
  198. {
  199. PROC1_STATE_SWITCH(PROCESS1_STATE_IDLE);
  200. uint32_t Can_ID;
  201. UINT8 value;
  202. NVIC_EnableIRQ(PadWakeup0_IRQn);
  203. Can_InitType param;
  204. Can_TxMsgType Can_TxMsg;
  205. param.baudrate = CAN_500Kbps;
  206. param.mode = REQOP_NORMAL;
  207. param.TxStdIDH = 0x00;
  208. param.TxStdIDL = 0x00;
  209. param.RxStdIDH[0] = 0x00;
  210. param.RxStdIDL[0] = 0x00;
  211. /*stdid 0000 0000 001x*/
  212. param.RxStdIDH[1] = 0x00;
  213. param.RxStdIDL[1] = 0x20;
  214. /*stdid 0000 0000 010x */
  215. param.RxStdIDH[2] = 0x00;
  216. param.RxStdIDL[2] = 0x40;
  217. /*stdid 0000 0000 011x*/
  218. param.RxStdIDH[3] = 0x00;
  219. param.RxStdIDL[3] =0x60;
  220. /*stdid 0000 0000 100x */
  221. param.RxStdIDH[4] = 0x00;
  222. param.RxStdIDL[4] = 0x80;
  223. /*stdid 0000 0000 101x*/
  224. param.RxStdIDH[5] = 0x00;
  225. param.RxStdIDL[5] =0xa0;
  226. param.packType = STD_PACK;
  227. HAL_Can_Init(param);
  228. int send_index = 0;
  229. while(1)
  230. {
  231. switch(gProc1State)
  232. {
  233. case PROCESS1_STATE_IDLE:
  234. {
  235. if(Can_Task_Flag)
  236. {
  237. PROC1_STATE_SWITCH(PROCESS1_STATE_WORK);
  238. FaultDisplay(LED_TURN_ON);
  239. }
  240. break;
  241. }
  242. case PROCESS1_STATE_WORK:
  243. {
  244. switch(send_index)
  245. {
  246. case 0:
  247. {
  248. Can_ID = 0x001;
  249. Can_TxMsg.Data[0] = *(Uart_Rece_BattCellU1_U4+0);
  250. Can_TxMsg.Data[1] = *(Uart_Rece_BattCellU1_U4+1);
  251. Can_TxMsg.Data[2] = *(Uart_Rece_BattCellU1_U4+2);
  252. Can_TxMsg.Data[3] = *(Uart_Rece_BattCellU1_U4+3);
  253. Can_TxMsg.Data[4] = *(Uart_Rece_BattCellU1_U4+4);
  254. Can_TxMsg.Data[5] = *(Uart_Rece_BattCellU1_U4+5);
  255. Can_TxMsg.Data[6] = *(Uart_Rece_BattCellU1_U4+6);
  256. Can_TxMsg.Data[7] = *(Uart_Rece_BattCellU1_U4+7);
  257. break;
  258. }
  259. case 1:
  260. {
  261. Can_ID = 0x011;
  262. Can_TxMsg.Data[0] = *(Uart_Rece_BattCellU5_U8+0);
  263. Can_TxMsg.Data[1] = *(Uart_Rece_BattCellU5_U8+1);
  264. Can_TxMsg.Data[2] = *(Uart_Rece_BattCellU5_U8+2);
  265. Can_TxMsg.Data[3] = *(Uart_Rece_BattCellU5_U8+3);
  266. Can_TxMsg.Data[4] = *(Uart_Rece_BattCellU5_U8+4);
  267. Can_TxMsg.Data[5] = *(Uart_Rece_BattCellU5_U8+5);
  268. Can_TxMsg.Data[6] = *(Uart_Rece_BattCellU5_U8+6);
  269. Can_TxMsg.Data[7] = *(Uart_Rece_BattCellU5_U8+7);
  270. break;
  271. }
  272. case 2:
  273. {
  274. Can_ID = 0x021;
  275. Can_TxMsg.Data[0] = *(Uart_Rece_BattCellU9_U12+0);
  276. Can_TxMsg.Data[1] = *(Uart_Rece_BattCellU9_U12+1);
  277. Can_TxMsg.Data[2] = *(Uart_Rece_BattCellU9_U12+2);
  278. Can_TxMsg.Data[3] = *(Uart_Rece_BattCellU9_U12+3);
  279. Can_TxMsg.Data[4] = *(Uart_Rece_BattCellU9_U12+4);
  280. Can_TxMsg.Data[5] = *(Uart_Rece_BattCellU9_U12+5);
  281. Can_TxMsg.Data[6] = *(Uart_Rece_BattCellU9_U12+6);
  282. Can_TxMsg.Data[7] = *(Uart_Rece_BattCellU9_U12+7);
  283. break;
  284. }
  285. case 3:
  286. {
  287. Can_ID = 0x031;
  288. Can_TxMsg.Data[0] = *(Uart_Rece_BattCellU13_U14+0);
  289. Can_TxMsg.Data[1] = *(Uart_Rece_BattCellU13_U14+1);
  290. Can_TxMsg.Data[2] = *(Uart_Rece_BattCellU13_U14+2);
  291. Can_TxMsg.Data[3] = *(Uart_Rece_BattCellU13_U14+3);
  292. Can_TxMsg.Data[4] = 0x00;
  293. Can_TxMsg.Data[5] = 0x00;
  294. Can_TxMsg.Data[6] = 0x00;
  295. Can_TxMsg.Data[7] = 0x00;
  296. break;
  297. }
  298. // case 4:
  299. // {
  300. // Can_ID = 0x041;
  301. // Can_TxMsg.Data[0] = send_index;
  302. // Can_TxMsg.Data[1] = 0x00;
  303. // Can_TxMsg.Data[2] = 0x00;
  304. // Can_TxMsg.Data[3] = 0x00;
  305. // Can_TxMsg.Data[4] = 0x00;
  306. // Can_TxMsg.Data[5] = 0x00;
  307. // Can_TxMsg.Data[6] = 0x00;
  308. // Can_TxMsg.Data[7] = 0x00;
  309. // break;
  310. // }
  311. }
  312. Can_TxMsg.stdIDH = Can_ID>>3;
  313. Can_TxMsg.stdIDL = Can_ID<<5;
  314. Can_TxMsg.DLC = 8;
  315. HAL_Can_Transmit(Can_TxMsg);
  316. send_index++;
  317. if (send_index >3)
  318. {
  319. send_index = 0;
  320. FaultDisplay(LED_TURN_OFF);
  321. Can_Task_Flag = false;
  322. PROC1_STATE_SWITCH(PROCESS1_STATE_IDLE);
  323. }
  324. break;
  325. }
  326. case PROCESS1_STATE_SLEEP:
  327. {
  328. break;
  329. }
  330. }
  331. }
  332. }
  333. static void process2AppTask(void* arg)
  334. {
  335. USARTdrv->Initialize(USART_callback);
  336. USARTdrv->PowerControl(ARM_POWER_FULL);
  337. USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
  338. ARM_USART_DATA_BITS_8 |
  339. ARM_USART_PARITY_NONE |
  340. ARM_USART_STOP_BITS_1 |
  341. ARM_USART_FLOW_CONTROL_NONE, 9600);
  342. uint8_t *chk_point;
  343. int Rece_index = 0;
  344. Uart_Receive_Type Uart_Receive_Msg;
  345. PROC2_STATE_SWITCH(PROCESS2_STATE_IDLE);
  346. while(1)
  347. {
  348. switch(gProc2State)
  349. {
  350. case PROCESS2_STATE_IDLE:
  351. {
  352. if(Uart_Task_Flag)
  353. {
  354. NetSocDisplay(3,LED_TURN_ON);
  355. Rece_index = 0;
  356. PROC2_STATE_SWITCH(PROCESS2_STATE_WORK);
  357. }
  358. break;
  359. }
  360. case PROCESS2_STATE_WORK:
  361. {
  362. Uart_Receive_Msg.Bms_Address = 0x01;
  363. Uart_Receive_Msg.Bms_Read_Funcode = 0x03;
  364. switch(Rece_index)
  365. {
  366. case 0://读取电压1-4
  367. {
  368. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  369. Uart_Receive_Msg.Reg_Begin_L = 0x02;
  370. Uart_Receive_Msg.Reg_Num_H = 0x00;
  371. Uart_Receive_Msg.Reg_Num_L = 0x04;
  372. Uart_Rece_BattCellU1_U4 = Uart_Receive_func(Uart_Receive_Msg);
  373. break;
  374. }
  375. case 1://读取电压5-8
  376. {
  377. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  378. Uart_Receive_Msg.Reg_Begin_L= 0x06;
  379. Uart_Receive_Msg.Reg_Num_H = 0x00;
  380. Uart_Receive_Msg.Reg_Num_L = 0x04;
  381. Uart_Rece_BattCellU5_U8 = Uart_Receive_func(Uart_Receive_Msg);
  382. break;
  383. }
  384. case 2:
  385. {
  386. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  387. Uart_Receive_Msg.Reg_Begin_L= 0x0A;
  388. Uart_Receive_Msg.Reg_Num_H = 0x00;
  389. Uart_Receive_Msg.Reg_Num_L = 0x04;
  390. Uart_Rece_BattCellU9_U12 = Uart_Receive_func(Uart_Receive_Msg);
  391. break;
  392. }
  393. case 3:
  394. {
  395. Uart_Receive_Msg.Reg_Begin_H = 0x00;
  396. Uart_Receive_Msg.Reg_Begin_L= 0x0E;
  397. Uart_Receive_Msg.Reg_Num_H = 0x00;
  398. Uart_Receive_Msg.Reg_Num_L = 0x04;
  399. Uart_Rece_BattCellU13_U14 = Uart_Receive_func(Uart_Receive_Msg);
  400. break;
  401. }
  402. }
  403. Rece_index++;
  404. if (Rece_index>3)
  405. {
  406. Rece_index=0;
  407. Uart_Task_Flag = false;
  408. NetSocDisplay(3,LED_TURN_OFF);
  409. PROC2_STATE_SWITCH(PROCESS2_STATE_IDLE);
  410. }
  411. break;
  412. }
  413. case PROCESS2_STATE_SLEEP:
  414. {
  415. //此处写休眠程序
  416. break;
  417. }
  418. }
  419. }
  420. }
  421. /**
  422. \fn process0Init(void)
  423. \brief process0Init function.
  424. \return
  425. */
  426. void process0Init(void)
  427. {
  428. osThreadAttr_t task_attr;
  429. #ifndef USING_PRINTF
  430. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  431. {
  432. HAL_UART_RecvFlowControl(false);
  433. }
  434. #endif
  435. memset(&task_attr,0,sizeof(task_attr));
  436. memset(gProcessTaskStack0, 0xA5,PROC_TASK_STACK_SIZE);
  437. task_attr.name = "Process0AppTask";
  438. task_attr.stack_mem = gProcessTaskStack0;
  439. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  440. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  441. task_attr.cb_mem = &gProcessTask0;//task control block
  442. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  443. osThreadNew(process0AppTask, NULL, &task_attr);
  444. }
  445. /**
  446. \fn process1Init(void)
  447. \brief process1Init function.
  448. \return
  449. */
  450. void process1Init(void)
  451. {
  452. osThreadAttr_t task_attr;
  453. #ifndef USING_PRINTF
  454. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  455. {
  456. HAL_UART_RecvFlowControl(false);
  457. }
  458. #endif
  459. memset(&task_attr,0,sizeof(task_attr));
  460. memset(gProcessTaskStack1, 0xA5,PROC_TASK_STACK_SIZE);
  461. task_attr.name = "Process1AppTask";
  462. task_attr.stack_mem = gProcessTaskStack1;
  463. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  464. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  465. task_attr.cb_mem = &gProcessTask1;//task control block
  466. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  467. osThreadNew(process1AppTask, NULL, &task_attr);
  468. }
  469. /**
  470. \fn process2Init(void)
  471. \brief process2Init function.
  472. \return
  473. */
  474. void process2Init(void)
  475. {
  476. osThreadAttr_t task_attr;
  477. memset(&task_attr,0,sizeof(task_attr));
  478. memset(gProcessTaskStack2, 0xA5,PROC_TASK_STACK_SIZE);
  479. task_attr.name = "Process2AppTask";
  480. task_attr.stack_mem = gProcessTaskStack2;
  481. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  482. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  483. task_attr.cb_mem = &gProcessTask2;//task control block
  484. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  485. osThreadNew(process2AppTask, NULL, &task_attr);
  486. }
  487. /**
  488. \fn appInit(void)
  489. \brief appInit function.
  490. \return
  491. */
  492. void appInit(void *arg)
  493. {
  494. process0Init();
  495. process1Init();
  496. process2Init();
  497. }
  498. /**
  499. \fn int main_entry(void)
  500. \brief main entry function.
  501. \return
  502. */
  503. void main_entry(void) {
  504. BSP_CommonInit();
  505. osKernelInitialize();
  506. registerAppEntry(appInit, NULL);
  507. if (osKernelGetState() == osKernelReady)
  508. {
  509. osKernelStart();
  510. }
  511. while(1);
  512. }