drv_pm.c 4.7 KB

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