rtc_time.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include "esp_rom_sys.h"
  8. #include "hal/clk_tree_ll.h"
  9. #include "hal/rtc_cntl_ll.h"
  10. #include "hal/timer_ll.h"
  11. #include "soc/rtc.h"
  12. #include "soc/timer_periph.h"
  13. #include "esp_hw_log.h"
  14. #include "esp_private/periph_ctrl.h"
  15. static const char *TAG = "rtc_time";
  16. /* Calibration of RTC_SLOW_CLK is performed using a special feature of TIMG0.
  17. * This feature counts the number of XTAL clock cycles within a given number of
  18. * RTC_SLOW_CLK cycles.
  19. *
  20. * Slow clock calibration feature has two modes of operation: one-off and cycling.
  21. * In cycling mode (which is enabled by default on SoC reset), counting of XTAL
  22. * cycles within RTC_SLOW_CLK cycle is done continuously. Cycling mode is enabled
  23. * using TIMG_RTC_CALI_START_CYCLING bit. In one-off mode counting is performed
  24. * once, and TIMG_RTC_CALI_RDY bit is set when counting is done. One-off mode is
  25. * enabled using TIMG_RTC_CALI_START bit.
  26. */
  27. /**
  28. * @brief Clock calibration function used by rtc_clk_cal and rtc_clk_cal_ratio
  29. * @param cal_clk which clock to calibrate
  30. * @param slowclk_cycles number of slow clock cycles to count. Max value is 32766.
  31. * @return number of XTAL clock cycles within the given number of slow clock cycles
  32. */
  33. static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
  34. {
  35. assert(slowclk_cycles < 32767);
  36. /* Enable requested clock (150k clock is always on) */
  37. bool dig_32k_xtal_enabled = clk_ll_xtal32k_digi_is_enabled();
  38. if (cal_clk == RTC_CAL_32K_XTAL && !dig_32k_xtal_enabled) {
  39. clk_ll_xtal32k_digi_enable();
  40. }
  41. bool rc_fast_enabled = clk_ll_rc_fast_is_enabled();
  42. bool rc_fast_d256_enabled = clk_ll_rc_fast_d256_is_enabled();
  43. if (cal_clk == RTC_CAL_8MD256) {
  44. rtc_clk_8m_enable(true, true);
  45. clk_ll_rc_fast_d256_digi_enable();
  46. }
  47. /* Prepare calibration */
  48. REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_CLK_SEL, cal_clk);
  49. CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START_CYCLING);
  50. REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_MAX, slowclk_cycles);
  51. /* Figure out how long to wait for calibration to finish */
  52. uint32_t expected_freq;
  53. soc_rtc_slow_clk_src_t slow_clk_src = rtc_clk_slow_src_get();
  54. if (cal_clk == RTC_CAL_32K_XTAL ||
  55. (cal_clk == RTC_CAL_RTC_MUX && slow_clk_src == SOC_RTC_SLOW_CLK_SRC_XTAL32K)) {
  56. expected_freq = SOC_CLK_XTAL32K_FREQ_APPROX; /* standard 32k XTAL */
  57. } else if (cal_clk == RTC_CAL_8MD256 ||
  58. (cal_clk == RTC_CAL_RTC_MUX && slow_clk_src == SOC_RTC_SLOW_CLK_SRC_RC_FAST_D256)) {
  59. expected_freq = SOC_CLK_RC_FAST_D256_FREQ_APPROX;
  60. } else {
  61. expected_freq = SOC_CLK_RC_SLOW_FREQ_APPROX; /* 150k internal oscillator */
  62. }
  63. uint32_t us_time_estimate = (uint32_t) (((uint64_t) slowclk_cycles) * MHZ / expected_freq);
  64. /* Check if the required number of slowclk_cycles may result in an overflow of TIMG_RTC_CALI_VALUE */
  65. rtc_xtal_freq_t xtal_freq = rtc_clk_xtal_freq_get();
  66. if (xtal_freq == RTC_XTAL_FREQ_AUTO) {
  67. /* XTAL frequency is not known yet; assume worst case (40 MHz) */
  68. xtal_freq = RTC_XTAL_FREQ_40M;
  69. }
  70. const uint32_t us_timer_max = TIMG_RTC_CALI_VALUE / (uint32_t) xtal_freq;
  71. if (us_time_estimate >= us_timer_max) {
  72. ESP_HW_LOGE(TAG, "slowclk_cycles value too large, possible overflow");
  73. return 0;
  74. }
  75. /* Start calibration */
  76. CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
  77. SET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
  78. /* Wait the expected time calibration should take.
  79. * TODO: if running under RTOS, and us_time_estimate > RTOS tick, use the
  80. * RTOS delay function.
  81. */
  82. esp_rom_delay_us(us_time_estimate);
  83. /* Wait for calibration to finish up to another us_time_estimate */
  84. int timeout_us = us_time_estimate;
  85. while (!GET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY) &&
  86. timeout_us > 0) {
  87. timeout_us--;
  88. esp_rom_delay_us(1);
  89. }
  90. /* if dig_32k_xtal was originally off and enabled due to calibration, then set back to off state */
  91. if (cal_clk == RTC_CAL_32K_XTAL && !dig_32k_xtal_enabled) {
  92. clk_ll_xtal32k_digi_disable();
  93. }
  94. if (cal_clk == RTC_CAL_8MD256) {
  95. clk_ll_rc_fast_d256_digi_disable();
  96. rtc_clk_8m_enable(rc_fast_enabled, rc_fast_d256_enabled);
  97. }
  98. if (timeout_us == 0) {
  99. /* timed out waiting for calibration */
  100. return 0;
  101. }
  102. return REG_GET_FIELD(TIMG_RTCCALICFG1_REG(0), TIMG_RTC_CALI_VALUE);
  103. }
  104. uint32_t rtc_clk_cal_ratio(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
  105. {
  106. uint64_t xtal_cycles = rtc_clk_cal_internal(cal_clk, slowclk_cycles);
  107. uint64_t ratio_64 = ((xtal_cycles << RTC_CLK_CAL_FRACT)) / slowclk_cycles;
  108. uint32_t ratio = (uint32_t)(ratio_64 & UINT32_MAX);
  109. return ratio;
  110. }
  111. static inline bool rtc_clk_cal_32k_valid(rtc_xtal_freq_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
  112. {
  113. uint64_t expected_xtal_cycles = (xtal_freq * 1000000ULL * slowclk_cycles) >> 15; // xtal_freq(hz) * slowclk_cycles / 32768
  114. uint64_t delta = expected_xtal_cycles / 2000; // 5/10000
  115. return (actual_xtal_cycles >= (expected_xtal_cycles - delta)) && (actual_xtal_cycles <= (expected_xtal_cycles + delta));
  116. }
  117. uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
  118. {
  119. rtc_xtal_freq_t xtal_freq = rtc_clk_xtal_freq_get();
  120. uint64_t xtal_cycles = rtc_clk_cal_internal(cal_clk, slowclk_cycles);
  121. if ((cal_clk == RTC_CAL_32K_XTAL) && !rtc_clk_cal_32k_valid(xtal_freq, slowclk_cycles, xtal_cycles)) {
  122. return 0;
  123. }
  124. uint64_t divider = ((uint64_t)xtal_freq) * slowclk_cycles;
  125. uint64_t period_64 = ((xtal_cycles << RTC_CLK_CAL_FRACT) + divider / 2 - 1) / divider;
  126. uint32_t period = (uint32_t)(period_64 & UINT32_MAX);
  127. return period;
  128. }
  129. uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
  130. {
  131. /* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
  132. * TODO: fix overflow.
  133. */
  134. return (time_in_us << RTC_CLK_CAL_FRACT) / period;
  135. }
  136. uint64_t rtc_time_slowclk_to_us(uint64_t rtc_cycles, uint32_t period)
  137. {
  138. return (rtc_cycles * period) >> RTC_CLK_CAL_FRACT;
  139. }
  140. uint64_t rtc_time_get(void)
  141. {
  142. return rtc_cntl_ll_get_rtc_time();
  143. }
  144. void rtc_clk_wait_for_slow_cycle(void)
  145. {
  146. REG_CLR_BIT(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START_CYCLING | TIMG_RTC_CALI_START);
  147. REG_CLR_BIT(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY);
  148. REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_CLK_SEL, RTC_CAL_RTC_MUX);
  149. /* Request to run calibration for 0 slow clock cycles.
  150. * RDY bit will be set on the nearest slow clock cycle.
  151. */
  152. REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_MAX, 0);
  153. REG_SET_BIT(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
  154. esp_rom_delay_us(1); /* RDY needs some time to go low */
  155. int attempts = 1000;
  156. while (!GET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY)) {
  157. esp_rom_delay_us(1);
  158. if (attempts) {
  159. if (--attempts == 0 && clk_ll_xtal32k_digi_is_enabled()) {
  160. ESP_HW_LOGE(TAG, "32kHz xtal has been stopped");
  161. }
  162. }
  163. }
  164. }
  165. uint32_t rtc_clk_freq_cal(uint32_t cal_val)
  166. {
  167. if (cal_val == 0) {
  168. return 0; // cal_val will be denominator, return 0 as the symbol of failure.
  169. }
  170. return 1000000ULL * (1 << RTC_CLK_CAL_FRACT) / cal_val;
  171. }
  172. /// @brief if the calibration is used, we need to enable the timer group0 first
  173. __attribute__((constructor))
  174. static void enable_timer_group0_for_calibration(void)
  175. {
  176. #ifndef BOOTLOADER_BUILD
  177. PERIPH_RCC_ACQUIRE_ATOMIC(PERIPH_TIMG0_MODULE, ref_count) {
  178. if (ref_count == 0) {
  179. timer_ll_enable_bus_clock(0, true);
  180. timer_ll_reset_register(0);
  181. }
  182. }
  183. #else
  184. // no critical section is needed for bootloader
  185. int __DECLARE_RCC_RC_ATOMIC_ENV;
  186. timer_ll_enable_bus_clock(0, true);
  187. timer_ll_reset_register(0);
  188. #endif
  189. }