nrf_clock.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**
  2. * Copyright (c) 2015 - 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_CLOCK_H__
  41. #define NRF_CLOCK_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_clock_hal Clock HAL
  48. * @{
  49. * @ingroup nrf_clock
  50. * @brief Hardware access layer for managing the CLOCK peripheral.
  51. *
  52. * This code can be used to managing low-frequency clock (LFCLK) and the high-frequency clock
  53. * (HFCLK) settings.
  54. */
  55. #define NRF_CLOCK_TASK_TRIGGER (1UL)
  56. #define NRF_CLOCK_EVENT_CLEAR (0UL)
  57. #if defined(NRF52810_XXAA) || \
  58. defined(NRF52832_XXAA) || defined(NRF52832_XXAB) || \
  59. defined(NRF52840_XXAA)
  60. // Enable support for external LFCLK sources. Read more in the Product Specification.
  61. #define NRF_CLOCK_USE_EXTERNAL_LFCLK_SOURCES
  62. #endif
  63. /**
  64. * @brief Low-frequency clock sources.
  65. * @details Used by LFCLKSRC, LFCLKSTAT, and LFCLKSRCCOPY registers.
  66. */
  67. typedef enum
  68. {
  69. NRF_CLOCK_LFCLK_RC = CLOCK_LFCLKSRC_SRC_RC, /**< Internal 32 kHz RC oscillator. */
  70. NRF_CLOCK_LFCLK_Xtal = CLOCK_LFCLKSRC_SRC_Xtal, /**< External 32 kHz crystal. */
  71. NRF_CLOCK_LFCLK_Synth = CLOCK_LFCLKSRC_SRC_Synth, /**< Internal 32 kHz synthesizer from HFCLK system clock. */
  72. #if defined(NRF_CLOCK_USE_EXTERNAL_LFCLK_SOURCES) || defined(__NRFX_DOXYGEN__)
  73. /**
  74. * External 32 kHz low swing signal. Used only with the LFCLKSRC register.
  75. * For the others @ref NRF_CLOCK_LFCLK_Xtal is returned for this setting.
  76. */
  77. NRF_CLOCK_LFCLK_Xtal_Low_Swing = (CLOCK_LFCLKSRC_SRC_Xtal |
  78. (CLOCK_LFCLKSRC_EXTERNAL_Enabled << CLOCK_LFCLKSRC_EXTERNAL_Pos)),
  79. /**
  80. * External 32 kHz full swing signal. Used only with the LFCLKSRC register.
  81. * For the others @ref NRF_CLOCK_LFCLK_Xtal is returned for this setting.
  82. */
  83. NRF_CLOCK_LFCLK_Xtal_Full_Swing = (CLOCK_LFCLKSRC_SRC_Xtal |
  84. (CLOCK_LFCLKSRC_BYPASS_Enabled << CLOCK_LFCLKSRC_BYPASS_Pos) |
  85. (CLOCK_LFCLKSRC_EXTERNAL_Enabled << CLOCK_LFCLKSRC_EXTERNAL_Pos)),
  86. #endif // defined(NRF_CLOCK_USE_EXTERNAL_LFCLK_SOURCES) || defined(__NRFX_DOXYGEN__)
  87. } nrf_clock_lfclk_t;
  88. /**
  89. * @brief High-frequency clock sources.
  90. */
  91. typedef enum
  92. {
  93. NRF_CLOCK_HFCLK_LOW_ACCURACY = CLOCK_HFCLKSTAT_SRC_RC, /**< Internal 16 MHz RC oscillator. */
  94. NRF_CLOCK_HFCLK_HIGH_ACCURACY = CLOCK_HFCLKSTAT_SRC_Xtal /**< External 16 MHz/32 MHz crystal oscillator. */
  95. } nrf_clock_hfclk_t;
  96. /**
  97. * @brief Trigger status of task LFCLKSTART/HFCLKSTART.
  98. * @details Used by LFCLKRUN and HFCLKRUN registers.
  99. */
  100. typedef enum
  101. {
  102. NRF_CLOCK_START_TASK_NOT_TRIGGERED = CLOCK_LFCLKRUN_STATUS_NotTriggered, /**< Task LFCLKSTART/HFCLKSTART has not been triggered. */
  103. NRF_CLOCK_START_TASK_TRIGGERED = CLOCK_LFCLKRUN_STATUS_Triggered /**< Task LFCLKSTART/HFCLKSTART has been triggered. */
  104. } nrf_clock_start_task_status_t;
  105. /**
  106. * @brief Interrupts.
  107. */
  108. typedef enum
  109. {
  110. NRF_CLOCK_INT_HF_STARTED_MASK = CLOCK_INTENSET_HFCLKSTARTED_Msk, /**< Interrupt on HFCLKSTARTED event. */
  111. NRF_CLOCK_INT_LF_STARTED_MASK = CLOCK_INTENSET_LFCLKSTARTED_Msk, /**< Interrupt on LFCLKSTARTED event. */
  112. NRF_CLOCK_INT_DONE_MASK = CLOCK_INTENSET_DONE_Msk, /**< Interrupt on DONE event. */
  113. NRF_CLOCK_INT_CTTO_MASK = CLOCK_INTENSET_CTTO_Msk /**< Interrupt on CTTO event. */
  114. } nrf_clock_int_mask_t;
  115. /**
  116. * @brief Tasks.
  117. *
  118. * @details The NRF_CLOCK_TASK_LFCLKSTOP task cannot be set when the low-frequency clock is not running.
  119. * The NRF_CLOCK_TASK_HFCLKSTOP task cannot be set when the high-frequency clock is not running.
  120. */
  121. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  122. {
  123. NRF_CLOCK_TASK_HFCLKSTART = offsetof(NRF_CLOCK_Type, TASKS_HFCLKSTART), /**< Start HFCLK clock source.*/
  124. NRF_CLOCK_TASK_HFCLKSTOP = offsetof(NRF_CLOCK_Type, TASKS_HFCLKSTOP), /**< Stop HFCLK clock source.*/
  125. NRF_CLOCK_TASK_LFCLKSTART = offsetof(NRF_CLOCK_Type, TASKS_LFCLKSTART), /**< Start LFCLK clock source.*/
  126. NRF_CLOCK_TASK_LFCLKSTOP = offsetof(NRF_CLOCK_Type, TASKS_LFCLKSTOP), /**< Stop LFCLK clock source.*/
  127. NRF_CLOCK_TASK_CAL = offsetof(NRF_CLOCK_Type, TASKS_CAL), /**< Start calibration of LFCLK RC oscillator.*/
  128. NRF_CLOCK_TASK_CTSTART = offsetof(NRF_CLOCK_Type, TASKS_CTSTART), /**< Start calibration timer.*/
  129. NRF_CLOCK_TASK_CTSTOP = offsetof(NRF_CLOCK_Type, TASKS_CTSTOP) /**< Stop calibration timer.*/
  130. } nrf_clock_task_t; /*lint -restore */
  131. /**
  132. * @brief Events.
  133. */
  134. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  135. {
  136. NRF_CLOCK_EVENT_HFCLKSTARTED = offsetof(NRF_CLOCK_Type, EVENTS_HFCLKSTARTED), /**< HFCLK oscillator started.*/
  137. NRF_CLOCK_EVENT_LFCLKSTARTED = offsetof(NRF_CLOCK_Type, EVENTS_LFCLKSTARTED), /**< LFCLK oscillator started.*/
  138. NRF_CLOCK_EVENT_DONE = offsetof(NRF_CLOCK_Type, EVENTS_DONE), /**< Calibration of LFCLK RC oscillator completed.*/
  139. NRF_CLOCK_EVENT_CTTO = offsetof(NRF_CLOCK_Type, EVENTS_CTTO) /**< Calibration timer time-out.*/
  140. } nrf_clock_event_t; /*lint -restore */
  141. /**
  142. * @brief Function for enabling a specific interrupt.
  143. *
  144. * @param[in] int_mask Interrupt.
  145. */
  146. __STATIC_INLINE void nrf_clock_int_enable(uint32_t int_mask);
  147. /**
  148. * @brief Function for disabling a specific interrupt.
  149. *
  150. * @param[in] int_mask Interrupt.
  151. */
  152. __STATIC_INLINE void nrf_clock_int_disable(uint32_t int_mask);
  153. /**
  154. * @brief Function for retrieving the state of a specific interrupt.
  155. *
  156. * @param[in] int_mask Interrupt.
  157. *
  158. * @retval true If the interrupt is enabled.
  159. * @retval false If the interrupt is not enabled.
  160. */
  161. __STATIC_INLINE bool nrf_clock_int_enable_check(nrf_clock_int_mask_t int_mask);
  162. /**
  163. * @brief Function for retrieving the address of a specific task.
  164. * @details This function can be used by the PPI module.
  165. *
  166. * @param[in] task Task.
  167. *
  168. * @return Address of the requested task register.
  169. */
  170. __STATIC_INLINE uint32_t nrf_clock_task_address_get(nrf_clock_task_t task);
  171. /**
  172. * @brief Function for setting a specific task.
  173. *
  174. * @param[in] task Task.
  175. */
  176. __STATIC_INLINE void nrf_clock_task_trigger(nrf_clock_task_t task);
  177. /**
  178. * @brief Function for retrieving the address of a specific event.
  179. * @details This function can be used by the PPI module.
  180. *
  181. * @param[in] event Event.
  182. *
  183. * @return Address of the requested event register.
  184. */
  185. __STATIC_INLINE uint32_t nrf_clock_event_address_get(nrf_clock_event_t event);
  186. /**
  187. * @brief Function for clearing a specific event.
  188. *
  189. * @param[in] event Event.
  190. */
  191. __STATIC_INLINE void nrf_clock_event_clear(nrf_clock_event_t event);
  192. /**
  193. * @brief Function for retrieving the state of a specific event.
  194. *
  195. * @param[in] event Event.
  196. *
  197. * @retval true If the event is set.
  198. * @retval false If the event is not set.
  199. */
  200. __STATIC_INLINE bool nrf_clock_event_check(nrf_clock_event_t event);
  201. /**
  202. * @brief Function for changing the low-frequency clock source.
  203. * @details This function cannot be called when the low-frequency clock is running.
  204. *
  205. * @param[in] source New low-frequency clock source.
  206. *
  207. */
  208. __STATIC_INLINE void nrf_clock_lf_src_set(nrf_clock_lfclk_t source);
  209. /**
  210. * @brief Function for retrieving the selected source for the low-frequency clock.
  211. *
  212. * @retval NRF_CLOCK_LFCLK_RC If the internal 32 kHz RC oscillator is the selected source for the low-frequency clock.
  213. * @retval NRF_CLOCK_LFCLK_Xtal If an external 32 kHz crystal oscillator is the selected source for the low-frequency clock.
  214. * @retval NRF_CLOCK_LFCLK_Synth If the internal 32 kHz synthesizer from the HFCLK is the selected source for the low-frequency clock.
  215. */
  216. __STATIC_INLINE nrf_clock_lfclk_t nrf_clock_lf_src_get(void);
  217. /**
  218. * @brief Function for retrieving the active source of the low-frequency clock.
  219. *
  220. * @retval NRF_CLOCK_LFCLK_RC If the internal 32 kHz RC oscillator is the active source of the low-frequency clock.
  221. * @retval NRF_CLOCK_LFCLK_Xtal If an external 32 kHz crystal oscillator is the active source of the low-frequency clock.
  222. * @retval NRF_CLOCK_LFCLK_Synth If the internal 32 kHz synthesizer from the HFCLK is the active source of the low-frequency clock.
  223. */
  224. __STATIC_INLINE nrf_clock_lfclk_t nrf_clock_lf_actv_src_get(void);
  225. /**
  226. * @brief Function for retrieving the clock source for the LFCLK clock when the task LKCLKSTART is triggered.
  227. *
  228. * @retval NRF_CLOCK_LFCLK_RC If the internal 32 kHz RC oscillator is running and generating the LFCLK clock.
  229. * @retval NRF_CLOCK_LFCLK_Xtal If an external 32 kHz crystal oscillator is running and generating the LFCLK clock.
  230. * @retval NRF_CLOCK_LFCLK_Synth If the internal 32 kHz synthesizer from the HFCLK is running and generating the LFCLK clock.
  231. */
  232. __STATIC_INLINE nrf_clock_lfclk_t nrf_clock_lf_srccopy_get(void);
  233. /**
  234. * @brief Function for retrieving the state of the LFCLK clock.
  235. *
  236. * @retval false If the LFCLK clock is not running.
  237. * @retval true If the LFCLK clock is running.
  238. */
  239. __STATIC_INLINE bool nrf_clock_lf_is_running(void);
  240. /**
  241. * @brief Function for retrieving the trigger status of the task LFCLKSTART.
  242. *
  243. * @retval NRF_CLOCK_START_TASK_NOT_TRIGGERED If the task LFCLKSTART has not been triggered.
  244. * @retval NRF_CLOCK_START_TASK_TRIGGERED If the task LFCLKSTART has been triggered.
  245. */
  246. __STATIC_INLINE nrf_clock_start_task_status_t nrf_clock_lf_start_task_status_get(void);
  247. /**
  248. * @brief Function for retrieving the active source of the high-frequency clock.
  249. *
  250. * @retval NRF_CLOCK_HFCLK_LOW_ACCURACY If the internal 16 MHz RC oscillator is the active source of the high-frequency clock.
  251. * @retval NRF_CLOCK_HFCLK_HIGH_ACCURACY If an external 16 MHz/32 MHz crystal oscillator is the active source of the high-frequency clock.
  252. */
  253. __STATIC_INLINE nrf_clock_hfclk_t nrf_clock_hf_src_get(void);
  254. /**
  255. * @brief Function for retrieving the state of the HFCLK clock.
  256. *
  257. * @param[in] clk_src Clock source to be checked.
  258. *
  259. * @retval false If the HFCLK clock is not running.
  260. * @retval true If the HFCLK clock is running.
  261. */
  262. __STATIC_INLINE bool nrf_clock_hf_is_running(nrf_clock_hfclk_t clk_src);
  263. /**
  264. * @brief Function for retrieving the trigger status of the task HFCLKSTART.
  265. *
  266. * @retval NRF_CLOCK_START_TASK_NOT_TRIGGERED If the task HFCLKSTART has not been triggered.
  267. * @retval NRF_CLOCK_START_TASK_TRIGGERED If the task HFCLKSTART has been triggered.
  268. */
  269. __STATIC_INLINE nrf_clock_start_task_status_t nrf_clock_hf_start_task_status_get(void);
  270. /**
  271. * @brief Function for changing the calibration timer interval.
  272. *
  273. * @param[in] interval New calibration timer interval in 0.25 s resolution (range: 0.25 seconds to 31.75 seconds).
  274. */
  275. __STATIC_INLINE void nrf_clock_cal_timer_timeout_set(uint32_t interval);
  276. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  277. __STATIC_INLINE void nrf_clock_int_enable(uint32_t int_mask)
  278. {
  279. NRF_CLOCK->INTENSET = int_mask;
  280. }
  281. __STATIC_INLINE void nrf_clock_int_disable(uint32_t int_mask)
  282. {
  283. NRF_CLOCK->INTENCLR = int_mask;
  284. }
  285. __STATIC_INLINE bool nrf_clock_int_enable_check(nrf_clock_int_mask_t int_mask)
  286. {
  287. return (bool)(NRF_CLOCK->INTENCLR & int_mask);
  288. }
  289. __STATIC_INLINE uint32_t nrf_clock_task_address_get(nrf_clock_task_t task)
  290. {
  291. return ((uint32_t )NRF_CLOCK + task);
  292. }
  293. __STATIC_INLINE void nrf_clock_task_trigger(nrf_clock_task_t task)
  294. {
  295. *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + task)) = NRF_CLOCK_TASK_TRIGGER;
  296. }
  297. __STATIC_INLINE uint32_t nrf_clock_event_address_get(nrf_clock_event_t event)
  298. {
  299. return ((uint32_t)NRF_CLOCK + event);
  300. }
  301. __STATIC_INLINE void nrf_clock_event_clear(nrf_clock_event_t event)
  302. {
  303. *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + event)) = NRF_CLOCK_EVENT_CLEAR;
  304. #if __CORTEX_M == 0x04
  305. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + event));
  306. (void)dummy;
  307. #endif
  308. }
  309. __STATIC_INLINE bool nrf_clock_event_check(nrf_clock_event_t event)
  310. {
  311. return (bool)*((volatile uint32_t *)((uint8_t *)NRF_CLOCK + event));
  312. }
  313. __STATIC_INLINE void nrf_clock_lf_src_set(nrf_clock_lfclk_t source)
  314. {
  315. NRF_CLOCK->LFCLKSRC = (uint32_t)(source);
  316. }
  317. __STATIC_INLINE nrf_clock_lfclk_t nrf_clock_lf_src_get(void)
  318. {
  319. return (nrf_clock_lfclk_t)(NRF_CLOCK->LFCLKSRC);
  320. }
  321. __STATIC_INLINE nrf_clock_lfclk_t nrf_clock_lf_actv_src_get(void)
  322. {
  323. return (nrf_clock_lfclk_t)((NRF_CLOCK->LFCLKSTAT &
  324. CLOCK_LFCLKSTAT_SRC_Msk) >> CLOCK_LFCLKSTAT_SRC_Pos);
  325. }
  326. __STATIC_INLINE nrf_clock_lfclk_t nrf_clock_lf_srccopy_get(void)
  327. {
  328. return (nrf_clock_lfclk_t)((NRF_CLOCK->LFCLKSRCCOPY &
  329. CLOCK_LFCLKSRCCOPY_SRC_Msk) >> CLOCK_LFCLKSRCCOPY_SRC_Pos);
  330. }
  331. __STATIC_INLINE bool nrf_clock_lf_is_running(void)
  332. {
  333. return ((NRF_CLOCK->LFCLKSTAT &
  334. CLOCK_LFCLKSTAT_STATE_Msk) >> CLOCK_LFCLKSTAT_STATE_Pos);
  335. }
  336. __STATIC_INLINE nrf_clock_start_task_status_t nrf_clock_lf_start_task_status_get(void)
  337. {
  338. return (nrf_clock_start_task_status_t)((NRF_CLOCK->LFCLKRUN &
  339. CLOCK_LFCLKRUN_STATUS_Msk) >>
  340. CLOCK_LFCLKRUN_STATUS_Pos);
  341. }
  342. __STATIC_INLINE nrf_clock_hfclk_t nrf_clock_hf_src_get(void)
  343. {
  344. return (nrf_clock_hfclk_t)((NRF_CLOCK->HFCLKSTAT &
  345. CLOCK_HFCLKSTAT_SRC_Msk) >> CLOCK_HFCLKSTAT_SRC_Pos);
  346. }
  347. __STATIC_INLINE bool nrf_clock_hf_is_running(nrf_clock_hfclk_t clk_src)
  348. {
  349. return (NRF_CLOCK->HFCLKSTAT & (CLOCK_HFCLKSTAT_STATE_Msk | CLOCK_HFCLKSTAT_SRC_Msk)) ==
  350. (CLOCK_HFCLKSTAT_STATE_Msk | (clk_src << CLOCK_HFCLKSTAT_SRC_Pos));
  351. }
  352. __STATIC_INLINE nrf_clock_start_task_status_t nrf_clock_hf_start_task_status_get(void)
  353. {
  354. return (nrf_clock_start_task_status_t)((NRF_CLOCK->HFCLKRUN &
  355. CLOCK_HFCLKRUN_STATUS_Msk) >>
  356. CLOCK_HFCLKRUN_STATUS_Pos);
  357. }
  358. __STATIC_INLINE void nrf_clock_cal_timer_timeout_set(uint32_t interval)
  359. {
  360. NRF_CLOCK->CTIV = ((interval << CLOCK_CTIV_CTIV_Pos) & CLOCK_CTIV_CTIV_Msk);
  361. }
  362. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  363. /** @} */
  364. #ifdef __cplusplus
  365. }
  366. #endif
  367. #endif // NRF_CLOCK_H__