nrfx_clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. * Copyright (c) 2016 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <nrfx.h>
  41. #if NRFX_CHECK(NRFX_CLOCK_ENABLED)
  42. #include <nrfx_clock.h>
  43. #define NRFX_LOG_MODULE CLOCK
  44. #include <nrfx_log.h>
  45. #if NRFX_CHECK(NRFX_POWER_ENABLED)
  46. extern bool nrfx_power_irq_enabled;
  47. #endif
  48. #define EVT_TO_STR(event) \
  49. (event == NRF_CLOCK_EVENT_HFCLKSTARTED ? "NRF_CLOCK_EVENT_HFCLKSTARTED" : \
  50. (event == NRF_CLOCK_EVENT_LFCLKSTARTED ? "NRF_CLOCK_EVENT_LFCLKSTARTED" : \
  51. (event == NRF_CLOCK_EVENT_DONE ? "NRF_CLOCK_EVENT_DONE" : \
  52. (event == NRF_CLOCK_EVENT_CTTO ? "NRF_CLOCK_EVENT_CTTO" : \
  53. "UNKNOWN EVENT"))))
  54. #if NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  55. #if (NRF_CLOCK_HAS_CALIBRATION == 0)
  56. #error "Calibration is not available in the SoC that is used."
  57. #endif
  58. #if (NRFX_CLOCK_CONFIG_LF_SRC != CLOCK_LFCLKSRC_SRC_RC)
  59. #error "Calibration can be performed only for the RC Oscillator."
  60. #endif
  61. #endif
  62. #if defined(NRF52810_XXAA) || \
  63. defined(NRF52832_XXAA) || defined(NRF52832_XXAB) || \
  64. defined(NRF52840_XXAA)
  65. // Enable workaround for nRF52 anomaly 192 (LFRC oscillator frequency is wrong
  66. // after calibration, exceeding 500 ppm).
  67. #define USE_WORKAROUND_FOR_ANOMALY_192
  68. // Enable workaround for nRF52 anomaly 201 (EVENTS_HFCLKSTARTED might be generated twice).
  69. #define USE_WORKAROUND_FOR_ANOMALY_201
  70. #endif
  71. #if NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  72. typedef enum
  73. {
  74. CAL_STATE_IDLE,
  75. CAL_STATE_CAL
  76. } nrfx_clock_cal_state_t;
  77. #endif
  78. /**@brief CLOCK control block. */
  79. typedef struct
  80. {
  81. nrfx_clock_event_handler_t event_handler;
  82. bool module_initialized; /*< Indicate the state of module */
  83. #if defined(USE_WORKAROUND_FOR_ANOMALY_201)
  84. bool hfclk_started; /*< Anomaly 201 workaround. */
  85. #endif
  86. #if NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  87. volatile nrfx_clock_cal_state_t cal_state;
  88. #endif
  89. } nrfx_clock_cb_t;
  90. static nrfx_clock_cb_t m_clock_cb;
  91. /**
  92. * This variable is used to check whether common POWER_CLOCK common interrupt
  93. * should be disabled or not if @ref nrfx_power tries to disable the interrupt.
  94. */
  95. #if NRFX_CHECK(NRFX_POWER_ENABLED)
  96. bool nrfx_clock_irq_enabled;
  97. #endif
  98. #if defined(NRF52832_XXAA) || defined(NRF52832_XXAB)
  99. // ANOMALY 132 - LFCLK needs to avoid frame from 66us to 138us after LFCLK stop. This solution
  100. // applies delay of 138us before starting LFCLK.
  101. #define ANOMALY_132_REQ_DELAY_US 138UL
  102. // nRF52832 is clocked with 64MHz.
  103. #define ANOMALY_132_NRF52832_FREQ_MHZ 64UL
  104. // Convert time to cycles.
  105. #define ANOMALY_132_DELAY_CYCLES (ANOMALY_132_REQ_DELAY_US * ANOMALY_132_NRF52832_FREQ_MHZ)
  106. /**
  107. * @brief Function for applying delay of 138us before starting LFCLK.
  108. */
  109. static void nrfx_clock_anomaly_132(void)
  110. {
  111. uint32_t cyccnt_inital;
  112. uint32_t core_debug;
  113. uint32_t dwt_ctrl;
  114. // Preserve DEMCR register to do not influence into its configuration. Enable the trace and
  115. // debug blocks. It is required to read and write data to DWT block.
  116. core_debug = CoreDebug->DEMCR;
  117. CoreDebug->DEMCR = core_debug | CoreDebug_DEMCR_TRCENA_Msk;
  118. // Preserve CTRL register in DWT block to do not influence into its configuration. Make sure
  119. // that cycle counter is enabled.
  120. dwt_ctrl = DWT->CTRL;
  121. DWT->CTRL = dwt_ctrl | DWT_CTRL_CYCCNTENA_Msk;
  122. // Store start value of cycle counter.
  123. cyccnt_inital = DWT->CYCCNT;
  124. // Delay required time.
  125. while ((DWT->CYCCNT - cyccnt_inital) < ANOMALY_132_DELAY_CYCLES)
  126. {}
  127. // Restore preserved registers.
  128. DWT->CTRL = dwt_ctrl;
  129. CoreDebug->DEMCR = core_debug;
  130. }
  131. #endif // defined(NRF52832_XXAA) || defined(NRF52832_XXAB)
  132. nrfx_err_t nrfx_clock_init(nrfx_clock_event_handler_t event_handler)
  133. {
  134. NRFX_ASSERT(event_handler);
  135. nrfx_err_t err_code = NRFX_SUCCESS;
  136. if (m_clock_cb.module_initialized)
  137. {
  138. err_code = NRFX_ERROR_ALREADY_INITIALIZED;
  139. }
  140. else
  141. {
  142. #if NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  143. m_clock_cb.cal_state = CAL_STATE_IDLE;
  144. #endif
  145. m_clock_cb.event_handler = event_handler;
  146. m_clock_cb.module_initialized = true;
  147. #if defined(USE_WORKAROUND_FOR_ANOMALY_201)
  148. m_clock_cb.hfclk_started = false;
  149. #endif
  150. }
  151. NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
  152. return err_code;
  153. }
  154. void nrfx_clock_enable(void)
  155. {
  156. NRFX_ASSERT(m_clock_cb.module_initialized);
  157. nrfx_power_clock_irq_init();
  158. nrf_clock_lf_src_set((nrf_clock_lfclk_t)NRFX_CLOCK_CONFIG_LF_SRC);
  159. #if NRFX_CHECK(NRFX_POWER_ENABLED)
  160. nrfx_clock_irq_enabled = true;
  161. #endif
  162. NRFX_LOG_INFO("Module enabled.");
  163. }
  164. void nrfx_clock_disable(void)
  165. {
  166. NRFX_ASSERT(m_clock_cb.module_initialized);
  167. #if NRFX_CHECK(NRFX_POWER_ENABLED)
  168. NRFX_ASSERT(nrfx_clock_irq_enabled);
  169. if (!nrfx_power_irq_enabled)
  170. #endif
  171. {
  172. NRFX_IRQ_DISABLE(nrfx_get_irq_number(NRF_CLOCK));
  173. }
  174. nrf_clock_int_disable(CLOCK_INTENSET_HFCLKSTARTED_Msk |
  175. CLOCK_INTENSET_LFCLKSTARTED_Msk |
  176. #if NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  177. CLOCK_INTENSET_DONE_Msk |
  178. CLOCK_INTENSET_CTTO_Msk |
  179. #endif
  180. 0);
  181. #if NRFX_CHECK(NRFX_POWER_ENABLED)
  182. nrfx_clock_irq_enabled = false;
  183. #endif
  184. NRFX_LOG_INFO("Module disabled.");
  185. }
  186. void nrfx_clock_uninit(void)
  187. {
  188. NRFX_ASSERT(m_clock_cb.module_initialized);
  189. nrfx_clock_lfclk_stop();
  190. nrfx_clock_hfclk_stop();
  191. m_clock_cb.module_initialized = false;
  192. NRFX_LOG_INFO("Uninitialized.");
  193. }
  194. void nrfx_clock_lfclk_start(void)
  195. {
  196. NRFX_ASSERT(m_clock_cb.module_initialized);
  197. nrf_clock_event_clear(NRF_CLOCK_EVENT_LFCLKSTARTED);
  198. nrf_clock_int_enable(NRF_CLOCK_INT_LF_STARTED_MASK);
  199. #if defined(NRF52832_XXAA) || defined(NRF52832_XXAB)
  200. nrfx_clock_anomaly_132();
  201. #endif
  202. nrf_clock_task_trigger(NRF_CLOCK_TASK_LFCLKSTART);
  203. }
  204. void nrfx_clock_lfclk_stop(void)
  205. {
  206. NRFX_ASSERT(m_clock_cb.module_initialized);
  207. nrf_clock_task_trigger(NRF_CLOCK_TASK_LFCLKSTOP);
  208. while (nrf_clock_lf_is_running())
  209. {}
  210. }
  211. void nrfx_clock_hfclk_start(void)
  212. {
  213. NRFX_ASSERT(m_clock_cb.module_initialized);
  214. nrf_clock_event_clear(NRF_CLOCK_EVENT_HFCLKSTARTED);
  215. nrf_clock_int_enable(NRF_CLOCK_INT_HF_STARTED_MASK);
  216. nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTART);
  217. }
  218. void nrfx_clock_hfclk_stop(void)
  219. {
  220. NRFX_ASSERT(m_clock_cb.module_initialized);
  221. nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTOP);
  222. while (nrf_clock_hf_is_running(NRF_CLOCK_HFCLK_HIGH_ACCURACY))
  223. {}
  224. #if defined(USE_WORKAROUND_FOR_ANOMALY_201)
  225. m_clock_cb.hfclk_started = false;
  226. #endif
  227. }
  228. nrfx_err_t nrfx_clock_calibration_start(void)
  229. {
  230. nrfx_err_t err_code = NRFX_SUCCESS;
  231. #if NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  232. if (nrfx_clock_hfclk_is_running() == false)
  233. {
  234. return NRFX_ERROR_INVALID_STATE;
  235. }
  236. if (nrfx_clock_lfclk_is_running() == false)
  237. {
  238. return NRFX_ERROR_INVALID_STATE;
  239. }
  240. if (m_clock_cb.cal_state == CAL_STATE_IDLE)
  241. {
  242. nrf_clock_event_clear(NRF_CLOCK_EVENT_DONE);
  243. nrf_clock_int_enable(NRF_CLOCK_INT_DONE_MASK);
  244. m_clock_cb.cal_state = CAL_STATE_CAL;
  245. #if defined(USE_WORKAROUND_FOR_ANOMALY_192)
  246. *(volatile uint32_t *)0x40000C34 = 0x00000002;
  247. #endif
  248. nrf_clock_task_trigger(NRF_CLOCK_TASK_CAL);
  249. }
  250. else
  251. {
  252. err_code = NRFX_ERROR_BUSY;
  253. }
  254. #endif // NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  255. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  256. __func__,
  257. NRFX_LOG_ERROR_STRING_GET(err_code));
  258. return err_code;
  259. }
  260. nrfx_err_t nrfx_clock_is_calibrating(void)
  261. {
  262. #if NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  263. if (m_clock_cb.cal_state == CAL_STATE_CAL)
  264. {
  265. return NRFX_ERROR_BUSY;
  266. }
  267. #endif
  268. return NRFX_SUCCESS;
  269. }
  270. void nrfx_clock_calibration_timer_start(uint8_t interval)
  271. {
  272. #if NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  273. nrf_clock_cal_timer_timeout_set(interval);
  274. nrf_clock_event_clear(NRF_CLOCK_EVENT_CTTO);
  275. nrf_clock_int_enable(NRF_CLOCK_INT_CTTO_MASK);
  276. nrf_clock_task_trigger(NRF_CLOCK_TASK_CTSTART);
  277. #endif
  278. }
  279. void nrfx_clock_calibration_timer_stop(void)
  280. {
  281. #if NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  282. nrf_clock_int_disable(NRF_CLOCK_INT_CTTO_MASK);
  283. nrf_clock_task_trigger(NRF_CLOCK_TASK_CTSTOP);
  284. #endif
  285. }
  286. void nrfx_clock_irq_handler(void)
  287. {
  288. if (nrf_clock_event_check(NRF_CLOCK_EVENT_HFCLKSTARTED))
  289. {
  290. nrf_clock_event_clear(NRF_CLOCK_EVENT_HFCLKSTARTED);
  291. NRFX_LOG_DEBUG("Event: %s.", EVT_TO_STR(NRF_CLOCK_EVENT_HFCLKSTARTED));
  292. nrf_clock_int_disable(NRF_CLOCK_INT_HF_STARTED_MASK);
  293. #if defined(USE_WORKAROUND_FOR_ANOMALY_201)
  294. if (!m_clock_cb.hfclk_started)
  295. {
  296. m_clock_cb.hfclk_started = true;
  297. m_clock_cb.event_handler(NRFX_CLOCK_EVT_HFCLK_STARTED);
  298. }
  299. #else
  300. m_clock_cb.event_handler(NRFX_CLOCK_EVT_HFCLK_STARTED);
  301. #endif
  302. }
  303. if (nrf_clock_event_check(NRF_CLOCK_EVENT_LFCLKSTARTED))
  304. {
  305. nrf_clock_event_clear(NRF_CLOCK_EVENT_LFCLKSTARTED);
  306. NRFX_LOG_DEBUG("Event: %s.", EVT_TO_STR(NRF_CLOCK_EVENT_LFCLKSTARTED));
  307. nrf_clock_int_disable(NRF_CLOCK_INT_LF_STARTED_MASK);
  308. m_clock_cb.event_handler(NRFX_CLOCK_EVT_LFCLK_STARTED);
  309. }
  310. #if NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  311. if (nrf_clock_event_check(NRF_CLOCK_EVENT_CTTO))
  312. {
  313. nrf_clock_event_clear(NRF_CLOCK_EVENT_CTTO);
  314. NRFX_LOG_DEBUG("Event: %s.", EVT_TO_STR(NRF_CLOCK_EVENT_CTTO));
  315. nrf_clock_int_disable(NRF_CLOCK_INT_CTTO_MASK);
  316. m_clock_cb.event_handler(NRFX_CLOCK_EVT_CTTO);
  317. }
  318. if (nrf_clock_event_check(NRF_CLOCK_EVENT_DONE))
  319. {
  320. #if defined(USE_WORKAROUND_FOR_ANOMALY_192)
  321. *(volatile uint32_t *)0x40000C34 = 0x00000000;
  322. #endif
  323. nrf_clock_event_clear(NRF_CLOCK_EVENT_DONE);
  324. NRFX_LOG_DEBUG("Event: %s.", EVT_TO_STR(NRF_CLOCK_EVENT_DONE));
  325. nrf_clock_int_disable(NRF_CLOCK_INT_DONE_MASK);
  326. m_clock_cb.cal_state = CAL_STATE_IDLE;
  327. m_clock_cb.event_handler(NRFX_CLOCK_EVT_CAL_DONE);
  328. }
  329. #endif // NRFX_CHECK(NRFX_CLOCK_CONFIG_LF_CAL_ENABLED)
  330. }
  331. #endif // NRFX_CHECK(NRFX_CLOCK_ENABLED)