1 /*
2 * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef NRFX_CLOCK_H__
33 #define NRFX_CLOCK_H__
34
35 #include <nrfx.h>
36 #include <hal/nrf_clock.h>
37 #include <nrfx_power_clock.h>
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /**
44 * @defgroup nrfx_clock CLOCK driver
45 * @{
46 * @ingroup nrf_clock
47 * @brief CLOCK peripheral driver.
48 */
49
50 /** @brief Clock events. */
51 typedef enum
52 {
53 NRFX_CLOCK_EVT_HFCLK_STARTED, ///< HFCLK has been started.
54 NRFX_CLOCK_EVT_LFCLK_STARTED, ///< LFCLK has been started.
55 NRFX_CLOCK_EVT_CTTO, ///< Calibration timeout.
56 NRFX_CLOCK_EVT_CAL_DONE, ///< Calibration has been done.
57 NRFX_CLOCK_EVT_HFCLKAUDIO_STARTED, ///< HFCLKAUDIO has been started.
58 NRFX_CLOCK_EVT_HFCLK192M_STARTED, ///< HFCLK192M has been started.
59 } nrfx_clock_evt_type_t;
60
61 /**
62 * @brief Clock event handler.
63 *
64 * @param[in] event Event.
65 */
66 typedef void (*nrfx_clock_event_handler_t)(nrfx_clock_evt_type_t event);
67
68 /**
69 * @brief Function for initializing internal structures in the nrfx_clock module.
70 *
71 * After initialization, the module is in power off state (clocks are not started).
72 *
73 * @param[in] event_handler Event handler provided by the user.
74 * Must not be NULL.
75 *
76 * @retval NRFX_SUCCESS The procedure is successful.
77 * @retval NRFX_ERROR_ALREADY_INITIALIZED The driver is already initialized.
78 */
79 nrfx_err_t nrfx_clock_init(nrfx_clock_event_handler_t event_handler);
80
81 /** @brief Function for enabling interrupts in the clock module. */
82 void nrfx_clock_enable(void);
83
84 /** @brief Function for disabling interrupts in the clock module. */
85 void nrfx_clock_disable(void);
86
87 /** @brief Function for uninitializing the clock module. */
88 void nrfx_clock_uninit(void);
89
90 /**
91 * @brief Function for starting the specified clock domain.
92 *
93 * @param[in] domain Clock domain.
94 */
95 void nrfx_clock_start(nrf_clock_domain_t domain);
96
97 /**
98 * @brief Function for stopping the specified clock domain.
99 *
100 * @param[in] domain Clock domain.
101 */
102 void nrfx_clock_stop(nrf_clock_domain_t domain);
103
104 /**
105 * @brief Function for checking the specified clock domain state.
106 *
107 * XTAL source is assumed for domains with multiple sources.
108 *
109 * @param[in] domain Clock domain.
110 * @param[out] p_clk_src Pointer to a clock source that is running. Set to NULL if not needed.
111 * Ignored for HFCLKAUDIO domain. Variable pointed by @p p_clk_src
112 * must be of either @ref nrf_clock_lfclk_t type for LFCLK
113 * or @ref nrf_clock_hfclk_t type for HFCLK and HFCLK192M.
114 *
115 * @retval true The clock domain is running.
116 * @retval false The clock domain is not running.
117 */
118 NRFX_STATIC_INLINE bool nrfx_clock_is_running(nrf_clock_domain_t domain, void * p_clk_src);
119
120 #if NRF_CLOCK_HAS_HFCLK_DIV || NRF_CLOCK_HAS_HFCLK_192M
121 /**
122 * @brief Function for setting the specified clock domain divider.
123 *
124 * @param[in] domain Clock domain.
125 * @param[in] div New divider for the clock domain.
126 *
127 * @retval NRFX_SUCCESS Divider successfully set.
128 * @retval NRFX_ERROR_NOT_SUPPORTED Domain does not support setting the divider.
129 * @retval NRFX_ERROR_INVALID_PARAM Divider not supported by the specified domain.
130 */
131 nrfx_err_t nrfx_clock_divider_set(nrf_clock_domain_t domain,
132 nrf_clock_hfclk_div_t div);
133
134 /**
135 * @brief Function for getting the specified clock domain divider.
136 *
137 * @param[in] domain Clock domain.
138 *
139 * @return Current divider for the specified clock domain.
140 */
141
142 NRFX_STATIC_INLINE nrf_clock_hfclk_div_t nrfx_clock_divider_get(nrf_clock_domain_t domain);
143 #endif
144
145 /**
146 * @brief Function for starting the LFCLK.
147 *
148 * @note This function is deprecated. Use @ref nrfx_clock_start instead.
149 */
150 NRFX_STATIC_INLINE void nrfx_clock_lfclk_start(void);
151
152 /**
153 * @brief Function for stopping the LFCLK.
154 *
155 * @note This function is deprecated. Use @ref nrfx_clock_stop instead.
156 */
157 NRFX_STATIC_INLINE void nrfx_clock_lfclk_stop(void);
158
159 /**
160 * @brief Function for checking the LFCLK state.
161 *
162 * @note This function is deprecated. Use @ref nrfx_clock_is_running instead.
163 *
164 * @retval true The LFCLK is running.
165 * @retval false The LFCLK is not running.
166 */
167 NRFX_STATIC_INLINE bool nrfx_clock_lfclk_is_running(void);
168
169 /**
170 * @brief Function for starting the high-accuracy source HFCLK.
171 *
172 * @note This function is deprecated. Use @ref nrfx_clock_start instead.
173 */
174 NRFX_STATIC_INLINE void nrfx_clock_hfclk_start(void);
175
176 /**
177 * @brief Function for stopping the external high-accuracy source HFCLK.
178 *
179 * @note This function is deprecated. Use @ref nrfx_clock_stop instead.
180 */
181 NRFX_STATIC_INLINE void nrfx_clock_hfclk_stop(void);
182
183 /**
184 * @brief Function for checking the HFCLK state.
185 *
186 * @note This function is deprecated. Use @ref nrfx_clock_is_running instead.
187 *
188 * @retval true The HFCLK is running (XTAL source).
189 * @retval false The HFCLK is not running.
190 */
191 NRFX_STATIC_INLINE bool nrfx_clock_hfclk_is_running(void);
192
193
194 #if NRF_CLOCK_HAS_HFCLKAUDIO
195 /**
196 * @brief Function for setting the HFCLKAUDIO configuration.
197 *
198 * The frequency of HFCLKAUDIO ranges from 10.666 MHz to 13.333 MHz in 40.7 Hz steps.
199 * To calculate @p freq_value corresponding to the chosen frequency, use the following equation:
200 * FREQ_VALUE = 2^16 * ((12 * f_out / 32M) - 4)
201 *
202 * @warning Chosen frequency must fit in 11.176 MHz - 11.402 MHz or 12.165 MHz - 12.411 MHz
203 * frequency bands.
204 *
205 * @param[in] freq_value New FREQ_VALUE for HFCLKAUDIO.
206 */
207 NRFX_STATIC_INLINE void nrfx_clock_hfclkaudio_config_set(uint16_t freq_value);
208
209 /**
210 * @brief Function for getting the HFCLKAUDIO configuration.
211 *
212 * The frequency of HFCLKAUDIO ranges from 10.666 MHz to 13.333 MHz in 40.7 Hz steps.
213 * To calculate frequency corresponding to the returned FREQ_VALUE, use the following equation:
214 * f_out = 32M * (4 + FREQ_VALUE * 2^(-16))/12
215 *
216 * @return Current value of FREQ_VALUE for HFCLKAUDIO.
217 */
218 NRFX_STATIC_INLINE uint16_t nrfx_clock_hfclkaudio_config_get(void);
219
220 #endif
221
222 /**
223 * @brief Function for starting the calibration of internal LFCLK.
224 *
225 * This function starts the calibration process. The process cannot be aborted. LFCLK and HFCLK
226 * must be running before this function is called.
227 *
228 * @retval NRFX_SUCCESS The procedure is successful.
229 * @retval NRFX_ERROR_INVALID_STATE The low-frequency of high-frequency clock is off.
230 * @retval NRFX_ERROR_BUSY Clock is in the calibration phase.
231 */
232 nrfx_err_t nrfx_clock_calibration_start(void);
233
234 /**
235 * @brief Function for checking if calibration is in progress.
236 *
237 * This function indicates that the system is in calibration phase.
238 *
239 * @retval NRFX_SUCCESS The procedure is successful.
240 * @retval NRFX_ERROR_BUSY Clock is in the calibration phase.
241 */
242 nrfx_err_t nrfx_clock_is_calibrating(void);
243
244 /**
245 * @brief Function for starting calibration timer.
246 *
247 * @param[in] interval Time after which the CTTO event and interrupt will be generated (in 0.25 s units).
248 */
249 void nrfx_clock_calibration_timer_start(uint8_t interval);
250
251 /** @brief Function for stopping the calibration timer. */
252 void nrfx_clock_calibration_timer_stop(void);
253
254 /**@brief Function for returning a requested task address for the clock driver module.
255 *
256 * @param[in] task One of the peripheral tasks.
257 *
258 * @return Task address.
259 */
260 NRFX_STATIC_INLINE uint32_t nrfx_clock_ppi_task_addr(nrf_clock_task_t task);
261
262 /**@brief Function for returning a requested event address for the clock driver module.
263 *
264 * @param[in] event One of the peripheral events.
265 *
266 * @return Event address.
267 */
268 NRFX_STATIC_INLINE uint32_t nrfx_clock_ppi_event_addr(nrf_clock_event_t event);
269
270 #ifndef NRFX_DECLARE_ONLY
271
272 #if NRF_CLOCK_HAS_HFCLK_DIV || NRF_CLOCK_HAS_HFCLK_192M
nrfx_clock_divider_get(nrf_clock_domain_t domain)273 NRFX_STATIC_INLINE nrf_clock_hfclk_div_t nrfx_clock_divider_get(nrf_clock_domain_t domain)
274 {
275 switch (domain)
276 {
277 #if NRF_CLOCK_HAS_HFCLK_DIV
278 case NRF_CLOCK_DOMAIN_HFCLK:
279 return nrf_clock_hfclk_div_get(NRF_CLOCK);
280 #endif
281 #if NRF_CLOCK_HAS_HFCLK192M
282 case NRF_CLOCK_DOMAIN_HFCLK192M:
283 return nrf_clock_hfclk192m_div_get(NRF_CLOCK);
284 #endif
285 default:
286 NRFX_ASSERT(0);
287 return 0;
288 }
289 }
290 #endif // NRF_CLOCK_HAS_HFCLK_DIV || NRF_CLOCK_HAS_HFCLK_192M
291
nrfx_clock_lfclk_start(void)292 NRFX_STATIC_INLINE void nrfx_clock_lfclk_start(void)
293 {
294 nrfx_clock_start(NRF_CLOCK_DOMAIN_LFCLK);
295 }
296
nrfx_clock_lfclk_stop(void)297 NRFX_STATIC_INLINE void nrfx_clock_lfclk_stop(void)
298 {
299 nrfx_clock_stop(NRF_CLOCK_DOMAIN_LFCLK);
300 }
301
nrfx_clock_hfclk_start(void)302 NRFX_STATIC_INLINE void nrfx_clock_hfclk_start(void)
303 {
304 nrfx_clock_start(NRF_CLOCK_DOMAIN_HFCLK);
305 }
306
nrfx_clock_hfclk_stop(void)307 NRFX_STATIC_INLINE void nrfx_clock_hfclk_stop(void)
308 {
309 nrfx_clock_stop(NRF_CLOCK_DOMAIN_HFCLK);
310 }
311
nrfx_clock_ppi_task_addr(nrf_clock_task_t task)312 NRFX_STATIC_INLINE uint32_t nrfx_clock_ppi_task_addr(nrf_clock_task_t task)
313 {
314 return nrf_clock_task_address_get(NRF_CLOCK, task);
315 }
316
nrfx_clock_ppi_event_addr(nrf_clock_event_t event)317 NRFX_STATIC_INLINE uint32_t nrfx_clock_ppi_event_addr(nrf_clock_event_t event)
318 {
319 return nrf_clock_event_address_get(NRF_CLOCK, event);
320 }
321
nrfx_clock_is_running(nrf_clock_domain_t domain,void * p_clk_src)322 NRFX_STATIC_INLINE bool nrfx_clock_is_running(nrf_clock_domain_t domain, void * p_clk_src)
323 {
324 return nrf_clock_is_running(NRF_CLOCK, domain, p_clk_src);
325 }
326
nrfx_clock_hfclk_is_running(void)327 NRFX_STATIC_INLINE bool nrfx_clock_hfclk_is_running(void)
328 {
329 nrf_clock_hfclk_t clk_src;
330 bool ret = nrfx_clock_is_running(NRF_CLOCK_DOMAIN_HFCLK, &clk_src);
331 return (ret && (clk_src == NRF_CLOCK_HFCLK_HIGH_ACCURACY));
332 }
333
nrfx_clock_lfclk_is_running(void)334 NRFX_STATIC_INLINE bool nrfx_clock_lfclk_is_running(void)
335 {
336 return nrfx_clock_is_running(NRF_CLOCK_DOMAIN_LFCLK, NULL);
337 }
338
339 #if NRF_CLOCK_HAS_HFCLKAUDIO
340
nrfx_clock_hfclkaudio_config_set(uint16_t freq_value)341 NRFX_STATIC_INLINE void nrfx_clock_hfclkaudio_config_set(uint16_t freq_value)
342 {
343 nrf_clock_hfclkaudio_config_set(NRF_CLOCK, freq_value);
344 }
345
nrfx_clock_hfclkaudio_config_get(void)346 NRFX_STATIC_INLINE uint16_t nrfx_clock_hfclkaudio_config_get(void)
347 {
348 return nrf_clock_hfclkaudio_config_get(NRF_CLOCK);
349 }
350
351 #endif
352
353 #endif // NRFX_DECLARE_ONLY
354
355 /** @} */
356
357
358 void nrfx_clock_irq_handler(void);
359
360
361 #ifdef __cplusplus
362 }
363 #endif
364
365 #endif // NRFX_CLOCK_H__
366