LowPower.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #include "stm32f10x.h"
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "LowPower.h"
  7. #include "ec800m.h"
  8. #include "bc260.h"
  9. #include "PumpBusiness.h"
  10. #if _4GFLAG
  11. // 流程
  12. enum PowerStep{
  13. STEP_START, // 开始
  14. STEP_EXIT_SLEEP, // 退出睡眠
  15. STEP_ENTER_SLEEP, // 进入睡眠
  16. STEP_SET_SLEEP ,// 设置休眠模式
  17. STEP_SET_CFUN_0, // 设置最小功能模式
  18. STEP_SET_CFUN_1, // 设置全功能模式
  19. STEP_WAIT_SET_CFUN_1, // 等待设置全功能模式结果
  20. STEP_WAIT, // 等待
  21. STEP_SET_CGREG_2, // 设置ps域允许上报网络注册和位置信息
  22. STEP_QUERY_CGREG, // 查询网络注册状态
  23. STEP_SUCCESS, // 成功
  24. STEP_FAILURE, // 失败
  25. };
  26. // 当前
  27. static enum PowerStep powerstep = STEP_START;
  28. // 下一步
  29. static enum PowerStep next_step = STEP_START;
  30. extern struct AT_Struct AT;
  31. extern uint8_t send_data_switch;
  32. // 计时相关的变量
  33. uint8_t Power_time_flag = 0;
  34. uint8_t Restart_flag = 0;//5分钟内重启次数计数
  35. uint8_t module_switch = 0;//4G模块开关 0正常,1是关闭标志
  36. uint32_t Power_timer_ms = 0;
  37. uint32_t Power_wait_time = 10;
  38. uint32_t Restart_time_ms = 0;//重启时间计时
  39. uint8_t set_mincfun_flag=1;//最小功能模式的标志
  40. static uint8_t cgreg_n;
  41. static uint8_t cgreg_stat;
  42. static uint16_t cgreg_lac;
  43. static uint32_t cgreg_ci;
  44. static uint8_t query_cgreg_times = 0; // 查询网络状态的次数
  45. //// 声明函数。步骤跳转
  46. void pownext_step(enum PowerStep ns);
  47. // 发送流程
  48. void Power_Handle_Handle(void);
  49. // 等待
  50. static void powerwait(void)
  51. {
  52. if(Power_time_flag == 0)
  53. {
  54. Power_timer_ms = 0;
  55. Power_time_flag = 1;
  56. }
  57. else if(Power_timer_ms >Power_wait_time)
  58. {
  59. pownext_step(next_step); // 进入下一步
  60. Power_time_flag = 0;
  61. }
  62. }
  63. // 失败
  64. static void power_failure(char * info)
  65. {
  66. Log_Printf_Debug("STEP: 数据发送失败,%s\r\n", info);
  67. networkTest_Flag=1;
  68. pownext_step(STEP_SET_CFUN_0);
  69. }
  70. // 成功
  71. static void power_success(char * info)
  72. {
  73. Log_Printf_Debug("STEP: 数据发送成功,%s\r\n", info);
  74. networkTest_Flag=2;
  75. pownext_step(STEP_SET_CFUN_0);
  76. }
  77. //直接跳转到下一步
  78. static void pownext_step(enum PowerStep ns)
  79. {
  80. // 重置ec800m状态
  81. ec800m.reset();
  82. powerstep = ns;
  83. }
  84. // 先等待再跳转到下一步
  85. static void pownext_wait_step(enum PowerStep ns, uint32_t t)
  86. {
  87. pownext_step(STEP_WAIT); // 等待
  88. Power_wait_time = t;
  89. next_step = ns; // 等待之后跳转
  90. }
  91. // 发送数据的逻辑
  92. static enum Result result = Result_None;
  93. void Power_Handle(void)
  94. {
  95. result =ec800m.ready();
  96. if(result==Result_Success)
  97. {
  98. set_mincfun_flag=0;
  99. pownext_step(STEP_START);
  100. Restart_flag++;
  101. Log_Printf_Debug("完成准备%d\r\n",Restart_flag);
  102. }
  103. if(Restart_time_ms>=240000)
  104. {
  105. Restart_flag=0;
  106. Restart_time_ms=0;
  107. }else
  108. {
  109. if(Restart_flag>=4&&module_switch==0)//4分钟内重启4次
  110. {
  111. module_switch=1;
  112. pownext_step(STEP_SET_CFUN_0);//模块不断重启,直接进入最小功能模式
  113. Log_Printf_Debug("模块关闭中...%d\r\n",module_switch);
  114. }
  115. }
  116. if(set_mincfun_flag==1)
  117. {
  118. return ;
  119. }
  120. // 流程
  121. switch(powerstep)
  122. {
  123. case STEP_START: // 开始
  124. pownext_step(STEP_EXIT_SLEEP);
  125. query_cgreg_times = 0; // 查询网络状态的次数
  126. break;
  127. case STEP_EXIT_SLEEP: // 退出休眠
  128. ec800m.exit_sleep();
  129. pownext_wait_step(STEP_SET_SLEEP, 5);
  130. break;
  131. case STEP_SET_SLEEP: // 设置休眠模式
  132. result = ec800m.set_sleep(1);
  133. // power_times++;
  134. if(result == Result_Success)
  135. {
  136. Log_Printf_Debug("设置休眠模式成功\r\n");
  137. pownext_wait_step(STEP_SET_CFUN_1,3);
  138. }
  139. else if(result == Result_Failed)
  140. {
  141. Log_Printf_Debug("设置休眠模式失败\r\n");
  142. pownext_step(STEP_FAILURE);
  143. }
  144. break;
  145. case STEP_SET_CFUN_1:
  146. result = ec800m.set_cfun(1);
  147. if(result == Result_Success)
  148. {
  149. pownext_step(STEP_SET_CGREG_2);
  150. }
  151. else if(result == Result_Failed)
  152. {
  153. power_failure("设置全功能模式失败");
  154. }
  155. break;
  156. case STEP_SET_CGREG_2: // 设置ps域
  157. result = ec800m.set_cgreg(2);
  158. if(result == Result_Success)
  159. {
  160. pownext_step(STEP_QUERY_CGREG);
  161. }
  162. else if(result == Result_Failed)
  163. {
  164. power_failure("设置ps域失败");
  165. }
  166. break;
  167. case STEP_QUERY_CGREG: // 查询ps域
  168. result = ec800m.query_cgreg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
  169. if(result == Result_Success)
  170. {
  171. if(cgreg_stat == 1)
  172. {
  173. // 下一步
  174. power_success("查询PS域成功");
  175. }
  176. else if(query_cgreg_times > 20) // 最多查询20次
  177. {
  178. power_failure("查询ps域次数过多");
  179. }
  180. else
  181. {
  182. pownext_wait_step(STEP_QUERY_CGREG,400);
  183. query_cgreg_times++;
  184. }
  185. }
  186. else if(result == Result_Failed)
  187. {
  188. power_failure("查询ps域失败");
  189. }
  190. break;
  191. case STEP_SET_CFUN_0: // 设置最小功能模式
  192. result = ec800m.set_cfun(0);
  193. if(result == Result_Success)
  194. {
  195. pownext_step(STEP_SUCCESS);
  196. Log_Printf_Debug("设置最小功能模式成功\r\n");
  197. }
  198. else if(result == Result_Failed)
  199. {
  200. Log_Printf_Debug("设置最小功能模式失败\r\n");
  201. pownext_step(STEP_FAILURE);
  202. }
  203. break;
  204. case STEP_WAIT: // 等待
  205. powerwait();
  206. break;
  207. case STEP_SUCCESS: // 成功
  208. ec800m.enter_sleep();
  209. pownext_wait_step(STEP_START, 5);
  210. // if(networkTest_Flag==1)
  211. // {
  212. set_mincfun_flag=1;
  213. // }else
  214. // {
  215. // set_mincfun_flag=0;
  216. // if(power_times>=10)
  217. // set_mincfun_flag=1;
  218. // }
  219. break;
  220. case STEP_FAILURE: // 失败
  221. ec800m.enter_sleep();
  222. if(module_switch==0)
  223. {
  224. pownext_wait_step(STEP_START, 5);
  225. }
  226. else
  227. {
  228. pownext_wait_step(STEP_SET_CFUN_0, 100);
  229. }
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. #endif
  236. #if NBFLAG
  237. // 流程
  238. enum PowerStep{
  239. STEP_START, // 开始
  240. STEP_EXIT_SLEEP, // 退出睡眠
  241. STEP_ENTER_SLEEP, // 进入睡眠
  242. STEP_SET_SLEEP ,// 设置休眠模式
  243. STEP_SET_CFUN_1, // 设置全功能模式
  244. STEP_WAIT_SET_CFUN_1, // 等待设置全功能模式结果
  245. STEP_WAIT, // 等待
  246. STEP_SET_QISDE_0,
  247. STEP_SET_CGREG_2, // 设置ps域允许上报网络注册和位置信息
  248. STEP_QUERY_CGREG, // 查询网络注册状态
  249. STEP_SUCCESS, // 成功
  250. STEP_FAILURE, // 失败
  251. };
  252. // 当前
  253. static enum PowerStep powerstep = STEP_START;
  254. // 下一步
  255. static enum PowerStep next_step = STEP_START;
  256. extern struct AT_Struct AT;
  257. extern uint8_t send_data_switch;
  258. // 计时相关的变量
  259. uint8_t Power_time_flag = 0;
  260. uint32_t Power_timer_ms = 0;
  261. uint32_t Power_wait_time = 10;
  262. uint8_t set_mincfun_flag=1;
  263. static uint8_t cgreg_n;
  264. static uint8_t cgreg_stat;
  265. static uint16_t cgreg_lac;
  266. static uint32_t cgreg_ci;
  267. static uint8_t query_cgreg_times = 0; // 查询网络状态的次数
  268. //// 声明函数。步骤跳转
  269. void pownext_step(enum PowerStep ns);
  270. // 发送流程
  271. void Power_Handle_Handle(void);
  272. // 等待
  273. static void powerwait(void)
  274. {
  275. if(Power_time_flag == 0)
  276. {
  277. Power_timer_ms = 0;
  278. Power_time_flag = 1;
  279. }
  280. else if(Power_timer_ms >Power_wait_time)
  281. {
  282. pownext_step(next_step); // 进入下一步
  283. Power_time_flag = 0;
  284. }
  285. }
  286. // 失败
  287. static void power_failure(char * info)
  288. {
  289. Log_Printf_Debug("STEP: 数据发送失败,%s\r\n", info);
  290. // UDP_Client5.status = Client_Status_Failure;
  291. // strcpy(UDP_Client5.info, info);
  292. networkTest_Flag=1;
  293. pownext_step(STEP_FAILURE);
  294. }
  295. // 成功
  296. static void power_success(char * info)
  297. {
  298. Log_Printf_Debug("STEP: 数据发送成功,%s\r\n", info);
  299. // UDP_Client5.status = Client_Status_Success;
  300. // strcpy(UDP_Client5.info, info);
  301. networkTest_Flag=2;
  302. pownext_step(STEP_SUCCESS);
  303. }
  304. //直接跳转到下一步
  305. static void pownext_step(enum PowerStep ns)
  306. {
  307. // 重置ec800m状态
  308. bc260y.reset();
  309. powerstep = ns;
  310. }
  311. // 先等待再跳转到下一步
  312. static void pownext_wait_step(enum PowerStep ns, uint32_t t)
  313. {
  314. pownext_step(STEP_WAIT); // 等待
  315. Power_wait_time = t;
  316. next_step = ns; // 等待之后跳转
  317. }
  318. // 发送数据的逻辑
  319. static enum Result result = Result_None;
  320. void Power_Handle(void)
  321. {
  322. result =bc260y.ready();
  323. if(result==Result_Success)
  324. {
  325. set_mincfun_flag=0;
  326. pownext_wait_step(STEP_START,2000);
  327. Log_Printf_Debug("完成准备\r\n");
  328. }
  329. if(set_mincfun_flag==1)
  330. {
  331. return ;
  332. }
  333. // 流程
  334. switch(powerstep)
  335. {
  336. case STEP_START: // 开始
  337. pownext_step(STEP_SET_SLEEP);
  338. query_cgreg_times = 0; // 查询网络状态的次数
  339. break;
  340. case STEP_SET_SLEEP: // 设置休眠模式
  341. result = bc260y.set_sleep(1);
  342. // power_times++;
  343. if(result == Result_Success)
  344. {
  345. Log_Printf_Debug("设置休眠模式成功\r\n");
  346. pownext_wait_step(STEP_SET_CFUN_1,3);
  347. }
  348. else if(result == Result_Failed)
  349. {
  350. Log_Printf_Debug("设置休眠模式失败\r\n");
  351. pownext_step(STEP_FAILURE);
  352. }
  353. break;
  354. case STEP_SET_CFUN_1:
  355. result = bc260y.set_cfun(1);
  356. if(result == Result_Success)
  357. {
  358. pownext_wait_step(STEP_SET_CGREG_2,3);
  359. }
  360. else if(result == Result_Failed)
  361. {
  362. power_failure("设置全功能模式失败");
  363. }
  364. break;
  365. case STEP_SET_CGREG_2: // 设置ps域
  366. result = bc260y.set_cgreg(2);
  367. if(result == Result_Success)
  368. {
  369. pownext_wait_step(STEP_SET_QISDE_0,5);
  370. }
  371. else if(result == Result_Failed)
  372. {
  373. power_failure("设置ps域失败");
  374. }
  375. break;
  376. case STEP_SET_QISDE_0:
  377. result = bc260y.set_qisde(0);
  378. if(result == Result_Success)
  379. {
  380. pownext_step(STEP_QUERY_CGREG);
  381. }
  382. else if(result == Result_Failed)
  383. {
  384. power_failure("关闭回显失败");
  385. }
  386. break;
  387. case STEP_QUERY_CGREG: // 查询ps域
  388. result = bc260y.query_cgreg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
  389. if(result == Result_Success)
  390. {
  391. if(cgreg_stat == 1)
  392. {
  393. // 下一步
  394. power_success("查询PS域成功");
  395. }
  396. else if(query_cgreg_times > 20) // 最多查询20次
  397. {
  398. power_failure("查询ps域次数过多");
  399. }
  400. else
  401. {
  402. pownext_wait_step(STEP_QUERY_CGREG,400);
  403. query_cgreg_times++;
  404. }
  405. }
  406. else if(result == Result_Failed)
  407. {
  408. power_failure("查询ps域失败");
  409. }
  410. break;
  411. case STEP_WAIT: // 等待
  412. powerwait();
  413. break;
  414. case STEP_SUCCESS: // 成功
  415. // bc260y.enter_sleep();
  416. pownext_wait_step(STEP_START, 5);
  417. set_mincfun_flag=1;
  418. break;
  419. case STEP_FAILURE: // 失败
  420. // bc260y.enter_sleep();
  421. pownext_wait_step(STEP_START, 5);
  422. break;
  423. default:
  424. break;
  425. }
  426. }
  427. #endif