app_0_3.c 15 KB

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