xref: /nrf52832-nimble/nordic/nrfx/drivers/include/nrfx_clock.h (revision 150812a83cab50279bd772ef6db1bfaf255f2c5b)
1 /*
2  * Copyright (c) 2016 - 2018, 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 /**
51  * @brief Clock events.
52  */
53 typedef enum
54 {
55     NRFX_CLOCK_EVT_HFCLK_STARTED, ///< HFCLK has been started.
56     NRFX_CLOCK_EVT_LFCLK_STARTED, ///< LFCLK has been started.
57     NRFX_CLOCK_EVT_CTTO,          ///< Calibration timeout.
58     NRFX_CLOCK_EVT_CAL_DONE       ///< Calibration has been done.
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                   If the procedure was successful.
77  * @retval NRFX_ERROR_ALREADY_INITIALIZED If the driver was already initialized.
78  */
79 nrfx_err_t nrfx_clock_init(nrfx_clock_event_handler_t  event_handler);
80 
81 /**
82  * @brief Function for enabling interrupts in the clock module.
83  */
84 void nrfx_clock_enable(void);
85 
86 /**
87  * @brief Function for disabling interrupts in the clock module.
88  */
89 void nrfx_clock_disable(void);
90 
91 /**
92  * @brief Function for uninitializing the clock module.
93  */
94 void nrfx_clock_uninit(void);
95 
96 /**
97  * @brief Function for starting the LFCLK.
98  */
99 void nrfx_clock_lfclk_start(void);
100 
101 /**
102  * @brief Function for stoping the LFCLK.
103  */
104 void nrfx_clock_lfclk_stop(void);
105 
106 /**
107  * @brief Function for checking the LFCLK state.
108  *
109  * @retval true If the LFCLK is running.
110  * @retval false If the LFCLK is not running.
111  */
112 __STATIC_INLINE bool nrfx_clock_lfclk_is_running(void);
113 
114 /**
115  * @brief Function for starting the high-accuracy source HFCLK.
116  */
117 void nrfx_clock_hfclk_start(void);
118 
119 /**
120  * @brief Function for stoping external high-accuracy source HFCLK.
121  */
122 void nrfx_clock_hfclk_stop(void);
123 
124 /**
125  * @brief Function for checking the HFCLK state.
126  *
127  * @retval true If the HFCLK is running (XTAL source).
128  * @retval false If the HFCLK is not running.
129  */
130 __STATIC_INLINE bool nrfx_clock_hfclk_is_running(void);
131 
132 /**
133  * @brief Function for starting calibration of internal LFCLK.
134  *
135  * This function starts the calibration process. The process cannot be aborted. LFCLK and HFCLK
136  * must be running before this function is called.
137  *
138  * @retval     NRFX_SUCCESS                        If the procedure was successful.
139  * @retval     NRFX_ERROR_INVALID_STATE            If the low-frequency of high-frequency clock is off.
140  * @retval     NRFX_ERROR_BUSY                     If calibration is in progress.
141  */
142 nrfx_err_t nrfx_clock_calibration_start(void);
143 
144 /**
145  * @brief Function for checking if calibration is in progress.
146  *
147  * This function indicates that the system is in calibration phase.
148  *
149  * @retval     NRFX_SUCCESS                        If the procedure was successful.
150  * @retval     NRFX_ERROR_BUSY                     If calibration is in progress.
151  */
152 nrfx_err_t nrfx_clock_is_calibrating(void);
153 
154 /**
155  * @brief Function for starting calibration timer.
156  * @param interval Time after which the CTTO event and interrupt will be generated (in 0.25 s units).
157  */
158 void nrfx_clock_calibration_timer_start(uint8_t interval);
159 
160 /**
161  * @brief Function for stoping calibration timer.
162  */
163 void nrfx_clock_calibration_timer_stop(void);
164 
165 /**@brief Function for returning a requested task address for the clock driver module.
166  *
167  * @param[in]  task                               One of the peripheral tasks.
168  *
169  * @return     Task address.
170  */
171 __STATIC_INLINE uint32_t nrfx_clock_ppi_task_addr(nrf_clock_task_t task);
172 
173 /**@brief Function for returning a requested event address for the clock driver module.
174  *
175  * @param[in]  event                              One of the peripheral events.
176  *
177  * @return     Event address.
178  */
179 __STATIC_INLINE uint32_t nrfx_clock_ppi_event_addr(nrf_clock_event_t event);
180 
181 
182 #ifndef SUPPRESS_INLINE_IMPLEMENTATION
nrfx_clock_ppi_task_addr(nrf_clock_task_t task)183 __STATIC_INLINE uint32_t nrfx_clock_ppi_task_addr(nrf_clock_task_t task)
184 {
185     return nrf_clock_task_address_get(task);
186 }
187 
nrfx_clock_ppi_event_addr(nrf_clock_event_t event)188 __STATIC_INLINE uint32_t nrfx_clock_ppi_event_addr(nrf_clock_event_t event)
189 {
190     return nrf_clock_event_address_get(event);
191 }
192 
nrfx_clock_hfclk_is_running(void)193 __STATIC_INLINE bool nrfx_clock_hfclk_is_running(void)
194 {
195     return nrf_clock_hf_is_running(NRF_CLOCK_HFCLK_HIGH_ACCURACY);
196 }
197 
nrfx_clock_lfclk_is_running(void)198 __STATIC_INLINE bool nrfx_clock_lfclk_is_running(void)
199 {
200     return nrf_clock_lf_is_running();
201 }
202 #endif //SUPPRESS_INLINE_IMPLEMENTATION
203 
204 
205 void nrfx_clock_irq_handler(void);
206 
207 
208 /** @} */
209 
210 #ifdef __cplusplus
211 }
212 #endif
213 
214 #endif // NRFX_CLOCK_H__
215