drv_clk.c 8.2 KB

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