power.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. //Do not auto power off
  113. if(ulAutoPowerOffDelayTime == 0)
  114. {
  115. AutoPowerOffTimeCnt = SysTickCnt;
  116. }
  117. //Stand for ulAutoPowerOffDelayTime seconds to POWER_OFF
  118. if((uwTorqueIn < 20) && (CadenceIn < 5) && (BikeSpeed < 10) && ((SysTickCnt - OBC_ButtonSetTimeCnt) > 1000))
  119. {
  120. if(SysTickCnt - AutoPowerOffTimeCnt > ulAutoPowerOffDelayTime * 1000) //unit s
  121. {
  122. power_stPowStateOut.powerstate = POWER_OFF;
  123. power_stPowStateOut.blPowerStartupFlg = FALSE;
  124. PowerOffDTimeOut = SysTickCnt;
  125. //Send power off command
  126. SendData(ID_MC_BC, MODE_REPORT, 0x1808, (uint8_t *)"SHUTDOWN");
  127. }
  128. }
  129. else
  130. {
  131. AutoPowerOffTimeCnt = SysTickCnt;
  132. }
  133. break;
  134. }
  135. case POWER_OFF: //关机处理,等待数据存储完成,关闭LOCK
  136. {
  137. //Finish save and wait 1s, or delay for 3s
  138. if (((EESaveFinishFlg == TRUE) && (ParaSaveEEFlg == FALSE) && ((SysTickCnt - PowerOffDTimeOut) > 1000)) ||
  139. ((SysTickCnt - PowerOffDTimeOut) > 3000))
  140. {
  141. power_stPowStateOut.powerstate = POWER_OFF_END;
  142. power_stPowStateOut.blPowerShutdownFlg = TRUE;
  143. power_stPowStateOut.uwPowerShutdnCnt = 0;
  144. IO_POWERLOCK_OFF;
  145. }
  146. break;
  147. }
  148. case POWER_OFF_END:
  149. {
  150. break;
  151. }
  152. default:break;
  153. }
  154. }
  155. /***************************************************************
  156. Function: power_voPowerOnManagement;
  157. Description: cadence frequency get initialization
  158. Call by: functions in main loop;
  159. Input Variables: N/A
  160. Output/Return Variables: N/A
  161. Subroutine Call: N/A;
  162. Reference: N/A
  163. ****************************************************************/
  164. // static void power_voPowerOnManagement(void)
  165. // {
  166. // if(power_stPowStateOut.powerstate == POWER_ON && GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) != RESET)
  167. // {
  168. // power_stPowStateOut.uwPowerStartupCnt++;
  169. // if(power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartTouchTimeCnt)
  170. // {
  171. // IO_POWERLOCK_ON;
  172. // }
  173. // else if (power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartEndCnt + power_stPowStateCof.uwPowerStartTouchTimeCnt)
  174. // {
  175. // power_stPowStateOut.powerstate = POWER_ON_END;
  176. // power_stPowStateOut.blPowerStartupFlg = TRUE;
  177. // power_stPowStateOut.uwPowerStartupCnt = 0;
  178. // power_stPowStateOut.uwPowerOn2OffCnt = 0;
  179. // }
  180. // }
  181. // if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) == RESET)
  182. // {
  183. // power_stPowStateOut.uwPowerStartupCnt = 0;
  184. // power_stPowStateOut.uwPowerShutdnCnt = 0;
  185. // }
  186. // }
  187. /***************************************************************
  188. Function: power_voPowerOffManagement;
  189. Description: cadence frequency get initialization
  190. Call by: functions in main loop;
  191. Input Variables: N/A
  192. Output/Return Variables: N/A
  193. Subroutine Call: N/A;
  194. Reference: N/A
  195. ****************************************************************/
  196. // static void power_voPowerOffManagement(void)
  197. // {
  198. // if(power_stPowStateOut.powerstate == POWER_ON_END)
  199. // {
  200. // if(power_stPowStateOut.uwPowerOn2OffCnt < power_stPowStateCof.uwPowerOn2OffTimeCnt)
  201. // {
  202. // power_stPowStateOut.uwPowerOn2OffCnt ++;
  203. // }
  204. // }
  205. // if(power_stPowStateOut.powerstate == POWER_ON_END && power_stPowStateOut.uwPowerOn2OffCnt == power_stPowStateCof.uwPowerOn2OffTimeCnt &&
  206. // GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) != RESET)
  207. // {
  208. // power_stPowStateOut.uwPowerShutdnCnt++;
  209. // if(power_stPowStateOut.uwPowerShutdnCnt == power_stPowStateCof.uwPowerShutTouchTimeCnt)
  210. // {
  211. // power_stPowStateOut.powerstate = POWER_OFF;
  212. // power_stPowStateOut.blPowerStartupFlg = FALSE;
  213. // power_stPowStateOut.uwPowerShutdnCnt = 0;
  214. // }
  215. // }
  216. // else if(power_stPowStateOut.powerstate == POWER_OFF)
  217. // {
  218. // power_stPowStateOut.uwPowerShutdnCnt++;
  219. // if(power_stPowStateOut.uwPowerShutdnCnt == power_stPowStateCof.uwPowerShutEndCnt)
  220. // {
  221. // power_stPowStateOut.powerstate = POWER_OFF_END;
  222. // power_stPowStateOut.blPowerShutdownFlg = TRUE;
  223. // power_stPowStateOut.uwPowerShutdnCnt = 0;
  224. // IO_POWERLOCK_OFF;
  225. // }
  226. // }
  227. // if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) == RESET)
  228. // {
  229. // power_stPowStateOut.powerstate = POWER_ON_END;
  230. // power_stPowStateOut.uwPowerStartupCnt = 0;
  231. // power_stPowStateOut.uwPowerShutdnCnt = 0;
  232. // }
  233. // }
  234. /*************************************************************************
  235. End of this File (EOF)!
  236. Do not put anything after this part!
  237. *************************************************************************/