LowPower.c 2.8 KB

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