drv_pm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. * 2020-12-15 liuhy the first version
  9. */
  10. #include "drv_pm.h"
  11. #ifdef RT_USING_PM
  12. //uint32_t save_mem[1024] __attribute__ ((aligned(4)));
  13. void save_register(void *p_head,uint32_t size,void *p_save)
  14. {
  15. memcpy(p_save,p_head,size);
  16. }
  17. void load_register(void *p_head,uint32_t size,void *p_load)
  18. {
  19. uint32_t tmp;
  20. memcpy(p_head,p_load,size);
  21. if((p_head == UART0) || (p_head == UART1) || (p_head == UART2) ||
  22. (p_head == UART3) || (p_head == UART4) || (p_head == UART5) )
  23. {
  24. tmp = ((UART_TypeDef*)p_load)->IVS;
  25. ((UART_TypeDef*)p_head)->IER = tmp;
  26. }
  27. }
  28. static void uart_console_reconfig(void)
  29. {
  30. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  31. rt_device_control(rt_console_get_device(), RT_DEVICE_CTRL_CONFIG, &config);
  32. }
  33. static void delay(void)
  34. {
  35. long i;
  36. rt_base_t level;
  37. level = rt_hw_interrupt_disable();
  38. i = 0;
  39. do{
  40. i++;
  41. }
  42. while (i < 4000000);
  43. rt_hw_interrupt_enable(level);
  44. }
  45. /**
  46. * This function will put ES32F369x into sleep mode.
  47. *
  48. * @param pm pointer to power manage structure
  49. */
  50. struct pm_callback_t
  51. {
  52. volatile int in_fun_times; /*进入函数的次数*/
  53. volatile char flag; /*标志*/
  54. volatile int mode; /*需要打印的模式*/
  55. };
  56. extern volatile struct pm_callback_t g_pm_data;
  57. static void sleep(struct rt_pm *pm, uint8_t mode)
  58. {
  59. switch (mode)
  60. {
  61. case PM_SLEEP_MODE_NONE:
  62. break;
  63. case PM_SLEEP_MODE_IDLE:
  64. break;
  65. case PM_SLEEP_MODE_LIGHT:
  66. /* Enter SLEEP Mode, Main regulator is ON */
  67. ald_pmu_stop1_enter();
  68. delay();
  69. break;
  70. case PM_SLEEP_MODE_DEEP:
  71. /* Enter STOP 2 mode */
  72. ald_pmu_stop2_enter();
  73. delay();
  74. break;
  75. case PM_SLEEP_MODE_STANDBY:
  76. /* Enter STANDBY mode */
  77. ald_pmu_stop2_enter();
  78. delay();
  79. break;
  80. case PM_SLEEP_MODE_SHUTDOWN:
  81. /* Enter SHUTDOWNN mode */
  82. ald_pmu_stop2_enter();
  83. delay();
  84. break;
  85. default:
  86. RT_ASSERT(0);
  87. break;
  88. }
  89. }
  90. static uint8_t run_speed[PM_RUN_MODE_MAX][2] =
  91. {
  92. {48, 0},
  93. {48, 1},
  94. {24, 2},
  95. {2, 3},
  96. };
  97. static void run(struct rt_pm *pm, uint8_t mode)
  98. {
  99. static uint8_t last_mode;
  100. static char *run_str[] = PM_RUN_MODE_NAMES;
  101. extern uint32_t __system_clock;
  102. if (mode == last_mode)
  103. return;
  104. last_mode = mode;
  105. ald_cmu_clock_config_default();
  106. __system_clock = 24000000;
  107. switch (mode)
  108. {
  109. case PM_RUN_MODE_HIGH_SPEED:
  110. case PM_RUN_MODE_NORMAL_SPEED:
  111. /* hosc 12MHz, from hosc/3 pll to 48MHz */
  112. ald_cmu_pll1_config(CMU_PLL1_INPUT_HRC_6, CMU_PLL1_OUTPUT_48M);
  113. /* MCLK 48MHz */
  114. ald_cmu_clock_config(CMU_CLOCK_PLL1, 48000000);
  115. break;
  116. case PM_RUN_MODE_MEDIUM_SPEED:
  117. break;
  118. case PM_RUN_MODE_LOW_SPEED:
  119. ald_cmu_clock_config(CMU_CLOCK_HRC, 2000000);
  120. break;
  121. default:
  122. break;
  123. }
  124. /* 4. 更新外设时钟 */
  125. uart_console_reconfig();
  126. /* Re-Configure the Systick time */
  127. SysTick_Config(ald_cmu_get_sys_clock() / RT_TICK_PER_SECOND);
  128. rt_kprintf("switch to %s mode, frequency = %d MHz\n", run_str[mode], run_speed[mode][0]);
  129. }
  130. /**
  131. * This function caculate the PM tick from OS tick
  132. *
  133. * @param tick OS tick
  134. *
  135. * @return the PM tick
  136. */
  137. static rt_tick_t es32f3_pm_tick_from_os_tick(rt_tick_t tick)
  138. {
  139. rt_uint32_t freq = 1;
  140. return (freq * tick / RT_TICK_PER_SECOND);
  141. }
  142. /**
  143. * This function caculate the OS tick from PM tick
  144. *
  145. * @param tick PM tick
  146. *
  147. * @return the OS tick
  148. */
  149. static rt_tick_t es32f3_os_tick_from_pm_tick(rt_uint32_t tick)
  150. {
  151. static rt_uint32_t os_tick_remain = 0;
  152. rt_uint32_t ret, freq;
  153. freq = 1;
  154. ret = (tick * RT_TICK_PER_SECOND + os_tick_remain) / freq;
  155. os_tick_remain += (tick * RT_TICK_PER_SECOND);
  156. os_tick_remain %= freq;
  157. return ret;
  158. }
  159. /**
  160. * This function start the timer of pm
  161. *
  162. * @param pm Pointer to power manage structure
  163. * @param timeout How many OS Ticks that MCU can sleep
  164. */
  165. static void pm_timer_start(struct rt_pm *pm, rt_uint32_t timeout)
  166. {
  167. RT_ASSERT(pm != RT_NULL);
  168. RT_ASSERT(timeout > 0);
  169. if (timeout != RT_TICK_MAX)
  170. {
  171. /* Convert OS Tick to pmtimer timeout value */
  172. timeout = es32f3_pm_tick_from_os_tick(timeout);
  173. /* MAX 0xFFFF */
  174. if (timeout > 0xFFFF)
  175. {
  176. timeout = 0xFFFF;
  177. }
  178. }
  179. }
  180. /**
  181. * This function stop the timer of pm
  182. *
  183. * @param pm Pointer to power manage structure
  184. */
  185. static void pm_timer_stop(struct rt_pm *pm)
  186. {
  187. RT_ASSERT(pm != RT_NULL);
  188. }
  189. /**
  190. * This function calculate how many OS Ticks that MCU have suspended
  191. *
  192. * @param pm Pointer to power manage structure
  193. *
  194. * @return OS Ticks
  195. */
  196. static rt_tick_t pm_timer_get_tick(struct rt_pm *pm)
  197. {
  198. rt_uint32_t timer_tick;
  199. RT_ASSERT(pm != RT_NULL);
  200. timer_tick = 1;
  201. return es32f3_os_tick_from_pm_tick(timer_tick);
  202. }
  203. /**
  204. * This function initialize the power manager
  205. */
  206. int drv_pm_hw_init(void)
  207. {
  208. static const struct rt_pm_ops _ops =
  209. {
  210. sleep,
  211. run,
  212. pm_timer_start,
  213. pm_timer_stop,
  214. pm_timer_get_tick
  215. };
  216. rt_uint8_t timer_mask = 0;
  217. /* initialize timer mask */
  218. timer_mask = 1UL << PM_SLEEP_MODE_DEEP;
  219. /* initialize system pm module */
  220. rt_system_pm_init(&_ops, timer_mask, RT_NULL);
  221. return 0;
  222. }
  223. INIT_BOARD_EXPORT(drv_pm_hw_init);
  224. #endif