nrf_clock.h 15 KB

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