Bc260y_Initialize.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_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. static enum Initialize_Result InitializeResult;
  32. static struct TIMER_Struct timer;
  33. // 计时相关的变量
  34. static uint32_t Power_wait_time = 10;
  35. static uint8_t set_mincfun_flag=0;//0代表正在初始化,1代表空闲
  36. static uint8_t cgreg_n;
  37. static uint8_t cgreg_stat;
  38. static uint16_t cgreg_lac;
  39. static uint32_t cgreg_ci;
  40. static uint8_t query_cgreg_times = 0; // 查询网络状态的次数
  41. //// 声明函数。步骤跳转
  42. void pownext_step(enum PowerStep ns);
  43. // 发送流程
  44. void Power_Handle(void);
  45. // 等待
  46. static void powerwait(void)
  47. {
  48. if(time_get_delay(&timer) > Power_wait_time)
  49. {
  50. pownext_step(next_step); // 进入下一步
  51. time_clear(&timer);
  52. }
  53. }
  54. // 失败
  55. static void power_failure(char * info)
  56. {
  57. Log_Printf_Debug("STEP: 数据发送失败,%s\r\n", info);
  58. // UDP_Client5.status = Client_Status_Failure;
  59. // strcpy(UDP_Client5.info, info);
  60. networkTest_Flag=1;
  61. pownext_step(STEP_FAILURE);
  62. }
  63. // 成功
  64. static void power_success(char * info)
  65. {
  66. Log_Printf_Debug("STEP: 数据发送成功,%s\r\n", info);
  67. // UDP_Client5.status = Client_Status_Success;
  68. // strcpy(UDP_Client5.info, info);
  69. networkTest_Flag=2;
  70. pownext_step(STEP_SUCCESS);
  71. }
  72. //直接跳转到下一步
  73. static void pownext_step(enum PowerStep ns)
  74. {
  75. // 重置ec800m状态
  76. bc260y.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 =bc260y.ready();
  103. if(result==Result_Success)
  104. {
  105. set_mincfun_flag=0;
  106. pownext_wait_step(STEP_START,2000);
  107. Log_Printf_Debug("完成准备\r\n");
  108. }
  109. if(set_mincfun_flag==1)
  110. {
  111. return ;
  112. }
  113. // 流程
  114. switch(powerstep)
  115. {
  116. case STEP_START: // 开始
  117. pownext_step(STEP_EXIT_SLEEP);
  118. query_cgreg_times = 0; // 查询网络状态的次数
  119. break;
  120. case STEP_EXIT_SLEEP: // 退出休眠
  121. bc260y.exit_sleep();
  122. pownext_wait_step(STEP_SET_SLEEP, 10);
  123. break;
  124. case STEP_SET_SLEEP: // 设置休眠模式
  125. result = bc260y.set_sleep(1);
  126. // power_times++;
  127. if(result == Result_Success)
  128. {
  129. Log_Printf_Debug("设置休眠模式成功\r\n");
  130. pownext_wait_step(STEP_SET_CFUN_1,3);
  131. }
  132. else if(result == Result_Failed)
  133. {
  134. Log_Printf_Debug("设置休眠模式失败\r\n");
  135. pownext_step(STEP_FAILURE);
  136. }
  137. break;
  138. case STEP_SET_CFUN_1:
  139. result = bc260y.set_cfun(1);
  140. if(result == Result_Success)
  141. {
  142. pownext_wait_step(STEP_SET_CGREG_2,3);
  143. }
  144. else if(result == Result_Failed)
  145. {
  146. power_failure("设置全功能模式失败");
  147. }
  148. break;
  149. case STEP_SET_CGREG_2: // 设置ps域
  150. result = bc260y.set_cgreg(2);
  151. if(result == Result_Success)
  152. {
  153. pownext_wait_step(STEP_SET_QISDE_0,5);
  154. }
  155. else if(result == Result_Failed)
  156. {
  157. power_failure("设置ps域失败");
  158. }
  159. break;
  160. case STEP_SET_QISDE_0:
  161. result = bc260y.set_qisde(0);
  162. if(result == Result_Success)
  163. {
  164. pownext_step(STEP_QUERY_CGREG);
  165. }
  166. else if(result == Result_Failed)
  167. {
  168. power_failure("关闭回显失败");
  169. }
  170. break;
  171. case STEP_QUERY_CGREG: // 查询ps域
  172. result = bc260y.query_cgreg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
  173. if(result == Result_Success)
  174. {
  175. if(cgreg_stat == 1)
  176. {
  177. // 下一步
  178. power_success("查询PS域成功");
  179. }
  180. else if(query_cgreg_times > 20) // 最多查询20次
  181. {
  182. power_failure("查询ps域次数过多");
  183. }
  184. else
  185. {
  186. pownext_wait_step(STEP_QUERY_CGREG,400);
  187. query_cgreg_times++;
  188. }
  189. }
  190. else if(result == Result_Failed)
  191. {
  192. power_failure("查询ps域失败");
  193. }
  194. break;
  195. case STEP_WAIT: // 等待
  196. powerwait();
  197. break;
  198. case STEP_SUCCESS: // 成功
  199. // bc260y.enter_sleep();
  200. pownext_wait_step(STEP_START, 50);
  201. set_mincfun_flag=1;
  202. Log_Printf_Debug("初始化成功\r\n");
  203. break;
  204. case STEP_FAILURE: // 失败
  205. // bc260y.enter_sleep();
  206. pownext_wait_step(STEP_START, 500);
  207. break;
  208. default:
  209. break;
  210. }
  211. }
  212. #endif