Bc260y_Initialize.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "CONFIG.h"
  2. #if BC260Y
  3. #include "stm32f10x.h"
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "bc260y.h"
  9. #include "PumpBusiness.h"
  10. #include "Log_Module.h"
  11. #include "Initialize.h"
  12. // 流程
  13. enum Step{
  14. STEP_START, // 开始
  15. STEP_EXIT_SLEEP, // 退出睡眠
  16. STEP_ENTER_SLEEP, // 进入睡眠
  17. STEP_SET_SLEEP ,// 设置休眠模式
  18. STEP_QUERY_CFUN,
  19. STEP_SET_CFUN_0,
  20. STEP_SET_CFUN_1, // 设置全功能模式
  21. STEP_WAIT_SET_CFUN_1, // 等待设置全功能模式结果
  22. STEP_WAIT, // 等待
  23. STEP_SET_QISDE_0,
  24. STEP_SET_QISDE_1,
  25. STEP_SET_CGREG_2, // 设置ps域允许上报网络注册和位置信息
  26. STEP_QUERY_CGREG, // 查询网络注册状态
  27. STEP_SUCCESS, // 成功
  28. STEP_FAILURE, // 失败
  29. STEP_FINISH,
  30. };
  31. // 当前
  32. static enum Step step = STEP_START;
  33. // 下一步
  34. static enum Step next_step = STEP_START;
  35. static enum Initialize_Result InitializeResult;
  36. static struct TIMER_Struct timer;
  37. // 计时相关的变量
  38. static uint32_t 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. static uint8_t set_func_0_times = 0; // 设置最小功能模式的次数
  46. //// 声明函数。步骤跳转
  47. void goto_step(enum Step ns);
  48. // 发送流程
  49. // 等待
  50. static void wait(void)
  51. {
  52. if(time_get_delay(&timer) > wait_time)
  53. {
  54. goto_step(next_step); // 进入下一步
  55. time_clear(&timer);
  56. }
  57. }
  58. // 失败
  59. static void goto_failure(char * info)
  60. {
  61. Log_Printf_Debug("STEP: 初始化失败,%s\r\n", info);
  62. networkTest_Flag=1;
  63. goto_step(STEP_FAILURE);
  64. }
  65. // 成功
  66. static void goto_success(char * info)
  67. {
  68. Log_Printf_Debug("STEP: 初始化成功,%s\r\n", info);
  69. networkTest_Flag=2;
  70. goto_step(STEP_SUCCESS);
  71. }
  72. //直接跳转到下一步
  73. static void goto_step(enum Step ns)
  74. {
  75. // 重置ec800m状态
  76. bc260y.reset();
  77. step = ns;
  78. }
  79. // 先等待再跳转到下一步
  80. static void goto_step_wait(enum Step ns, uint32_t t)
  81. {
  82. goto_step(STEP_WAIT); // 等待
  83. wait_time = t;
  84. next_step = ns; // 等待之后跳转
  85. }
  86. // 只等待,等待之后返回原来的步骤
  87. static void goto_wait(uint32_t t)
  88. {
  89. goto_step_wait(step, t);
  90. }
  91. // 发送数据的逻辑
  92. static enum Result result = Result_None;
  93. static uint8_t cfun_mode = 0;
  94. //获取初始化状态
  95. enum Initialize_Result get_initialize_status(void){
  96. if(set_mincfun_flag==0)
  97. {
  98. InitializeResult=Initialize_Result_Busy;
  99. }
  100. else
  101. {
  102. InitializeResult=Initialize_Result_free;
  103. }
  104. return InitializeResult;
  105. }
  106. void Initialize_Handle(void)
  107. {
  108. result =bc260y.ready();
  109. if(result==Result_Success)
  110. {
  111. set_mincfun_flag=0;
  112. goto_step(STEP_START);
  113. }
  114. if(set_mincfun_flag==1)
  115. {
  116. return ;
  117. }
  118. // 流程
  119. switch(step)
  120. {
  121. case STEP_START: // 开始
  122. query_cgreg_times = 0; // 查询网络状态的次数
  123. set_func_0_times = 0;
  124. goto_step(STEP_EXIT_SLEEP);
  125. break;
  126. case STEP_EXIT_SLEEP: // 退出休眠
  127. result = bc260y.exit_sleep_2();
  128. if(result == Result_Success)
  129. {
  130. Log_Printf_Debug("退出休眠成功\r\n");
  131. goto_step_wait(STEP_SET_QISDE_1, 10);
  132. }
  133. else if(result == Result_Failed)
  134. {
  135. Log_Printf_Debug("退出休眠失败\r\n");
  136. goto_failure("退出休眠失败");
  137. }
  138. break;
  139. case STEP_SET_QISDE_1: // 开启回显
  140. result = bc260y.set_qisde(1);
  141. if(result == Result_Success)
  142. {
  143. goto_step(STEP_SET_CFUN_0);
  144. }
  145. else if(result == Result_Failed)
  146. {
  147. goto_failure("开启回显失败");
  148. }
  149. break;
  150. case STEP_SET_SLEEP: // 设置休眠模式
  151. result = bc260y.set_sleep(2);
  152. if(result == Result_Success)
  153. {
  154. Log_Printf_Debug("设置休眠模式成功\r\n");
  155. goto_step(STEP_QUERY_CFUN);
  156. }
  157. else if(result == Result_Failed)
  158. {
  159. Log_Printf_Debug("设置休眠模式失败\r\n");
  160. goto_failure("设置休眠模式失败");
  161. }
  162. break;
  163. case STEP_QUERY_CFUN: // 查询功能模式
  164. result = bc260y.query_cfun(&cfun_mode);
  165. if(result == Result_Success)
  166. {
  167. if(cfun_mode == 0)
  168. {
  169. goto_step(STEP_SET_CFUN_1);
  170. }
  171. else
  172. {
  173. goto_step(STEP_SET_CGREG_2);
  174. }
  175. }
  176. else if(result == Result_Failed)
  177. {
  178. goto_failure("查询功能模式失败");
  179. }
  180. break;
  181. case STEP_SET_CFUN_1:
  182. result = bc260y.set_cfun(1);
  183. if(result == Result_Success)
  184. {
  185. goto_step(STEP_SET_CGREG_2);
  186. }
  187. else if(result == Result_Failed)
  188. {
  189. goto_failure("设置全功能模式失败");
  190. }
  191. break;
  192. case STEP_SET_CGREG_2: // 设置ps域
  193. result = bc260y.set_cereg(2);
  194. if(result == Result_Success)
  195. {
  196. goto_step(STEP_QUERY_CGREG);
  197. }
  198. else if(result == Result_Failed)
  199. {
  200. goto_failure("设置ps域失败");
  201. }
  202. break;
  203. case STEP_QUERY_CGREG: // 查询ps域
  204. result = bc260y.query_cereg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
  205. if(result == Result_Success)
  206. {
  207. if(cgreg_stat == 1)
  208. {
  209. // 下一步
  210. goto_success("查询PS域成功");
  211. }
  212. else
  213. {
  214. goto_failure("网络注册失败");
  215. }
  216. }
  217. else if(result == Result_Failed)
  218. {
  219. goto_failure("网络注册失败");
  220. }
  221. break;
  222. case STEP_WAIT: // 等待
  223. wait();
  224. break;
  225. case STEP_SUCCESS: // 成功
  226. Log_Printf_Debug("初始化成功\r\n");
  227. goto_step(STEP_FINISH);
  228. break;
  229. case STEP_FAILURE: // 失败
  230. goto_step(STEP_SET_CFUN_0);
  231. break;
  232. case STEP_SET_CFUN_0: // 设置最小功能模式
  233. result = bc260y.set_cfun(0);
  234. if(result == Result_Success)
  235. {
  236. if(set_func_0_times < 1)
  237. {
  238. set_func_0_times++;
  239. goto_step(STEP_SET_SLEEP);
  240. }
  241. else
  242. {
  243. goto_step(STEP_FINISH);
  244. }
  245. }
  246. else if(result == Result_Failed)
  247. {
  248. goto_step(STEP_FINISH);
  249. }
  250. break;
  251. case STEP_FINISH: // 结束流程
  252. set_mincfun_flag=1;
  253. break;
  254. default:
  255. break;
  256. }
  257. }
  258. #endif