app_1_8.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. /** \brief 电压传输 */
  50. uint8_t Uart_Rece_BattCellU1_U4[8];
  51. uint8_t Uart_Rece_BattCellU5_U8[8];
  52. uint8_t Uart_Rece_BattCellU9_U12[8];
  53. uint8_t Uart_Rece_BattCellU13_U14[8];
  54. uint8_t Uart_Rece_BattT[8];
  55. int16_t Uart_Rece_BattI;
  56. uint8_t Uart_Rece_Batt_states[6];
  57. uint16_t Uart_Rece_BattU;
  58. uint16_t Uart_Rece_Batt_MaxcellU;
  59. uint16_t Uart_Rece_Batt_MincellU;
  60. typedef enum
  61. {
  62. PROCESS1_STATE_IDLE = 0,
  63. PROCESS1_STATE_WORK,
  64. PROCESS1_STATE_SLEEP
  65. }process1SM;
  66. typedef enum
  67. {
  68. PROCESS2_STATE_IDLE = 0,
  69. PROCESS2_STATE_WORK,
  70. PROCESS2_STATE_SLEEP
  71. }process2SM;
  72. static StaticTask_t gProcessTask0;
  73. static UINT8 gProcessTaskStack0[PROC_TASK_STACK_SIZE];
  74. static StaticTask_t gProcessTask1;
  75. static UINT8 gProcessTaskStack1[PROC_TASK_STACK_SIZE];
  76. static StaticTask_t gProcessTask2;
  77. static UINT8 gProcessTaskStack2[PROC_TASK_STACK_SIZE];
  78. process1SM gProc1State = PROCESS1_STATE_IDLE;
  79. process2SM gProc2State = PROCESS2_STATE_IDLE;
  80. #define PROC1_STATE_SWITCH(a) (gProc1State = a)
  81. #define PROC2_STATE_SWITCH(a) (gProc2State = a)
  82. unsigned int crc_chk(uint8_t* data, uint8_t length)
  83. {
  84. int j;
  85. uint16_t reg_crc=0xFFFF;
  86. while(length--)
  87. {
  88. reg_crc ^= *data++;
  89. for(j=0;j<8;j++)
  90. {
  91. if(reg_crc & 0x01)
  92. {
  93. reg_crc=(reg_crc>>1) ^ 0xA001;
  94. }
  95. else
  96. {
  97. reg_crc=reg_crc >>1;
  98. }
  99. }
  100. }
  101. return reg_crc;
  102. }
  103. uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg)
  104. {
  105. uint8_t Uart_Rece_buffer[Uart_Rece_LEN];
  106. uint16_t CRC_Reve_buffer;
  107. uint16_t CRC_chk_buffer;
  108. uint8_t Uart_Send_buffer[8];
  109. uint8_t Rece_Data_Len;
  110. Uart_Send_buffer[0] = Uart_Receive_Msg.Bms_Address;
  111. Uart_Send_buffer[1] = Uart_Receive_Msg.Bms_Read_Funcode;
  112. Uart_Send_buffer[2] = Uart_Receive_Msg.Reg_Begin_H;
  113. Uart_Send_buffer[3] = Uart_Receive_Msg.Reg_Begin_L;
  114. Uart_Send_buffer[4] = Uart_Receive_Msg.Reg_Num_H;
  115. Uart_Send_buffer[5] = Uart_Receive_Msg.Reg_Num_L;
  116. CRC_chk_buffer = crc_chk(Uart_Send_buffer,6);
  117. Uart_Send_buffer[6] = CRC_chk_buffer;
  118. Uart_Send_buffer[7] = CRC_chk_buffer>>8;
  119. Uart_Rece_buffer[0]=0xfe;
  120. uint32_t timeout=0;
  121. USARTdrv->Send(Uart_Send_buffer,8);
  122. USARTdrv->Receive(Uart_Rece_buffer,13);
  123. Rece_Data_Len = 11;
  124. while((isRecvTimeout == false) && (isRecvComplete == false))// 未收到数据不叫时间超时,收到数据但是不全叫时间超时
  125. {
  126. timeout++;
  127. if (timeout>7000000)
  128. {
  129. timeout =0;
  130. isRecvTimeout = true;
  131. break;
  132. }
  133. }
  134. if (isRecvComplete == true)
  135. {
  136. isRecvComplete = false;
  137. CRC_chk_buffer =Uart_Rece_buffer[12]<<8|Uart_Rece_buffer[11];
  138. CRC_Reve_buffer = crc_chk(Uart_Rece_buffer,11);
  139. if (CRC_Reve_buffer == CRC_chk_buffer)//满足校验
  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. else //接收数据的校验不过暂时屏蔽
  148. {
  149. for (uint8_t i = 0; i < 8; i++)
  150. {
  151. Uart_Data_buffer[i]=0xff;
  152. }
  153. return Uart_Data_buffer;
  154. }
  155. }
  156. if (isRecvTimeout == true)//没收到数据,全部为空值
  157. {
  158. Uart_Data_buffer[0] = 0x00;
  159. Uart_Data_buffer[1] = 0x00;
  160. Uart_Data_buffer[2] = 0x00;
  161. Uart_Data_buffer[3] = 0x00;
  162. Uart_Data_buffer[4] = 0x00;
  163. Uart_Data_buffer[5] = 0x00;
  164. Uart_Data_buffer[6] = 0x00;
  165. Uart_Data_buffer[7] = 0xff;
  166. isRecvTimeout = false;
  167. osDelay(1000);
  168. return Uart_Data_buffer;
  169. }
  170. }
  171. void USART_callback(uint32_t event)
  172. {
  173. if(event & ARM_USART_EVENT_RX_TIMEOUT)
  174. {
  175. isRecvTimeout = true;
  176. }
  177. if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
  178. {
  179. isRecvComplete = true;
  180. }
  181. }
  182. static void process0AppTask(void* arg)
  183. {
  184. uint32_t inParam = 0xAABBCCDD;
  185. uint32_t cnt;
  186. slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
  187. slpManApplyPlatVoteHandle("process0slp",&process0SlpHandler);
  188. slpManSlpState_t slpstate = slpManGetLastSlpState();
  189. UINT8 Can_index = 0;
  190. UINT8 Uart_index = 0;
  191. while(1)
  192. {
  193. osDelay(10);//10ms
  194. Can_index++;
  195. Uart_index++;
  196. if (Uart_index >10)//Uart 100ms 调用一次
  197. {
  198. PROC2_STATE_SWITCH(PROCESS2_STATE_WORK);
  199. Uart_index = 0;
  200. }
  201. if (Can_index >=100)//Can 1000ms 调用一次
  202. {
  203. PROC1_STATE_SWITCH(PROCESS1_STATE_WORK);
  204. Can_index = 0;
  205. }
  206. }
  207. }
  208. void TcpCallBack(void)
  209. {
  210. #ifdef USING_PRINTF
  211. printf("[%d]TcpCallBacl",__LINE__);
  212. #endif
  213. }
  214. static void process1AppTask(void* arg)
  215. {
  216. int TcpConnectID = -1;
  217. int TcpSendID = 0;
  218. uint8_t TcpSendBuffer[71] = {0x23,0x23,0x01,0xfe,0x54,
  219. 0x54,0x54,0x54,0x54,0x54,
  220. 0x54,0x54,0x54,0x54,0x54,
  221. 0x54,0x54,0x54,0x30,0x30,
  222. 0x31,0x01,0x00,0x2e,0x11,
  223. 0x03,0x10,0x09,0x39,0x37,
  224. 0x00,0x02,0x38,0x39,0x38,
  225. 0x36,0x30,0x32,0x62,0x34,
  226. 0x32,0x32,0x31,0x36,0x63,
  227. 0x30,0x32,0x39,0x39,0x35,
  228. 0x31,0x39,0x01,0x10,0x01,
  229. 0x00,0x15,0x01,0x20,0x01,
  230. 0x01,0x92,0x01,0x00,0x05,
  231. 0x0f,0x04,0x01,0x00,0x01,
  232. 0x5F};
  233. PROC1_STATE_SWITCH(PROCESS1_STATE_IDLE);
  234. CHAR serverip[] = "47.97.127.222";
  235. UINT16 serverport = 8712;
  236. UINT16 localport = NULL;
  237. while(1)
  238. {
  239. switch(gProc1State)
  240. {
  241. case PROCESS1_STATE_IDLE:
  242. {
  243. FaultDisplay(LED_TURN_OFF);
  244. break;
  245. }
  246. case PROCESS1_STATE_WORK:
  247. {
  248. while(TcpConnectID<=0)
  249. {
  250. TcpConnectID = tcpipConnectionCreate(1,PNULL,PNULL,serverip,serverport,TcpCallBack);
  251. }
  252. TcpSendID = tcpipConnectionSend(TcpConnectID,TcpSendBuffer,71,PNULL,PNULL,PNULL);
  253. #ifdef USING_PRINTF
  254. printf("[%d]TcpConnectID:%d \r\n",__LINE__,TcpConnectID);
  255. #endif
  256. #ifdef USING_PRINTF
  257. printf("[%d]TcpSendID:%d \r\n",__LINE__,TcpSendID);
  258. #endif
  259. ECOMM_TRACE(UNILOG_PLA_APP, ec_autoReg102, P_SIG, 0, "ECOMM_Test");
  260. FaultDisplay(LED_TURN_ON);
  261. PROC1_STATE_SWITCH(PROCESS1_STATE_SLEEP);
  262. break;
  263. }
  264. case PROCESS1_STATE_SLEEP:
  265. {
  266. break;
  267. }
  268. }
  269. }
  270. }
  271. static void process2AppTask(void* arg)
  272. {
  273. USARTdrv->Initialize(USART_callback);
  274. USARTdrv->PowerControl(ARM_POWER_FULL);
  275. USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
  276. ARM_USART_DATA_BITS_8 |
  277. ARM_USART_PARITY_NONE |
  278. ARM_USART_STOP_BITS_1 |
  279. ARM_USART_FLOW_CONTROL_NONE, 9600);
  280. int Rece_index = 0;
  281. uint8_t *Uart_Reve_Point = NULL;
  282. Uart_Receive_Type Uart_Receive_Msg;
  283. PROC2_STATE_SWITCH(PROCESS2_STATE_IDLE);
  284. while(1)
  285. {
  286. switch(gProc2State)
  287. {
  288. case PROCESS2_STATE_IDLE:
  289. {
  290. NetSocDisplay(3,LED_TURN_OFF);
  291. break;
  292. }
  293. case PROCESS2_STATE_WORK:
  294. {
  295. break;
  296. }
  297. case PROCESS2_STATE_SLEEP:
  298. {
  299. //此处写休眠程序
  300. break;
  301. }
  302. }
  303. }
  304. }
  305. /**
  306. \fn process0Init(void)
  307. \brief process0Init function.
  308. \return
  309. */
  310. void process0Init(void)
  311. {
  312. osThreadAttr_t task_attr;
  313. #ifndef USING_PRINTF
  314. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  315. {
  316. HAL_UART_RecvFlowControl(false);
  317. }
  318. #endif
  319. memset(&task_attr,0,sizeof(task_attr));
  320. memset(gProcessTaskStack0, 0xA5,PROC_TASK_STACK_SIZE);
  321. task_attr.name = "Process0AppTask";
  322. task_attr.stack_mem = gProcessTaskStack0;
  323. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  324. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  325. task_attr.cb_mem = &gProcessTask0;//task control block
  326. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  327. osThreadNew(process0AppTask, NULL, &task_attr);
  328. }
  329. /**
  330. \fn process1Init(void)
  331. \brief process1Init function.
  332. \return
  333. */
  334. void process1Init(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(gProcessTaskStack1, 0xA5,PROC_TASK_STACK_SIZE);
  345. task_attr.name = "Process1AppTask";
  346. task_attr.stack_mem = gProcessTaskStack1;
  347. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  348. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  349. task_attr.cb_mem = &gProcessTask1;//task control block
  350. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  351. osThreadNew(process1AppTask, NULL, &task_attr);
  352. }
  353. /**
  354. \fn process2Init(void)
  355. \brief process2Init function.
  356. \return
  357. */
  358. void process2Init(void)
  359. {
  360. osThreadAttr_t task_attr;
  361. memset(&task_attr,0,sizeof(task_attr));
  362. memset(gProcessTaskStack2, 0xA5,PROC_TASK_STACK_SIZE);
  363. task_attr.name = "Process2AppTask";
  364. task_attr.stack_mem = gProcessTaskStack2;
  365. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  366. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  367. task_attr.cb_mem = &gProcessTask2;//task control block
  368. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  369. osThreadNew(process2AppTask, NULL, &task_attr);
  370. }
  371. /**
  372. \fn appInit(void)
  373. \brief appInit function.
  374. \return
  375. */
  376. void appInit(void *arg)
  377. {
  378. process0Init();
  379. process1Init();
  380. process2Init();
  381. }
  382. /**
  383. \fn int main_entry(void)
  384. \brief main entry function.
  385. \return
  386. */
  387. void main_entry(void) {
  388. BSP_CommonInit();
  389. osKernelInitialize();
  390. registerAppEntry(appInit, NULL);
  391. if (osKernelGetState() == osKernelReady)
  392. {
  393. osKernelStart();
  394. }
  395. while(1);
  396. }