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