app_1_8_2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. uint8_t deepslpTimerID = 7;
  50. /** \brief 电压传输 */
  51. uint8_t Uart_Rece_BattCellU1_U4[8];
  52. uint8_t Uart_Rece_BattCellU5_U8[8];
  53. uint8_t Uart_Rece_BattCellU9_U12[8];
  54. uint8_t Uart_Rece_BattCellU13_U14[8];
  55. uint8_t Uart_Rece_BattT[8];
  56. int16_t Uart_Rece_BattI=0x0000;
  57. uint8_t Uart_Rece_Batt_states[6];
  58. uint16_t Uart_Rece_BattU;
  59. uint16_t Uart_Rece_Batt_MaxcellU;
  60. uint16_t Uart_Rece_Batt_MincellU;
  61. uint8_t Can_Rece_buffer[8];
  62. typedef enum
  63. {
  64. PROCESS_STATE_IDLE = 0,
  65. PROCESS_STATE_WORK,
  66. PROCESS_STATE_SLEEP
  67. }processSM;
  68. typedef enum
  69. {
  70. PROCESS_Uart_STATE_IDLE = 0,
  71. PROCESS_Uart_STATE_WORK,
  72. PROCESS_Uart_STATE_CHECK,
  73. PROCESS_Uart_STATE_SLEEP
  74. }process_Uart;
  75. static StaticTask_t gProcessTask0;
  76. static UINT8 gProcessTaskStack0[PROC_TASK_STACK_SIZE];
  77. static StaticTask_t gProcessTask1;
  78. static UINT8 gProcessTaskStack1[PROC_TASK_STACK_SIZE];
  79. static StaticTask_t gProcessTask2;
  80. static UINT8 gProcessTaskStack2[PROC_TASK_STACK_SIZE];
  81. static StaticTask_t gProcessTask3;
  82. static UINT8 gProcessTaskStack3[PROC_TASK_STACK_SIZE];
  83. processSM gProc0State = PROCESS_STATE_IDLE;
  84. process_Uart gProc1State = PROCESS_Uart_STATE_IDLE;
  85. processSM gProc2State = PROCESS_STATE_IDLE;
  86. processSM gProc3State = PROCESS_STATE_IDLE;
  87. #define PROC_Task_STATE_SWITCH(a) (gProc0State = a) //任务调度切换
  88. #define PROC_Uart_STATE_SWITCH(a) (gProc1State = a) //uart 状态切换
  89. #define PROC_Can_STATE_SWITCH(a) (gProc2State = a) //can 状态切换
  90. #define PROC_NB_STATE_SWITCH(a) (gProc3State = a) //can 状态切换
  91. unsigned int crc_chk(uint8_t* data, uint8_t length)
  92. {
  93. int j;
  94. uint16_t reg_crc=0xFFFF;
  95. while(length--)
  96. {
  97. reg_crc ^= *data++;
  98. for(j=0;j<8;j++)
  99. {
  100. if(reg_crc & 0x01)
  101. {
  102. reg_crc=(reg_crc>>1) ^ 0xA001;
  103. }
  104. else
  105. {
  106. reg_crc=reg_crc >>1;
  107. }
  108. }
  109. }
  110. return reg_crc;
  111. }
  112. uint8_t bcc_chk(uint8_t* data, uint8_t length)
  113. {
  114. uint8_t bcc_chk_return = 0x00;
  115. uint8_t count = 0;
  116. while (count<length)
  117. {
  118. bcc_chk_return^=data[count];
  119. count++;
  120. }
  121. return bcc_chk_return;
  122. }
  123. void TcpCallBack(void)
  124. {
  125. #ifdef USING_PRINTF
  126. printf("[%d]TcpCallBacl\n",__LINE__);
  127. #endif
  128. }
  129. static void process0AppTask(void* arg)
  130. {
  131. uint32_t inParam = 0xAABBCCDD;
  132. uint32_t cnt;
  133. UINT8 Can_index = 0;
  134. UINT8 Uart_index = 0;
  135. uint32_t sleep_index = 0;
  136. PROC_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
  137. NetSocDisplay(0,LED_TURN_OFF);
  138. NetSocDisplay(1,LED_TURN_OFF);
  139. NetSocDisplay(2,LED_TURN_OFF);
  140. NetSocDisplay(3,LED_TURN_OFF);
  141. NetSocDisplay(4,LED_TURN_OFF);
  142. slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
  143. slpManApplyPlatVoteHandle("process0slp",&process0SlpHandler);
  144. slpManPlatVoteDisableSleep(process0SlpHandler, SLP_SLP2_STATE);
  145. slpManSlpState_t slpstate = slpManGetLastSlpState();
  146. if((slpstate == SLP_SLP2_STATE) || (slpstate == SLP_HIB_STATE))
  147. {
  148. PROC_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
  149. }
  150. else
  151. {
  152. PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
  153. }
  154. while(1)
  155. {
  156. switch(gProc0State)
  157. {
  158. case PROCESS_STATE_IDLE:
  159. {
  160. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_CHECK);
  161. NetSocDisplay(0,LED_TURN_ON);
  162. osDelay(100);//delay 100ms
  163. if(Uart_Rece_BattI!=0x0000)
  164. {
  165. PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
  166. }
  167. sleep_index++;
  168. if(sleep_index>=100)
  169. {
  170. NetSocDisplay(0,LED_TURN_OFF);
  171. sleep_index=0;
  172. PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
  173. }
  174. break;
  175. }
  176. case PROCESS_STATE_WORK:
  177. {
  178. osDelay(10);//10ms
  179. Can_index++;
  180. Uart_index++;
  181. if (Uart_index >10)//Uart 100ms 调用一次
  182. {
  183. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_WORK);
  184. Uart_index = 0;
  185. }
  186. if (Can_index >=100)//Can 1000ms 调用一次
  187. {
  188. PROC_Can_STATE_SWITCH(PROCESS_STATE_WORK);
  189. PROC_NB_STATE_SWITCH(PROCESS_STATE_WORK);
  190. Can_index = 0;
  191. }
  192. if((Uart_Rece_BattI==0x0000)&&(Can_Rece_buffer[0]==0xff))
  193. {
  194. sleep_index++;
  195. }
  196. else
  197. {
  198. sleep_index = 0;
  199. }
  200. if (sleep_index >=6000)
  201. {
  202. PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
  203. sleep_index = 0;
  204. }
  205. break;
  206. }
  207. case PROCESS_STATE_SLEEP:
  208. {
  209. slpManSlpState_t State;
  210. uint8_t cnt;
  211. PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_SLEEP);
  212. PROC_Can_STATE_SWITCH(PROCESS_STATE_SLEEP);
  213. slpManPlatVoteEnableSleep(process0SlpHandler,SLP_SLP2_STATE);
  214. slpManPlatVoteDisableSleep(process0SlpHandler, SLP_HIB_STATE);
  215. slpManDeepSlpTimerStart(deepslpTimerID, 300000); // create a 10s timer, DeepSleep Timer is always oneshoot
  216. while(1) // now app can enter hib, but ps and phy maybe not, so wait here
  217. {
  218. osDelay(3000);
  219. }
  220. }
  221. }
  222. }
  223. }
  224. static void process3AppTask(void* arg)
  225. {
  226. CHAR serverip[] = "47.97.127.222";
  227. UINT16 serverport = 8712;
  228. int TcpConnectID = -1;
  229. int TcpSendID = -1;
  230. OsaUtcTimeTValue timestracture;
  231. uint8_t TcpSendBuffer[92] = {0x23,0x23,0x02,0xFE,0x47,0x59,0x54,0x45,0x53,0x54,
  232. 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,
  233. 0x32,0x01,0x00,0x43,0x14,0x0B,0x07,0x0E,0x39,0x37,
  234. 0x80,0x14,0x0B,0x07,0x0E,0x39,0x37,0x1A,0x00,0x00,
  235. 0x00,0x00,0x00,0x02,0x15,0x02,0x15,0x03,0x43,0x64,
  236. 0x00,0x00,0x00,0x00,0x0E,0x0E,0x53,0x0E,0x53,0x0E,
  237. 0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,
  238. 0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,
  239. 0x53,0x0E,0x53,0x05,0x13,0x13,0x13,0x13,0x13,0x00,
  240. 0x00,0x00};
  241. PROC_NB_STATE_SWITCH(PROCESS_STATE_IDLE);
  242. while(1)
  243. {
  244. switch(gProc3State)
  245. {
  246. case PROCESS_STATE_IDLE:
  247. {
  248. break;
  249. }
  250. case PROCESS_STATE_WORK:
  251. {
  252. while(TcpConnectID<0)
  253. {
  254. TcpConnectID = tcpipConnectionCreate(1,PNULL,PNULL,serverip,serverport,TcpCallBack);
  255. }
  256. appGetSystemTimeUtcSync(&timestracture);
  257. TcpSendBuffer[91] = bcc_chk(TcpSendBuffer,91);
  258. TcpSendBuffer[24] = timestracture.UTCtimer1>>16;
  259. TcpSendBuffer[24] = TcpSendBuffer[24]-0x07D0;
  260. TcpSendBuffer[25] = timestracture.UTCtimer1>>8;
  261. TcpSendBuffer[26] = timestracture.UTCtimer1;
  262. TcpSendBuffer[27] = timestracture.UTCtimer2>>24;
  263. TcpSendBuffer[28] = timestracture.UTCtimer2>>16;
  264. TcpSendBuffer[29] = timestracture.UTCtimer2>>8;
  265. TcpSendID = tcpipConnectionSend(TcpConnectID,TcpSendBuffer,92,PNULL,PNULL,PNULL);
  266. #ifdef USING_PRINTF
  267. printf("[%d]TcpConnectID:%d \r\n",__LINE__,TcpConnectID);
  268. #endif
  269. #ifdef USING_PRINTF
  270. printf("[%d]TcpSendID:%d \r\n",__LINE__,TcpSendID);
  271. #endif
  272. #ifdef USING_PRINTF
  273. printf("[%d]Time1:%x TIme2:%x \r\n",__LINE__,timestracture.UTCtimer1,timestracture.UTCtimer2);
  274. #endif
  275. for (int i = 0; i < 92; i++)
  276. {
  277. #ifdef USING_PRINTF
  278. printf("%x ",TcpSendBuffer[i]);
  279. #endif
  280. }
  281. #ifdef USING_PRINTF
  282. printf("\r\n ");
  283. #endif
  284. PROC_NB_STATE_SWITCH(PROCESS_STATE_IDLE);
  285. break;
  286. }
  287. case PROCESS_STATE_SLEEP:
  288. {
  289. while(1)
  290. {
  291. osDelay(3000);
  292. }
  293. break;
  294. }
  295. }
  296. }
  297. }
  298. /**
  299. \fn process0Init(void)
  300. \brief process0Init function.
  301. \return
  302. */
  303. void process0Init(void)
  304. {
  305. osThreadAttr_t task_attr;
  306. #ifndef USING_PRINTF
  307. if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
  308. {
  309. HAL_UART_RecvFlowControl(false);
  310. }
  311. #endif
  312. memset(&task_attr,0,sizeof(task_attr));
  313. memset(gProcessTaskStack0, 0xA5,PROC_TASK_STACK_SIZE);
  314. task_attr.name = "Process0AppTask";
  315. task_attr.stack_mem = gProcessTaskStack0;
  316. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  317. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  318. task_attr.cb_mem = &gProcessTask0;//task control block
  319. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  320. osThreadNew(process0AppTask, NULL, &task_attr);
  321. }
  322. void process3Init(void)
  323. {
  324. osThreadAttr_t task_attr;
  325. memset(&task_attr,0,sizeof(task_attr));
  326. memset(gProcessTaskStack3, 0xA5,PROC_TASK_STACK_SIZE);
  327. task_attr.name = "Process3AppTask";
  328. task_attr.stack_mem = gProcessTaskStack3;
  329. task_attr.stack_size = PROC_TASK_STACK_SIZE;
  330. task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
  331. task_attr.cb_mem = &gProcessTask3;//task control block
  332. task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
  333. osThreadNew(process3AppTask, NULL, &task_attr);
  334. }
  335. /**
  336. \fn appInit(void)
  337. \brief appInit function.
  338. \return
  339. */
  340. void appInit(void *arg)
  341. {
  342. process0Init();//任务调度和检测程序
  343. process3Init();//NB程序
  344. }
  345. /**
  346. \fn int main_entry(void)
  347. \brief main entry function.
  348. \return
  349. */
  350. void main_entry(void) {
  351. BSP_CommonInit();
  352. osKernelInitialize();
  353. registerAppEntry(appInit, NULL);
  354. if (osKernelGetState() == osKernelReady)
  355. {
  356. osKernelStart();
  357. }
  358. while(1);
  359. }