Bc260y_Initialize.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 PowerStep{
  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 PowerStep powerstep = STEP_START;
  33. // 下一步
  34. static enum PowerStep next_step = STEP_START;
  35. static enum Initialize_Result InitializeResult;
  36. static struct TIMER_Struct timer;
  37. // 计时相关的变量
  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. // 等待
  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_FAILURE);
  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_SUCCESS);
  70. }
  71. //直接跳转到下一步
  72. static void pownext_step(enum PowerStep ns)
  73. {
  74. // 重置ec800m状态
  75. bc260y.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 void pownext_wait(uint32_t t)
  87. {
  88. pownext_wait_step(powerstep, t);
  89. }
  90. // 发送数据的逻辑
  91. static enum Result result = Result_None;
  92. static uint8_t cfun_mode = 0;
  93. //获取初始化状态
  94. enum Initialize_Result get_initialize_status(void){
  95. if(set_mincfun_flag==0)
  96. {
  97. InitializeResult=Initialize_Result_Busy;
  98. }
  99. else
  100. {
  101. InitializeResult=Initialize_Result_free;
  102. }
  103. return InitializeResult;
  104. }
  105. void Initialize_Handle(void)
  106. {
  107. result =bc260y.ready();
  108. if(result==Result_Success)
  109. {
  110. set_mincfun_flag=0;
  111. pownext_wait_step(STEP_START,2000);
  112. }
  113. if(set_mincfun_flag==1)
  114. {
  115. return ;
  116. }
  117. // 流程
  118. switch(powerstep)
  119. {
  120. case STEP_START: // 开始
  121. pownext_step(STEP_EXIT_SLEEP);
  122. query_cgreg_times = 0; // 查询网络状态的次数
  123. break;
  124. case STEP_EXIT_SLEEP: // 退出休眠
  125. result = bc260y.exit_sleep_2();
  126. if(result == Result_Success)
  127. {
  128. Log_Printf_Debug("退出休眠成功\r\n");
  129. pownext_wait_step(STEP_SET_QISDE_1, 10);
  130. }
  131. else if(result == Result_Failed)
  132. {
  133. Log_Printf_Debug("退出休眠失败\r\n");
  134. power_failure("退出休眠失败");
  135. }
  136. break;
  137. case STEP_SET_QISDE_1: // 开启回显
  138. result = bc260y.set_qisde(1);
  139. if(result == Result_Success)
  140. {
  141. pownext_step(STEP_SET_SLEEP);
  142. }
  143. else if(result == Result_Failed)
  144. {
  145. power_failure("开启回显失败");
  146. }
  147. break;
  148. case STEP_SET_SLEEP: // 设置休眠模式
  149. result = bc260y.set_sleep(2);
  150. if(result == Result_Success)
  151. {
  152. Log_Printf_Debug("设置休眠模式成功\r\n");
  153. pownext_wait_step(STEP_QUERY_CFUN,3);
  154. }
  155. else if(result == Result_Failed)
  156. {
  157. Log_Printf_Debug("设置休眠模式失败\r\n");
  158. pownext_step(STEP_FAILURE);
  159. }
  160. break;
  161. case STEP_QUERY_CFUN: // 查询功能模式
  162. result = bc260y.query_cfun(&cfun_mode);
  163. if(result == Result_Success)
  164. {
  165. if(cfun_mode == 0)
  166. {
  167. pownext_step(STEP_SET_CFUN_1);
  168. }
  169. else
  170. {
  171. pownext_step(STEP_SET_CGREG_2);
  172. }
  173. }
  174. else if(result == Result_Failed)
  175. {
  176. power_failure("查询功能模式失败");
  177. }
  178. break;
  179. case STEP_SET_CFUN_1:
  180. result = bc260y.set_cfun(1);
  181. if(result == Result_Success)
  182. {
  183. pownext_wait_step(STEP_SET_CGREG_2,3);
  184. }
  185. else if(result == Result_Failed)
  186. {
  187. power_failure("设置全功能模式失败");
  188. }
  189. break;
  190. case STEP_SET_CGREG_2: // 设置ps域
  191. result = bc260y.set_cereg(2);
  192. if(result == Result_Success)
  193. {
  194. pownext_wait_step(STEP_QUERY_CGREG,5);
  195. }
  196. else if(result == Result_Failed)
  197. {
  198. power_failure("设置ps域失败");
  199. }
  200. break;
  201. case STEP_QUERY_CGREG: // 查询ps域
  202. result = bc260y.query_cereg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
  203. if(result == Result_Success)
  204. {
  205. if(cgreg_stat == 1)
  206. {
  207. // 下一步
  208. power_success("查询PS域成功");
  209. }
  210. else if(query_cgreg_times > 10) // 最多查询10次
  211. {
  212. power_failure("网络注册失败");
  213. }
  214. else
  215. {
  216. query_cgreg_times++;
  217. pownext_wait(400);
  218. }
  219. }
  220. else if(result == Result_Failed)
  221. {
  222. power_failure("网络注册失败");
  223. }
  224. break;
  225. case STEP_WAIT: // 等待
  226. powerwait();
  227. break;
  228. case STEP_SUCCESS: // 成功
  229. Log_Printf_Debug("初始化成功\r\n");
  230. pownext_step(STEP_FINISH);
  231. break;
  232. case STEP_FAILURE: // 失败
  233. pownext_step(STEP_SET_CFUN_0);
  234. break;
  235. case STEP_SET_CFUN_0: // 设置最小功能模式
  236. result = bc260y.set_cfun(0);
  237. if(result == Result_Success)
  238. {
  239. pownext_step(STEP_FINISH);
  240. }
  241. else if(result == Result_Failed)
  242. {
  243. pownext_step(STEP_FINISH);
  244. }
  245. break;
  246. case STEP_FINISH: // 结束流程
  247. set_mincfun_flag=1;
  248. break;
  249. default:
  250. break;
  251. }
  252. }
  253. #endif