Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sun May 14 23:18:57 2017 +0000
Revision:
18:6a4db94011d3
Publishing again

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /*
sahilmgandhi 18:6a4db94011d3 2 * Copyright (c) 2015 Nordic Semiconductor ASA
sahilmgandhi 18:6a4db94011d3 3 * All rights reserved.
sahilmgandhi 18:6a4db94011d3 4 *
sahilmgandhi 18:6a4db94011d3 5 * Redistribution and use in source and binary forms, with or without modification,
sahilmgandhi 18:6a4db94011d3 6 * are permitted provided that the following conditions are met:
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * 1. Redistributions of source code must retain the above copyright notice, this list
sahilmgandhi 18:6a4db94011d3 9 * of conditions and the following disclaimer.
sahilmgandhi 18:6a4db94011d3 10 *
sahilmgandhi 18:6a4db94011d3 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
sahilmgandhi 18:6a4db94011d3 12 * integrated circuit in a product or a software update for such product, must reproduce
sahilmgandhi 18:6a4db94011d3 13 * the above copyright notice, this list of conditions and the following disclaimer in
sahilmgandhi 18:6a4db94011d3 14 * the documentation and/or other materials provided with the distribution.
sahilmgandhi 18:6a4db94011d3 15 *
sahilmgandhi 18:6a4db94011d3 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
sahilmgandhi 18:6a4db94011d3 17 * used to endorse or promote products derived from this software without specific prior
sahilmgandhi 18:6a4db94011d3 18 * written permission.
sahilmgandhi 18:6a4db94011d3 19 *
sahilmgandhi 18:6a4db94011d3 20 * 4. This software, with or without modification, must only be used with a
sahilmgandhi 18:6a4db94011d3 21 * Nordic Semiconductor ASA integrated circuit.
sahilmgandhi 18:6a4db94011d3 22 *
sahilmgandhi 18:6a4db94011d3 23 * 5. Any software provided in binary or object form under this license must not be reverse
sahilmgandhi 18:6a4db94011d3 24 * engineered, decompiled, modified and/or disassembled.
sahilmgandhi 18:6a4db94011d3 25 *
sahilmgandhi 18:6a4db94011d3 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
sahilmgandhi 18:6a4db94011d3 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
sahilmgandhi 18:6a4db94011d3 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
sahilmgandhi 18:6a4db94011d3 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
sahilmgandhi 18:6a4db94011d3 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
sahilmgandhi 18:6a4db94011d3 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sahilmgandhi 18:6a4db94011d3 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
sahilmgandhi 18:6a4db94011d3 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
sahilmgandhi 18:6a4db94011d3 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
sahilmgandhi 18:6a4db94011d3 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sahilmgandhi 18:6a4db94011d3 36 *
sahilmgandhi 18:6a4db94011d3 37 */
sahilmgandhi 18:6a4db94011d3 38
sahilmgandhi 18:6a4db94011d3 39
sahilmgandhi 18:6a4db94011d3 40 #include "nrf_drv_clock.h"
sahilmgandhi 18:6a4db94011d3 41 #include "nrf_error.h"
sahilmgandhi 18:6a4db94011d3 42 #include "nordic_common.h"
sahilmgandhi 18:6a4db94011d3 43
sahilmgandhi 18:6a4db94011d3 44 #ifdef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 45 #include "nrf_sdm.h"
sahilmgandhi 18:6a4db94011d3 46 #include "nrf_soc.h"
sahilmgandhi 18:6a4db94011d3 47 #include "app_util_platform.h"
sahilmgandhi 18:6a4db94011d3 48 #else
sahilmgandhi 18:6a4db94011d3 49 #include "app_util_platform.h"
sahilmgandhi 18:6a4db94011d3 50 #endif // SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 51
sahilmgandhi 18:6a4db94011d3 52 /*lint -save -e652 */
sahilmgandhi 18:6a4db94011d3 53 #define NRF_CLOCK_LFCLK_RC CLOCK_LFCLKSRC_SRC_RC
sahilmgandhi 18:6a4db94011d3 54 #define NRF_CLOCK_LFCLK_Xtal CLOCK_LFCLKSRC_SRC_Xtal
sahilmgandhi 18:6a4db94011d3 55 #define NRF_CLOCK_LFCLK_Synth CLOCK_LFCLKSRC_SRC_Synth
sahilmgandhi 18:6a4db94011d3 56 /*lint -restore */
sahilmgandhi 18:6a4db94011d3 57
sahilmgandhi 18:6a4db94011d3 58 #define INT_MAX 0xFFFFFFFF
sahilmgandhi 18:6a4db94011d3 59
sahilmgandhi 18:6a4db94011d3 60 #if (CLOCK_CONFIG_LF_SRC == NRF_CLOCK_LFCLK_RC) && !defined(SOFTDEVICE_PRESENT)
sahilmgandhi 18:6a4db94011d3 61 #define CALIBRATION_SUPPORT 1
sahilmgandhi 18:6a4db94011d3 62 #else
sahilmgandhi 18:6a4db94011d3 63 #define CALIBRATION_SUPPORT 0
sahilmgandhi 18:6a4db94011d3 64 #endif
sahilmgandhi 18:6a4db94011d3 65 typedef enum
sahilmgandhi 18:6a4db94011d3 66 {
sahilmgandhi 18:6a4db94011d3 67 CAL_STATE_IDLE,
sahilmgandhi 18:6a4db94011d3 68 CAL_STATE_CT,
sahilmgandhi 18:6a4db94011d3 69 CAL_STATE_HFCLK_REQ,
sahilmgandhi 18:6a4db94011d3 70 CAL_STATE_CAL,
sahilmgandhi 18:6a4db94011d3 71 CAL_STATE_ABORT,
sahilmgandhi 18:6a4db94011d3 72 } nrf_drv_clock_cal_state_t;
sahilmgandhi 18:6a4db94011d3 73
sahilmgandhi 18:6a4db94011d3 74 /**@brief CLOCK control block. */
sahilmgandhi 18:6a4db94011d3 75 typedef struct
sahilmgandhi 18:6a4db94011d3 76 {
sahilmgandhi 18:6a4db94011d3 77 volatile uint32_t hfclk_requests; /*< High-frequency clock request counter. */
sahilmgandhi 18:6a4db94011d3 78 volatile nrf_drv_clock_handler_item_t * p_hf_head;
sahilmgandhi 18:6a4db94011d3 79 bool module_initialized; /*< Indicate the state of module */
sahilmgandhi 18:6a4db94011d3 80 volatile bool hfclk_on; /*< High-frequency clock state. */
sahilmgandhi 18:6a4db94011d3 81 #ifndef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 82 volatile bool lfclk_on; /*< Low-frequency clock state. */
sahilmgandhi 18:6a4db94011d3 83 uint32_t lfclk_requests; /*< Low-frequency clock request counter. */
sahilmgandhi 18:6a4db94011d3 84 volatile nrf_drv_clock_handler_item_t * p_lf_head;
sahilmgandhi 18:6a4db94011d3 85 #if CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 86 nrf_drv_clock_handler_item_t cal_hfclk_started_handler_item;
sahilmgandhi 18:6a4db94011d3 87 nrf_drv_clock_event_handler_t cal_done_handler;
sahilmgandhi 18:6a4db94011d3 88 volatile nrf_drv_clock_cal_state_t cal_state;
sahilmgandhi 18:6a4db94011d3 89 #endif //CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 90 #endif //SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 91 }nrf_drv_clock_cb_t;
sahilmgandhi 18:6a4db94011d3 92
sahilmgandhi 18:6a4db94011d3 93 static nrf_drv_clock_cb_t m_clock_cb;
sahilmgandhi 18:6a4db94011d3 94
sahilmgandhi 18:6a4db94011d3 95 #ifndef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 96 /**@brief Function for starting LFCLK. This function will return immediately without waiting for start.
sahilmgandhi 18:6a4db94011d3 97 */
sahilmgandhi 18:6a4db94011d3 98 static void lfclk_start(void)
sahilmgandhi 18:6a4db94011d3 99 {
sahilmgandhi 18:6a4db94011d3 100 nrf_clock_event_clear(NRF_CLOCK_EVENT_LFCLKSTARTED);
sahilmgandhi 18:6a4db94011d3 101 nrf_clock_int_enable(NRF_CLOCK_INT_LF_STARTED_MASK);
sahilmgandhi 18:6a4db94011d3 102 nrf_clock_task_trigger(NRF_CLOCK_TASK_LFCLKSTART);
sahilmgandhi 18:6a4db94011d3 103 }
sahilmgandhi 18:6a4db94011d3 104
sahilmgandhi 18:6a4db94011d3 105 /**@brief Function for stopping LFCLK and calibration (if it was set up).
sahilmgandhi 18:6a4db94011d3 106 */
sahilmgandhi 18:6a4db94011d3 107 static void lfclk_stop(void)
sahilmgandhi 18:6a4db94011d3 108 {
sahilmgandhi 18:6a4db94011d3 109 #if CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 110 (void)nrf_drv_clock_calibration_abort();
sahilmgandhi 18:6a4db94011d3 111 #endif //CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 112
sahilmgandhi 18:6a4db94011d3 113 nrf_clock_task_trigger(NRF_CLOCK_TASK_LFCLKSTOP);
sahilmgandhi 18:6a4db94011d3 114 while (nrf_clock_lf_is_running())
sahilmgandhi 18:6a4db94011d3 115 {}
sahilmgandhi 18:6a4db94011d3 116 }
sahilmgandhi 18:6a4db94011d3 117 #endif
sahilmgandhi 18:6a4db94011d3 118 static void hfclk_start(void)
sahilmgandhi 18:6a4db94011d3 119 {
sahilmgandhi 18:6a4db94011d3 120 #ifndef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 121 nrf_clock_event_clear(NRF_CLOCK_EVENT_HFCLKSTARTED);
sahilmgandhi 18:6a4db94011d3 122 nrf_clock_int_enable(NRF_CLOCK_INT_HF_STARTED_MASK);
sahilmgandhi 18:6a4db94011d3 123 nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTART);
sahilmgandhi 18:6a4db94011d3 124 #else
sahilmgandhi 18:6a4db94011d3 125 UNUSED_VARIABLE(sd_clock_hfclk_request());
sahilmgandhi 18:6a4db94011d3 126 #endif
sahilmgandhi 18:6a4db94011d3 127 }
sahilmgandhi 18:6a4db94011d3 128
sahilmgandhi 18:6a4db94011d3 129 static void hfclk_stop(void)
sahilmgandhi 18:6a4db94011d3 130 {
sahilmgandhi 18:6a4db94011d3 131 #ifndef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 132 nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTOP);
sahilmgandhi 18:6a4db94011d3 133 while (nrf_clock_hf_is_running(NRF_CLOCK_HFCLK_HIGH_ACCURACY))
sahilmgandhi 18:6a4db94011d3 134 {}
sahilmgandhi 18:6a4db94011d3 135 #else
sahilmgandhi 18:6a4db94011d3 136 UNUSED_VARIABLE(sd_clock_hfclk_release());
sahilmgandhi 18:6a4db94011d3 137 #endif
sahilmgandhi 18:6a4db94011d3 138 }
sahilmgandhi 18:6a4db94011d3 139
sahilmgandhi 18:6a4db94011d3 140 ret_code_t nrf_drv_clock_init(void)
sahilmgandhi 18:6a4db94011d3 141 {
sahilmgandhi 18:6a4db94011d3 142 uint32_t result = NRF_SUCCESS;
sahilmgandhi 18:6a4db94011d3 143
sahilmgandhi 18:6a4db94011d3 144 if (m_clock_cb.module_initialized)
sahilmgandhi 18:6a4db94011d3 145 {
sahilmgandhi 18:6a4db94011d3 146 return MODULE_ALREADY_INITIALIZED;
sahilmgandhi 18:6a4db94011d3 147 }
sahilmgandhi 18:6a4db94011d3 148
sahilmgandhi 18:6a4db94011d3 149 m_clock_cb.p_hf_head = NULL;
sahilmgandhi 18:6a4db94011d3 150 m_clock_cb.hfclk_requests = 0;
sahilmgandhi 18:6a4db94011d3 151 #ifndef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 152 m_clock_cb.p_lf_head = NULL;
sahilmgandhi 18:6a4db94011d3 153 m_clock_cb.lfclk_requests = 0;
sahilmgandhi 18:6a4db94011d3 154 nrf_clock_xtalfreq_set(CLOCK_CONFIG_XTAL_FREQ);
sahilmgandhi 18:6a4db94011d3 155 nrf_clock_lf_src_set((nrf_clock_lfclk_t)CLOCK_CONFIG_LF_SRC);
sahilmgandhi 18:6a4db94011d3 156 nrf_drv_common_irq_enable(POWER_CLOCK_IRQn, CLOCK_CONFIG_IRQ_PRIORITY);
sahilmgandhi 18:6a4db94011d3 157 #if CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 158 m_clock_cb.cal_state = CAL_STATE_IDLE;
sahilmgandhi 18:6a4db94011d3 159 #endif // CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 160 #else // SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 161 uint8_t is_enabled;
sahilmgandhi 18:6a4db94011d3 162 result = sd_softdevice_is_enabled(&is_enabled);
sahilmgandhi 18:6a4db94011d3 163 if((result == NRF_SUCCESS) && !is_enabled)
sahilmgandhi 18:6a4db94011d3 164 {
sahilmgandhi 18:6a4db94011d3 165 result = NRF_ERROR_SOFTDEVICE_NOT_ENABLED;
sahilmgandhi 18:6a4db94011d3 166 }
sahilmgandhi 18:6a4db94011d3 167 #endif // SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 168 m_clock_cb.module_initialized = true;
sahilmgandhi 18:6a4db94011d3 169 return result;
sahilmgandhi 18:6a4db94011d3 170 }
sahilmgandhi 18:6a4db94011d3 171
sahilmgandhi 18:6a4db94011d3 172 void nrf_drv_clock_uninit(void)
sahilmgandhi 18:6a4db94011d3 173 {
sahilmgandhi 18:6a4db94011d3 174 ASSERT(m_clock_cb.module_initialized);
sahilmgandhi 18:6a4db94011d3 175 #ifndef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 176 nrf_drv_common_irq_disable(POWER_CLOCK_IRQn);
sahilmgandhi 18:6a4db94011d3 177 nrf_clock_int_disable(0xFFFFFFFF);
sahilmgandhi 18:6a4db94011d3 178 lfclk_stop();
sahilmgandhi 18:6a4db94011d3 179 #endif
sahilmgandhi 18:6a4db94011d3 180 hfclk_stop();
sahilmgandhi 18:6a4db94011d3 181 m_clock_cb.module_initialized = false;
sahilmgandhi 18:6a4db94011d3 182 }
sahilmgandhi 18:6a4db94011d3 183
sahilmgandhi 18:6a4db94011d3 184 static void item_enqueue(nrf_drv_clock_handler_item_t ** p_head,
sahilmgandhi 18:6a4db94011d3 185 nrf_drv_clock_handler_item_t * p_item)
sahilmgandhi 18:6a4db94011d3 186 {
sahilmgandhi 18:6a4db94011d3 187 if (*p_head)
sahilmgandhi 18:6a4db94011d3 188 {
sahilmgandhi 18:6a4db94011d3 189 p_item->p_next = *p_head;
sahilmgandhi 18:6a4db94011d3 190 *p_head = p_item;
sahilmgandhi 18:6a4db94011d3 191 }
sahilmgandhi 18:6a4db94011d3 192 else
sahilmgandhi 18:6a4db94011d3 193 {
sahilmgandhi 18:6a4db94011d3 194 p_item->p_next = NULL;
sahilmgandhi 18:6a4db94011d3 195 *p_head = p_item;
sahilmgandhi 18:6a4db94011d3 196 }
sahilmgandhi 18:6a4db94011d3 197 }
sahilmgandhi 18:6a4db94011d3 198
sahilmgandhi 18:6a4db94011d3 199 static nrf_drv_clock_handler_item_t * item_dequeue(nrf_drv_clock_handler_item_t ** p_head)
sahilmgandhi 18:6a4db94011d3 200 {
sahilmgandhi 18:6a4db94011d3 201 nrf_drv_clock_handler_item_t * p_item = *p_head;
sahilmgandhi 18:6a4db94011d3 202 if (p_item)
sahilmgandhi 18:6a4db94011d3 203 {
sahilmgandhi 18:6a4db94011d3 204 *p_head = p_item->p_next;
sahilmgandhi 18:6a4db94011d3 205 }
sahilmgandhi 18:6a4db94011d3 206
sahilmgandhi 18:6a4db94011d3 207 return p_item;
sahilmgandhi 18:6a4db94011d3 208 }
sahilmgandhi 18:6a4db94011d3 209
sahilmgandhi 18:6a4db94011d3 210 void nrf_drv_clock_lfclk_request(nrf_drv_clock_handler_item_t * p_handler_item)
sahilmgandhi 18:6a4db94011d3 211 {
sahilmgandhi 18:6a4db94011d3 212 ASSERT(m_clock_cb.module_initialized);
sahilmgandhi 18:6a4db94011d3 213 #ifndef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 214 ASSERT(m_clock_cb.lfclk_requests != INT_MAX);
sahilmgandhi 18:6a4db94011d3 215 CRITICAL_REGION_ENTER();
sahilmgandhi 18:6a4db94011d3 216 if (m_clock_cb.lfclk_on)
sahilmgandhi 18:6a4db94011d3 217 {
sahilmgandhi 18:6a4db94011d3 218 if (p_handler_item)
sahilmgandhi 18:6a4db94011d3 219 {
sahilmgandhi 18:6a4db94011d3 220 p_handler_item->event_handler(NRF_DRV_CLOCK_EVT_LFCLK_STARTED);
sahilmgandhi 18:6a4db94011d3 221 }
sahilmgandhi 18:6a4db94011d3 222 }
sahilmgandhi 18:6a4db94011d3 223 else
sahilmgandhi 18:6a4db94011d3 224 {
sahilmgandhi 18:6a4db94011d3 225
sahilmgandhi 18:6a4db94011d3 226 if (p_handler_item)
sahilmgandhi 18:6a4db94011d3 227 {
sahilmgandhi 18:6a4db94011d3 228 item_enqueue((nrf_drv_clock_handler_item_t **)&m_clock_cb.p_lf_head, p_handler_item);
sahilmgandhi 18:6a4db94011d3 229 }
sahilmgandhi 18:6a4db94011d3 230 if (m_clock_cb.lfclk_requests == 0)
sahilmgandhi 18:6a4db94011d3 231 {
sahilmgandhi 18:6a4db94011d3 232 lfclk_start();
sahilmgandhi 18:6a4db94011d3 233 }
sahilmgandhi 18:6a4db94011d3 234 }
sahilmgandhi 18:6a4db94011d3 235 m_clock_cb.lfclk_requests++;
sahilmgandhi 18:6a4db94011d3 236 CRITICAL_REGION_EXIT();
sahilmgandhi 18:6a4db94011d3 237 #else
sahilmgandhi 18:6a4db94011d3 238 if (p_handler_item)
sahilmgandhi 18:6a4db94011d3 239 {
sahilmgandhi 18:6a4db94011d3 240 p_handler_item->event_handler(NRF_DRV_CLOCK_EVT_LFCLK_STARTED);
sahilmgandhi 18:6a4db94011d3 241 }
sahilmgandhi 18:6a4db94011d3 242 #endif // SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 243 }
sahilmgandhi 18:6a4db94011d3 244
sahilmgandhi 18:6a4db94011d3 245
sahilmgandhi 18:6a4db94011d3 246 void nrf_drv_clock_lfclk_release(void)
sahilmgandhi 18:6a4db94011d3 247 {
sahilmgandhi 18:6a4db94011d3 248 ASSERT(m_clock_cb.module_initialized);
sahilmgandhi 18:6a4db94011d3 249 #ifndef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 250 ASSERT(m_clock_cb.lfclk_requests > 0);
sahilmgandhi 18:6a4db94011d3 251
sahilmgandhi 18:6a4db94011d3 252 CRITICAL_REGION_ENTER();
sahilmgandhi 18:6a4db94011d3 253 m_clock_cb.lfclk_requests--;
sahilmgandhi 18:6a4db94011d3 254 if (m_clock_cb.lfclk_requests == 0)
sahilmgandhi 18:6a4db94011d3 255 {
sahilmgandhi 18:6a4db94011d3 256 lfclk_stop();
sahilmgandhi 18:6a4db94011d3 257 m_clock_cb.lfclk_on = false;
sahilmgandhi 18:6a4db94011d3 258 m_clock_cb.p_lf_head = NULL;
sahilmgandhi 18:6a4db94011d3 259 }
sahilmgandhi 18:6a4db94011d3 260 CRITICAL_REGION_EXIT();
sahilmgandhi 18:6a4db94011d3 261 #endif // SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 262 }
sahilmgandhi 18:6a4db94011d3 263
sahilmgandhi 18:6a4db94011d3 264
sahilmgandhi 18:6a4db94011d3 265 bool nrf_drv_clock_lfclk_is_running(void)
sahilmgandhi 18:6a4db94011d3 266 {
sahilmgandhi 18:6a4db94011d3 267 ASSERT(m_clock_cb.module_initialized);
sahilmgandhi 18:6a4db94011d3 268 bool result;
sahilmgandhi 18:6a4db94011d3 269 #ifndef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 270 result = nrf_clock_lf_is_running();
sahilmgandhi 18:6a4db94011d3 271 #else
sahilmgandhi 18:6a4db94011d3 272 result = true;
sahilmgandhi 18:6a4db94011d3 273 #endif
sahilmgandhi 18:6a4db94011d3 274 return result;
sahilmgandhi 18:6a4db94011d3 275 }
sahilmgandhi 18:6a4db94011d3 276
sahilmgandhi 18:6a4db94011d3 277 void nrf_drv_clock_hfclk_request(nrf_drv_clock_handler_item_t * p_handler_item)
sahilmgandhi 18:6a4db94011d3 278 {
sahilmgandhi 18:6a4db94011d3 279 ASSERT(m_clock_cb.module_initialized);
sahilmgandhi 18:6a4db94011d3 280 ASSERT(m_clock_cb.hfclk_requests != INT_MAX);
sahilmgandhi 18:6a4db94011d3 281
sahilmgandhi 18:6a4db94011d3 282 CRITICAL_REGION_ENTER();
sahilmgandhi 18:6a4db94011d3 283 if (m_clock_cb.hfclk_on)
sahilmgandhi 18:6a4db94011d3 284 {
sahilmgandhi 18:6a4db94011d3 285 if (p_handler_item)
sahilmgandhi 18:6a4db94011d3 286 {
sahilmgandhi 18:6a4db94011d3 287 p_handler_item->event_handler(NRF_DRV_CLOCK_EVT_HFCLK_STARTED);
sahilmgandhi 18:6a4db94011d3 288 }
sahilmgandhi 18:6a4db94011d3 289 }
sahilmgandhi 18:6a4db94011d3 290 else
sahilmgandhi 18:6a4db94011d3 291 {
sahilmgandhi 18:6a4db94011d3 292 if (p_handler_item)
sahilmgandhi 18:6a4db94011d3 293 {
sahilmgandhi 18:6a4db94011d3 294 item_enqueue((nrf_drv_clock_handler_item_t **)&m_clock_cb.p_hf_head, p_handler_item);
sahilmgandhi 18:6a4db94011d3 295 }
sahilmgandhi 18:6a4db94011d3 296 if (m_clock_cb.hfclk_requests == 0)
sahilmgandhi 18:6a4db94011d3 297 {
sahilmgandhi 18:6a4db94011d3 298 hfclk_start();
sahilmgandhi 18:6a4db94011d3 299 }
sahilmgandhi 18:6a4db94011d3 300 }
sahilmgandhi 18:6a4db94011d3 301 m_clock_cb.hfclk_requests++;
sahilmgandhi 18:6a4db94011d3 302 CRITICAL_REGION_EXIT();
sahilmgandhi 18:6a4db94011d3 303 }
sahilmgandhi 18:6a4db94011d3 304
sahilmgandhi 18:6a4db94011d3 305 void nrf_drv_clock_hfclk_release(void)
sahilmgandhi 18:6a4db94011d3 306 {
sahilmgandhi 18:6a4db94011d3 307 ASSERT(m_clock_cb.module_initialized);
sahilmgandhi 18:6a4db94011d3 308 ASSERT(m_clock_cb.hfclk_requests > 0);
sahilmgandhi 18:6a4db94011d3 309
sahilmgandhi 18:6a4db94011d3 310 //disable interrupts CLOCK or SoftDevice events
sahilmgandhi 18:6a4db94011d3 311 CRITICAL_REGION_ENTER();
sahilmgandhi 18:6a4db94011d3 312 m_clock_cb.hfclk_requests--;
sahilmgandhi 18:6a4db94011d3 313 if (m_clock_cb.hfclk_requests == 0)
sahilmgandhi 18:6a4db94011d3 314 {
sahilmgandhi 18:6a4db94011d3 315 hfclk_stop();
sahilmgandhi 18:6a4db94011d3 316 m_clock_cb.hfclk_on = false;
sahilmgandhi 18:6a4db94011d3 317 m_clock_cb.p_hf_head = NULL;
sahilmgandhi 18:6a4db94011d3 318 }
sahilmgandhi 18:6a4db94011d3 319 CRITICAL_REGION_EXIT();
sahilmgandhi 18:6a4db94011d3 320 //enable interrupts CLOCK or SoftDevice events
sahilmgandhi 18:6a4db94011d3 321 }
sahilmgandhi 18:6a4db94011d3 322
sahilmgandhi 18:6a4db94011d3 323 bool nrf_drv_clock_hfclk_is_running(void)
sahilmgandhi 18:6a4db94011d3 324 {
sahilmgandhi 18:6a4db94011d3 325 bool result;
sahilmgandhi 18:6a4db94011d3 326 ASSERT(m_clock_cb.module_initialized);
sahilmgandhi 18:6a4db94011d3 327 #ifndef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 328 result = nrf_clock_hf_is_running(NRF_CLOCK_HFCLK_HIGH_ACCURACY);
sahilmgandhi 18:6a4db94011d3 329 #else
sahilmgandhi 18:6a4db94011d3 330 uint32_t is_running;
sahilmgandhi 18:6a4db94011d3 331 UNUSED_VARIABLE(sd_clock_hfclk_is_running(&is_running));
sahilmgandhi 18:6a4db94011d3 332 result = is_running ? true : false;
sahilmgandhi 18:6a4db94011d3 333 #endif
sahilmgandhi 18:6a4db94011d3 334 return result;
sahilmgandhi 18:6a4db94011d3 335 }
sahilmgandhi 18:6a4db94011d3 336
sahilmgandhi 18:6a4db94011d3 337 #if CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 338 static void clock_calibration_hf_started(nrf_drv_clock_evt_type_t event)
sahilmgandhi 18:6a4db94011d3 339 {
sahilmgandhi 18:6a4db94011d3 340 if (m_clock_cb.cal_state == CAL_STATE_ABORT)
sahilmgandhi 18:6a4db94011d3 341 {
sahilmgandhi 18:6a4db94011d3 342 nrf_drv_clock_hfclk_release();
sahilmgandhi 18:6a4db94011d3 343 m_clock_cb.cal_state = CAL_STATE_IDLE;
sahilmgandhi 18:6a4db94011d3 344 if (m_clock_cb.cal_done_handler)
sahilmgandhi 18:6a4db94011d3 345 {
sahilmgandhi 18:6a4db94011d3 346 m_clock_cb.cal_done_handler(NRF_DRV_CLOCK_EVT_CAL_ABORTED);
sahilmgandhi 18:6a4db94011d3 347 }
sahilmgandhi 18:6a4db94011d3 348 }
sahilmgandhi 18:6a4db94011d3 349 else
sahilmgandhi 18:6a4db94011d3 350 {
sahilmgandhi 18:6a4db94011d3 351 nrf_clock_int_enable(NRF_CLOCK_INT_DONE_MASK);
sahilmgandhi 18:6a4db94011d3 352 m_clock_cb.cal_state = CAL_STATE_CAL;
sahilmgandhi 18:6a4db94011d3 353 nrf_clock_task_trigger(NRF_CLOCK_TASK_CAL);
sahilmgandhi 18:6a4db94011d3 354 }
sahilmgandhi 18:6a4db94011d3 355 }
sahilmgandhi 18:6a4db94011d3 356 #endif
sahilmgandhi 18:6a4db94011d3 357
sahilmgandhi 18:6a4db94011d3 358 ret_code_t nrf_drv_clock_calibration_start(uint8_t interval, nrf_drv_clock_event_handler_t handler)
sahilmgandhi 18:6a4db94011d3 359 {
sahilmgandhi 18:6a4db94011d3 360 #if CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 361 ASSERT(m_clock_cb.cal_state == CAL_STATE_IDLE);
sahilmgandhi 18:6a4db94011d3 362 ret_code_t ret = NRF_SUCCESS;
sahilmgandhi 18:6a4db94011d3 363 if (m_clock_cb.lfclk_on == false)
sahilmgandhi 18:6a4db94011d3 364 {
sahilmgandhi 18:6a4db94011d3 365 ret = NRF_ERROR_INVALID_STATE;
sahilmgandhi 18:6a4db94011d3 366 }
sahilmgandhi 18:6a4db94011d3 367 else if (m_clock_cb.cal_state == CAL_STATE_IDLE)
sahilmgandhi 18:6a4db94011d3 368 {
sahilmgandhi 18:6a4db94011d3 369 m_clock_cb.cal_done_handler = handler;
sahilmgandhi 18:6a4db94011d3 370 m_clock_cb.cal_hfclk_started_handler_item.event_handler = clock_calibration_hf_started;
sahilmgandhi 18:6a4db94011d3 371 if (interval == 0)
sahilmgandhi 18:6a4db94011d3 372 {
sahilmgandhi 18:6a4db94011d3 373 m_clock_cb.cal_state = CAL_STATE_HFCLK_REQ;
sahilmgandhi 18:6a4db94011d3 374 nrf_drv_clock_hfclk_request(&m_clock_cb.cal_hfclk_started_handler_item);
sahilmgandhi 18:6a4db94011d3 375 }
sahilmgandhi 18:6a4db94011d3 376 else
sahilmgandhi 18:6a4db94011d3 377 {
sahilmgandhi 18:6a4db94011d3 378 m_clock_cb.cal_state = CAL_STATE_CT;
sahilmgandhi 18:6a4db94011d3 379 nrf_clock_cal_timer_timeout_set(interval);
sahilmgandhi 18:6a4db94011d3 380 nrf_clock_int_enable(NRF_CLOCK_INT_CTTO_MASK);
sahilmgandhi 18:6a4db94011d3 381 nrf_clock_task_trigger(NRF_CLOCK_TASK_CTSTART);
sahilmgandhi 18:6a4db94011d3 382 }
sahilmgandhi 18:6a4db94011d3 383 }
sahilmgandhi 18:6a4db94011d3 384 else
sahilmgandhi 18:6a4db94011d3 385 {
sahilmgandhi 18:6a4db94011d3 386 ret = NRF_ERROR_BUSY;
sahilmgandhi 18:6a4db94011d3 387 }
sahilmgandhi 18:6a4db94011d3 388 return ret;
sahilmgandhi 18:6a4db94011d3 389 #else //CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 390 return NRF_ERROR_FORBIDDEN;
sahilmgandhi 18:6a4db94011d3 391 #endif
sahilmgandhi 18:6a4db94011d3 392 }
sahilmgandhi 18:6a4db94011d3 393
sahilmgandhi 18:6a4db94011d3 394
sahilmgandhi 18:6a4db94011d3 395 ret_code_t nrf_drv_clock_calibration_abort(void)
sahilmgandhi 18:6a4db94011d3 396 {
sahilmgandhi 18:6a4db94011d3 397 #if CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 398 CRITICAL_REGION_ENTER();
sahilmgandhi 18:6a4db94011d3 399 switch(m_clock_cb.cal_state)
sahilmgandhi 18:6a4db94011d3 400 {
sahilmgandhi 18:6a4db94011d3 401 case CAL_STATE_CT:
sahilmgandhi 18:6a4db94011d3 402 nrf_clock_int_disable(NRF_CLOCK_INT_CTTO_MASK);
sahilmgandhi 18:6a4db94011d3 403 nrf_clock_task_trigger(NRF_CLOCK_TASK_CTSTOP);
sahilmgandhi 18:6a4db94011d3 404 m_clock_cb.cal_state = CAL_STATE_IDLE;
sahilmgandhi 18:6a4db94011d3 405 if (m_clock_cb.cal_done_handler)
sahilmgandhi 18:6a4db94011d3 406 {
sahilmgandhi 18:6a4db94011d3 407 m_clock_cb.cal_done_handler(NRF_DRV_CLOCK_EVT_CAL_ABORTED);
sahilmgandhi 18:6a4db94011d3 408 }
sahilmgandhi 18:6a4db94011d3 409 break;
sahilmgandhi 18:6a4db94011d3 410 case CAL_STATE_HFCLK_REQ:
sahilmgandhi 18:6a4db94011d3 411 /* fall through. */
sahilmgandhi 18:6a4db94011d3 412 case CAL_STATE_CAL:
sahilmgandhi 18:6a4db94011d3 413 m_clock_cb.cal_state = CAL_STATE_ABORT;
sahilmgandhi 18:6a4db94011d3 414 break;
sahilmgandhi 18:6a4db94011d3 415 default:
sahilmgandhi 18:6a4db94011d3 416 break;
sahilmgandhi 18:6a4db94011d3 417 }
sahilmgandhi 18:6a4db94011d3 418 CRITICAL_REGION_EXIT();
sahilmgandhi 18:6a4db94011d3 419 return NRF_SUCCESS;
sahilmgandhi 18:6a4db94011d3 420 #else //CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 421 return NRF_ERROR_FORBIDDEN;
sahilmgandhi 18:6a4db94011d3 422 #endif
sahilmgandhi 18:6a4db94011d3 423 }
sahilmgandhi 18:6a4db94011d3 424
sahilmgandhi 18:6a4db94011d3 425 ret_code_t nrf_drv_clock_is_calibrating(bool * p_is_calibrating)
sahilmgandhi 18:6a4db94011d3 426 {
sahilmgandhi 18:6a4db94011d3 427 #if CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 428 ASSERT(m_clock_cb.module_initialized);
sahilmgandhi 18:6a4db94011d3 429 *p_is_calibrating = (m_clock_cb.cal_state != CAL_STATE_IDLE);
sahilmgandhi 18:6a4db94011d3 430 return NRF_SUCCESS;
sahilmgandhi 18:6a4db94011d3 431 #else //CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 432 return NRF_ERROR_FORBIDDEN;
sahilmgandhi 18:6a4db94011d3 433 #endif
sahilmgandhi 18:6a4db94011d3 434 }
sahilmgandhi 18:6a4db94011d3 435
sahilmgandhi 18:6a4db94011d3 436 static __INLINE void clock_clk_started_notify(nrf_drv_clock_handler_item_t **p_head,
sahilmgandhi 18:6a4db94011d3 437 nrf_drv_clock_evt_type_t evt_type)
sahilmgandhi 18:6a4db94011d3 438 {
sahilmgandhi 18:6a4db94011d3 439 while(1)
sahilmgandhi 18:6a4db94011d3 440 {
sahilmgandhi 18:6a4db94011d3 441 nrf_drv_clock_handler_item_t * p_item = item_dequeue(p_head);
sahilmgandhi 18:6a4db94011d3 442 if (p_item)
sahilmgandhi 18:6a4db94011d3 443 {
sahilmgandhi 18:6a4db94011d3 444 p_item->event_handler(evt_type);
sahilmgandhi 18:6a4db94011d3 445 }
sahilmgandhi 18:6a4db94011d3 446 else
sahilmgandhi 18:6a4db94011d3 447 {
sahilmgandhi 18:6a4db94011d3 448 break;
sahilmgandhi 18:6a4db94011d3 449 }
sahilmgandhi 18:6a4db94011d3 450 }
sahilmgandhi 18:6a4db94011d3 451 }
sahilmgandhi 18:6a4db94011d3 452
sahilmgandhi 18:6a4db94011d3 453 #ifndef SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 454 void POWER_CLOCK_IRQHandler(void)
sahilmgandhi 18:6a4db94011d3 455 {
sahilmgandhi 18:6a4db94011d3 456 if (nrf_clock_event_check(NRF_CLOCK_EVENT_HFCLKSTARTED))
sahilmgandhi 18:6a4db94011d3 457 {
sahilmgandhi 18:6a4db94011d3 458 nrf_clock_event_clear(NRF_CLOCK_EVENT_HFCLKSTARTED);
sahilmgandhi 18:6a4db94011d3 459 nrf_clock_int_disable(NRF_CLOCK_INT_HF_STARTED_MASK);
sahilmgandhi 18:6a4db94011d3 460 m_clock_cb.hfclk_on = true;
sahilmgandhi 18:6a4db94011d3 461 clock_clk_started_notify((nrf_drv_clock_handler_item_t **)&m_clock_cb.p_hf_head, NRF_DRV_CLOCK_EVT_HFCLK_STARTED);
sahilmgandhi 18:6a4db94011d3 462 }
sahilmgandhi 18:6a4db94011d3 463 if (nrf_clock_event_check(NRF_CLOCK_EVENT_LFCLKSTARTED))
sahilmgandhi 18:6a4db94011d3 464 {
sahilmgandhi 18:6a4db94011d3 465 nrf_clock_event_clear(NRF_CLOCK_EVENT_LFCLKSTARTED);
sahilmgandhi 18:6a4db94011d3 466 nrf_clock_int_disable(NRF_CLOCK_INT_LF_STARTED_MASK);
sahilmgandhi 18:6a4db94011d3 467 m_clock_cb.lfclk_on = true;
sahilmgandhi 18:6a4db94011d3 468 clock_clk_started_notify((nrf_drv_clock_handler_item_t **)&m_clock_cb.p_lf_head, NRF_DRV_CLOCK_EVT_LFCLK_STARTED);
sahilmgandhi 18:6a4db94011d3 469 }
sahilmgandhi 18:6a4db94011d3 470 #if CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 471 if (nrf_clock_event_check(NRF_CLOCK_EVENT_CTTO))
sahilmgandhi 18:6a4db94011d3 472 {
sahilmgandhi 18:6a4db94011d3 473 nrf_clock_event_clear(NRF_CLOCK_EVENT_CTTO);
sahilmgandhi 18:6a4db94011d3 474 nrf_clock_int_disable(NRF_CLOCK_INT_CTTO_MASK);
sahilmgandhi 18:6a4db94011d3 475 nrf_drv_clock_hfclk_request(&m_clock_cb.cal_hfclk_started_handler_item);
sahilmgandhi 18:6a4db94011d3 476 }
sahilmgandhi 18:6a4db94011d3 477
sahilmgandhi 18:6a4db94011d3 478 if (nrf_clock_event_check(NRF_CLOCK_EVENT_DONE))
sahilmgandhi 18:6a4db94011d3 479 {
sahilmgandhi 18:6a4db94011d3 480 nrf_clock_event_clear(NRF_CLOCK_EVENT_DONE);
sahilmgandhi 18:6a4db94011d3 481 nrf_clock_int_disable(NRF_CLOCK_INT_DONE_MASK);
sahilmgandhi 18:6a4db94011d3 482
sahilmgandhi 18:6a4db94011d3 483 nrf_drv_clock_hfclk_release();
sahilmgandhi 18:6a4db94011d3 484 nrf_drv_clock_evt_type_t evt_type = (m_clock_cb.cal_state == CAL_STATE_ABORT) ?
sahilmgandhi 18:6a4db94011d3 485 NRF_DRV_CLOCK_EVT_CAL_ABORTED : NRF_DRV_CLOCK_EVT_CAL_DONE;
sahilmgandhi 18:6a4db94011d3 486 m_clock_cb.cal_state = CAL_STATE_IDLE;
sahilmgandhi 18:6a4db94011d3 487 if (m_clock_cb.cal_done_handler)
sahilmgandhi 18:6a4db94011d3 488 {
sahilmgandhi 18:6a4db94011d3 489 m_clock_cb.cal_done_handler(evt_type);
sahilmgandhi 18:6a4db94011d3 490 }
sahilmgandhi 18:6a4db94011d3 491 }
sahilmgandhi 18:6a4db94011d3 492 #endif //CALIBRATION_SUPPORT
sahilmgandhi 18:6a4db94011d3 493 }
sahilmgandhi 18:6a4db94011d3 494 #else
sahilmgandhi 18:6a4db94011d3 495 void nrf_drv_clock_on_soc_event(uint32_t evt_id)
sahilmgandhi 18:6a4db94011d3 496 {
sahilmgandhi 18:6a4db94011d3 497 if (evt_id == NRF_EVT_HFCLKSTARTED)
sahilmgandhi 18:6a4db94011d3 498 {
sahilmgandhi 18:6a4db94011d3 499 clock_clk_started_notify((nrf_drv_clock_handler_item_t **)&m_clock_cb.p_hf_head, NRF_DRV_CLOCK_EVT_HFCLK_STARTED);
sahilmgandhi 18:6a4db94011d3 500 }
sahilmgandhi 18:6a4db94011d3 501 }
sahilmgandhi 18:6a4db94011d3 502 #endif // SOFTDEVICE_PRESENT
sahilmgandhi 18:6a4db94011d3 503
sahilmgandhi 18:6a4db94011d3 504 #undef NRF_CLOCK_LFCLK_RC
sahilmgandhi 18:6a4db94011d3 505 #undef NRF_CLOCK_LFCLK_Xtal
sahilmgandhi 18:6a4db94011d3 506 #undef NRF_CLOCK_LFCLK_Synth