Bc260y_Initialize.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "Initialize.h"
  9. #include "bc260y.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_1, // 设置全功能模式
  19. STEP_WAIT_SET_CFUN_1, // 等待设置全功能模式结果
  20. STEP_WAIT, // 等待
  21. STEP_SET_QISDE_0,
  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 struct AT_Struct AT;
  32. extern uint8_t send_data_switch;
  33. // 计时相关的变量
  34. uint8_t Power_time_flag = 0;
  35. uint32_t Power_timer_ms = 0;
  36. uint32_t Power_wait_time = 10;
  37. uint8_t set_mincfun_flag=1;
  38. static uint8_t cgreg_n;
  39. static uint8_t cgreg_stat;
  40. static uint16_t cgreg_lac;
  41. static uint32_t cgreg_ci;
  42. static uint8_t query_cgreg_times = 0; // 查询网络状态的次数
  43. //// 声明函数。步骤跳转
  44. void pownext_step(enum PowerStep ns);
  45. // 发送流程
  46. void Power_Handle_Handle(void);
  47. // 等待
  48. static void powerwait(void)
  49. {
  50. if(Power_time_flag == 0)
  51. {
  52. Power_timer_ms = 0;
  53. Power_time_flag = 1;
  54. }
  55. else if(Power_timer_ms >Power_wait_time)
  56. {
  57. pownext_step(next_step); // 进入下一步
  58. Power_time_flag = 0;
  59. }
  60. }
  61. // 失败
  62. static void power_failure(char * info)
  63. {
  64. Log_Printf_Debug("STEP: 数据发送失败,%s\r\n", info);
  65. // UDP_Client5.status = Client_Status_Failure;
  66. // strcpy(UDP_Client5.info, info);
  67. networkTest_Flag=1;
  68. pownext_step(STEP_FAILURE);
  69. }
  70. // 成功
  71. static void power_success(char * info)
  72. {
  73. Log_Printf_Debug("STEP: 数据发送成功,%s\r\n", info);
  74. // UDP_Client5.status = Client_Status_Success;
  75. // strcpy(UDP_Client5.info, info);
  76. networkTest_Flag=2;
  77. pownext_step(STEP_SUCCESS);
  78. }
  79. //直接跳转到下一步
  80. static void pownext_step(enum PowerStep ns)
  81. {
  82. // 重置ec800m状态
  83. bc260y.reset();
  84. powerstep = ns;
  85. }
  86. // 先等待再跳转到下一步
  87. static void pownext_wait_step(enum PowerStep ns, uint32_t t)
  88. {
  89. pownext_step(STEP_WAIT); // 等待
  90. Power_wait_time = t;
  91. next_step = ns; // 等待之后跳转
  92. }
  93. // 发送数据的逻辑
  94. static enum Result result = Result_None;
  95. void Power_Handle(void)
  96. {
  97. result =bc260y.ready();
  98. if(result==Result_Success)
  99. {
  100. set_mincfun_flag=0;
  101. pownext_wait_step(STEP_START,2000);
  102. Log_Printf_Debug("完成准备\r\n");
  103. }
  104. if(set_mincfun_flag==1)
  105. {
  106. return ;
  107. }
  108. // 流程
  109. switch(powerstep)
  110. {
  111. case STEP_START: // 开始
  112. pownext_step(STEP_SET_SLEEP);
  113. query_cgreg_times = 0; // 查询网络状态的次数
  114. break;
  115. case STEP_SET_SLEEP: // 设置休眠模式
  116. result = bc260y.set_sleep(1);
  117. // power_times++;
  118. if(result == Result_Success)
  119. {
  120. Log_Printf_Debug("设置休眠模式成功\r\n");
  121. pownext_wait_step(STEP_SET_CFUN_1,3);
  122. }
  123. else if(result == Result_Failed)
  124. {
  125. Log_Printf_Debug("设置休眠模式失败\r\n");
  126. pownext_step(STEP_FAILURE);
  127. }
  128. break;
  129. case STEP_SET_CFUN_1:
  130. result = bc260y.set_cfun(1);
  131. if(result == Result_Success)
  132. {
  133. pownext_wait_step(STEP_SET_CGREG_2,3);
  134. }
  135. else if(result == Result_Failed)
  136. {
  137. power_failure("设置全功能模式失败");
  138. }
  139. break;
  140. case STEP_SET_CGREG_2: // 设置ps域
  141. result = bc260y.set_cgreg(2);
  142. if(result == Result_Success)
  143. {
  144. pownext_wait_step(STEP_SET_QISDE_0,5);
  145. }
  146. else if(result == Result_Failed)
  147. {
  148. power_failure("设置ps域失败");
  149. }
  150. break;
  151. case STEP_SET_QISDE_0:
  152. result = bc260y.set_qisde(0);
  153. if(result == Result_Success)
  154. {
  155. pownext_step(STEP_QUERY_CGREG);
  156. }
  157. else if(result == Result_Failed)
  158. {
  159. power_failure("关闭回显失败");
  160. }
  161. break;
  162. case STEP_QUERY_CGREG: // 查询ps域
  163. result = bc260y.query_cgreg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
  164. if(result == Result_Success)
  165. {
  166. if(cgreg_stat == 1)
  167. {
  168. // 下一步
  169. power_success("查询PS域成功");
  170. }
  171. else if(query_cgreg_times > 20) // 最多查询20次
  172. {
  173. power_failure("查询ps域次数过多");
  174. }
  175. else
  176. {
  177. pownext_wait_step(STEP_QUERY_CGREG,400);
  178. query_cgreg_times++;
  179. }
  180. }
  181. else if(result == Result_Failed)
  182. {
  183. power_failure("查询ps域失败");
  184. }
  185. break;
  186. case STEP_WAIT: // 等待
  187. powerwait();
  188. break;
  189. case STEP_SUCCESS: // 成功
  190. // bc260y.enter_sleep();
  191. pownext_wait_step(STEP_START, 5);
  192. set_mincfun_flag=1;
  193. break;
  194. case STEP_FAILURE: // 失败
  195. // bc260y.enter_sleep();
  196. pownext_wait_step(STEP_START, 5);
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. #endif