nrf_drv_clock.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_DRV_CLOCK_H__
  41. #define NRF_DRV_CLOCK_H__
  42. #include <nrfx_clock.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_drv_clock Clock driver - legacy layer
  48. * @{
  49. * @ingroup nrf_clock
  50. *
  51. * @brief Layer providing compatibility with the former API.
  52. */
  53. /**
  54. * @brief Clock events.
  55. */
  56. typedef enum
  57. {
  58. NRF_DRV_CLOCK_EVT_HFCLK_STARTED, ///< HFCLK has been started.
  59. NRF_DRV_CLOCK_EVT_LFCLK_STARTED, ///< LFCLK has been started.
  60. NRF_DRV_CLOCK_EVT_CAL_DONE, ///< Calibration is done.
  61. NRF_DRV_CLOCK_EVT_CAL_ABORTED, ///< Calibration has been aborted.
  62. } nrf_drv_clock_evt_type_t;
  63. /**
  64. * @brief Clock event handler.
  65. *
  66. * @param[in] event Event.
  67. */
  68. typedef void (*nrf_drv_clock_event_handler_t)(nrf_drv_clock_evt_type_t event);
  69. // Forward declaration of the nrf_drv_clock_handler_item_t type.
  70. typedef struct nrf_drv_clock_handler_item_s nrf_drv_clock_handler_item_t;
  71. struct nrf_drv_clock_handler_item_s
  72. {
  73. nrf_drv_clock_handler_item_t * p_next; ///< A pointer to the next handler that should be called when the clock is started.
  74. nrf_drv_clock_event_handler_t event_handler; ///< Function to be called when the clock is started.
  75. };
  76. /**
  77. * @brief Function for checking if driver is already initialized
  78. *
  79. * @retval true Driver is initialized
  80. * @retval false Driver is uninitialized
  81. */
  82. bool nrf_drv_clock_init_check(void);
  83. /**
  84. * @brief Function for initializing the nrf_drv_clock module.
  85. *
  86. * After initialization, the module is in power off state (clocks are not requested).
  87. *
  88. * @retval NRF_SUCCESS If the procedure was successful.
  89. * @retval NRF_ERROR_MODULE_ALREADY_INITIALIZED If the driver was already initialized.
  90. */
  91. ret_code_t nrf_drv_clock_init(void);
  92. /**
  93. * @brief Function for uninitializing the clock module.
  94. *
  95. */
  96. void nrf_drv_clock_uninit(void);
  97. /**
  98. * @brief Function for requesting the LFCLK.
  99. *
  100. * The low-frequency clock can be requested by different modules
  101. * or contexts. The driver ensures that the clock will be started only when it is requested
  102. * the first time. If the clock is not ready but it was already started, the handler item that is
  103. * provided as an input parameter is added to the list of handlers that will be notified
  104. * when the clock is started. If the clock is already enabled, user callback is called from the
  105. * current context.
  106. *
  107. * The first request will start the selected LFCLK source. If an event handler is
  108. * provided, it will be called once the LFCLK is started. If the LFCLK was already started at this
  109. * time, the event handler will be called from the context of this function. Additionally,
  110. * the @ref nrf_drv_clock_lfclk_is_running function can be polled to check if the clock has started.
  111. *
  112. * @note When a SoftDevice is enabled, the LFCLK is always running and the driver cannot control it.
  113. *
  114. * @note The handler item provided by the user cannot be an automatic variable.
  115. *
  116. * @param[in] p_handler_item A pointer to the event handler structure.
  117. */
  118. void nrf_drv_clock_lfclk_request(nrf_drv_clock_handler_item_t * p_handler_item);
  119. /**
  120. * @brief Function for releasing the LFCLK.
  121. *
  122. * If there are no more requests, the LFCLK source will be stopped.
  123. *
  124. * @note When a SoftDevice is enabled, the LFCLK is always running.
  125. */
  126. void nrf_drv_clock_lfclk_release(void);
  127. /**
  128. * @brief Function for checking the LFCLK state.
  129. *
  130. * @retval true If the LFCLK is running.
  131. * @retval false If the LFCLK is not running.
  132. */
  133. bool nrf_drv_clock_lfclk_is_running(void);
  134. /**
  135. * @brief Function for requesting the high-accuracy source HFCLK.
  136. *
  137. * The high-accuracy source
  138. * can be requested by different modules or contexts. The driver ensures that the high-accuracy
  139. * clock will be started only when it is requested the first time. If the clock is not ready
  140. * but it was already started, the handler item that is provided as an input parameter is added
  141. * to the list of handlers that will be notified when the clock is started.
  142. *
  143. * If an event handler is provided, it will be called once the clock is started. If the clock was already
  144. * started at this time, the event handler will be called from the context of this function. Additionally,
  145. * the @ref nrf_drv_clock_hfclk_is_running function can be polled to check if the clock has started.
  146. *
  147. * @note If a SoftDevice is running, the clock is managed by the SoftDevice and all requests are handled by
  148. * the SoftDevice. This function cannot be called from all interrupt priority levels in that case.
  149. * @note The handler item provided by the user cannot be an automatic variable.
  150. *
  151. * @param[in] p_handler_item A pointer to the event handler structure.
  152. */
  153. void nrf_drv_clock_hfclk_request(nrf_drv_clock_handler_item_t * p_handler_item);
  154. /**
  155. * @brief Function for releasing the high-accuracy source HFCLK.
  156. *
  157. * If there are no more requests, the high-accuracy source will be released.
  158. */
  159. void nrf_drv_clock_hfclk_release(void);
  160. /**
  161. * @brief Function for checking the HFCLK state.
  162. *
  163. * @retval true If the HFCLK is running (for \nRFXX XTAL source).
  164. * @retval false If the HFCLK is not running.
  165. */
  166. bool nrf_drv_clock_hfclk_is_running(void);
  167. /**
  168. * @brief Function for starting a single calibration process.
  169. *
  170. * This function can also delay the start of calibration by a user-specified value. The delay will use
  171. * a low-power timer that is part of the CLOCK module. @ref nrf_drv_clock_is_calibrating can be called to
  172. * check if calibration is still in progress. If a handler is provided, the user can be notified when
  173. * calibration is completed. The ext calibration can be started from the handler context.
  174. *
  175. * The calibration process consists of three phases:
  176. * - Delay (optional)
  177. * - Requesting the high-accuracy HFCLK
  178. * - Hardware-supported calibration
  179. *
  180. * @param[in] delay Time after which the calibration will be started (in 0.25 s units).
  181. * @param[in] handler NULL or user function to be called when calibration is completed or aborted.
  182. *
  183. * @retval NRF_SUCCESS If the procedure was successful.
  184. * @retval NRF_ERROR_FORBIDDEN If a SoftDevice is present or the selected LFCLK source is not an RC oscillator.
  185. * @retval NRF_ERROR_INVALID_STATE If the low-frequency clock is off.
  186. * @retval NRF_ERROR_BUSY If calibration is in progress.
  187. */
  188. ret_code_t nrf_drv_clock_calibration_start(uint8_t delay, nrf_drv_clock_event_handler_t handler);
  189. /**
  190. * @brief Function for aborting calibration.
  191. *
  192. * This function aborts on-going calibration. If calibration was started, it cannot be stopped. If a handler
  193. * was provided by @ref nrf_drv_clock_calibration_start, this handler will be called once
  194. * aborted calibration is completed. @ref nrf_drv_clock_is_calibrating can also be used to check
  195. * if the system is calibrating.
  196. *
  197. * @retval NRF_SUCCESS If the procedure was successful.
  198. * @retval NRF_ERROR_FORBIDDEN If a SoftDevice is present or the selected LFCLK source is not an RC oscillator.
  199. */
  200. ret_code_t nrf_drv_clock_calibration_abort(void);
  201. /**
  202. * @brief Function for checking if calibration is in progress.
  203. *
  204. * This function indicates that the system is
  205. * in calibration if it is in any of the calibration process phases (see @ref nrf_drv_clock_calibration_start).
  206. *
  207. * @param[out] p_is_calibrating True if calibration is in progress, false if not.
  208. *
  209. * @retval NRF_SUCCESS If the procedure was successful.
  210. * @retval NRF_ERROR_FORBIDDEN If a SoftDevice is present or the selected LFCLK source is not an RC oscillator.
  211. */
  212. ret_code_t nrf_drv_clock_is_calibrating(bool * p_is_calibrating);
  213. /**@brief Function for returning a requested task address for the clock driver module.
  214. *
  215. * @param[in] task One of the peripheral tasks.
  216. *
  217. * @return Task address.
  218. */
  219. __STATIC_INLINE uint32_t nrf_drv_clock_ppi_task_addr(nrf_clock_task_t task);
  220. /**@brief Function for returning a requested event address for the clock driver module.
  221. *
  222. * @param[in] event One of the peripheral events.
  223. *
  224. * @return Event address.
  225. */
  226. __STATIC_INLINE uint32_t nrf_drv_clock_ppi_event_addr(nrf_clock_event_t event);
  227. #ifdef SOFTDEVICE_PRESENT
  228. /**
  229. * @brief Function called by the SoftDevice handler if an @ref NRF_SOC_EVTS event is received from the SoftDevice.
  230. *
  231. * @param[in] evt_id One of NRF_SOC_EVTS values.
  232. */
  233. void nrf_drv_clock_on_soc_event(uint32_t evt_id);
  234. /**
  235. * @brief Function called by the SoftDevice handler when the SoftDevice has been enabled.
  236. *
  237. * This function is called just after the SoftDevice has been properly enabled.
  238. * Its main purpose is to mark that LFCLK has been requested by SD.
  239. */
  240. void nrf_drv_clock_on_sd_enable(void);
  241. /**
  242. * @brief Function called by the SoftDevice handler when the SoftDevice has been disabled.
  243. *
  244. * This function is called just after the SoftDevice has been properly disabled.
  245. * It has two purposes:
  246. * 1. Releases the LFCLK from the SD.
  247. * 2. Reinitializes an interrupt after the SD releases POWER_CLOCK_IRQ.
  248. */
  249. void nrf_drv_clock_on_sd_disable(void);
  250. #endif
  251. /**
  252. *@}
  253. **/
  254. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  255. __STATIC_INLINE uint32_t nrf_drv_clock_ppi_task_addr(nrf_clock_task_t task)
  256. {
  257. return nrf_clock_task_address_get(task);
  258. }
  259. __STATIC_INLINE uint32_t nrf_drv_clock_ppi_event_addr(nrf_clock_event_t event)
  260. {
  261. return nrf_clock_event_address_get(event);
  262. }
  263. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  264. #ifdef __cplusplus
  265. }
  266. #endif
  267. #endif // NRF_DRV_CLOCK_H__