Ec800m_Initialize.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "CONFIG.h"
  2. #if EC800M
  3. #include "stm32f10x.h"
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "Initialize.h"
  9. #include "ec800m.h"
  10. #include "PumpBusiness.h"
  11. #include "Log_Module.h"
  12. // 流程
  13. enum PowerStep{
  14. STEP_START, // 开始
  15. STEP_EXIT_SLEEP, // 退出睡眠
  16. STEP_ENTER_SLEEP, // 进入睡眠
  17. STEP_SET_SLEEP ,// 设置休眠模式
  18. STEP_SET_CFUN_0, // 设置最小功能模式
  19. STEP_SET_CFUN_1, // 设置全功能模式
  20. STEP_WAIT_SET_CFUN_1, // 等待设置全功能模式结果
  21. STEP_WAIT, // 等待
  22. STEP_SET_CGREG_2, // 设置ps域允许上报网络注册和位置信息
  23. STEP_QUERY_CGREG, // 查询网络注册状态
  24. STEP_SUCCESS, // 成功
  25. STEP_FAILURE, // 失败
  26. STEP_FINISH,
  27. };
  28. // 当前
  29. static enum PowerStep powerstep = STEP_START;
  30. // 下一步
  31. static enum PowerStep next_step = STEP_START;
  32. static enum Initialize_Result InitializeResult;
  33. static struct TIMER_Struct timer;
  34. static struct TIMER_Struct timer2;
  35. // 计时相关的变量
  36. static uint8_t Restart_flag = 0;//4分钟内重启次数计数
  37. uint8_t module_switch = 0;//4G模块开关 0正常,1是关闭标志
  38. static uint32_t Power_wait_time = 10;
  39. static uint8_t set_mincfun_flag=1;//0代表正在初始化,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. static void powerwait(void)
  49. {
  50. if(time_get_delay(&timer) > Power_wait_time)
  51. {
  52. pownext_step(next_step); // 进入下一步
  53. time_clear(&timer);
  54. }
  55. }
  56. // 失败
  57. static void power_failure(char * info)
  58. {
  59. Log_Printf_Debug("STEP: 数据发送失败,%s\r\n", info);
  60. networkTest_Flag=1;
  61. pownext_step(STEP_SET_CFUN_0);
  62. }
  63. // 成功
  64. static void power_success(char * info)
  65. {
  66. Log_Printf_Debug("STEP: 数据发送成功,%s\r\n", info);
  67. networkTest_Flag=2;
  68. pownext_step(STEP_SET_CFUN_0);
  69. }
  70. //直接跳转到下一步
  71. static void pownext_step(enum PowerStep ns)
  72. {
  73. // 重置ec800m状态
  74. ec800m.reset();
  75. powerstep = ns;
  76. }
  77. // 先等待再跳转到下一步
  78. static void pownext_wait_step(enum PowerStep ns, uint32_t t)
  79. {
  80. pownext_step(STEP_WAIT); // 等待
  81. Power_wait_time = t;
  82. next_step = ns; // 等待之后跳转
  83. }
  84. // 发送数据的逻辑
  85. static enum Result result = Result_None;
  86. //获取初始化状态
  87. enum Initialize_Result get_initialize_status(void){
  88. if(set_mincfun_flag==0)
  89. {
  90. InitializeResult=Initialize_Result_Busy;
  91. }
  92. else
  93. {
  94. InitializeResult=Initialize_Result_free;
  95. }
  96. return InitializeResult;
  97. }
  98. void Initialize_Handle(void)
  99. {
  100. result =ec800m.ready();
  101. if(result==Result_Success)
  102. {
  103. set_mincfun_flag=0;
  104. pownext_step(STEP_START);
  105. Restart_flag++;
  106. Log_Printf_Debug("完成准备%d\r\n",Restart_flag);
  107. }
  108. if(time_get_delay(&timer2) >= 240000)
  109. {
  110. Restart_flag=0;
  111. time_clear(&timer2);
  112. }
  113. else if(Restart_flag >= 4 && module_switch == 0)//4分钟内重启4次
  114. {
  115. module_switch=1;
  116. pownext_step(STEP_SET_CFUN_0);//模块不断重启,直接进入最小功能模式
  117. Log_Printf_Debug("模块关闭中...%d\r\n",module_switch);
  118. }
  119. if(set_mincfun_flag == 1)
  120. {
  121. return ;
  122. }
  123. // 流程
  124. switch(powerstep)
  125. {
  126. case STEP_START: // 开始
  127. pownext_step(STEP_EXIT_SLEEP);
  128. query_cgreg_times = 0; // 查询网络状态的次数
  129. break;
  130. case STEP_EXIT_SLEEP: // 退出休眠
  131. ec800m.exit_sleep();
  132. pownext_wait_step(STEP_SET_SLEEP, 10);
  133. break;
  134. case STEP_SET_SLEEP: // 设置休眠模式
  135. result = ec800m.set_sleep(1);
  136. if(result == Result_Success)
  137. {
  138. Log_Printf_Debug("设置休眠模式成功\r\n");
  139. pownext_wait_step(STEP_SET_CFUN_1,3);
  140. }
  141. else if(result == Result_Failed)
  142. {
  143. Log_Printf_Debug("设置休眠模式失败\r\n");
  144. pownext_step(STEP_FAILURE);
  145. }
  146. break;
  147. case STEP_SET_CFUN_1:
  148. result = ec800m.set_cfun(1);
  149. if(result == Result_Success)
  150. {
  151. pownext_step(STEP_SET_CGREG_2);
  152. }
  153. else if(result == Result_Failed)
  154. {
  155. power_failure("设置全功能模式失败");
  156. }
  157. break;
  158. case STEP_SET_CGREG_2: // 设置ps域
  159. result = ec800m.set_cgreg(2);
  160. if(result == Result_Success)
  161. {
  162. pownext_step(STEP_QUERY_CGREG);
  163. }
  164. else if(result == Result_Failed)
  165. {
  166. power_failure("设置ps域失败");
  167. }
  168. break;
  169. case STEP_QUERY_CGREG: // 查询ps域
  170. result = ec800m.query_cgreg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
  171. if(result == Result_Success)
  172. {
  173. if(cgreg_stat == 1)
  174. {
  175. // 下一步
  176. power_success("查询PS域成功");
  177. }
  178. else if(query_cgreg_times > 20) // 最多查询20次
  179. {
  180. power_failure("查询ps域次数过多");
  181. }
  182. else
  183. {
  184. pownext_wait_step(STEP_QUERY_CGREG,400);
  185. query_cgreg_times++;
  186. }
  187. }
  188. else if(result == Result_Failed)
  189. {
  190. power_failure("查询ps域失败");
  191. }
  192. break;
  193. case STEP_WAIT: // 等待
  194. powerwait();
  195. break;
  196. case STEP_SUCCESS: // 成功
  197. pownext_step(STEP_SET_CFUN_0);
  198. Log_Printf_Debug("初始化成功\r\n");
  199. break;
  200. case STEP_FAILURE: // 失败
  201. pownext_step(STEP_SET_CFUN_0);
  202. Log_Printf_Debug("初始化失败\r\n");
  203. break;
  204. case STEP_SET_CFUN_0: // 设置最小功能模式
  205. result = ec800m.set_cfun(0);
  206. if(result == Result_Success)
  207. {
  208. Log_Printf_Debug("设置最小功能模式成功\r\n");
  209. pownext_step(STEP_ENTER_SLEEP);
  210. }
  211. else if(result == Result_Failed)
  212. {
  213. Log_Printf_Debug("设置最小功能模式失败\r\n");
  214. pownext_step(STEP_ENTER_SLEEP);
  215. }
  216. break;
  217. case STEP_ENTER_SLEEP: // 进入睡眠
  218. ec800m.enter_sleep();
  219. pownext_step(STEP_FINISH);
  220. break;
  221. case STEP_FINISH: // 结束流程
  222. set_mincfun_flag=1;
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228. #endif