drv_pm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-04-08 wangyq the first version
  9. * 2019-11-01 wangyq adapt to the new power management interface
  10. */
  11. #include <board.h>
  12. #include <rtdevice.h>
  13. #include <drv_lptim.h>
  14. #include <ald_cmu.h>
  15. #include <ald_pmu.h>
  16. #ifdef RT_USING_PM
  17. static void uart_console_reconfig(void)
  18. {
  19. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  20. rt_device_control(rt_console_get_device(), RT_DEVICE_CTRL_CONFIG, &config);
  21. }
  22. /**
  23. * This function will put ES32F033x into sleep mode.
  24. *
  25. * @param pm pointer to power manage structure
  26. */
  27. static void sleep(struct rt_pm *pm, uint8_t mode)
  28. {
  29. switch (mode)
  30. {
  31. case PM_SLEEP_MODE_NONE:
  32. break;
  33. case PM_SLEEP_MODE_IDLE:
  34. //__WFI();
  35. break;
  36. case PM_SLEEP_MODE_LIGHT:
  37. if (pm->run_mode == PM_RUN_MODE_LOW_SPEED)
  38. {
  39. /* Enter LP SLEEP Mode, Enable low-power regulator */
  40. ald_pmu_lprun_config(PMU_LDO_LPMODE_OUTPUT_1_5, ENABLE);
  41. }
  42. else
  43. {
  44. /* Enter SLEEP Mode, Main regulator is ON */
  45. ald_pmu_stop1_enter();
  46. }
  47. break;
  48. case PM_SLEEP_MODE_DEEP:
  49. /* Enter STOP 2 mode */
  50. ald_pmu_stop2_enter();
  51. break;
  52. case PM_SLEEP_MODE_STANDBY:
  53. /* Enter STANDBY mode */
  54. ald_pmu_stop2_enter();
  55. break;
  56. case PM_SLEEP_MODE_SHUTDOWN:
  57. /* Enter SHUTDOWNN mode */
  58. ald_pmu_stop2_enter();
  59. break;
  60. default:
  61. RT_ASSERT(0);
  62. break;
  63. }
  64. }
  65. static uint8_t run_speed[PM_RUN_MODE_MAX][2] =
  66. {
  67. {48, 0},
  68. {48, 1},
  69. {24, 2},
  70. {2, 3},
  71. };
  72. static void run(struct rt_pm *pm, uint8_t mode)
  73. {
  74. static uint8_t last_mode;
  75. static char *run_str[] = PM_RUN_MODE_NAMES;
  76. extern uint32_t __system_clock;
  77. if (mode == last_mode)
  78. return;
  79. last_mode = mode;
  80. ald_cmu_clock_config_default();
  81. __system_clock = 24000000;
  82. switch (mode)
  83. {
  84. case PM_RUN_MODE_HIGH_SPEED:
  85. case PM_RUN_MODE_NORMAL_SPEED:
  86. /* hosc 12MHz, from hosc/3 pll to 48MHz */
  87. ald_cmu_pll1_config(CMU_PLL1_INPUT_HRC_6, CMU_PLL1_OUTPUT_48M);
  88. /* MCLK 48MHz */
  89. ald_cmu_clock_config(CMU_CLOCK_PLL1, 48000000);
  90. break;
  91. case PM_RUN_MODE_MEDIUM_SPEED:
  92. break;
  93. case PM_RUN_MODE_LOW_SPEED:
  94. ald_cmu_clock_config(CMU_CLOCK_HRC, 2000000);
  95. break;
  96. default:
  97. break;
  98. }
  99. /* 4. 更新外设时钟 */
  100. uart_console_reconfig();
  101. /* Re-Configure the Systick time */
  102. SysTick_Config(ald_cmu_get_sys_clock() / RT_TICK_PER_SECOND);
  103. rt_kprintf("switch to %s mode, frequency = %d MHz\n", run_str[mode], run_speed[mode][0]);
  104. }
  105. /**
  106. * This function caculate the PM tick from OS tick
  107. *
  108. * @param tick OS tick
  109. *
  110. * @return the PM tick
  111. */
  112. static rt_tick_t es32f0_pm_tick_from_os_tick(rt_tick_t tick)
  113. {
  114. rt_uint32_t freq = es32f0_lptim_get_countfreq();
  115. return (freq * tick / RT_TICK_PER_SECOND);
  116. }
  117. /**
  118. * This function caculate the OS tick from PM tick
  119. *
  120. * @param tick PM tick
  121. *
  122. * @return the OS tick
  123. */
  124. static rt_tick_t es32f0_os_tick_from_pm_tick(rt_uint32_t tick)
  125. {
  126. static rt_uint32_t os_tick_remain = 0;
  127. rt_uint32_t ret, freq;
  128. freq = es32f0_lptim_get_countfreq();
  129. ret = (tick * RT_TICK_PER_SECOND + os_tick_remain) / freq;
  130. os_tick_remain += (tick * RT_TICK_PER_SECOND);
  131. os_tick_remain %= freq;
  132. return ret;
  133. }
  134. /**
  135. * This function start the timer of pm
  136. *
  137. * @param pm Pointer to power manage structure
  138. * @param timeout How many OS Ticks that MCU can sleep
  139. */
  140. static void pm_timer_start(struct rt_pm *pm, rt_uint32_t timeout)
  141. {
  142. RT_ASSERT(pm != RT_NULL);
  143. RT_ASSERT(timeout > 0);
  144. if (timeout != RT_TICK_MAX)
  145. {
  146. /* Convert OS Tick to pmtimer timeout value */
  147. timeout = es32f0_pm_tick_from_os_tick(timeout);
  148. /* MAX 0xFFFF */
  149. if (timeout > es32f0_lptim_get_tick_max())
  150. {
  151. timeout = es32f0_lptim_get_tick_max();
  152. }
  153. /* Enter PM_TIMER_MODE */
  154. es32f0_lptim_start(timeout);
  155. }
  156. }
  157. /**
  158. * This function stop the timer of pm
  159. *
  160. * @param pm Pointer to power manage structure
  161. */
  162. static void pm_timer_stop(struct rt_pm *pm)
  163. {
  164. RT_ASSERT(pm != RT_NULL);
  165. /* Reset pmtimer status */
  166. es32f0_lptim_stop();
  167. }
  168. /**
  169. * This function calculate how many OS Ticks that MCU have suspended
  170. *
  171. * @param pm Pointer to power manage structure
  172. *
  173. * @return OS Ticks
  174. */
  175. static rt_tick_t pm_timer_get_tick(struct rt_pm *pm)
  176. {
  177. rt_uint32_t timer_tick;
  178. RT_ASSERT(pm != RT_NULL);
  179. timer_tick = es32f0_lptim_get_current_tick();
  180. return es32f0_os_tick_from_pm_tick(timer_tick);
  181. }
  182. /**
  183. * This function initialize the power manager
  184. */
  185. int drv_pm_hw_init(void)
  186. {
  187. static const struct rt_pm_ops _ops =
  188. {
  189. sleep,
  190. run,
  191. pm_timer_start,
  192. pm_timer_stop,
  193. pm_timer_get_tick
  194. };
  195. rt_uint8_t timer_mask = 0;
  196. /* initialize timer mask */
  197. timer_mask = 1UL << PM_SLEEP_MODE_DEEP;
  198. /* initialize system pm module */
  199. rt_system_pm_init(&_ops, timer_mask, RT_NULL);
  200. return 0;
  201. }
  202. INIT_BOARD_EXPORT(drv_pm_hw_init);
  203. #endif