power.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * @file Power.c
  3. * @author Wang, Zhiyu(wangzy49@midea.com)
  4. * @brief Power of ebike
  5. * @version 0.1
  6. * @date 2021-09-29
  7. *
  8. * @copyright Copyright (c) 2021
  9. *
  10. */
  11. /************************************************************************
  12. Beginning of File, do not put anything above here except notes
  13. Compiler Directives:
  14. *************************************************************************/
  15. #include "syspar.h"
  16. #include "typedefine.h"
  17. #include "mathtool.h"
  18. #include "hwsetup.h"
  19. #include "power.h"
  20. #include "can.h"
  21. /******************************
  22. *
  23. * Parameter
  24. *
  25. ******************************/
  26. POWER_OUT power_stPowStateOut = POWER_OUT_DEFAULT;
  27. static POWER_COF power_stPowStateCof = POWER_COF_DEFAULT;
  28. ULONG AutoPowerOffTimeCnt = 0;
  29. ULONG PowerOffDTimeOut = 0;
  30. /***************************************************************
  31. Function: power_voPowerInit;
  32. Description: cadence frequency get initialization
  33. Call by: functions in main loop;
  34. Input Variables: N/A
  35. Output/Return Variables: N/A
  36. Subroutine Call: N/A;
  37. Reference: N/A
  38. ****************************************************************/
  39. void power_voPowerInit(void)
  40. {
  41. power_stPowStateOut.powerstate = POWER_START;
  42. power_stPowStateOut.blPowerShutdownFlg = FALSE;
  43. power_stPowStateOut.blPowerStartupFlg = FALSE;
  44. power_stPowStateOut.uwPowerOn2OffCnt = 0;
  45. power_stPowStateOut.uwPowerShutdnCnt = 0;
  46. power_stPowStateOut.uwPowerStartupCnt = 0;
  47. power_stPowStateCof.uwPowerStartTouchTimeCnt = POWER_START_TOUCHING_TIME / POWER_START_TIMERUNIT;
  48. power_stPowStateCof.uwPowerShutTouchTimeCnt = POWER_SHUT_TOUCHING_TIME / POWER_SHUT_TIMERUNIT;
  49. power_stPowStateCof.uwPowerStartEndCnt = POWER_START_FULLYON_TIME / POWER_START_TIMERUNIT;
  50. power_stPowStateCof.uwPowerShutEndCnt = POWER_SHUT_FULLYOFF_TIME / POWER_SHUT_TIMERUNIT;
  51. power_stPowStateCof.uwPowerOn2OffTimeCnt = POWER_ON2OFF_TIME / POWER_SHUT_TIMERUNIT;
  52. }
  53. /***************************************************************
  54. Function: power_voPowerManagement;
  55. Description: cadence frequency get initialization
  56. Call by: functions in main loop;
  57. Input Variables: N/A
  58. Output/Return Variables: N/A
  59. Subroutine Call: N/A;
  60. Reference: N/A
  61. ****************************************************************/
  62. void power_voPowerManagement(ULONG ulAutoPowerOffDelayTime, ULONG SysTickCnt, ULONG OBC_ButtonSetTimeCnt, UWORD uwTorqueIn, UWORD CadenceIn, UWORD BikeSpeed, BOOL EESaveFinishFlg, BOOL ParaSaveEEFlg)
  63. {
  64. switch(power_stPowStateOut.powerstate)
  65. {
  66. case POWER_START: //电源键按下,超过1s进入POWER ON
  67. {
  68. if(IO_POWER_STATE != 0)
  69. {
  70. power_stPowStateOut.uwPowerStartupCnt++;
  71. if (power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartTouchTimeCnt)
  72. {
  73. IO_POWERLOCK_ON;
  74. // GPIOC->ODR |= (1 << POWER_LOCK_PORT);
  75. power_stPowStateOut.powerstate = POWER_ON;
  76. power_stPowStateOut.uwPowerOn2OffCnt = 0;
  77. }
  78. }
  79. AutoPowerOffTimeCnt = SysTickCnt;
  80. break;
  81. }
  82. case POWER_ON: // 3s后,检测PC0按键是否弹起,PC0弹起后进入POWER_ON_END
  83. {
  84. power_stPowStateOut.uwPowerOn2OffCnt++;
  85. if (power_stPowStateOut.uwPowerOn2OffCnt >= power_stPowStateCof.uwPowerOn2OffTimeCnt)
  86. {
  87. if(IO_POWER_STATE == 0)
  88. {
  89. power_stPowStateOut.powerstate = POWER_ON_END;
  90. power_stPowStateOut.blPowerStartupFlg = TRUE;
  91. power_stPowStateOut.uwPowerShutdnCnt = 0;
  92. AutoPowerOffTimeCnt = SysTickCnt;
  93. }
  94. }
  95. break;
  96. }
  97. case POWER_ON_END: //电源键按下进入POWER_OFF,或待机设定时候后自动进入POWER_OFF
  98. {
  99. //The key pushed down for "uwPowerShutTouchTimeCnt" to POWER_OFF
  100. if (IO_POWER_STATE != 0)
  101. {
  102. power_stPowStateOut.uwPowerShutdnCnt++;
  103. if (power_stPowStateOut.uwPowerShutdnCnt >= power_stPowStateCof.uwPowerShutTouchTimeCnt)
  104. {
  105. power_stPowStateOut.powerstate = POWER_OFF;
  106. power_stPowStateOut.blPowerStartupFlg = FALSE;
  107. PowerOffDTimeOut = SysTickCnt;
  108. //Send power off command
  109. SendData(ID_MC_BC, MODE_REPORT, 0x1808, (uint8_t *)"SHUTDOWN");
  110. }
  111. }
  112. else
  113. {
  114. power_stPowStateOut.uwPowerShutdnCnt = 0;
  115. }
  116. //Do not auto power off
  117. if(ulAutoPowerOffDelayTime == 0)
  118. {
  119. AutoPowerOffTimeCnt = SysTickCnt;
  120. }
  121. //Stand for ulAutoPowerOffDelayTime seconds to POWER_OFF
  122. if((uwTorqueIn < 20) && (CadenceIn < 5) && (BikeSpeed < 10) && ((SysTickCnt - OBC_ButtonSetTimeCnt) > 1000))
  123. {
  124. if(SysTickCnt - AutoPowerOffTimeCnt > ulAutoPowerOffDelayTime * 1000) //unit s
  125. {
  126. power_stPowStateOut.powerstate = POWER_OFF;
  127. power_stPowStateOut.blPowerStartupFlg = FALSE;
  128. PowerOffDTimeOut = SysTickCnt;
  129. //Send power off command
  130. SendData(ID_MC_BC, MODE_REPORT, 0x1808, (uint8_t *)"SHUTDOWN");
  131. }
  132. }
  133. else
  134. {
  135. AutoPowerOffTimeCnt = SysTickCnt;
  136. }
  137. break;
  138. }
  139. case POWER_OFF: //关机处理,等待数据存储完成,关闭LOCK
  140. {
  141. //Finish save and wait 1s, or delay for 3s
  142. if (((EESaveFinishFlg == TRUE) && (ParaSaveEEFlg == FALSE) && ((SysTickCnt - PowerOffDTimeOut) > 1000)) ||
  143. ((SysTickCnt - PowerOffDTimeOut) > 3000))
  144. {
  145. power_stPowStateOut.powerstate = POWER_OFF_END;
  146. power_stPowStateOut.blPowerShutdownFlg = TRUE;
  147. power_stPowStateOut.uwPowerShutdnCnt = 0;
  148. IO_POWERLOCK_OFF;
  149. }
  150. break;
  151. }
  152. case POWER_OFF_END:
  153. {
  154. break;
  155. }
  156. default:break;
  157. }
  158. }
  159. /***************************************************************
  160. Function: power_voPowerOnManagement;
  161. Description: cadence frequency get initialization
  162. Call by: functions in main loop;
  163. Input Variables: N/A
  164. Output/Return Variables: N/A
  165. Subroutine Call: N/A;
  166. Reference: N/A
  167. ****************************************************************/
  168. // static void power_voPowerOnManagement(void)
  169. // {
  170. // if(power_stPowStateOut.powerstate == POWER_ON && GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) != RESET)
  171. // {
  172. // power_stPowStateOut.uwPowerStartupCnt++;
  173. // if(power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartTouchTimeCnt)
  174. // {
  175. // IO_POWERLOCK_ON;
  176. // }
  177. // else if (power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartEndCnt + power_stPowStateCof.uwPowerStartTouchTimeCnt)
  178. // {
  179. // power_stPowStateOut.powerstate = POWER_ON_END;
  180. // power_stPowStateOut.blPowerStartupFlg = TRUE;
  181. // power_stPowStateOut.uwPowerStartupCnt = 0;
  182. // power_stPowStateOut.uwPowerOn2OffCnt = 0;
  183. // }
  184. // }
  185. // if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) == RESET)
  186. // {
  187. // power_stPowStateOut.uwPowerStartupCnt = 0;
  188. // power_stPowStateOut.uwPowerShutdnCnt = 0;
  189. // }
  190. // }
  191. /***************************************************************
  192. Function: power_voPowerOffManagement;
  193. Description: cadence frequency get initialization
  194. Call by: functions in main loop;
  195. Input Variables: N/A
  196. Output/Return Variables: N/A
  197. Subroutine Call: N/A;
  198. Reference: N/A
  199. ****************************************************************/
  200. // static void power_voPowerOffManagement(void)
  201. // {
  202. // if(power_stPowStateOut.powerstate == POWER_ON_END)
  203. // {
  204. // if(power_stPowStateOut.uwPowerOn2OffCnt < power_stPowStateCof.uwPowerOn2OffTimeCnt)
  205. // {
  206. // power_stPowStateOut.uwPowerOn2OffCnt ++;
  207. // }
  208. // }
  209. // if(power_stPowStateOut.powerstate == POWER_ON_END && power_stPowStateOut.uwPowerOn2OffCnt == power_stPowStateCof.uwPowerOn2OffTimeCnt &&
  210. // GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) != RESET)
  211. // {
  212. // power_stPowStateOut.uwPowerShutdnCnt++;
  213. // if(power_stPowStateOut.uwPowerShutdnCnt == power_stPowStateCof.uwPowerShutTouchTimeCnt)
  214. // {
  215. // power_stPowStateOut.powerstate = POWER_OFF;
  216. // power_stPowStateOut.blPowerStartupFlg = FALSE;
  217. // power_stPowStateOut.uwPowerShutdnCnt = 0;
  218. // }
  219. // }
  220. // else if(power_stPowStateOut.powerstate == POWER_OFF)
  221. // {
  222. // power_stPowStateOut.uwPowerShutdnCnt++;
  223. // if(power_stPowStateOut.uwPowerShutdnCnt == power_stPowStateCof.uwPowerShutEndCnt)
  224. // {
  225. // power_stPowStateOut.powerstate = POWER_OFF_END;
  226. // power_stPowStateOut.blPowerShutdownFlg = TRUE;
  227. // power_stPowStateOut.uwPowerShutdnCnt = 0;
  228. // IO_POWERLOCK_OFF;
  229. // }
  230. // }
  231. // if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) == RESET)
  232. // {
  233. // power_stPowStateOut.powerstate = POWER_ON_END;
  234. // power_stPowStateOut.uwPowerStartupCnt = 0;
  235. // power_stPowStateOut.uwPowerShutdnCnt = 0;
  236. // }
  237. // }
  238. /*************************************************************************
  239. End of this File (EOF)!
  240. Do not put anything after this part!
  241. *************************************************************************/