drv_clk.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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-09-29 PLWang 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 (72000000ul)
  22. #define CONFIG_NORMAL_SPEED_FREQ (72000000ul)
  23. #define CONFIG_MEDIUM_SPEED_FREQ (54000000ul)
  24. #define CONFIG_LOW_SPEED_FREQ (36000000ul)
  25. /* Timer module assigned for pm device usage. */
  26. /* e.g. If TIMERn is reserved for pm, then define the PM_TIMER_USE_INSTANCE
  27. macro to n value (without parentheses). */
  28. #define PM_TIMER_USE_INSTANCE 3
  29. /* Concatenate */
  30. #define _CONCAT2_(x, y) x##y
  31. #define _CONCAT3_(x, y, z) x##y##z
  32. #define CONCAT2(x, y) _CONCAT2_(x, y)
  33. #define CONCAT3(x, y, z) _CONCAT3_(x,y,z)
  34. /* Concatenate the macros of timer instance for driver usage. */
  35. #define PM_TIMER CONCAT2(TIMER, PM_TIMER_USE_INSTANCE)
  36. #define PM_TMR CONCAT2(TMR, PM_TIMER_USE_INSTANCE)
  37. #define PM_TIMER_MODULE CONCAT2(PM_TMR, _MODULE)
  38. #define PM_TIMER_IRQn CONCAT2(PM_TMR, _IRQn)
  39. #define PM_TIMER_IRQHandler CONCAT2(PM_TMR, _IRQHandler)
  40. #define PM_TIMER_SEL_LXT CONCAT3(CLK_CLKSEL1_, PM_TMR, SEL_LXT)
  41. /* Private typedef --------------------------------------------------------------*/
  42. #define TIMER_RESET_CNT(timer) do{(timer)->CTL |= TIMER_CTL_RSTCNT_Msk; \
  43. while(((timer)->CTL & TIMER_CTL_RSTCNT_Msk) == TIMER_CTL_RSTCNT_Msk); \
  44. }while(0)
  45. /* Private functions ------------------------------------------------------------*/
  46. static void pm_sleep(struct rt_pm *pm, rt_uint8_t mode);
  47. static void pm_run(struct rt_pm *pm, rt_uint8_t mode);
  48. static void pm_timer_start(struct rt_pm *pm, rt_uint32_t timeout);
  49. static void pm_timer_stop(struct rt_pm *pm);
  50. static rt_tick_t pm_timer_get_tick(struct rt_pm *pm);
  51. static rt_tick_t pm_tick_from_os_tick(rt_tick_t os_tick);
  52. static rt_tick_t os_tick_from_pm_tick(rt_tick_t pm_tick);
  53. /* Public functions -------------------------------------------------------------*/
  54. int rt_hw_pm_init(void);
  55. /* Private variables ------------------------------------------------------------*/
  56. static struct rt_pm_ops ops =
  57. {
  58. .sleep = pm_sleep,
  59. .run = pm_run,
  60. .timer_start = pm_timer_start,
  61. .timer_stop = pm_timer_stop,
  62. .timer_get_tick = pm_timer_get_tick,
  63. };
  64. struct rt_device pm;
  65. /* pm sleep() entry */
  66. static void pm_sleep(struct rt_pm *pm, rt_uint8_t mode)
  67. {
  68. SYS_UnlockReg();
  69. switch (mode)
  70. {
  71. /* wake-up source: */
  72. /* PM_SLEEP_MODE_LIGHT : TIMERn */
  73. /* PM_SLEEP_MODE_DEEP : TIMERn */
  74. /* PM_SLEEP_MODE_STANDBY : wake-up timer (optional) */
  75. /* PM_SLEEP_MODE_SHUTDOWN : wake-up timer (optional) */
  76. case PM_SLEEP_MODE_NONE:
  77. case PM_SLEEP_MODE_IDLE:
  78. break;
  79. case PM_SLEEP_MODE_LIGHT:
  80. case PM_SLEEP_MODE_DEEP:
  81. CLK_PowerDown();
  82. break;
  83. case PM_SLEEP_MODE_STANDBY:
  84. LOG_W("Device does not support PM_SLEEP_MODE_STANDBY!");
  85. break;
  86. case PM_SLEEP_MODE_SHUTDOWN:
  87. LOG_W("Device does not support PM_SLEEP_MODE_SHUTDOWN!");
  88. break;
  89. default:
  90. RT_ASSERT(0);
  91. break;
  92. }
  93. SYS_LockReg();
  94. }
  95. /* pm run() entry */
  96. static void pm_run(struct rt_pm *pm, rt_uint8_t mode)
  97. {
  98. static uint8_t prev_mode = RT_PM_DEFAULT_RUN_MODE;
  99. /* ignore it if power mode is the same. */
  100. if (mode == prev_mode)
  101. return;
  102. prev_mode = mode;
  103. SYS_UnlockReg();
  104. /* Switch run mode frequency using PLL + HXT if HXT is enabled.
  105. Otherwise, the system clock will use PLL + HIRC. */
  106. switch (mode)
  107. {
  108. case PM_RUN_MODE_HIGH_SPEED:
  109. CLK_SetCoreClock(CONFIG_HIGH_SPEED_FREQ);
  110. break;
  111. case PM_RUN_MODE_NORMAL_SPEED:
  112. CLK_SetCoreClock(CONFIG_NORMAL_SPEED_FREQ);
  113. break;
  114. case PM_RUN_MODE_MEDIUM_SPEED:
  115. CLK_SetCoreClock(CONFIG_MEDIUM_SPEED_FREQ);
  116. break;
  117. case PM_RUN_MODE_LOW_SPEED:
  118. CLK_SetCoreClock(CONFIG_LOW_SPEED_FREQ);
  119. break;
  120. default:
  121. RT_ASSERT(0);
  122. break;
  123. }
  124. SystemCoreClockUpdate();
  125. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  126. SYS_LockReg();
  127. }
  128. static void hw_timer_init(void)
  129. {
  130. /* Assign a hardware timer for pm usage. */
  131. SYS_UnlockReg();
  132. CLK_SetModuleClock(PM_TIMER_MODULE, PM_TIMER_SEL_LXT, MODULE_NoMsk);
  133. CLK_EnableModuleClock(PM_TIMER_MODULE);
  134. SYS_LockReg();
  135. /* Initialize timer and enable wakeup function. */
  136. TIMER_Open(PM_TIMER, TIMER_CONTINUOUS_MODE, 1);
  137. TIMER_SET_PRESCALE_VALUE(PM_TIMER, 0);
  138. TIMER_EnableInt(PM_TIMER);
  139. TIMER_EnableWakeup(PM_TIMER);
  140. NVIC_EnableIRQ(PM_TIMER_IRQn);
  141. }
  142. /* convert os tick to pm timer tick */
  143. static rt_tick_t pm_tick_from_os_tick(rt_tick_t os_tick)
  144. {
  145. rt_uint32_t hz = TIMER_GetModuleClock(PM_TIMER);
  146. return (rt_tick_t)(hz * os_tick / RT_TICK_PER_SECOND);
  147. }
  148. /* convert pm timer tick to os tick */
  149. static rt_tick_t os_tick_from_pm_tick(rt_tick_t pm_tick)
  150. {
  151. static rt_uint32_t os_tick_remain = 0;
  152. rt_uint32_t ret, hz;
  153. hz = TIMER_GetModuleClock(PM_TIMER);
  154. ret = (pm_tick * RT_TICK_PER_SECOND + os_tick_remain) / hz;
  155. os_tick_remain += (pm_tick * RT_TICK_PER_SECOND);
  156. os_tick_remain %= hz;
  157. return ret;
  158. }
  159. /* pm_ops timer_get_tick() entry */
  160. static rt_tick_t pm_timer_get_tick(struct rt_pm *pm)
  161. {
  162. rt_tick_t tick;
  163. tick = TIMER_GetCounter(PM_TIMER);
  164. return os_tick_from_pm_tick(tick);
  165. }
  166. /* pm timer_start() entry */
  167. static void pm_timer_start(struct rt_pm *pm, rt_uint32_t timeout)
  168. {
  169. int tick;
  170. if (timeout == RT_TICK_MAX)
  171. return;
  172. /* start pm timer to compensate the os tick in power down mode */
  173. tick = pm_tick_from_os_tick(timeout);
  174. TIMER_SET_CMP_VALUE(PM_TIMER, tick);
  175. TIMER_Start(PM_TIMER);
  176. }
  177. /* pm timer_stop() entry */
  178. static void pm_timer_stop(struct rt_pm *pm)
  179. {
  180. TIMER_Stop(PM_TIMER);
  181. TIMER_RESET_CNT(PM_TIMER);
  182. }
  183. /* pm device driver initialize. */
  184. int rt_hw_pm_init(void)
  185. {
  186. rt_uint8_t timer_mask;
  187. hw_timer_init();
  188. /* initialize timer mask */
  189. timer_mask = (1UL << PM_SLEEP_MODE_LIGHT) |
  190. (1UL << PM_SLEEP_MODE_DEEP);
  191. /* initialize system pm module */
  192. rt_system_pm_init(&ops, timer_mask, RT_NULL);
  193. return RT_EOK;
  194. }
  195. INIT_BOARD_EXPORT(rt_hw_pm_init);
  196. /* pm timer interrupt entry */
  197. void PM_TIMER_IRQHandler(void)
  198. {
  199. rt_interrupt_enter();
  200. if (TIMER_GetIntFlag(PM_TIMER))
  201. {
  202. TIMER_ClearIntFlag(PM_TIMER);
  203. }
  204. if (TIMER_GetWakeupFlag(PM_TIMER))
  205. {
  206. TIMER_ClearWakeupFlag(PM_TIMER);
  207. }
  208. rt_interrupt_leave();
  209. }
  210. #endif /* BSP_USING_CLK */