hpm_pllctlv2_drv.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2022-2024 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_PLLCTLV2_DRV_H
  8. #define HPM_PLLCTLV2_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_soc_feature.h"
  11. #include "hpm_pllctlv2_regs.h"
  12. #define PLLCTLV2_CLK_SRC_XTAL24M (0U)
  13. #define PLLCTLV2_CLK_SRC_IRC24M (1U)
  14. /**
  15. * @brief PLLCTLV2 driver APIs
  16. * @defgroup pllctlv2_interface PLLCTLV2 driver APIs
  17. * @{
  18. */
  19. /**
  20. * @brief Check if external crystal is stable
  21. * @param [in] ptr PLLCTLV2 base address
  22. * @return true if external crystal is stable
  23. */
  24. static inline bool pllctlv2_xtal_is_stable(PLLCTLV2_Type *ptr)
  25. {
  26. return IS_HPM_BITMASK_SET(ptr->XTAL, PLLCTLV2_XTAL_RESPONSE_MASK);
  27. }
  28. /**
  29. * @brief Check if external crystal is enabled
  30. * @param [in] ptr PLLCTLV2 base address
  31. * @return true if external crystal is enabled
  32. */
  33. static inline bool pllctlv2_xtal_is_enabled(PLLCTLV2_Type *ptr)
  34. {
  35. return IS_HPM_BITMASK_SET(ptr->XTAL, PLLCTLV2_XTAL_ENABLE_MASK);
  36. }
  37. /**
  38. * @brief Set external crystal ramp-up time
  39. * @param [in] ptr PLLCTLV2 base address
  40. * @param [in] rc24m_cycles Cycles of RC24M clock
  41. */
  42. static inline void pllctlv2_xtal_set_rampup_time(PLLCTLV2_Type *ptr, uint32_t rc24m_cycles)
  43. {
  44. ptr->XTAL = (ptr->XTAL & ~PLLCTLV2_XTAL_RAMP_TIME_MASK) | PLLCTLV2_XTAL_RAMP_TIME_SET(rc24m_cycles);
  45. }
  46. /**
  47. * @brief Check is PLL is stable
  48. * @param [in] ptr PLLCTLv2 base address
  49. * @param [in] pll PLL index
  50. * @return true if PLL is stable
  51. */
  52. static inline bool pllctlv2_pll_is_stable(PLLCTLV2_Type *ptr, uint8_t pll)
  53. {
  54. return IS_HPM_BITMASK_SET(ptr->PLL[pll].MFI, PLLCTLV2_PLL_MFI_RESPONSE_MASK);
  55. }
  56. /**
  57. * @brief Check if PLL is enabled
  58. * @param [in] ptr PLLCTLV2 base address
  59. * @param [in] pll PLL index
  60. * @return true if PLL is enabled
  61. */
  62. static inline bool pllctlv2_pll_is_enabled(PLLCTLV2_Type *ptr, uint8_t pll)
  63. {
  64. return IS_HPM_BITMASK_SET(ptr->PLL[pll].MFI, PLLCTLV2_PLL_MFI_ENABLE_MASK);
  65. }
  66. /**
  67. * @brief Select the PLL reference clock
  68. * @param [in] ptr PLLCTLV2 base address
  69. * @param [in] pll PLL index
  70. * @param [in] src PLL reference lcock source
  71. * @arg 0 - XTAL24M
  72. * @arg 1 - IRC24M
  73. */
  74. static inline void pllctlv2_select_reference_clock(PLLCTLV2_Type *ptr, uint8_t pll, uint8_t src)
  75. {
  76. ptr->PLL[pll].CONFIG = (ptr->PLL[pll].CONFIG & PLLCTLV2_PLL_CONFIG_REFSEL_MASK) | PLLCTLV2_PLL_CONFIG_REFSEL_SET(src);
  77. }
  78. /**
  79. * @brief Enable PLL Spread Spectrum feature
  80. * @param [in] ptr PLLCTLV2 base address
  81. * @param [in] pll PLL index
  82. * @param [in] step Step of spread spectrum modulator
  83. * @param [in] stop Stop point of spread spectrum modulator
  84. */
  85. void pllctlv2_enable_spread_spectrum(PLLCTLV2_Type *ptr, uint8_t pll, uint32_t step, uint32_t stop);
  86. /**
  87. * @brief Disable PLL Spread spectrum
  88. * @param [in] ptr PLLCTLV2 base address
  89. * @param [in] pll PLL index
  90. */
  91. static inline void pllctlv2_disable_spread_spectrum(PLLCTLV2_Type *ptr, uint8_t pll)
  92. {
  93. ptr->PLL[pll].CONFIG &= ~PLLCTLV2_PLL_CONFIG_SPREAD_MASK;
  94. }
  95. /**
  96. * @brief Set PLL lock time
  97. * @param [in] ptr PLLCTLV2 base address
  98. * @param [in] pll PLL index
  99. * @param [in] xtal_cycles external Crystal cycles
  100. */
  101. static inline void pllctlv2_set_pll_lock_time(PLLCTLV2_Type *ptr, uint8_t pll, uint32_t xtal_cycles)
  102. {
  103. ptr->PLL[pll].LOCKTIME = PLLCTLV2_PLL_LOCKTIME_LOCKTIME_SET(xtal_cycles);
  104. }
  105. /**
  106. * @brief Set PLL step time
  107. * @param [in] ptr PLLCTLV2 base address
  108. * @param [in] pll PLL index
  109. * @param [in] xtal_cycles external Crystal cycles
  110. */
  111. static inline void pllctlv2_set_pll_step_time(PLLCTLV2_Type *ptr, uint8_t pll, uint32_t xtal_cycles)
  112. {
  113. ptr->PLL[pll].STEPTIME = PLLCTLV2_PLL_STEPTIME_STEPTIME_SET(xtal_cycles);
  114. }
  115. /**
  116. * @brief Set PLL Post divider
  117. * @param [in] ptr PLLCTLV2 base
  118. * @param [in] pll PLL index
  119. * @param [in] div_index Divider index
  120. * @param [in] div_value divider value, divider factor is 1 + div_value / 5
  121. */
  122. void pllctlv2_set_postdiv(PLLCTLV2_Type *ptr, uint8_t pll, uint8_t div_index, uint8_t div_value);
  123. /**
  124. * @brief Set the PLL via the low-level MFI, MFD and MFN
  125. * PLL frequency = REF CLOCK * (mfi + 1.0 * mfn / mfd)
  126. * @param [in] ptr PLLCTLV2 base
  127. * @param [in] pll PLL index
  128. * @param [in] mfi MFI value
  129. * @param [in] mfn MFN value
  130. * @retval status_invalid_argument some parameters are invalid
  131. * @retval status_success operation is successful
  132. */
  133. hpm_stat_t pllctlv2_set_pll_with_mfi_mfn(PLLCTLV2_Type *ptr, uint8_t pll, uint32_t mfi, uint32_t mfn);
  134. /**
  135. * @brief Initialize PLL to specified frequency
  136. * Note: the specified PLL clock needs to be enabled before being configured
  137. * @param [in] ptr PLLCTLV2 base
  138. * @param [in] pll PLL index
  139. * @param [in] freq_in_hz expected PLL frequency
  140. * @retval status_invalid_argument some parameters are invalid
  141. * @retval status_success operation is successful
  142. */
  143. hpm_stat_t pllctlv2_init_pll_with_freq(PLLCTLV2_Type *ptr, uint8_t pll, uint32_t freq_in_hz);
  144. /**
  145. * @brief Get the specified PLl clock frequency
  146. * @param [in] ptr PLLCTLV2 base
  147. * @param [in] pll PLL index
  148. * @return PLL frequency in Hz
  149. */
  150. uint32_t pllctlv2_get_pll_freq_in_hz(PLLCTLV2_Type *ptr, uint8_t pll);
  151. /**
  152. * @brief Get the selected PLL post divider frequency
  153. * @param [in] ptr PLLCTLV2 base
  154. * @param [in] pll PLL index
  155. * @param [in] div_index Post divider index
  156. * @return PLL frequency in Hz
  157. */
  158. uint32_t pllctlv2_get_pll_postdiv_freq_in_hz(PLLCTLV2_Type *ptr, uint8_t pll, uint8_t div_index);
  159. /**
  160. * @}
  161. */
  162. #endif /* HPM_PLLCTLV2_DRV_H */