Ec800m_Initialize.c 5.6 KB

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