drv_clk.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-03-25 klcheng First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(BSP_USING_CLK)
  14. #include <rtdevice.h>
  15. #include <rtdbg.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include "NuMicro.h"
  19. /* Private define ---------------------------------------------------------------*/
  20. /* pm run mode speed mapping */
  21. #define CONFIG_HIGH_SPEED_FREQ (192000000ul)
  22. #define CONFIG_NORMAL_SPEED_FREQ (192000000ul)
  23. #define CONFIG_MEDIMUM_SPEED_FREQ (144000000ul)
  24. #define CONFIG_LOW_SPEED_FREQ (72000000ul)
  25. /* pm sleep mode mapping */
  26. #define CONFIG_MODE_LIGHT (CLK_PMUCTL_PDMSEL_FWPD)
  27. #define CONFIG_MODE_DEEP (CLK_PMUCTL_PDMSEL_PD)
  28. #define CONFIG_MODE_STANDBY (CLK_PMUCTL_PDMSEL_SPD1)
  29. #define CONFIG_MODE_SHUTDOWN (CLK_PMUCTL_PDMSEL_DPD)
  30. #if defined (NU_CLK_INVOKE_WKTMR)
  31. /* Wake-up timer clock source is OSC10K */
  32. #define WKTMR_INTERVAL (CLK_PMUCTL_WKTMRIS_65536)
  33. #endif
  34. /* Timer module assigned for pm device usage. */
  35. /* e.g. If TIMERn is reserved for pm, then define the PM_TIMER_USE_INSTANCE
  36. macro to n value (without parentheses). */
  37. #define PM_TIMER_USE_INSTANCE 3
  38. /* Concatenate */
  39. #define _CONCAT2_(x, y) x##y
  40. #define _CONCAT3_(x, y, z) x##y##z
  41. #define CONCAT2(x, y) _CONCAT2_(x, y)
  42. #define CONCAT3(x, y, z) _CONCAT3_(x,y,z)
  43. /* Concatenate the macros of timer instance for driver usage. */
  44. #define PM_TIMER CONCAT2(TIMER, PM_TIMER_USE_INSTANCE)
  45. #define PM_TMR CONCAT2(TMR, PM_TIMER_USE_INSTANCE)
  46. #define PM_TIMER_MODULE CONCAT2(PM_TMR, _MODULE)
  47. #define PM_TIMER_IRQn CONCAT2(PM_TMR, _IRQn)
  48. #define PM_TIMER_IRQHandler CONCAT2(PM_TMR, _IRQHandler)
  49. #define PM_TIMER_SEL_LXT CONCAT3(CLK_CLKSEL1_, PM_TMR, SEL_LXT)
  50. /* Private typedef --------------------------------------------------------------*/
  51. /* Private functions ------------------------------------------------------------*/
  52. static void pm_sleep(struct rt_pm *pm, rt_uint8_t mode);
  53. static void pm_run(struct rt_pm *pm, rt_uint8_t mode);
  54. static void pm_timer_start(struct rt_pm *pm, rt_uint32_t timeout);
  55. static void pm_timer_stop(struct rt_pm *pm);
  56. static rt_tick_t pm_timer_get_tick(struct rt_pm *pm);
  57. static rt_tick_t pm_tick_from_os_tick(rt_tick_t os_tick);
  58. static rt_tick_t os_tick_from_pm_tick(rt_tick_t pm_tick);
  59. /* Public functions -------------------------------------------------------------*/
  60. int rt_hw_pm_init(void);
  61. /* Private variables ------------------------------------------------------------*/
  62. static struct rt_pm_ops ops =
  63. {
  64. .sleep = pm_sleep,
  65. .run = pm_run,
  66. .timer_start = pm_timer_start,
  67. .timer_stop = pm_timer_stop,
  68. .timer_get_tick = pm_timer_get_tick,
  69. };
  70. struct rt_device pm;
  71. /* Sleep and power-down mapping */
  72. const static uint32_t g_au32SleepingMode[PM_SLEEP_MODE_MAX] =
  73. {
  74. 0,
  75. 0,
  76. CONFIG_MODE_LIGHT,
  77. CONFIG_MODE_DEEP,
  78. CONFIG_MODE_STANDBY,
  79. CONFIG_MODE_SHUTDOWN
  80. };
  81. /* pm sleep() entry */
  82. static void pm_sleep(struct rt_pm *pm, rt_uint8_t mode)
  83. {
  84. RT_ASSERT(mode < PM_SLEEP_MODE_MAX);
  85. if ((mode == PM_SLEEP_MODE_NONE) || (mode == PM_SLEEP_MODE_IDLE))
  86. return;
  87. /* wake-up source: */
  88. /* PM_SLEEP_MODE_LIGHT : TIMERn */
  89. /* PM_SLEEP_MODE_DEEP : TIMERn */
  90. /* PM_SLEEP_MODE_STANDBY : wake-up timer (optional) */
  91. /* PM_SLEEP_MODE_SHUTDOWN : wake-up timer (optional) */
  92. SYS_UnlockReg();
  93. #if defined (NU_CLK_INVOKE_WKTMR)
  94. if ((mode == PM_SLEEP_MODE_SHUTDOWN) || (mode == PM_SLEEP_MODE_STANDBY))
  95. {
  96. /* Enable wake-up timer with pre-defined interval if it is invoked */
  97. CLK_SET_WKTMR_INTERVAL(WKTMR_INTERVAL);
  98. CLK_ENABLE_WKTMR();
  99. }
  100. #endif
  101. CLK_SetPowerDownMode(g_au32SleepingMode[mode]);
  102. CLK_PowerDown();
  103. SYS_LockReg();
  104. }
  105. /* pm run() entry */
  106. static void pm_run(struct rt_pm *pm, rt_uint8_t mode)
  107. {
  108. static uint8_t prev_mode = RT_PM_DEFAULT_RUN_MODE;
  109. /* ignore it if power mode is the same. */
  110. if (mode == prev_mode)
  111. return;
  112. prev_mode = mode;
  113. SYS_UnlockReg();
  114. /* Switch run mode frequency using PLL + HXT if HXT is enabled.
  115. Otherwise, the system clock will use PLL + HIRC. */
  116. switch (mode)
  117. {
  118. case PM_RUN_MODE_HIGH_SPEED:
  119. CLK_SetCoreClock(CONFIG_HIGH_SPEED_FREQ);
  120. break;
  121. case PM_RUN_MODE_NORMAL_SPEED:
  122. CLK_SetCoreClock(CONFIG_NORMAL_SPEED_FREQ);
  123. break;
  124. case PM_RUN_MODE_MEDIUM_SPEED:
  125. CLK_SetCoreClock(CONFIG_MEDIMUM_SPEED_FREQ);
  126. break;
  127. case PM_RUN_MODE_LOW_SPEED:
  128. CLK_SetCoreClock(CONFIG_LOW_SPEED_FREQ);
  129. break;
  130. default:
  131. RT_ASSERT(0);
  132. break;
  133. }
  134. SystemCoreClockUpdate();
  135. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  136. SYS_LockReg();
  137. }
  138. static void hw_timer_init(void)
  139. {
  140. /* Assign a hardware timer for pm usage. */
  141. SYS_UnlockReg();
  142. CLK_SetModuleClock(PM_TIMER_MODULE, PM_TIMER_SEL_LXT, MODULE_NoMsk);
  143. CLK_EnableModuleClock(PM_TIMER_MODULE);
  144. SYS_LockReg();
  145. /* Initialize timer and enable wakeup function. */
  146. TIMER_Open(PM_TIMER, TIMER_CONTINUOUS_MODE, 1);
  147. TIMER_SET_PRESCALE_VALUE(PM_TIMER, 0);
  148. TIMER_EnableInt(PM_TIMER);
  149. TIMER_EnableWakeup(PM_TIMER);
  150. NVIC_EnableIRQ(PM_TIMER_IRQn);
  151. }
  152. /* convert os tick to pm timer tick */
  153. static rt_tick_t pm_tick_from_os_tick(rt_tick_t os_tick)
  154. {
  155. rt_uint32_t hz = TIMER_GetModuleClock(PM_TIMER);
  156. return (rt_tick_t)(hz * os_tick / RT_TICK_PER_SECOND);
  157. }
  158. /* convert pm timer tick to os tick */
  159. static rt_tick_t os_tick_from_pm_tick(rt_tick_t pm_tick)
  160. {
  161. static rt_uint32_t os_tick_remain = 0;
  162. rt_uint32_t ret, hz;
  163. hz = TIMER_GetModuleClock(PM_TIMER);
  164. ret = (pm_tick * RT_TICK_PER_SECOND + os_tick_remain) / hz;
  165. os_tick_remain += (pm_tick * RT_TICK_PER_SECOND);
  166. os_tick_remain %= hz;
  167. return ret;
  168. }
  169. /* pm_ops timer_get_tick() entry */
  170. static rt_tick_t pm_timer_get_tick(struct rt_pm *pm)
  171. {
  172. rt_tick_t tick;
  173. tick = TIMER_GetCounter(PM_TIMER);
  174. return os_tick_from_pm_tick(tick);
  175. }
  176. /* pm timer_start() entry */
  177. static void pm_timer_start(struct rt_pm *pm, rt_uint32_t timeout)
  178. {
  179. int tick;
  180. if (timeout == RT_TICK_MAX)
  181. return;
  182. /* start pm timer to compensate the os tick in power down mode */
  183. tick = pm_tick_from_os_tick(timeout);
  184. TIMER_SET_CMP_VALUE(PM_TIMER, tick);
  185. TIMER_Start(PM_TIMER);
  186. }
  187. /* pm timer_stop() entry */
  188. static void pm_timer_stop(struct rt_pm *pm)
  189. {
  190. TIMER_Stop(PM_TIMER);
  191. TIMER_ResetCounter(PM_TIMER);
  192. }
  193. /* pm device driver initialize. */
  194. int rt_hw_pm_init(void)
  195. {
  196. rt_uint8_t timer_mask;
  197. if (CLK_GetPMUWKSrc())
  198. {
  199. /* Release I/O hold status after wake-up from Standby Power-down Mode (SPD) */
  200. CLK->IOPDCTL = 1;
  201. /* Clear Power Manager Status register */
  202. CLK->PMUSTS = CLK_PMUSTS_CLRWK_Msk;
  203. }
  204. hw_timer_init();
  205. /* initialize timer mask */
  206. timer_mask = (1UL << PM_SLEEP_MODE_LIGHT) |
  207. (1UL << PM_SLEEP_MODE_DEEP);
  208. /* initialize system pm module */
  209. rt_system_pm_init(&ops, timer_mask, RT_NULL);
  210. return RT_EOK;
  211. }
  212. INIT_BOARD_EXPORT(rt_hw_pm_init);
  213. /* pm timer interrupt entry */
  214. void PM_TIMER_IRQHandler(void)
  215. {
  216. rt_interrupt_enter();
  217. if (TIMER_GetIntFlag(PM_TIMER))
  218. {
  219. TIMER_ClearIntFlag(PM_TIMER);
  220. }
  221. if (TIMER_GetWakeupFlag(PM_TIMER))
  222. {
  223. TIMER_ClearWakeupFlag(PM_TIMER);
  224. }
  225. rt_interrupt_leave();
  226. }
  227. #endif /* BSP_USING_CLK */