LowPower.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "PumpBusiness.h"
  9. #if _4GFLAG
  10. // 流程
  11. enum PowerStep{
  12. STEP_START, // 开始
  13. STEP_EXIT_SLEEP, // 退出睡眠
  14. STEP_ENTER_SLEEP, // 进入睡眠
  15. STEP_SET_SLEEP ,// 设置休眠模式
  16. STEP_SET_CFUN_0, // 设置最小功能模式
  17. STEP_SET_CFUN_1, // 设置全功能模式
  18. STEP_WAIT_SET_CFUN_1, // 等待设置全功能模式结果
  19. STEP_WAIT, // 等待
  20. STEP_SET_CGREG_2, // 设置ps域允许上报网络注册和位置信息
  21. STEP_QUERY_CGREG, // 查询网络注册状态
  22. STEP_SUCCESS, // 成功
  23. STEP_FAILURE, // 失败
  24. };
  25. // 当前
  26. static enum PowerStep powerstep = STEP_START;
  27. // 下一步
  28. static enum PowerStep next_step = STEP_START;
  29. extern struct AT_Struct AT;
  30. extern uint8_t send_data_switch;
  31. // 计时相关的变量
  32. uint8_t Power_time_flag = 0;
  33. uint32_t Power_timer_ms = 0;
  34. uint32_t Power_wait_time = 10;
  35. uint8_t set_mincfun_flag=0;
  36. static uint8_t cgreg_n;
  37. static uint8_t cgreg_stat;
  38. static uint16_t cgreg_lac;
  39. static uint32_t cgreg_ci;
  40. static uint8_t query_cgreg_times = 0; // 查询网络状态的次数
  41. //// 声明函数。步骤跳转
  42. void pownext_step(enum PowerStep ns);
  43. // 发送流程
  44. void Power_Handle_Handle(void);
  45. // 等待
  46. static void powerwait(void)
  47. {
  48. if(Power_time_flag == 0)
  49. {
  50. Power_timer_ms = 0;
  51. Power_time_flag = 1;
  52. }
  53. else if(Power_timer_ms >Power_wait_time)
  54. {
  55. pownext_step(next_step); // 进入下一步
  56. Power_time_flag = 0;
  57. }
  58. }
  59. // 失败
  60. static void power_failure(char * info)
  61. {
  62. Log_Printf_Debug("STEP: 数据发送失败,%s\r\n", info);
  63. // UDP_Client5.status = Client_Status_Failure;
  64. // strcpy(UDP_Client5.info, info);
  65. networkTest_Flag=0;
  66. pownext_step(STEP_SET_CFUN_0);
  67. }
  68. // 成功
  69. static void power_success(char * info)
  70. {
  71. Log_Printf_Debug("STEP: 数据发送成功,%s\r\n", info);
  72. // UDP_Client5.status = Client_Status_Success;
  73. // strcpy(UDP_Client5.info, info);
  74. networkTest_Flag=1;
  75. pownext_step(STEP_SUCCESS);
  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. Log_Printf_Debug("完成准备\r\n");
  101. }
  102. if(set_mincfun_flag==1)
  103. {
  104. return ;
  105. }
  106. // 流程
  107. switch(powerstep)
  108. {
  109. case STEP_START: // 开始
  110. pownext_step(STEP_EXIT_SLEEP);
  111. break;
  112. case STEP_EXIT_SLEEP: // 退出休眠
  113. ec800m.exit_sleep();
  114. pownext_wait_step(STEP_SET_SLEEP, 5);
  115. break;
  116. case STEP_SET_SLEEP: // 设置休眠模式
  117. result = ec800m.set_sleep(1);
  118. if(result == Result_Success)
  119. {
  120. Log_Printf_Debug("设置休眠模式成功\r\n");
  121. pownext_wait_step(STEP_SET_CFUN_1,3);
  122. }
  123. else if(result == Result_Failed)
  124. {
  125. Log_Printf_Debug("设置休眠模式失败\r\n");
  126. pownext_step(STEP_FAILURE);
  127. }
  128. break;
  129. case STEP_SET_CFUN_1:
  130. result = ec800m.set_cfun(1);
  131. if(result == Result_Success)
  132. {
  133. pownext_step(STEP_SET_CGREG_2);
  134. }
  135. else if(result == Result_Failed)
  136. {
  137. power_failure("设置全功能模式失败");
  138. }
  139. break;
  140. case STEP_SET_CGREG_2: // 设置ps域
  141. result = ec800m.set_cgreg(2);
  142. if(result == Result_Success)
  143. {
  144. pownext_step(STEP_QUERY_CGREG);
  145. }
  146. else if(result == Result_Failed)
  147. {
  148. power_failure("设置ps域失败");
  149. }
  150. break;
  151. case STEP_QUERY_CGREG: // 查询ps域
  152. result = ec800m.query_cgreg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
  153. if(result == Result_Success)
  154. {
  155. if(cgreg_stat == 1)
  156. {
  157. // 下一步
  158. pownext_step(STEP_SET_CFUN_0);
  159. }
  160. else if(query_cgreg_times > 20) // 最多查询20次
  161. {
  162. power_failure("查询ps域次数过多");
  163. }
  164. else
  165. {
  166. pownext_wait_step(STEP_QUERY_CGREG,400);
  167. query_cgreg_times++;
  168. }
  169. }
  170. else if(result == Result_Failed)
  171. {
  172. power_failure("查询ps域失败");
  173. }
  174. break;
  175. case STEP_SET_CFUN_0: // 设置最小功能模式
  176. result = ec800m.set_cfun(0);
  177. if(result == Result_Success)
  178. {
  179. pownext_step(STEP_SUCCESS);
  180. Log_Printf_Debug("设置最小功能模式成功\r\n");
  181. }
  182. else if(result == Result_Failed)
  183. {
  184. Log_Printf_Debug("设置最小功能模式失败\r\n");
  185. pownext_step(STEP_FAILURE);
  186. // power_failure("设置最小功能模式失败");
  187. // pownext_wait_step(STEP_START, 5);
  188. }
  189. break;
  190. case STEP_WAIT: // 等待
  191. powerwait();
  192. break;
  193. case STEP_SUCCESS: // 成功
  194. ec800m.enter_sleep();
  195. pownext_wait_step(STEP_START, 5);
  196. set_mincfun_flag=1;
  197. break;
  198. case STEP_FAILURE: // 失败
  199. ec800m.enter_sleep();
  200. pownext_wait_step(STEP_SET_CFUN_0, 5);
  201. break;
  202. default:
  203. break;
  204. }
  205. }
  206. #endif