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