LowPower.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "stm32f10x.h"
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "LowPower.h"
  7. #include "ec800m.h"
  8. #include "PumpBusiness.h"
  9. #if _4GFLAG
  10. // 流程
  11. enum PowerStep{
  12. STEP_START, // 开始
  13. STEP_EXIT_SLEEP, // 退出睡眠
  14. STEP_ENTER_SLEEP, // 进入睡眠
  15. STEP_SET_SLEEP ,// 设置休眠模式
  16. STEP_SET_CFUN_0, // 设置最小功能模式
  17. STEP_WAIT, // 等待
  18. STEP_SUCCESS, // 成功
  19. STEP_FAILURE, // 失败
  20. };
  21. // 当前
  22. static enum PowerStep powerstep = STEP_START;
  23. // 下一步
  24. static enum PowerStep next_step = STEP_START;
  25. extern struct AT_Struct AT;
  26. extern uint8_t send_data_switch;
  27. // 计时相关的变量
  28. uint8_t Power_time_flag = 0;
  29. uint32_t Power_timer_ms = 0;
  30. uint32_t Power_wait_time = 10;
  31. uint8_t set_mincfun_flag=1;
  32. //// 声明函数。步骤跳转
  33. void pownext_step(enum PowerStep ns);
  34. // 发送流程
  35. void Power_Handle_Handle(void);
  36. // 等待
  37. static void powerwait(void)
  38. {
  39. if(Power_time_flag == 0)
  40. {
  41. Power_timer_ms = 0;
  42. Power_time_flag = 1;
  43. }
  44. else if(Power_timer_ms >Power_wait_time)
  45. {
  46. pownext_step(next_step); // 进入下一步
  47. Power_time_flag = 0;
  48. }
  49. }
  50. //直接跳转到下一步
  51. static void pownext_step(enum PowerStep ns)
  52. {
  53. // 重置ec800m状态
  54. ec800m.reset();
  55. powerstep = ns;
  56. }
  57. // 先等待再跳转到下一步
  58. static void pownext_wait_step(enum PowerStep ns, uint32_t t)
  59. {
  60. pownext_step(STEP_WAIT); // 等待
  61. Power_wait_time = t;
  62. next_step = ns; // 等待之后跳转
  63. }
  64. // 发送数据的逻辑
  65. static enum Result result = Result_None;
  66. void Power_Handle(void)
  67. {
  68. result =ec800m.ready();
  69. if(result==Result_Success)
  70. {
  71. set_mincfun_flag=0;
  72. pownext_step(STEP_START);
  73. Log_Printf_Debug("完成准备\r\n");
  74. }
  75. if(set_mincfun_flag==1)
  76. {
  77. return ;
  78. }
  79. // 流程
  80. switch(powerstep)
  81. {
  82. case STEP_START: // 开始
  83. pownext_step(STEP_EXIT_SLEEP);
  84. break;
  85. case STEP_EXIT_SLEEP: // 退出休眠
  86. ec800m.exit_sleep();
  87. pownext_wait_step(STEP_SET_SLEEP, 5);
  88. break;
  89. case STEP_SET_SLEEP: // 设置休眠模式
  90. result = ec800m.set_sleep(1);
  91. if(result == Result_Success)
  92. {
  93. Log_Printf_Debug("设置休眠模式成功\r\n");
  94. pownext_step(STEP_SET_CFUN_0);
  95. }
  96. else if(result == Result_Failed)
  97. {
  98. Log_Printf_Debug("设置休眠模式失败\r\n");
  99. pownext_step(STEP_FAILURE);
  100. }
  101. break;
  102. case STEP_SET_CFUN_0: // 设置最小功能模式
  103. result = ec800m.set_cfun(0);
  104. if(result == Result_Success)
  105. {
  106. pownext_step(STEP_SUCCESS);
  107. Log_Printf_Debug("设置最小功能模式成功\r\n");
  108. }
  109. else if(result == Result_Failed)
  110. {
  111. Log_Printf_Debug("设置最小功能模式失败\r\n");
  112. pownext_step(STEP_FAILURE);
  113. }
  114. break;
  115. case STEP_WAIT: // 等待
  116. powerwait();
  117. break;
  118. case STEP_SUCCESS: // 成功
  119. ec800m.enter_sleep();
  120. pownext_wait_step(STEP_START, 5);
  121. set_mincfun_flag=1;
  122. break;
  123. case STEP_FAILURE: // 失败
  124. ec800m.enter_sleep();
  125. pownext_wait_step(STEP_START, 5);
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. #endif