|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 - 2025, 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. Nittner 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_LFCLK_H__ |
| 35 | +#define NRFX_CLOCK_LFCLK_H__ |
| 36 | + |
| 37 | +#include <nrfx.h> |
| 38 | +#include <hal/nrf_clock.h> |
| 39 | + |
| 40 | +#if defined(LFRC_PRESENT) |
| 41 | +#include <hal/nrf_lfrc.h> |
| 42 | +#endif |
| 43 | + |
| 44 | +#ifdef __cplusplus |
| 45 | +extern "C" { |
| 46 | +#endif |
| 47 | + |
| 48 | +/** |
| 49 | + * @defgroup nrfx_clock_lfclk LFCLK driver |
| 50 | + * @{ |
| 51 | + * @ingroup nrf_clock_lfclk |
| 52 | + * @brief LFCLK clock driver. |
| 53 | + */ |
| 54 | + |
| 55 | + /** @brief Symbol specifying driver event offset for LFRC hardware events. */ |
| 56 | +#define NRFX_CLOCK_LFCLK_LFRC_EVT_OFFSET 32 |
| 57 | + |
| 58 | +/** @brief Clock events. */ |
| 59 | +typedef enum |
| 60 | +{ |
| 61 | + NRFX_CLOCK_LFCLK_EVT_LFCLK_STARTED = NRFX_BITMASK_TO_BITPOS(NRF_CLOCK_INT_LF_STARTED_MASK), ///< LFCLK has been started. |
| 62 | +#if NRF_CLOCK_HAS_CALIBRATION_TIMER |
| 63 | + NRFX_CLOCK_LFCLK_EVT_CTTO = NRFX_BITMASK_TO_BITPOS(NRF_CLOCK_INT_CTTO_MASK), ///< Calibration timeout. |
| 64 | +#endif |
| 65 | +#if NRF_CLOCK_HAS_CALIBRATION |
| 66 | + NRFX_CLOCK_LFCLK_EVT_CAL_DONE = NRFX_BITMASK_TO_BITPOS(NRF_CLOCK_INT_DONE_MASK), ///< Calibration has been done. |
| 67 | +#elif NRFX_CHECK(NRF_LFRC_HAS_CALIBRATION) |
| 68 | + NRFX_CLOCK_LFCLK_EVT_CAL_DONE = (NRFX_BITMASK_TO_BITPOS(NRF_LFRC_INT_CALDONE_MASK) + \ |
| 69 | + NRFX_CLOCK_LFCLK_LFRC_EVT_OFFSET), ///< Calibration has been done. |
| 70 | +#endif |
| 71 | + |
| 72 | + |
| 73 | +} nrfx_clock_lfclk_evt_type_t; |
| 74 | +//TODO check if all commens and functions are lfclk specific |
| 75 | +/** |
| 76 | + * @brief Lfclk event handler. |
| 77 | + * |
| 78 | + * @param[in] event Event. |
| 79 | + */ |
| 80 | +typedef void (*nrfx_clock_lfclk_event_handler_t)(nrfx_clock_lfclk_evt_type_t event); |
| 81 | + |
| 82 | +/** |
| 83 | + * @brief Function for initializing internal structures in the nrfx_clock_lfclk module. |
| 84 | + * |
| 85 | + * After initialization, the module is in power off state (clocks are not started). |
| 86 | + * |
| 87 | + * @param[in] event_handler Event handler provided by the user. |
| 88 | + * If not provided, driver works in blocking mode. |
| 89 | + * |
| 90 | + * @retval NRFX_SUCCESS The procedure is successful. |
| 91 | + * @retval NRFX_ERROR_ALREADY The driver is already initialized. |
| 92 | + */ |
| 93 | +nrfx_err_t nrfx_clock_lfclk_init(nrfx_clock_lfclk_event_handler_t event_handler); |
| 94 | + |
| 95 | +/** @brief Function for uninitializing the lfclk module. */ |
| 96 | +void nrfx_clock_lfclk_uninit(void); |
| 97 | + |
| 98 | +/** |
| 99 | + * @brief Function for checking if the lfclk driver is initialized. |
| 100 | + * |
| 101 | + * @retval true Driver is already initialized. |
| 102 | + * @retval false Driver is not initialized. |
| 103 | + */ |
| 104 | +bool nrfx_clock_lfclk_init_check(void); |
| 105 | + |
| 106 | +/** |
| 107 | + * @brief Function for starting the LFCLK. |
| 108 | + */ |
| 109 | +void nrfx_clock_lfclk_start(void); |
| 110 | + |
| 111 | +/** |
| 112 | + * @brief Function for stopping the LFCLK. |
| 113 | + */ |
| 114 | +void nrfx_clock_lfclk_stop(void); |
| 115 | + |
| 116 | +/** |
| 117 | + * @brief Function for checking the LFCLK state. |
| 118 | + * |
| 119 | + * XTAL source is assumed for domains with multiple sources. |
| 120 | + * |
| 121 | + * @param[out] p_clk_src Pointer to a clock source that is running. Set to NULL if not needed. |
| 122 | + * |
| 123 | + * @retval true The clock domain is running. |
| 124 | + * @retval false The clock domain is not running. |
| 125 | + */ |
| 126 | +NRFX_STATIC_INLINE bool nrfx_clock_lfclk_running_check(nrf_clock_lfclk_t * p_clk_src); |
| 127 | + |
| 128 | +#if ((NRF_CLOCK_HAS_CALIBRATION || NRFX_CHECK(NRF_LFRC_HAS_CALIBRATION)) && \ |
| 129 | + NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)) || defined(__NRFX_DOXYGEN__) |
| 130 | +/** |
| 131 | + * @brief Function for starting the calibration of internal LFCLK. |
| 132 | + * |
| 133 | + * This function starts the calibration process. The process cannot be aborted. LFCLK and (HFCLK or XO) |
| 134 | + * must be running before this function is called. |
| 135 | + * |
| 136 | + * @retval NRFX_SUCCESS The procedure is successful. |
| 137 | + * @retval NRFX_ERROR_INVALID_STATE The low-frequency or high-frequency clock is off. |
| 138 | + * @retval NRFX_ERROR_BUSY Clock is in the calibration phase. |
| 139 | + */ |
| 140 | +nrfx_err_t nrfx_clock_lfclk_calibration_start(void); |
| 141 | + |
| 142 | +/** |
| 143 | + * @brief Function for checking if calibration is in progress. |
| 144 | + * |
| 145 | + * This function indicates that the system is in calibration phase. |
| 146 | + * |
| 147 | + * @retval NRFX_SUCCESS The procedure is successful. |
| 148 | + * @retval NRFX_ERROR_BUSY Clock is in the calibration phase. |
| 149 | + */ |
| 150 | +nrfx_err_t nrfx_clock_lfclk_calibrating_check(void); |
| 151 | + |
| 152 | +#if (NRF_CLOCK_HAS_CALIBRATION_TIMER && NRFX_CHECK(NRFX_CLOCK_CONFIG_CT_ENABLED)) || \ |
| 153 | + defined(__NRFX_DOXYGEN__) |
| 154 | +/** |
| 155 | + * @brief Function for starting calibration timer. |
| 156 | + * |
| 157 | + * @param[in] interval Time after which the CTTO event and interrupt will be generated (in 0.25 s units). |
| 158 | + */ |
| 159 | +void nrfx_clock_lfclk_calibration_timer_start(uint8_t interval); |
| 160 | + |
| 161 | +/** @brief Function for stopping the calibration timer. */ |
| 162 | +void nrfx_clock_lfclk_calibration_timer_stop(void); |
| 163 | +#endif |
| 164 | +#endif /* ((NRF_CLOCK_HAS_CALIBRATION || NRFX_CHECK(NRF_LFRC_HAS_CALIBRATION)) && \ |
| 165 | + NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)) || defined(__NRFX_DOXYGEN__) */ |
| 166 | + |
| 167 | +#ifndef NRFX_DECLARE_ONLY |
| 168 | + |
| 169 | +NRFX_STATIC_INLINE bool nrfx_clock_lfclk_running_check(nrf_clock_lfclk_t * p_clk_src) |
| 170 | +{ |
| 171 | + return nrf_clock_is_running(NRF_CLOCK, NRF_CLOCK_DOMAIN_LFCLK, (void*)p_clk_src); |
| 172 | +} |
| 173 | + |
| 174 | +#endif // NRFX_DECLARE_ONLY |
| 175 | + |
| 176 | +/** @} */ |
| 177 | + |
| 178 | +void nrfx_clock_lfclk_irq_handler(void); |
| 179 | + |
| 180 | +#ifdef __cplusplus |
| 181 | +} |
| 182 | +#endif |
| 183 | + |
| 184 | +#endif // NRFX_CLOCK_LFCLK_H__ |
0 commit comments