drv_clk.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. /* pm sleep() entry */
  72. static void pm_sleep(struct rt_pm *pm, rt_uint8_t mode)
  73. {
  74. SYS_UnlockReg();
  75. switch (mode)
  76. {
  77. /* wake-up source: */
  78. /* PM_SLEEP_MODE_LIGHT : TIMERn */
  79. /* PM_SLEEP_MODE_DEEP : TIMERn */
  80. /* PM_SLEEP_MODE_STANDBY : wake-up timer (optional) */
  81. /* PM_SLEEP_MODE_SHUTDOWN : wake-up timer (optional) */
  82. case PM_SLEEP_MODE_NONE:
  83. case PM_SLEEP_MODE_IDLE:
  84. break;
  85. case PM_SLEEP_MODE_LIGHT:
  86. CLK_SetPowerDownMode(CONFIG_MODE_LIGHT);
  87. CLK_PowerDown();
  88. break;
  89. case PM_SLEEP_MODE_DEEP:
  90. CLK_SetPowerDownMode(CONFIG_MODE_DEEP);
  91. CLK_PowerDown();
  92. break;
  93. case PM_SLEEP_MODE_STANDBY:
  94. #if defined (NU_CLK_INVOKE_WKTMR)
  95. /* Enable wake-up timer with pre-defined interval if it is invoked */
  96. CLK_SET_WKTMR_INTERVAL(WKTMR_INTERVAL);
  97. CLK_ENABLE_WKTMR();
  98. #endif
  99. CLK_SetPowerDownMode(CONFIG_MODE_STANDBY);
  100. CLK_PowerDown();
  101. break;
  102. case PM_SLEEP_MODE_SHUTDOWN:
  103. #if defined (NU_CLK_INVOKE_WKTMR)
  104. /* Enable wake-up timer with pre-defined interval if it is invoked */
  105. CLK_SET_WKTMR_INTERVAL(WKTMR_INTERVAL);
  106. CLK_ENABLE_WKTMR();
  107. #endif
  108. CLK_SetPowerDownMode(CONFIG_MODE_SHUTDOWN);
  109. CLK_PowerDown();
  110. break;
  111. default:
  112. RT_ASSERT(0);
  113. break;
  114. }
  115. SYS_LockReg();
  116. }
  117. /* pm run() entry */
  118. static void pm_run(struct rt_pm *pm, rt_uint8_t mode)
  119. {
  120. static uint8_t prev_mode = RT_PM_DEFAULT_RUN_MODE;
  121. /* ignore it if power mode is the same. */
  122. if (mode == prev_mode)
  123. return;
  124. prev_mode = mode;
  125. SYS_UnlockReg();
  126. /* Switch run mode frequency using PLL + HXT if HXT is enabled.
  127. Otherwise, the system clock will use PLL + HIRC. */
  128. switch (mode)
  129. {
  130. case PM_RUN_MODE_HIGH_SPEED:
  131. CLK_SetCoreClock(CONFIG_HIGH_SPEED_FREQ);
  132. break;
  133. case PM_RUN_MODE_NORMAL_SPEED:
  134. CLK_SetCoreClock(CONFIG_NORMAL_SPEED_FREQ);
  135. break;
  136. case PM_RUN_MODE_MEDIUM_SPEED:
  137. CLK_SetCoreClock(CONFIG_MEDIMUM_SPEED_FREQ);
  138. break;
  139. case PM_RUN_MODE_LOW_SPEED:
  140. CLK_SetCoreClock(CONFIG_LOW_SPEED_FREQ);
  141. break;
  142. default:
  143. RT_ASSERT(0);
  144. break;
  145. }
  146. SystemCoreClockUpdate();
  147. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  148. SYS_LockReg();
  149. }
  150. static void hw_timer_init(void)
  151. {
  152. /* Assign a hardware timer for pm usage. */
  153. SYS_UnlockReg();
  154. CLK_SetModuleClock(PM_TIMER_MODULE, PM_TIMER_SEL_LXT, MODULE_NoMsk);
  155. CLK_EnableModuleClock(PM_TIMER_MODULE);
  156. SYS_LockReg();
  157. /* Initialize timer and enable wakeup function. */
  158. TIMER_Open(PM_TIMER, TIMER_CONTINUOUS_MODE, 1);
  159. TIMER_SET_PRESCALE_VALUE(PM_TIMER, 0);
  160. TIMER_EnableInt(PM_TIMER);
  161. TIMER_EnableWakeup(PM_TIMER);
  162. NVIC_EnableIRQ(PM_TIMER_IRQn);
  163. }
  164. /* convert os tick to pm timer tick */
  165. static rt_tick_t pm_tick_from_os_tick(rt_tick_t os_tick)
  166. {
  167. rt_uint32_t hz = TIMER_GetModuleClock(PM_TIMER);
  168. return (rt_tick_t)(hz * os_tick / RT_TICK_PER_SECOND);
  169. }
  170. /* convert pm timer tick to os tick */
  171. static rt_tick_t os_tick_from_pm_tick(rt_tick_t pm_tick)
  172. {
  173. static rt_uint32_t os_tick_remain = 0;
  174. rt_uint32_t ret, hz;
  175. hz = TIMER_GetModuleClock(PM_TIMER);
  176. ret = (pm_tick * RT_TICK_PER_SECOND + os_tick_remain) / hz;
  177. os_tick_remain += (pm_tick * RT_TICK_PER_SECOND);
  178. os_tick_remain %= hz;
  179. return ret;
  180. }
  181. /* pm_ops timer_get_tick() entry */
  182. static rt_tick_t pm_timer_get_tick(struct rt_pm *pm)
  183. {
  184. rt_tick_t tick;
  185. tick = TIMER_GetCounter(PM_TIMER);
  186. return os_tick_from_pm_tick(tick);
  187. }
  188. /* pm timer_start() entry */
  189. static void pm_timer_start(struct rt_pm *pm, rt_uint32_t timeout)
  190. {
  191. int tick;
  192. if (timeout == RT_TICK_MAX)
  193. return;
  194. /* start pm timer to compensate the os tick in power down mode */
  195. tick = pm_tick_from_os_tick(timeout);
  196. TIMER_SET_CMP_VALUE(PM_TIMER, tick);
  197. TIMER_Start(PM_TIMER);
  198. }
  199. /* pm timer_stop() entry */
  200. static void pm_timer_stop(struct rt_pm *pm)
  201. {
  202. TIMER_Stop(PM_TIMER);
  203. TIMER_ResetCounter(PM_TIMER);
  204. }
  205. /* pm device driver initialize. */
  206. int rt_hw_pm_init(void)
  207. {
  208. rt_uint8_t timer_mask;
  209. if (CLK_GetPMUWKSrc())
  210. {
  211. /* Release I/O hold status after wake-up from Standby Power-down Mode (SPD) */
  212. CLK->IOPDCTL = 1;
  213. /* Clear Power Manager Status register */
  214. CLK->PMUSTS = CLK_PMUSTS_CLRWK_Msk;
  215. }
  216. hw_timer_init();
  217. /* initialize timer mask */
  218. timer_mask = (1UL << PM_SLEEP_MODE_LIGHT) |
  219. (1UL << PM_SLEEP_MODE_DEEP);
  220. /* initialize system pm module */
  221. rt_system_pm_init(&ops, timer_mask, RT_NULL);
  222. return RT_EOK;
  223. }
  224. INIT_BOARD_EXPORT(rt_hw_pm_init);
  225. /* pm timer interrupt entry */
  226. void PM_TIMER_IRQHandler(void)
  227. {
  228. rt_interrupt_enter();
  229. if (TIMER_GetIntFlag(PM_TIMER))
  230. {
  231. TIMER_ClearIntFlag(PM_TIMER);
  232. }
  233. if (TIMER_GetWakeupFlag(PM_TIMER))
  234. {
  235. TIMER_ClearWakeupFlag(PM_TIMER);
  236. }
  237. rt_interrupt_leave();
  238. }
  239. #endif /* BSP_USING_CLK */