drv_pm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Change Logs:
  19. * Date Author Notes
  20. * 2020-12-15 liuhy the first version
  21. */
  22. #include "drv_pm.h"
  23. #ifdef RT_USING_PM
  24. void save_register(void *p_head,uint32_t size,void *p_save)
  25. {
  26. memcpy(p_save,p_head,size);
  27. }
  28. void load_register(void *p_head,uint32_t size,void *p_load)
  29. {
  30. memcpy(p_head,p_load,size);
  31. #ifdef ES_PMU_SAVE_LOAD_UART
  32. if((p_head == UART0) || (p_head == UART1) || (p_head == UART2) ||
  33. (p_head == UART3) || (p_head == UART4) || (p_head == UART5) )
  34. {
  35. ((UART_TypeDef*)p_head)->IER = ((UART_TypeDef*)p_load)->IVS;
  36. }
  37. #endif
  38. }
  39. static void uart_console_reconfig(void)
  40. {
  41. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  42. rt_device_control(rt_console_get_device(), RT_DEVICE_CTRL_CONFIG, &config);
  43. }
  44. /**
  45. * This function will put ES32F369x into sleep mode.
  46. *
  47. * @param pm pointer to power manage structure
  48. */
  49. /* 注意:进入睡眠前,如果有中断挂起(SYSTICK、PENDSV、UART、EXTI等),睡眠将被瞬间唤醒。*/
  50. static void sleep(struct rt_pm *pm, uint8_t mode)
  51. {
  52. switch (mode)
  53. {
  54. case PM_SLEEP_MODE_NONE:
  55. break;
  56. case PM_SLEEP_MODE_IDLE:
  57. break;
  58. case PM_SLEEP_MODE_LIGHT:
  59. /* Enter SLEEP Mode, Main regulator is ON */
  60. ald_pmu_stop1_enter();
  61. break;
  62. case PM_SLEEP_MODE_DEEP:
  63. /* Enter STOP 2 mode */
  64. ald_pmu_stop2_enter();
  65. break;
  66. case PM_SLEEP_MODE_STANDBY:
  67. /* Enter STANDBY mode */
  68. ald_pmu_stop2_enter();
  69. break;
  70. case PM_SLEEP_MODE_SHUTDOWN:
  71. /* Enter SHUTDOWNN mode */
  72. ald_pmu_stop2_enter();
  73. break;
  74. default:
  75. RT_ASSERT(0);
  76. break;
  77. }
  78. }
  79. static uint8_t run_speed[PM_RUN_MODE_MAX][2] =
  80. {
  81. {48, 0},
  82. {48, 1},
  83. {24, 2},
  84. {2, 3},
  85. };
  86. static void run(struct rt_pm *pm, uint8_t mode)
  87. {
  88. static uint8_t last_mode;
  89. static char *run_str[] = PM_RUN_MODE_NAMES;
  90. extern uint32_t __system_clock;
  91. if (mode == last_mode)
  92. return;
  93. last_mode = mode;
  94. ald_cmu_clock_config_default();
  95. __system_clock = 24000000;
  96. switch (mode)
  97. {
  98. case PM_RUN_MODE_HIGH_SPEED:
  99. case PM_RUN_MODE_NORMAL_SPEED:
  100. /* hosc 12MHz, from hosc/3 pll to 48MHz */
  101. ald_cmu_pll1_config(CMU_PLL1_INPUT_HRC_6, CMU_PLL1_OUTPUT_48M);
  102. /* MCLK 48MHz */
  103. ald_cmu_clock_config(CMU_CLOCK_PLL1, 48000000);
  104. break;
  105. case PM_RUN_MODE_MEDIUM_SPEED:
  106. break;
  107. case PM_RUN_MODE_LOW_SPEED:
  108. ald_cmu_clock_config(CMU_CLOCK_HRC, 2000000);
  109. break;
  110. default:
  111. break;
  112. }
  113. /* 4. 更新外设时钟 */
  114. uart_console_reconfig();
  115. /* Re-Configure the Systick time */
  116. SysTick_Config(ald_cmu_get_sys_clock() / RT_TICK_PER_SECOND);
  117. rt_kprintf("switch to %s mode, frequency = %d MHz\n", run_str[mode], run_speed[mode][0]);
  118. }
  119. /**
  120. * This function caculate the PM tick from OS tick
  121. *
  122. * @param tick OS tick
  123. *
  124. * @return the PM tick
  125. */
  126. static rt_tick_t es32f3_pm_tick_from_os_tick(rt_tick_t tick)
  127. {
  128. rt_uint32_t freq = 1;
  129. return (freq * tick / RT_TICK_PER_SECOND);
  130. }
  131. /**
  132. * This function caculate the OS tick from PM tick
  133. *
  134. * @param tick PM tick
  135. *
  136. * @return the OS tick
  137. */
  138. static rt_tick_t es32f3_os_tick_from_pm_tick(rt_uint32_t tick)
  139. {
  140. static rt_uint32_t os_tick_remain = 0;
  141. rt_uint32_t ret, freq;
  142. freq = 1;
  143. ret = (tick * RT_TICK_PER_SECOND + os_tick_remain) / freq;
  144. os_tick_remain += (tick * RT_TICK_PER_SECOND);
  145. os_tick_remain %= freq;
  146. return ret;
  147. }
  148. /**
  149. * This function start the timer of pm
  150. *
  151. * @param pm Pointer to power manage structure
  152. * @param timeout How many OS Ticks that MCU can sleep
  153. */
  154. static void pm_timer_start(struct rt_pm *pm, rt_uint32_t timeout)
  155. {
  156. RT_ASSERT(pm != RT_NULL);
  157. RT_ASSERT(timeout > 0);
  158. if (timeout != RT_TICK_MAX)
  159. {
  160. /* Convert OS Tick to pmtimer timeout value */
  161. timeout = es32f3_pm_tick_from_os_tick(timeout);
  162. /* MAX 0xFFFF */
  163. if (timeout > 0xFFFF)
  164. {
  165. timeout = 0xFFFF;
  166. }
  167. }
  168. }
  169. /**
  170. * This function stop the timer of pm
  171. *
  172. * @param pm Pointer to power manage structure
  173. */
  174. static void pm_timer_stop(struct rt_pm *pm)
  175. {
  176. RT_ASSERT(pm != RT_NULL);
  177. }
  178. /**
  179. * This function calculate how many OS Ticks that MCU have suspended
  180. *
  181. * @param pm Pointer to power manage structure
  182. *
  183. * @return OS Ticks
  184. */
  185. static rt_tick_t pm_timer_get_tick(struct rt_pm *pm)
  186. {
  187. rt_uint32_t timer_tick;
  188. RT_ASSERT(pm != RT_NULL);
  189. timer_tick = 1;
  190. return es32f3_os_tick_from_pm_tick(timer_tick);
  191. }
  192. /**
  193. * This function initialize the power manager
  194. */
  195. int drv_pm_hw_init(void)
  196. {
  197. static const struct rt_pm_ops _ops =
  198. {
  199. sleep,
  200. run,
  201. pm_timer_start,
  202. pm_timer_stop,
  203. pm_timer_get_tick
  204. };
  205. rt_uint8_t timer_mask = 0;
  206. /* initialize timer mask */
  207. timer_mask = 1UL << PM_SLEEP_MODE_DEEP;
  208. /* initialize system pm module */
  209. rt_system_pm_init(&_ops, timer_mask, RT_NULL);
  210. return 0;
  211. }
  212. INIT_BOARD_EXPORT(drv_pm_hw_init);
  213. #endif