app_0_2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. volatile bool Rece_complete_flag = false;
  50. uint8_t Uart_Send_buffer[Uart_Send_LEN];
  51. uint8_t Uart_Send_reg[Uart_Send_LEN];
  52. uint8_t Uart_Rece_CRCbuffer[Uart_Rece_LEN];
  53. static UINT8 Uart_BMS_Address = 0x01;
  54. static UINT8 Uart_BMS_read_func = 0x03;
  55. volatile uint8_t Uart_Send_reg_beginH;
  56. volatile uint8_t Uart_Send_reg_beginL;
  57. volatile uint8_t Uart_Send_reg_numH;
  58. volatile uint8_t Uart_Send_reg_numL;
  59. volatile uint16_t CRC_chk_Sebuffer;
  60. volatile uint16_t CRC_chk_Rebuffer;
  61. volatile uint8_t CAN_stdID;
  62. /** \brief 电压传输 */
  63. UINT8 *Uart_Rece_battv1_v4;
  64. UINT8 *Uart_Rece_battv5_v8;
  65. UINT8 *Uart_Rece_battv8_v12;
  66. UINT8 *Uart_Rece_battv12_v16;
  67. volatile uint8_t CAN_ID_0[8];
  68. volatile uint8_t CAN_ID_1[8];
  69. volatile uint8_t CAN_ID_2[8];
  70. volatile uint8_t CAN_ID_3[8];
  71. int send_index = 0;
  72. typedef enum
  73. {
  74. PROCESS_STATE_IDLE = 0,
  75. PROCESS_STATE_WORK1,
  76. PROCESS_STATE_WORK2,
  77. PROCESS_STATE_WORK3,
  78. PROCESS_STATE_SLEEP
  79. }processSM;
  80. static StaticTask_t gProcessTask0;
  81. static UINT8 gProcessTaskStack0[PROC_TASK_STACK_SIZE];
  82. static StaticTask_t gProcessTask1;
  83. static UINT8 gProcessTaskStack1[PROC_TASK_STACK_SIZE];
  84. static StaticTask_t gProcessTask2;
  85. static UINT8 gProcessTaskStack2[PROC_TASK_STACK_SIZE];
  86. static processSM gProcState = PROCESS_STATE_IDLE;
  87. #define PROC_STATE_SWITCH(a) (gProcState = a)
  88. unsigned int crc_chk(unsigned char* data, unsigned char length)
  89. {
  90. int j;
  91. unsigned int reg_crc=0xFFFF;
  92. while(length--)
  93. {
  94. reg_crc ^= *data++;
  95. for(j=0;j<8;j++)
  96. {
  97. if(reg_crc & 0x01)
  98. {
  99. reg_crc=(reg_crc>>1) ^ 0xA001;
  100. }
  101. else
  102. {
  103. reg_crc=reg_crc >>1;
  104. }
  105. }
  106. }
  107. return reg_crc;
  108. }
  109. void LED_Control_func(Void)
  110. {
  111. for (int i = 0; i <=3; i++)
  112. {
  113. NetSocDisplay(i,LED_TURN_ON);
  114. osDelay(100/portTICK_PERIOD_MS);
  115. }
  116. for (int i = 3; i >= 0; i--)
  117. {
  118. NetSocDisplay(i,LED_TURN_OFF);
  119. osDelay(100/portTICK_PERIOD_MS);
  120. }
  121. }
  122. uint8_t* Uart_Receive_func(Void)
  123. {
  124. static uint8_t Uart_Rece_buffer[Uart_Rece_LEN];
  125. Uart_Send_buffer[0] = Uart_BMS_Address;
  126. Uart_Send_buffer[1] = Uart_BMS_read_func;
  127. Uart_Send_buffer[2] = Uart_Send_reg_beginH;
  128. Uart_Send_buffer[3] = Uart_Send_reg_beginL;
  129. Uart_Send_buffer[4] = Uart_Send_reg_numH;
  130. Uart_Send_buffer[5] = Uart_Send_reg_numL;
  131. CRC_chk_Sebuffer = crc_chk(Uart_Send_buffer,6);
  132. Uart_Send_buffer[6] = CRC_chk_Sebuffer;
  133. Uart_Send_buffer[7] = CRC_chk_Sebuffer>>8;
  134. Uart_Rece_buffer[0]= 0x00;
  135. USARTdrv->Send(Uart_Send_buffer,Uart_Send_LEN );
  136. USARTdrv->Receive(Uart_Rece_buffer,Uart_Rece_LEN);
  137. for(int i =0;i<11;i++)
  138. {
  139. Uart_Rece_CRCbuffer[i] = Uart_Rece_buffer[i];
  140. }
  141. CRC_chk_Rebuffer = crc_chk(Uart_Rece_CRCbuffer,11);
  142. if (Uart_Rece_buffer[11]==CRC_chk_Rebuffer && Uart_Rece_buffer[12]==CRC_chk_Rebuffer>>8)
  143. {
  144. return Uart_Rece_buffer;
  145. }
  146. else
  147. {
  148. NetSocDisplay(3,LED_TURN_OFF);
  149. for (int i = 0; i < 16; i++)
  150. {
  151. Uart_Rece_buffer[i]=0xff;
  152. }
  153. return Uart_Rece_buffer;
  154. }
  155. //接收数据的校验
  156. }
  157. void USART_callback(uint32_t event)
  158. {
  159. if(event & ARM_USART_EVENT_RX_TIMEOUT)
  160. {
  161. isRecvTimeout = true;
  162. }
  163. if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
  164. {
  165. isRecvComplete = true;
  166. }
  167. }
  168. static void process0AppTask(void* arg)
  169. {
  170. while(1)
  171. {
  172. uint32_t msDelay = 0;
  173. while (1)
  174. {
  175. for (uint32_t i = 0; i < 70000; i++)
  176. {;}
  177. msDelay++;
  178. }
  179. }
  180. }
  181. static void process1AppTask(void* arg)
  182. {
  183. PROC_STATE_SWITCH(PROCESS_STATE_IDLE);
  184. uint32_t Can_ID;
  185. NVIC_EnableIRQ(PadWakeup0_IRQn);
  186. Can_InitType param;
  187. Can_TxMsgType Can_TxMsg;
  188. param.baudrate = CAN_500Kbps;
  189. param.mode = REQOP_NORMAL;
  190. param.TxStdIDH = 0x00;
  191. param.TxStdIDL = 0x00;
  192. param.RxStdIDH[0] = 0x00;
  193. param.RxStdIDL[0] = 0x00;
  194. /*stdid 0000 0000 001x*/
  195. param.RxStdIDH[1] = 0x00;
  196. param.RxStdIDL[1] = 0x20;
  197. /*stdid 0000 0000 010x */
  198. param.RxStdIDH[2] = 0x00;
  199. param.RxStdIDL[2] = 0x40;
  200. /*stdid 0000 0000 011x*/
  201. param.RxStdIDH[3] = 0x00;
  202. param.RxStdIDL[3] =0x60;
  203. /*stdid 0000 0000 100x */
  204. param.RxStdIDH[4] = 0x00;
  205. param.RxStdIDL[4] = 0x80;
  206. /*stdid 0000 0000 101x*/
  207. param.RxStdIDH[5] = 0x00;
  208. param.RxStdIDL[5] =0xa0;
  209. param.packType = STD_PACK;
  210. HAL_Can_Init(param);
  211. while(1)
  212. {
  213. switch(gProcState)
  214. {
  215. case PROCESS_STATE_IDLE:
  216. {
  217. PROC_STATE_SWITCH(PROCESS_STATE_WORK1);
  218. break;
  219. }
  220. case PROCESS_STATE_WORK1:
  221. {
  222. FaultDisplay(LED_TURN_ON);
  223. switch(send_index)
  224. {
  225. case 0:
  226. case 1:
  227. case 2:
  228. case 3:
  229. case 4:
  230. case 5:
  231. {
  232. Can_ID = 0x0009;
  233. Can_TxMsg.stdIDH = Can_ID>>3;
  234. Can_TxMsg.stdIDL = Can_ID<<5;
  235. Can_TxMsg.DLC = 8;
  236. Can_TxMsg.Data[0] = *(Uart_Rece_battv1_v4+3);
  237. Can_TxMsg.Data[1] = *(Uart_Rece_battv1_v4+4);
  238. Can_TxMsg.Data[2] = *(Uart_Rece_battv1_v4+5);
  239. Can_TxMsg.Data[3] = *(Uart_Rece_battv1_v4+6);
  240. Can_TxMsg.Data[4] = *(Uart_Rece_battv1_v4+7);
  241. Can_TxMsg.Data[5] = *(Uart_Rece_battv1_v4+8);
  242. Can_TxMsg.Data[6] = *(Uart_Rece_battv1_v4+9);
  243. Can_TxMsg.Data[7] = *(Uart_Rece_battv1_v4+10);
  244. HAL_Can_Transmit(Can_TxMsg);
  245. break;
  246. }
  247. }
  248. send_index++;
  249. if (send_index >10)
  250. {
  251. send_index=0;
  252. PROC_STATE_SWITCH(PROCESS_STATE_SLEEP);
  253. }
  254. break;
  255. }
  256. case PROCESS_STATE_SLEEP:
  257. {
  258. FaultDisplay(LED_TURN_OFF);
  259. osDelay(100/portTICK_PERIOD_MS);
  260. PROC_STATE_SWITCH(PROCESS_STATE_WORK1);
  261. break;
  262. }
  263. }
  264. }
  265. }
  266. static void process2AppTask(void* arg)
  267. {
  268. USARTdrv->Initialize(USART_callback);
  269. USARTdrv->PowerControl(ARM_POWER_FULL);
  270. USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
  271. ARM_USART_DATA_BITS_8 |
  272. ARM_USART_PARITY_NONE |
  273. ARM_USART_STOP_BITS_1 |
  274. ARM_USART_FLOW_CONTROL_NONE, 9600);
  275. uint8_t *chk_point;
  276. int Rece_index = 0;
  277. PROC_STATE_SWITCH(PROCESS_STATE_IDLE);
  278. while(1)
  279. {
  280. switch(gProcState)
  281. {
  282. case PROCESS_STATE_IDLE:
  283. {
  284. PROC_STATE_SWITCH(PROCESS_STATE_WORK1);
  285. break;
  286. }
  287. case PROCESS_STATE_WORK1:
  288. {
  289. NetSocDisplay(3,LED_TURN_ON);
  290. Uart_Send_reg_beginH = 0x00;
  291. Uart_Send_reg_numH = 0x00;
  292. switch(Rece_index)
  293. {
  294. case 0:
  295. {
  296. Uart_Send_reg_beginL = 0x02;
  297. Uart_Send_reg_numL = 0x04;
  298. chk_point = Uart_Receive_func();
  299. Uart_Rece_battv1_v4 = chk_point;
  300. break;
  301. }
  302. default:
  303. {
  304. break;
  305. }
  306. }
  307. Rece_index++;
  308. if (Rece_index!=0)
  309. {
  310. Rece_index=0;
  311. }
  312. PROC_STATE_SWITCH(PROCESS_STATE_WORK2);
  313. break;
  314. }
  315. case PROCESS_STATE_WORK2:
  316. {
  317. osDelay(100/portTICK_PERIOD_MS);
  318. PROC_STATE_SWITCH(PROCESS_STATE_WORK1);
  319. break;
  320. }
  321. case PROCESS_STATE_SLEEP:
  322. {
  323. //此处写休眠程序
  324. break;
  325. }
  326. }
  327. }
  328. }
  329. /**
  330. \fn process0Init(void)
  331. \brief process0Init function.
  332. \return
  333. */
  334. void process0Init(void)
  335. {
  336. osThreadAttr_t task_attr;
  337. #ifndef USING_PRINTF
  338. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  339. {
  340. HAL_UART_RecvFlowControl(false);
  341. }
  342. #endif
  343. memset(&task_attr,0,sizeof(task_attr));
  344. memset(gProcessTaskStack0, 0xA5,PROC_TASK_STACK_SIZE);
  345. task_attr.name = "Process0AppTask";
  346. task_attr.stack_mem = gProcessTaskStack0;
  347. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  348. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  349. task_attr.cb_mem = &gProcessTask0;//task control block
  350. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  351. osThreadNew(process0AppTask, NULL, &task_attr);
  352. }
  353. /**
  354. \fn process1Init(void)
  355. \brief process1Init function.
  356. \return
  357. */
  358. void process1Init(void)
  359. {
  360. osThreadAttr_t task_attr;
  361. #ifndef USING_PRINTF
  362. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  363. {
  364. HAL_UART_RecvFlowControl(false);
  365. }
  366. #endif
  367. memset(&task_attr,0,sizeof(task_attr));
  368. memset(gProcessTaskStack1, 0xA5,PROC_TASK_STACK_SIZE);
  369. task_attr.name = "Process1AppTask";
  370. task_attr.stack_mem = gProcessTaskStack1;
  371. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  372. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  373. task_attr.cb_mem = &gProcessTask1;//task control block
  374. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  375. osThreadNew(process1AppTask, NULL, &task_attr);
  376. }
  377. /**
  378. \fn process2Init(void)
  379. \brief process2Init function.
  380. \return
  381. */
  382. void process2Init(void)
  383. {
  384. osThreadAttr_t task_attr;
  385. memset(&task_attr,0,sizeof(task_attr));
  386. memset(gProcessTaskStack2, 0xA5,PROC_TASK_STACK_SIZE);
  387. task_attr.name = "Process2AppTask";
  388. task_attr.stack_mem = gProcessTaskStack2;
  389. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  390. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  391. task_attr.cb_mem = &gProcessTask2;//task control block
  392. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  393. osThreadNew(process2AppTask, NULL, &task_attr);
  394. }
  395. /**
  396. \fn appInit(void)
  397. \brief appInit function.
  398. \return
  399. */
  400. void appInit(void *arg)
  401. {
  402. process0Init();
  403. process1Init();
  404. process2Init();
  405. }
  406. /**
  407. \fn int main_entry(void)
  408. \brief main entry function.
  409. \return
  410. */
  411. void main_entry(void) {
  412. BSP_CommonInit();
  413. osKernelInitialize();
  414. registerAppEntry(appInit, NULL);
  415. if (osKernelGetState() == osKernelReady)
  416. {
  417. osKernelStart();
  418. }
  419. while(1);
  420. }