nrf_drv_clock.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef NRF_DRV_CLOCK_H__
  13. #define NRF_DRV_CLOCK_H__
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include "sdk_errors.h"
  17. #include "nrf_assert.h"
  18. #include "nrf_clock.h"
  19. #include "nrf_drv_config.h"
  20. #include "nrf_drv_common.h"
  21. /**
  22. *
  23. * @addtogroup nrf_clock Clock HAL and driver
  24. * @ingroup nrf_drivers
  25. * @brief Clock APIs.
  26. * @details The clock HAL provides basic APIs for accessing the registers of the clock.
  27. * The clock driver provides APIs on a higher level.
  28. *
  29. * @defgroup nrf_clock_drv Clock driver
  30. * @{
  31. * @ingroup nrf_clock
  32. * @brief Driver for managing the low-frequency clock (LFCLK) and the high-frequency clock (HFCLK).
  33. */
  34. /**
  35. * @brief Clock events.
  36. */
  37. typedef enum
  38. {
  39. NRF_DRV_CLOCK_EVT_HFCLK_STARTED, ///< HFCLK has been started.
  40. NRF_DRV_CLOCK_EVT_LFCLK_STARTED, ///< LFCLK has been started.
  41. NRF_DRV_CLOCK_EVT_CAL_DONE, ///< Calibration is done.
  42. NRF_DRV_CLOCK_EVT_CAL_ABORTED, ///< Calibration has been aborted.
  43. } nrf_drv_clock_evt_type_t;
  44. /**
  45. * @brief Clock event handler.
  46. *
  47. * @param[in] event Event.
  48. */
  49. typedef void (*nrf_drv_clock_event_handler_t)(nrf_drv_clock_evt_type_t event);
  50. // Forward declaration of the nrf_drv_clock_handler_item_t type.
  51. typedef struct nrf_drv_clock_handler_item_s nrf_drv_clock_handler_item_t;
  52. struct nrf_drv_clock_handler_item_s
  53. {
  54. nrf_drv_clock_handler_item_t * p_next; ///< A pointer to the next handler that should be called when the clock is started.
  55. nrf_drv_clock_event_handler_t event_handler; ///< Function to be called when the clock is started.
  56. };
  57. /**
  58. * @brief Function for initializing the nrf_drv_clock module.
  59. *
  60. * After initialization, the module is in power off state (clocks are not requested).
  61. *
  62. * @retval NRF_SUCCESS If the procedure was successful.
  63. * @retval MODULE_ALREADY_INITIALIZED If the driver was already initialized.
  64. * @retval NRF_ERROR_SOFTDEVICE_NOT_ENABLED If the SoftDevice was not enabled.
  65. */
  66. ret_code_t nrf_drv_clock_init(void);
  67. /**
  68. * @brief Function for uninitializing the clock module.
  69. *
  70. */
  71. void nrf_drv_clock_uninit(void);
  72. /**
  73. * @brief Function for requesting the LFCLK.
  74. *
  75. * The low-frequency clock can be requested by different modules
  76. * or contexts. The driver ensures that the clock will be started only when it is requested
  77. * the first time. If the clock is not ready but it was already started, the handler item that is
  78. * provided as an input parameter is added to the list of handlers that will be notified
  79. * when the clock is started. If the clock is already enabled, user callback is called from the
  80. * current context.
  81. *
  82. * The first request will start the selected LFCLK source. If an event handler is
  83. * provided, it will be called once the LFCLK is started. If the LFCLK was already started at this
  84. * time, the event handler will be called from the context of this function. Additionally,
  85. * the @ref nrf_drv_clock_lfclk_is_running function can be polled to check if the clock has started.
  86. *
  87. * @note When a SoftDevice is enabled, the LFCLK is always running and the driver cannot control it.
  88. *
  89. * @note The handler item provided by the user cannot be an automatic variable.
  90. *
  91. * @param[in] p_handler_item A pointer to the event handler structure.
  92. */
  93. void nrf_drv_clock_lfclk_request(nrf_drv_clock_handler_item_t * p_handler_item);
  94. /**
  95. * @brief Function for releasing the LFCLK.
  96. *
  97. * If there are no more requests, the LFCLK source will be stopped.
  98. *
  99. * @note When a SoftDevice is enabled, the LFCLK is always running.
  100. */
  101. void nrf_drv_clock_lfclk_release(void);
  102. /**
  103. * @brief Function for checking the LFCLK state.
  104. *
  105. * @retval true If the LFCLK is running.
  106. * @retval false If the LFCLK is not running.
  107. */
  108. bool nrf_drv_clock_lfclk_is_running(void);
  109. /**
  110. * @brief Function for requesting the high-accuracy source HFCLK.
  111. *
  112. * The high-accuracy source
  113. * can be requested by different modules or contexts. The driver ensures that the high-accuracy
  114. * clock will be started only when it is requested the first time. If the clock is not ready
  115. * but it was already started, the handler item that is provided as an input parameter is added
  116. * to the list of handlers that will be notified when the clock is started.
  117. *
  118. * If an event handler is provided, it will be called once the clock is started. If the clock was already
  119. * started at this time, the event handler will be called from the context of this function. Additionally,
  120. * the @ref nrf_drv_clock_hfclk_is_running function can be polled to check if the clock has started.
  121. *
  122. * @note If a SoftDevice is running, the clock is managed by the SoftDevice and all requests are handled by
  123. * the SoftDevice. This function cannot be called from all interrupt priority levels in that case.
  124. * @note The handler item provided by the user cannot be an automatic variable.
  125. *
  126. * @param[in] p_handler_item A pointer to the event handler structure.
  127. */
  128. void nrf_drv_clock_hfclk_request(nrf_drv_clock_handler_item_t * p_handler_item);
  129. /**
  130. * @brief Function for releasing the high-accuracy source HFCLK.
  131. *
  132. * If there are no more requests, the high-accuracy source will be released.
  133. */
  134. void nrf_drv_clock_hfclk_release(void);
  135. /**
  136. * @brief Function for checking the HFCLK state.
  137. *
  138. * @retval true If the HFCLK is running (for \nRFXX XTAL source).
  139. * @retval false If the HFCLK is not running.
  140. */
  141. bool nrf_drv_clock_hfclk_is_running(void);
  142. /**
  143. * @brief Function for starting a single calibration process.
  144. *
  145. * This function can also delay the start of calibration by a user-specified value. The delay will use
  146. * a low-power timer that is part of the CLOCK module. @ref nrf_drv_clock_is_calibrating can be called to
  147. * check if calibration is still in progress. If a handler is provided, the user can be notified when
  148. * calibration is completed. The ext calibration can be started from the handler context.
  149. *
  150. * The calibration process consists of three phases:
  151. * - Delay (optional)
  152. * - Requesting the high-accuracy HFCLK
  153. * - Hardware-supported calibration
  154. *
  155. * @param[in] delay Time after which the calibration will be started (in 0.25 s units).
  156. * @param[in] handler NULL or user function to be called when calibration is completed or aborted.
  157. *
  158. * @retval NRF_SUCCESS If the procedure was successful.
  159. * @retval NRF_ERROR_FORBIDDEN If a SoftDevice is present or the selected LFCLK source is not an RC oscillator.
  160. * @retval NRF_ERROR_INVALID_STATE If the low-frequency clock is off.
  161. * @retval NRF_ERROR_BUSY If calibration is in progress.
  162. */
  163. ret_code_t nrf_drv_clock_calibration_start(uint8_t delay, nrf_drv_clock_event_handler_t handler);
  164. /**
  165. * @brief Function for aborting calibration.
  166. *
  167. * This function aborts on-going calibration. If calibration was started, it cannot be stopped. If a handler
  168. * was provided by @ref nrf_drv_clock_calibration_start, this handler will be called once
  169. * aborted calibration is completed. @ref nrf_drv_clock_is_calibrating can also be used to check
  170. * if the system is calibrating.
  171. *
  172. * @retval NRF_SUCCESS If the procedure was successful.
  173. * @retval NRF_ERROR_FORBIDDEN If a SoftDevice is present or the selected LFCLK source is not an RC oscillator.
  174. */
  175. ret_code_t nrf_drv_clock_calibration_abort(void);
  176. /**
  177. * @brief Function for checking if calibration is in progress.
  178. *
  179. * This function indicates that the system is
  180. * in calibration if it is in any of the calibration process phases (see @ref nrf_drv_clock_calibration_start).
  181. *
  182. * @param[out] p_is_calibrating True if calibration is in progress, false if not.
  183. *
  184. * @retval NRF_SUCCESS If the procedure was successful.
  185. * @retval NRF_ERROR_FORBIDDEN If a SoftDevice is present or the selected LFCLK source is not an RC oscillator.
  186. */
  187. ret_code_t nrf_drv_clock_is_calibrating(bool * p_is_calibrating);
  188. /**@brief Function for returning a requested task address for the clock driver module.
  189. *
  190. * @param[in] task One of the peripheral tasks.
  191. *
  192. * @return Task address.
  193. */
  194. __STATIC_INLINE uint32_t nrf_drv_clock_ppi_task_addr(nrf_clock_task_t task);
  195. /**@brief Function for returning a requested event address for the clock driver module.
  196. *
  197. * @param[in] event One of the peripheral events.
  198. *
  199. * @return Event address.
  200. */
  201. __STATIC_INLINE uint32_t nrf_drv_clock_ppi_event_addr(nrf_clock_event_t event);
  202. /**
  203. * @brief Function called by the SoftDevice handler if an @ref nrf_soc event is received from the SoftDevice.
  204. */
  205. #ifdef SOFTDEVICE_PRESENT
  206. void nrf_drv_clock_on_soc_event(uint32_t evt_id);
  207. #endif
  208. /**
  209. *@}
  210. **/
  211. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  212. __STATIC_INLINE uint32_t nrf_drv_clock_ppi_task_addr(nrf_clock_task_t task)
  213. {
  214. return nrf_clock_task_address_get(task);
  215. }
  216. __STATIC_INLINE uint32_t nrf_drv_clock_ppi_event_addr(nrf_clock_event_t event)
  217. {
  218. return nrf_clock_event_address_get(event);
  219. }
  220. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  221. /*lint --flb "Leave library region" */
  222. #endif // NRF_CLOCK_H__