power.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 "at32f421.h"//#include "stm32f10x.h"
  19. #include "power.h"
  20. #include "AssistCurve.h"
  21. #include "bikeinformation.h"
  22. //#include "api.h"
  23. #include "board_config.h"
  24. #include "FSM_2nd.h"
  25. #include "usart.h"
  26. #include "UserGpio_Config.h"
  27. /******************************
  28. *
  29. * Parameter
  30. *
  31. ******************************/
  32. POWER_OUT power_stPowStateOut = POWER_OUT_DEFAULT;
  33. static POWER_COF power_stPowStateCof = POWER_COF_DEFAULT;
  34. /***************************************************************
  35. Function: power_voPowerInit;
  36. Description: cadence frequency get initialization
  37. Call by: functions in main loop;
  38. Input Variables: N/A
  39. Output/Return Variables: N/A
  40. Subroutine Call: N/A;
  41. Reference: N/A
  42. ****************************************************************/
  43. void power_voPowerInit(void)
  44. {
  45. power_stPowStateOut.powerstate = POWER_START;
  46. power_stPowStateOut.blPowerShutdownFlg = FALSE;
  47. power_stPowStateOut.blPowerStartupFlg = FALSE;
  48. power_stPowStateOut.uwPowerOn2OffCnt = 0;
  49. power_stPowStateOut.uwPowerShutdnCnt = 0;
  50. power_stPowStateOut.uwPowerStartupCnt = 0;
  51. power_stPowStateOut.uwPowerKeySta =0;
  52. power_stPowStateOut.uwPowerKeyMode =0;
  53. UWORD uwStartUpTime,uwShutDownTime;
  54. uwStartUpTime = (ass_ParaCong.uwAutoPowerOffTime & 0xF000)>>12;
  55. // uwStartUpTime = 2;
  56. uwShutDownTime = (ass_ParaCong.uwAutoPowerOffTime & 0x0F00)>>8;
  57. if(uwStartUpTime != 0)
  58. {
  59. power_stPowStateCof.uwPowerStartTouchTimeCnt = uwStartUpTime * 200 / POWER_START_TIMERUNIT;
  60. }
  61. else
  62. {
  63. power_stPowStateCof.uwPowerStartTouchTimeCnt = POWER_START_TOUCHING_TIME / POWER_START_TIMERUNIT;
  64. }
  65. if(uwShutDownTime != 0)
  66. {
  67. power_stPowStateCof.uwPowerShutTouchTimeCnt = uwShutDownTime * 200 / POWER_SHUT_TIMERUNIT;
  68. }
  69. else
  70. {
  71. power_stPowStateCof.uwPowerShutTouchTimeCnt = POWER_SHUT_TOUCHING_TIME / POWER_SHUT_TIMERUNIT;
  72. }
  73. power_stPowStateCof.uwPowerStartEndCnt = POWER_START_FULLYON_TIME / POWER_START_TIMERUNIT;
  74. power_stPowStateCof.uwPowerShutEndCnt = POWER_SHUT_FULLYOFF_TIME / POWER_SHUT_TIMERUNIT;
  75. power_stPowStateCof.uwPowerOn2OffTimeCnt = POWER_ON2OFF_TIME / POWER_SHUT_TIMERUNIT;
  76. }
  77. /***************************************************************
  78. Function: power_voPowerManagement;
  79. Description: cadence frequency get initialization
  80. Call by: functions in main loop;
  81. Input Variables: N/A
  82. Output/Return Variables: N/A
  83. Subroutine Call: N/A;
  84. Reference: N/A
  85. ****************************************************************/
  86. void power_voPowerManagement( _Bool ParaSaveEEFlg, _Bool PowerSleepFlg)
  87. {
  88. #if(BIKE_OXFORD_EN!=0)
  89. if (power_stPowStateOut.powerstate == POWER_START && GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_12) != 0)
  90. {
  91. power_stPowStateOut.uwPowerStartupCnt++;
  92. if (power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartTouchTimeCnt)
  93. {
  94. cp_stHistoryPara.uwOpenTimes++;
  95. GPIO_SetBits(POWER_LOCK_PORT,POWER_LOCK_PIN);// iGpio_Write(HW_GPIO_POWERLOCK_PIN,ApiGpio_HighLevel);
  96. power_stPowStateOut.powerstate = POWER_ON;
  97. power_stPowStateOut.uwPowerStartupCnt = 0;
  98. }
  99. }
  100. else if (power_stPowStateOut.powerstate == POWER_ON)
  101. {
  102. power_stPowStateOut.uwPowerStartupCnt++;
  103. if (power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartEndCnt)
  104. {
  105. power_stPowStateOut.powerstate = POWER_ON_END;
  106. power_stPowStateOut.blPowerStartupFlg = TRUE;
  107. power_stPowStateOut.uwPowerStartupCnt = 0;
  108. power_stPowStateOut.uwPowerOn2OffCnt = 0;
  109. }
  110. }
  111. /////////////////////////////////////////////////////// off /////////////////////////////////////////////
  112. // if ( (GPIO_ReadInputDataBit(POWER_SWSTATE_PORT, POWER_SWSTATE_PIN) == RESET))
  113. if(( GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_12) == 0)&&(power_stPowStateOut.powerstate == POWER_ON_END))
  114. {
  115. power_stPowStateOut.uwPowerShutdnCnt++;
  116. if (power_stPowStateOut.uwPowerShutdnCnt >= power_stPowStateCof.uwPowerShutTouchTimeCnt)
  117. {
  118. power_stPowStateOut.powerstate = POWER_OFF;
  119. cp_stFlg.ParaHistorySaveEEFinishFlg = FALSE;
  120. power_stPowStateOut.blPowerStartupFlg = FALSE;
  121. power_stPowStateOut.uwPowerShutdnCnt = 0;
  122. }
  123. }
  124. else if (power_stPowStateOut.powerstate == POWER_OFF)
  125. {
  126. power_stPowStateOut.uwPowerShutdnCnt++;
  127. if (power_stPowStateOut.uwPowerShutdnCnt >= power_stPowStateCof.uwPowerShutEndCnt && cp_stFlg.ParaHistorySaveEEFinishFlg == TRUE && ParaSaveEEFlg == FALSE)
  128. {
  129. power_stPowStateOut.powerstate = POWER_OFF_END;
  130. power_stPowStateOut.blPowerShutdownFlg = TRUE;
  131. power_stPowStateOut.uwPowerShutdnCnt = 0;
  132. GPIO_ResetBits(POWER_LOCK_PORT,POWER_LOCK_PIN);// iGpio_Write(HW_GPIO_POWERLOCK_PIN,ApiGpio_LowLevel);
  133. }
  134. }
  135. else
  136. {
  137. power_stPowStateOut.uwPowerShutdnCnt = 0;
  138. }
  139. // if (power_stPowStateOut.powerstate == POWER_ON_END && GPIO_ReadInputDataBit(POWER_SWSTATE_PORT, POWER_SWSTATE_PIN) == RESET)
  140. // {
  141. // power_stPowStateOut.powerstate = POWER_ON_END;
  142. // power_stPowStateOut.uwPowerStartupCnt = 0;
  143. // power_stPowStateOut.uwPowerShutdnCnt = 0;
  144. // }
  145. #else
  146. if((POWERKEYVOLREG() < NULL_KEYMAXVOL)&&(POWERKEYVOLREG() > NULL_KEYMINVOL))
  147. {
  148. power_stPowStateOut.uwPowerKeySta=0;
  149. }
  150. else if(POWERKEYVOLREG() > High_KEYVOL)
  151. {
  152. power_stPowStateOut.uwPowerKeySta=1;
  153. if(power_stPowStateOut.powerstate == POWER_START)
  154. {
  155. power_stPowStateOut.uwPowerKeyMode=1;
  156. }
  157. }
  158. else if(POWERKEYVOLREG() < LOW_KEYVOL)
  159. {
  160. power_stPowStateOut.uwPowerKeySta=1;
  161. if(power_stPowStateOut.powerstate == POWER_START)
  162. {
  163. power_stPowStateOut.uwPowerKeyMode=2;
  164. }
  165. }
  166. ////////////////////////////////////////////////// on and standby ////////////////
  167. if( power_stPowStateOut.uwPowerKeyMode ==1)
  168. {
  169. if (((power_stPowStateOut.powerstate == POWER_START)||(power_stPowStateOut.powerstate == POWER_OFF_END))
  170. && power_stPowStateOut.uwPowerKeySta != 0)
  171. // if (power_stPowStateOut.powerstate == POWER_START )
  172. {
  173. power_stPowStateOut.uwPowerStartupCnt++;
  174. if (power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartTouchTimeCnt)
  175. {
  176. cp_stHistoryPara.uwOpenTimes++;
  177. Set_POWER_LOCK_PORT();// GPIO_SetBits(POWER_LOCK_PORT,POWER_LOCK_PIN);//iGpio_Write(HW_GPIO_POWERLOCK_PIN,ApiGpio_HighLevel);
  178. power_stPowStateOut.powerstate = POWER_ON;
  179. power_stPowStateOut.uwPowerStartupCnt = 0;
  180. }
  181. }
  182. else if (power_stPowStateOut.powerstate == POWER_ON)
  183. {
  184. power_stPowStateOut.uwPowerStartupCnt++;
  185. if (power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartEndCnt)
  186. {
  187. power_stPowStateOut.powerstate = POWER_ON_END;
  188. power_stPowStateOut.blPowerStartupFlg = TRUE;
  189. power_stPowStateOut.uwPowerStartupCnt = 0;
  190. power_stPowStateOut.uwPowerOn2OffCnt = 0;
  191. }
  192. }
  193. else
  194. {
  195. power_stPowStateOut.uwPowerStartupCnt = 0;
  196. }
  197. /////////////////////////////////////////////////////// off /////////////////////////////////////////////
  198. if ((power_stPowStateOut.powerstate == POWER_ON_END) && (power_stPowStateOut.uwPowerKeySta == 0 /* || PowerSleepFlg == TRUE*/))
  199. {
  200. power_stPowStateOut.uwPowerShutdnCnt++;
  201. if (power_stPowStateOut.uwPowerShutdnCnt >= power_stPowStateCof.uwPowerShutTouchTimeCnt)
  202. {
  203. power_stPowStateOut.powerstate = POWER_OFF;
  204. // if(adc_stUpOut.uwVdcLpfPu> POWERMINVOLT)
  205. {
  206. cp_stFlg.ParaHistorySaveEEFinishFlg = FALSE;
  207. }
  208. power_stPowStateOut.blPowerStartupFlg = FALSE;
  209. power_stPowStateOut.uwPowerShutdnCnt = 0;
  210. //SendData(ID_MC_BC, MODE_REPORT, 0x1808, (uint8_t *)"SHUTDOWN");
  211. }
  212. }
  213. else if (power_stPowStateOut.powerstate == POWER_OFF)
  214. {
  215. power_stPowStateOut.uwPowerShutdnCnt++;
  216. if ((power_stPowStateOut.uwPowerShutdnCnt >= POWER_SHUT_STOPDELAY_TIME)||(FSM2nd_Run_state.state == Exit))
  217. {
  218. power_stPowStateOut.powerstate = POWER_OFF_END;
  219. // if(adc_stUpOut.uwVdcLpfPu> POWERMINVOLT)
  220. {
  221. cp_stFlg.ParaHistorySaveEEFinishFlg = FALSE;
  222. }
  223. power_stPowStateOut.blPowerStartupFlg = FALSE;
  224. power_stPowStateOut.uwPowerShutdnCnt = 0;
  225. }
  226. }
  227. else if(power_stPowStateOut.powerstate == POWER_OFF_END)
  228. {
  229. power_stPowStateOut.uwPowerShutdnCnt++;
  230. if ((power_stPowStateOut.uwPowerShutdnCnt >= power_stPowStateCof.uwPowerShutEndCnt)
  231. && (cp_stFlg.ParaHistorySaveEEFinishFlg == TRUE)
  232. && (ParaSaveEEFlg == FALSE))
  233. {
  234. power_stPowStateOut.blPowerShutdownFlg = TRUE;
  235. power_stPowStateOut.uwPowerShutdnCnt = 0;
  236. Reset_POWER_LOCK_PORT();// GPIO_ResetBits(POWER_LOCK_PORT,POWER_LOCK_PIN);// iGpio_Write(HW_GPIO_POWERLOCK_PIN,ApiGpio_LowLevel);
  237. }
  238. }
  239. else
  240. {
  241. power_stPowStateOut.uwPowerShutdnCnt = 0;
  242. }
  243. }
  244. ////////////////////////////--------------------------uwPowerKeyMode 2-------- ///////////////////////////////////////////////////////
  245. else if(power_stPowStateOut.uwPowerKeyMode==2)
  246. {
  247. if (power_stPowStateOut.powerstate == POWER_START && power_stPowStateOut.uwPowerKeySta != 0)
  248. {
  249. power_stPowStateOut.uwPowerStartupCnt++;
  250. if (power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartTouchTimeCnt)
  251. {
  252. cp_stHistoryPara.uwOpenTimes++;
  253. Set_POWER_LOCK_PORT();
  254. power_stPowStateOut.powerstate = POWER_ON;
  255. power_stPowStateOut.uwPowerStartupCnt = 0;
  256. }
  257. }
  258. else if (power_stPowStateOut.powerstate == POWER_ON)
  259. {
  260. power_stPowStateOut.uwPowerStartupCnt++;
  261. if (power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartEndCnt)
  262. {
  263. power_stPowStateOut.powerstate = POWER_ON_END;
  264. power_stPowStateOut.blPowerStartupFlg = TRUE;
  265. power_stPowStateOut.uwPowerStartupCnt = 0;
  266. power_stPowStateOut.uwPowerOn2OffCnt = 0;
  267. }
  268. }
  269. else if (power_stPowStateOut.powerstate == POWER_ON_END)
  270. {
  271. if ((power_stPowStateOut.uwPowerOn2OffCnt < power_stPowStateCof.uwPowerOn2OffTimeCnt) && (power_stPowStateOut.uwPowerKeySta == 0))
  272. {
  273. power_stPowStateOut.uwPowerOn2OffCnt++;
  274. }
  275. }
  276. else
  277. {
  278. power_stPowStateOut.uwPowerStartupCnt = 0;
  279. }
  280. /////////////////////////////////////////////////////// off /////////////////////////////////////////////
  281. if (((power_stPowStateOut.powerstate == POWER_ON_END) )
  282. && (power_stPowStateOut.uwPowerOn2OffCnt == power_stPowStateCof.uwPowerOn2OffTimeCnt)
  283. &&((power_stPowStateOut.uwPowerKeySta != 0) || (PowerSleepFlg == TRUE)))
  284. {
  285. power_stPowStateOut.uwPowerShutdnCnt++;
  286. if (power_stPowStateOut.uwPowerShutdnCnt >= power_stPowStateCof.uwPowerShutTouchTimeCnt)
  287. {
  288. power_stPowStateOut.powerstate = POWER_OFF;
  289. cp_stFlg.ParaHistorySaveEEFinishFlg = FALSE;
  290. power_stPowStateOut.blPowerStartupFlg = FALSE;
  291. power_stPowStateOut.uwPowerShutdnCnt = 0;
  292. power_stPowStateOut.uwPOwerForceShutdnCnt=0;
  293. // SendData(ID_MC_BC, MODE_REPORT, 0x1808, (uint8_t *)"SHUTDOWN");
  294. }
  295. }
  296. else if (power_stPowStateOut.powerstate == POWER_OFF)
  297. {
  298. power_stPowStateOut.uwPowerShutdnCnt++;
  299. if ((power_stPowStateOut.uwPowerShutdnCnt >= POWER_SHUT_STOPDELAY_TIME)||(FSM2nd_Run_state.state == Exit))
  300. {
  301. power_stPowStateOut.powerstate = POWER_OFF_END;
  302. cp_stFlg.ParaHistorySaveEEFinishFlg = FALSE;
  303. power_stPowStateOut.blPowerStartupFlg = FALSE;
  304. power_stPowStateOut.uwPowerShutdnCnt = 0;
  305. power_stPowStateOut.uwPOwerForceShutdnCnt=0;
  306. }
  307. }
  308. else if(power_stPowStateOut.powerstate == POWER_OFF_END)
  309. {
  310. power_stPowStateOut.uwPowerShutdnCnt++;
  311. if (power_stPowStateOut.uwPowerShutdnCnt >= power_stPowStateCof.uwPowerShutEndCnt && cp_stFlg.ParaHistorySaveEEFinishFlg == TRUE && ParaSaveEEFlg == FALSE)
  312. {
  313. power_stPowStateOut.blPowerShutdownFlg = TRUE;
  314. power_stPowStateOut.uwPowerShutdnCnt = 0;
  315. Reset_POWER_LOCK_PORT();
  316. }
  317. if(power_stPowStateOut.uwPowerKeySta != 0)
  318. {
  319. if(++power_stPowStateOut.uwPOwerForceShutdnCnt>(power_stPowStateCof.uwPowerShutEndCnt+50))
  320. {
  321. power_stPowStateOut.blPowerShutdownFlg = TRUE;
  322. power_stPowStateOut.uwPowerShutdnCnt = 0;
  323. Reset_POWER_LOCK_PORT();
  324. }
  325. }
  326. else
  327. {
  328. power_stPowStateOut.uwPOwerForceShutdnCnt=0;
  329. }
  330. }
  331. else
  332. {
  333. power_stPowStateOut.uwPowerShutdnCnt = 0;
  334. power_stPowStateOut.uwPOwerForceShutdnCnt=0;
  335. }
  336. if ((power_stPowStateOut.powerstate == POWER_ON_END && (power_stPowStateOut.uwPowerKeySta == 0)&&(PowerSleepFlg == FALSE)))
  337. {
  338. power_stPowStateOut.powerstate = POWER_ON_END;
  339. power_stPowStateOut.uwPowerStartupCnt = 0;
  340. power_stPowStateOut.uwPowerShutdnCnt = 0;
  341. }
  342. }
  343. #endif
  344. }
  345. /*************************************************************************
  346. End of this File (EOF)!
  347. Do not put anything after this part!
  348. *************************************************************************/