pwrmgr.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. #include "types.h"
  29. #include "ll_sleep.h"
  30. #include "hal_mcu.h"
  31. #include "string.h"
  32. #include "common.h"
  33. #include "pwrmgr.h"
  34. #include "error.h"
  35. #include "gpio.h"
  36. #include "log.h"
  37. #include "clock.h"
  38. #if(CFG_SLEEP_MODE == PWR_MODE_NO_SLEEP)
  39. static uint8_t mPwrMode = PWR_MODE_NO_SLEEP;
  40. #elif(CFG_SLEEP_MODE == PWR_MODE_SLEEP)
  41. static uint8_t mPwrMode = PWR_MODE_SLEEP;
  42. #elif(CFG_SLEEP_MODE == PWR_MODE_PWROFF)
  43. static uint8_t mPwrMode = PWR_MODE_PWROFF;
  44. #elif(CFG_SLEEP_MODE == PWR_MODE_PWROFF_NO_SLEEP)
  45. static uint8_t mPwrMode = PWR_MODE_PWROFF_NO_SLEEP;
  46. #else
  47. #error "CFG_SLEEP_MODE define incorrect"
  48. #endif
  49. typedef struct _pwrmgr_Context_t{
  50. MODULE_e moudle_id;
  51. bool lock;
  52. pwrmgr_Hdl_t sleep_handler;
  53. pwrmgr_Hdl_t wakeup_handler;
  54. }pwrmgr_Ctx_t;
  55. static pwrmgr_Ctx_t mCtx[HAL_PWRMGR_TASK_MAX_NUM];
  56. static uint32_t sramRet_config;
  57. int hal_pwrmgr_init(void)
  58. {
  59. memset(&mCtx, 0, sizeof(mCtx));
  60. switch(mPwrMode){
  61. case PWR_MODE_NO_SLEEP:
  62. case PWR_MODE_PWROFF_NO_SLEEP:
  63. disableSleep();
  64. break;
  65. case PWR_MODE_SLEEP:
  66. enableSleep();
  67. break;
  68. case PWR_MODE_PWROFF:
  69. break;
  70. }
  71. return PPlus_SUCCESS;
  72. }
  73. bool hal_pwrmgr_is_lock(MODULE_e mod)
  74. {
  75. int i;
  76. int ret = FALSE;
  77. if(mPwrMode == PWR_MODE_NO_SLEEP || mPwrMode == PWR_MODE_PWROFF_NO_SLEEP ){
  78. return TRUE;
  79. }
  80. HAL_ENTER_CRITICAL_SECTION();
  81. for(i = 0; i< HAL_PWRMGR_TASK_MAX_NUM; i++){
  82. if(mCtx[i].moudle_id == MOD_NONE)
  83. break;
  84. if(mCtx[i].moudle_id == mod){
  85. if(mCtx[i].lock == TRUE)
  86. ret = TRUE;
  87. break;
  88. }
  89. }
  90. HAL_EXIT_CRITICAL_SECTION();
  91. return ret;
  92. }
  93. int hal_pwrmgr_lock(MODULE_e mod)
  94. {
  95. int i;
  96. int ret = PPlus_ERR_NOT_REGISTED;
  97. if(mPwrMode == PWR_MODE_NO_SLEEP || mPwrMode == PWR_MODE_PWROFF_NO_SLEEP ){
  98. disableSleep();
  99. return PPlus_SUCCESS;
  100. }
  101. HAL_ENTER_CRITICAL_SECTION();
  102. for(i = 0; i< HAL_PWRMGR_TASK_MAX_NUM; i++){
  103. if(mCtx[i].moudle_id == MOD_NONE)
  104. break;
  105. if(mCtx[i].moudle_id == mod){
  106. mCtx[i].lock = TRUE;
  107. disableSleep();
  108. //LOG("LOCK\n");
  109. ret = PPlus_SUCCESS;
  110. break;
  111. }
  112. }
  113. HAL_EXIT_CRITICAL_SECTION();
  114. return ret;
  115. }
  116. int hal_pwrmgr_unlock(MODULE_e mod)
  117. {
  118. int i, cnt = 0;
  119. if(mPwrMode == PWR_MODE_NO_SLEEP || mPwrMode == PWR_MODE_PWROFF_NO_SLEEP ){
  120. disableSleep();
  121. return PPlus_SUCCESS;
  122. }
  123. HAL_ENTER_CRITICAL_SECTION();
  124. for(i = 0; i< HAL_PWRMGR_TASK_MAX_NUM; i++){
  125. if(mCtx[i].moudle_id == MOD_NONE)
  126. break;
  127. if(mCtx[i].moudle_id == mod){
  128. mCtx[i].lock = FALSE;
  129. }
  130. if(mCtx[i].lock)
  131. cnt ++;
  132. }
  133. if(cnt == 0)
  134. enableSleep();
  135. else
  136. disableSleep();
  137. HAL_EXIT_CRITICAL_SECTION();
  138. return PPlus_SUCCESS;
  139. }
  140. int hal_pwrmgr_register(MODULE_e mod, pwrmgr_Hdl_t sleepHandle, pwrmgr_Hdl_t wakeupHandle)
  141. {
  142. int i;
  143. pwrmgr_Ctx_t* pctx = NULL;
  144. for(i = 0; i< HAL_PWRMGR_TASK_MAX_NUM; i++){
  145. if(mCtx[i].moudle_id == mod)
  146. return PPlus_ERR_INVALID_STATE;
  147. if(mCtx[i].moudle_id == MOD_NONE){
  148. pctx = &mCtx[i];
  149. break;
  150. }
  151. }
  152. if(pctx == NULL)
  153. return PPlus_ERR_NO_MEM;
  154. pctx->lock = FALSE;
  155. pctx->moudle_id = mod;
  156. pctx->sleep_handler = sleepHandle;
  157. pctx->wakeup_handler = wakeupHandle;
  158. return PPlus_SUCCESS;
  159. }
  160. int hal_pwrmgr_unregister(MODULE_e mod)
  161. {
  162. int i;
  163. pwrmgr_Ctx_t* pctx = NULL;
  164. for(i = 0; i< HAL_PWRMGR_TASK_MAX_NUM; i++){
  165. if(mCtx[i].moudle_id == mod){
  166. pctx = &mCtx[i];
  167. break;
  168. }
  169. if(mCtx[i].moudle_id == MOD_NONE){
  170. return PPlus_ERR_NOT_REGISTED;
  171. }
  172. }
  173. if(pctx == NULL)
  174. return PPlus_ERR_NOT_REGISTED;
  175. HAL_ENTER_CRITICAL_SECTION();
  176. memcpy(pctx, pctx+1, sizeof(pwrmgr_Ctx_t)*(HAL_PWRMGR_TASK_MAX_NUM-i-1));
  177. HAL_EXIT_CRITICAL_SECTION();
  178. return PPlus_SUCCESS;
  179. }
  180. int __attribute__((used)) hal_pwrmgr_wakeup_process(void)
  181. {
  182. int i;
  183. //hal_system_init(SYS_CLK_XTAL_16M);//(SYS_CLK_DLL_32M);//SYS_CLK_XTAL_16M); //system init
  184. for(i = 0; i< HAL_PWRMGR_TASK_MAX_NUM; i++){
  185. if(mCtx[i].moudle_id == MOD_NONE){
  186. return PPlus_ERR_NOT_REGISTED;
  187. }
  188. if(mCtx[i].wakeup_handler)
  189. mCtx[i].wakeup_handler();
  190. }
  191. return PPlus_SUCCESS;
  192. }
  193. int __attribute__((used)) hal_pwrmgr_sleep_process(void)
  194. {
  195. int i;
  196. //20181013 ZQ :
  197. hal_pwrmgr_RAM_retention_set();
  198. //LOG("Sleep\n");
  199. for(i = 0; i< HAL_PWRMGR_TASK_MAX_NUM; i++){
  200. if(mCtx[i].moudle_id == MOD_NONE){
  201. return PPlus_ERR_NOT_REGISTED;
  202. }
  203. if(mCtx[i].sleep_handler)
  204. mCtx[i].sleep_handler();
  205. }
  206. return PPlus_SUCCESS;
  207. }
  208. /**************************************************************************************
  209. * @fn hal_pwrmgr_RAM_retention
  210. *
  211. * @brief This function process for enable retention sram
  212. *
  213. * input parameters
  214. *
  215. * @param uint32_t sram: sram bit map
  216. *
  217. * output parameters
  218. *
  219. * @param None.
  220. *
  221. * @return refer error.h.
  222. **************************************************************************************/
  223. int hal_pwrmgr_RAM_retention(uint32_t sram)
  224. {
  225. if(sram & 0xffffffe0)
  226. {
  227. sramRet_config = 0x00;
  228. return PPlus_ERR_INVALID_PARAM;
  229. }
  230. sramRet_config = sram;
  231. return PPlus_SUCCESS;
  232. }
  233. int hal_pwrmgr_RAM_retention_clr(void)
  234. {
  235. subWriteReg(0x4000f01c,21,17,0);
  236. return PPlus_SUCCESS;
  237. }
  238. int hal_pwrmgr_RAM_retention_set(void)
  239. {
  240. subWriteReg(0x4000f01c,21,17,sramRet_config);
  241. return PPlus_SUCCESS;
  242. }
  243. int hal_pwrmgr_LowCurrentLdo_enable(void)
  244. {
  245. subWriteReg(0x4000f014,26,26, 1);
  246. return PPlus_SUCCESS;
  247. }
  248. int hal_pwrmgr_LowCurrentLdo_disable(void)
  249. {
  250. subWriteReg(0x4000f014,26,26, 0);
  251. return PPlus_SUCCESS;
  252. }
  253. int hal_pwrmgr_poweroff(pwroff_cfg_t* pcfg, uint8_t wakeup_pin_num)
  254. {
  255. subWriteReg(0x4000f01c,6,6,0x00); //disable software control
  256. uint8_t i = 0;
  257. for(i = 0; i < wakeup_pin_num; i++){
  258. hal_gpio_wakeup_set(pcfg[i].pin, pcfg[i].type);
  259. }
  260. //system off
  261. write_reg(0x4000f000,0x5a5aa5a5);
  262. return PPlus_SUCCESS;
  263. }
  264. void system_on_handler(GPIO_Pin_e pin,uint32_t timer)
  265. {
  266. bool system_on_flag=FALSE;
  267. hal_rtc_clock_config(CLK_32K_RCOSC);
  268. uint32_t start_clock_tick;
  269. hal_gpio_pull_set(pin,PULL_DOWN); //pull down the wake up pin
  270. pwroff_cfg_t pwr_wkp_cfg[]={{pin,POSEDGE}};
  271. WaitMs(10);
  272. if(hal_gpio_read(pin)==1){
  273. // LOG("wkp");
  274. uint8_t tick_per_ms = 32;
  275. STOP_RTC;
  276. CLAER_RTC_COUNT;
  277. HAL_WAIT_CONDITION_TIMEOUT_WO_RETURN(FALSE,500);
  278. RUN_RTC;
  279. start_clock_tick = clock_time_rtc();
  280. // HAL_WAIT_CONDITION(hal_gpio_read(P14)==0);
  281. while(hal_gpio_read(pin)!=0){
  282. if((clock_time_rtc()-start_clock_tick) >= (tick_per_ms*timer)){
  283. system_on_flag=TRUE;
  284. break;
  285. }
  286. }
  287. if(system_on_flag==FALSE){
  288. hal_pwrmgr_poweroff(pwr_wkp_cfg,sizeof(pwr_wkp_cfg)/sizeof(pwr_wkp_cfg[0]));
  289. while(1);
  290. }
  291. }else if(hal_gpio_read(pin)==0){
  292. hal_pwrmgr_poweroff(pwr_wkp_cfg,sizeof(pwr_wkp_cfg)/sizeof(pwr_wkp_cfg[0]));
  293. while(1);
  294. }
  295. }