app_0_5.c 16 KB

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