drv_pm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-05-06 Zero-Free first version
  9. */
  10. #include <board.h>
  11. #include <drv_lptim.h>
  12. static void uart_console_reconfig(void)
  13. {
  14. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  15. rt_device_control(rt_console_get_device(), RT_DEVICE_CTRL_CONFIG, &config);
  16. }
  17. /**
  18. * This function will put STM32L4xx into sleep mode.
  19. *
  20. * @param pm pointer to power manage structure
  21. */
  22. static void sleep(struct rt_pm *pm, uint8_t mode)
  23. {
  24. switch (mode)
  25. {
  26. case PM_SLEEP_MODE_NONE:
  27. break;
  28. case PM_SLEEP_MODE_IDLE:
  29. // __WFI();
  30. break;
  31. case PM_SLEEP_MODE_LIGHT:
  32. if (pm->run_mode == PM_RUN_MODE_LOW_SPEED)
  33. {
  34. /* Enter LP SLEEP Mode, Enable low-power regulator */
  35. HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);
  36. }
  37. else
  38. {
  39. /* Enter SLEEP Mode, Main regulator is ON */
  40. HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
  41. }
  42. break;
  43. case PM_SLEEP_MODE_DEEP:
  44. /* Enter STOP 2 mode */
  45. HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
  46. /* Re-configure the system clock */
  47. SystemClock_ReConfig(pm->run_mode);
  48. break;
  49. case PM_SLEEP_MODE_STANDBY:
  50. /* Enter STANDBY mode */
  51. HAL_PWR_EnterSTANDBYMode();
  52. break;
  53. case PM_SLEEP_MODE_SHUTDOWN:
  54. /* Enter SHUTDOWNN mode */
  55. HAL_PWREx_EnterSHUTDOWNMode();
  56. break;
  57. default:
  58. RT_ASSERT(0);
  59. break;
  60. }
  61. }
  62. static uint8_t run_speed[PM_RUN_MODE_MAX][2] =
  63. {
  64. {80, 0},
  65. {80, 1},
  66. {24, 2},
  67. {2, 3},
  68. };
  69. static void run(struct rt_pm *pm, uint8_t mode)
  70. {
  71. static uint8_t last_mode;
  72. static char *run_str[] = PM_RUN_MODE_NAMES;
  73. if (mode == last_mode)
  74. return;
  75. last_mode = mode;
  76. /* 1. 设置 MSI 作为 SYSCLK 时钟源,以修改 PLL */
  77. SystemClock_MSI_ON();
  78. /* 2. 根据RUN模式切换时钟频率(HSI) */
  79. switch (mode)
  80. {
  81. case PM_RUN_MODE_HIGH_SPEED:
  82. case PM_RUN_MODE_NORMAL_SPEED:
  83. SystemClock_80M();
  84. /* Configure the main internal regulator output voltage (Range1 by default)*/
  85. HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  86. break;
  87. case PM_RUN_MODE_MEDIUM_SPEED:
  88. SystemClock_24M();
  89. /* Configure the main internal regulator output voltage */
  90. HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2);
  91. break;
  92. case PM_RUN_MODE_LOW_SPEED:
  93. SystemClock_2M();
  94. /* Enter LP RUN mode */
  95. HAL_PWREx_EnableLowPowerRunMode();
  96. break;
  97. default:
  98. break;
  99. }
  100. /* 3. 关闭 MSI 时钟 */
  101. // SystemClock_MSI_OFF();
  102. /* 4. 更新外设时钟 */
  103. uart_console_reconfig();
  104. /* Re-Configure the Systick time */
  105. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  106. /* Re-Configure the Systick */
  107. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  108. rt_kprintf("switch to %s mode, frequency = %d MHz\n", run_str[mode], run_speed[mode][0]);
  109. }
  110. /**
  111. * This function caculate the PM tick from OS tick
  112. *
  113. * @param tick OS tick
  114. *
  115. * @return the PM tick
  116. */
  117. static rt_tick_t stm32l4_pm_tick_from_os_tick(rt_tick_t tick)
  118. {
  119. rt_uint32_t freq = stm32l4_lptim_get_countfreq();
  120. return (freq * tick / RT_TICK_PER_SECOND);
  121. }
  122. /**
  123. * This function caculate the OS tick from PM tick
  124. *
  125. * @param tick PM tick
  126. *
  127. * @return the OS tick
  128. */
  129. static rt_tick_t stm32l4_os_tick_from_pm_tick(rt_uint32_t tick)
  130. {
  131. static rt_uint32_t os_tick_remain = 0;
  132. rt_uint32_t ret, freq;
  133. freq = stm32l4_lptim_get_countfreq();
  134. ret = (tick * RT_TICK_PER_SECOND + os_tick_remain) / freq;
  135. os_tick_remain += (tick * RT_TICK_PER_SECOND);
  136. os_tick_remain %= freq;
  137. return ret;
  138. }
  139. /**
  140. * This function start the timer of pm
  141. *
  142. * @param pm Pointer to power manage structure
  143. * @param timeout How many OS Ticks that MCU can sleep
  144. */
  145. static void pm_timer_start(struct rt_pm *pm, rt_uint32_t timeout)
  146. {
  147. RT_ASSERT(pm != RT_NULL);
  148. RT_ASSERT(timeout > 0);
  149. if (timeout != RT_TICK_MAX)
  150. {
  151. /* Convert OS Tick to pmtimer timeout value */
  152. timeout = stm32l4_pm_tick_from_os_tick(timeout);
  153. if (timeout > stm32l4_lptim_get_tick_max())
  154. {
  155. timeout = stm32l4_lptim_get_tick_max();
  156. }
  157. /* Enter PM_TIMER_MODE */
  158. stm32l4_lptim_start(timeout);
  159. }
  160. }
  161. /**
  162. * This function stop the timer of pm
  163. *
  164. * @param pm Pointer to power manage structure
  165. */
  166. static void pm_timer_stop(struct rt_pm *pm)
  167. {
  168. RT_ASSERT(pm != RT_NULL);
  169. /* Reset pmtimer status */
  170. stm32l4_lptim_stop();
  171. }
  172. /**
  173. * This function calculate how many OS Ticks that MCU have suspended
  174. *
  175. * @param pm Pointer to power manage structure
  176. *
  177. * @return OS Ticks
  178. */
  179. static rt_tick_t pm_timer_get_tick(struct rt_pm *pm)
  180. {
  181. rt_uint32_t timer_tick;
  182. RT_ASSERT(pm != RT_NULL);
  183. timer_tick = stm32l4_lptim_get_current_tick();
  184. return stm32l4_os_tick_from_pm_tick(timer_tick);
  185. }
  186. /**
  187. * This function initialize the power manager
  188. */
  189. int drv_pm_hw_init(void)
  190. {
  191. static const struct rt_pm_ops _ops =
  192. {
  193. sleep,
  194. run,
  195. pm_timer_start,
  196. pm_timer_stop,
  197. pm_timer_get_tick
  198. };
  199. rt_uint8_t timer_mask = 0;
  200. /* Enable Power Clock */
  201. __HAL_RCC_PWR_CLK_ENABLE();
  202. /* initialize timer mask */
  203. timer_mask = 1UL << PM_SLEEP_MODE_DEEP;
  204. /* initialize system pm module */
  205. rt_system_pm_init(&_ops, timer_mask, RT_NULL);
  206. return 0;
  207. }
  208. INIT_BOARD_EXPORT(drv_pm_hw_init);