nrf_clock_control.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2016 Nordic Semiconductor ASA
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ZEPHYR_INCLUDE_DRIVERS_CLOCK_CONTROL_NRF_CLOCK_CONTROL_H_
  7. #define ZEPHYR_INCLUDE_DRIVERS_CLOCK_CONTROL_NRF_CLOCK_CONTROL_H_
  8. #include <device.h>
  9. #include <hal/nrf_clock.h>
  10. #include <sys/onoff.h>
  11. #include <drivers/clock_control.h>
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /** @brief Clocks handled by the CLOCK peripheral.
  16. *
  17. * Enum shall be used as a sys argument in clock_control API.
  18. */
  19. enum clock_control_nrf_type {
  20. CLOCK_CONTROL_NRF_TYPE_HFCLK,
  21. CLOCK_CONTROL_NRF_TYPE_LFCLK,
  22. #if NRF_CLOCK_HAS_HFCLK192M
  23. CLOCK_CONTROL_NRF_TYPE_HFCLK192M,
  24. #endif
  25. #if NRF_CLOCK_HAS_HFCLKAUDIO
  26. CLOCK_CONTROL_NRF_TYPE_HFCLKAUDIO,
  27. #endif
  28. CLOCK_CONTROL_NRF_TYPE_COUNT
  29. };
  30. /* Define can be used with clock control API instead of enum directly to
  31. * increase code readability.
  32. */
  33. #define CLOCK_CONTROL_NRF_SUBSYS_HF \
  34. ((clock_control_subsys_t)CLOCK_CONTROL_NRF_TYPE_HFCLK)
  35. #define CLOCK_CONTROL_NRF_SUBSYS_LF \
  36. ((clock_control_subsys_t)CLOCK_CONTROL_NRF_TYPE_LFCLK)
  37. #define CLOCK_CONTROL_NRF_SUBSYS_HF192M \
  38. ((clock_control_subsys_t)CLOCK_CONTROL_NRF_TYPE_HFCLK192M)
  39. #define CLOCK_CONTROL_NRF_SUBSYS_HFAUDIO \
  40. ((clock_control_subsys_t)CLOCK_CONTROL_NRF_TYPE_HFCLKAUDIO)
  41. /** @brief LF clock start modes. */
  42. enum nrf_lfclk_start_mode {
  43. CLOCK_CONTROL_NRF_LF_START_NOWAIT,
  44. CLOCK_CONTROL_NRF_LF_START_AVAILABLE,
  45. CLOCK_CONTROL_NRF_LF_START_STABLE,
  46. };
  47. /* Define 32KHz clock source */
  48. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC
  49. #define CLOCK_CONTROL_NRF_K32SRC NRF_CLOCK_LFCLK_RC
  50. #endif
  51. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_XTAL
  52. #define CLOCK_CONTROL_NRF_K32SRC NRF_CLOCK_LFCLK_Xtal
  53. #endif
  54. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_SYNTH
  55. #define CLOCK_CONTROL_NRF_K32SRC NRF_CLOCK_LFCLK_Synth
  56. #endif
  57. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_EXT_LOW_SWING
  58. #define CLOCK_CONTROL_NRF_K32SRC NRF_CLOCK_LFCLK_Xtal_Low_Swing
  59. #endif
  60. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_EXT_FULL_SWING
  61. #define CLOCK_CONTROL_NRF_K32SRC NRF_CLOCK_LFCLK_Xtal_Full_Swing
  62. #endif
  63. /* Define 32KHz clock accuracy */
  64. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_500PPM
  65. #define CLOCK_CONTROL_NRF_K32SRC_ACCURACY 0
  66. #endif
  67. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_250PPM
  68. #define CLOCK_CONTROL_NRF_K32SRC_ACCURACY 1
  69. #endif
  70. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_150PPM
  71. #define CLOCK_CONTROL_NRF_K32SRC_ACCURACY 2
  72. #endif
  73. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_100PPM
  74. #define CLOCK_CONTROL_NRF_K32SRC_ACCURACY 3
  75. #endif
  76. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_75PPM
  77. #define CLOCK_CONTROL_NRF_K32SRC_ACCURACY 4
  78. #endif
  79. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_50PPM
  80. #define CLOCK_CONTROL_NRF_K32SRC_ACCURACY 5
  81. #endif
  82. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_30PPM
  83. #define CLOCK_CONTROL_NRF_K32SRC_ACCURACY 6
  84. #endif
  85. #ifdef CONFIG_CLOCK_CONTROL_NRF_K32SRC_20PPM
  86. #define CLOCK_CONTROL_NRF_K32SRC_ACCURACY 7
  87. #endif
  88. /** @brief Force LF clock calibration. */
  89. void z_nrf_clock_calibration_force_start(void);
  90. /** @brief Return number of calibrations performed.
  91. *
  92. * Valid when @ref CONFIG_CLOCK_CONTROL_NRF_CALIBRATION_DEBUG is set.
  93. *
  94. * @return Number of calibrations or -1 if feature is disabled.
  95. */
  96. int z_nrf_clock_calibration_count(void);
  97. /** @brief Return number of attempts when calibration was skipped.
  98. *
  99. * Valid when @ref CONFIG_CLOCK_CONTROL_NRF_CALIBRATION_DEBUG is set.
  100. *
  101. * @return Number of calibrations or -1 if feature is disabled.
  102. */
  103. int z_nrf_clock_calibration_skips_count(void);
  104. /** @brief Get onoff service for given clock subsystem.
  105. *
  106. * @param sys Subsystem.
  107. *
  108. * @return Service handler or NULL.
  109. */
  110. struct onoff_manager *z_nrf_clock_control_get_onoff(clock_control_subsys_t sys);
  111. /** @brief Permanently enable low frequency clock.
  112. *
  113. * Low frequency clock is usually enabled during application lifetime because
  114. * of long startup time and low power consumption. Multiple modules can request
  115. * it but never release.
  116. *
  117. * @param start_mode Specify if function should block until clock is available.
  118. */
  119. void z_nrf_clock_control_lf_on(enum nrf_lfclk_start_mode start_mode);
  120. /** @brief Request high frequency clock from Bluetooth Controller.
  121. *
  122. * Function is optimized for Bluetooth Controller which turns HF clock before
  123. * each radio activity and has hard timing requirements but does not require
  124. * any confirmation when clock is ready because it assumes that request is
  125. * performed long enough before radio activity. Clock is released immediately
  126. * after radio activity.
  127. *
  128. * Function does not perform any validation. It is the caller responsibility to
  129. * ensure that every z_nrf_clock_bt_ctlr_hf_request matches
  130. * z_nrf_clock_bt_ctlr_hf_release call.
  131. */
  132. void z_nrf_clock_bt_ctlr_hf_request(void);
  133. /** @brief Release high frequency clock from Bluetooth Controller.
  134. *
  135. * See z_nrf_clock_bt_ctlr_hf_request for details.
  136. */
  137. void z_nrf_clock_bt_ctlr_hf_release(void);
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. #endif /* ZEPHYR_INCLUDE_DRIVERS_CLOCK_CONTROL_NRF_CLOCK_CONTROL_H_ */