rtc_time.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 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 "soc/rtc.h"
  9. #include "soc/timer_periph.h"
  10. #include "soc_log.h"
  11. #define MHZ (1000000)
  12. static const char* TAG = "rtc_time";
  13. /* Calibration of RTC_SLOW_CLK is performed using a special feature of TIMG0.
  14. * This feature counts the number of XTAL clock cycles within a given number of
  15. * RTC_SLOW_CLK cycles.
  16. *
  17. * Slow clock calibration feature has two modes of operation: one-off and cycling.
  18. * In cycling mode (which is enabled by default on SoC reset), counting of XTAL
  19. * cycles within RTC_SLOW_CLK cycle is done continuously. Cycling mode is enabled
  20. * using TIMG_RTC_CALI_START_CYCLING bit. In one-off mode counting is performed
  21. * once, and TIMG_RTC_CALI_RDY bit is set when counting is done. One-off mode is
  22. * enabled using TIMG_RTC_CALI_START bit.
  23. */
  24. /**
  25. * @brief Clock calibration function used by rtc_clk_cal and rtc_clk_cal_ratio
  26. * @param cal_clk which clock to calibrate
  27. * @param slowclk_cycles number of slow clock cycles to count. Max value is 32766.
  28. * @return number of XTAL clock cycles within the given number of slow clock cycles
  29. */
  30. static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
  31. {
  32. assert(slowclk_cycles < 32767);
  33. /* Enable requested clock (150k clock is always on) */
  34. int dig_32k_xtal_state = REG_GET_FIELD(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_DIG_XTAL32K_EN);
  35. if (cal_clk == RTC_CAL_32K_XTAL && !dig_32k_xtal_state) {
  36. REG_SET_FIELD(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_DIG_XTAL32K_EN, 1);
  37. }
  38. if (cal_clk == RTC_CAL_8MD256) {
  39. SET_PERI_REG_MASK(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_DIG_CLK8M_D256_EN);
  40. }
  41. /* Prepare calibration */
  42. REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_CLK_SEL, cal_clk);
  43. CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START_CYCLING);
  44. REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_MAX, slowclk_cycles);
  45. /* Figure out how long to wait for calibration to finish */
  46. uint32_t expected_freq;
  47. rtc_slow_freq_t slow_freq = REG_GET_FIELD(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_ANA_CLK_RTC_SEL);
  48. if (cal_clk == RTC_CAL_32K_XTAL ||
  49. (cal_clk == RTC_CAL_RTC_MUX && slow_freq == RTC_SLOW_FREQ_32K_XTAL)) {
  50. expected_freq = 32768; /* standard 32k XTAL */
  51. } else if (cal_clk == RTC_CAL_8MD256 ||
  52. (cal_clk == RTC_CAL_RTC_MUX && slow_freq == RTC_SLOW_FREQ_8MD256)) {
  53. expected_freq = RTC_FAST_CLK_FREQ_APPROX / 256;
  54. } else {
  55. expected_freq = 150000; /* 150k internal oscillator */
  56. }
  57. uint32_t us_time_estimate = (uint32_t) (((uint64_t) slowclk_cycles) * MHZ / expected_freq);
  58. /* Check if the required number of slowclk_cycles may result in an overflow of TIMG_RTC_CALI_VALUE */
  59. rtc_xtal_freq_t xtal_freq = rtc_clk_xtal_freq_get();
  60. if (xtal_freq == RTC_XTAL_FREQ_AUTO) {
  61. /* XTAL frequency is not known yet; assume worst case (40 MHz) */
  62. xtal_freq = RTC_XTAL_FREQ_40M;
  63. }
  64. const uint32_t us_timer_max = TIMG_RTC_CALI_VALUE / (uint32_t) xtal_freq;
  65. if (us_time_estimate >= us_timer_max) {
  66. SOC_LOGE(TAG, "slowclk_cycles value too large, possible overflow");
  67. return 0;
  68. }
  69. /* Start calibration */
  70. CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
  71. SET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
  72. /* Wait the expected time calibration should take.
  73. * TODO: if running under RTOS, and us_time_estimate > RTOS tick, use the
  74. * RTOS delay function.
  75. */
  76. esp_rom_delay_us(us_time_estimate);
  77. /* Wait for calibration to finish up to another us_time_estimate */
  78. int timeout_us = us_time_estimate;
  79. while (!GET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY) &&
  80. timeout_us > 0) {
  81. timeout_us--;
  82. esp_rom_delay_us(1);
  83. }
  84. REG_SET_FIELD(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_DIG_XTAL32K_EN, dig_32k_xtal_state);
  85. if (cal_clk == RTC_CAL_8MD256) {
  86. CLEAR_PERI_REG_MASK(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_DIG_CLK8M_D256_EN);
  87. }
  88. if (timeout_us == 0) {
  89. /* timed out waiting for calibration */
  90. return 0;
  91. }
  92. return REG_GET_FIELD(TIMG_RTCCALICFG1_REG(0), TIMG_RTC_CALI_VALUE);
  93. }
  94. uint32_t rtc_clk_cal_ratio(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
  95. {
  96. uint64_t xtal_cycles = rtc_clk_cal_internal(cal_clk, slowclk_cycles);
  97. uint64_t ratio_64 = ((xtal_cycles << RTC_CLK_CAL_FRACT)) / slowclk_cycles;
  98. uint32_t ratio = (uint32_t)(ratio_64 & UINT32_MAX);
  99. return ratio;
  100. }
  101. uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
  102. {
  103. rtc_xtal_freq_t xtal_freq = rtc_clk_xtal_freq_get();
  104. uint64_t xtal_cycles = rtc_clk_cal_internal(cal_clk, slowclk_cycles);
  105. uint64_t divider = ((uint64_t)xtal_freq) * slowclk_cycles;
  106. uint64_t period_64 = ((xtal_cycles << RTC_CLK_CAL_FRACT) + divider / 2 - 1) / divider;
  107. uint32_t period = (uint32_t)(period_64 & UINT32_MAX);
  108. return period;
  109. }
  110. uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
  111. {
  112. /* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
  113. * TODO: fix overflow.
  114. */
  115. return (time_in_us << RTC_CLK_CAL_FRACT) / period;
  116. }
  117. uint64_t rtc_time_slowclk_to_us(uint64_t rtc_cycles, uint32_t period)
  118. {
  119. return (rtc_cycles * period) >> RTC_CLK_CAL_FRACT;
  120. }
  121. uint64_t rtc_time_get(void)
  122. {
  123. SET_PERI_REG_MASK(RTC_CNTL_TIME_UPDATE_REG, RTC_CNTL_TIME_UPDATE);
  124. while (GET_PERI_REG_MASK(RTC_CNTL_TIME_UPDATE_REG, RTC_CNTL_TIME_VALID) == 0) {
  125. esp_rom_delay_us(1); // might take 1 RTC slowclk period, don't flood RTC bus
  126. }
  127. SET_PERI_REG_MASK(RTC_CNTL_INT_CLR_REG, RTC_CNTL_TIME_VALID_INT_CLR);
  128. uint64_t t = READ_PERI_REG(RTC_CNTL_TIME0_REG);
  129. t |= ((uint64_t) READ_PERI_REG(RTC_CNTL_TIME1_REG)) << 32;
  130. return t;
  131. }
  132. void rtc_clk_wait_for_slow_cycle(void)
  133. {
  134. REG_CLR_BIT(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START_CYCLING | TIMG_RTC_CALI_START);
  135. REG_CLR_BIT(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY);
  136. REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_CLK_SEL, RTC_CAL_RTC_MUX);
  137. /* Request to run calibration for 0 slow clock cycles.
  138. * RDY bit will be set on the nearest slow clock cycle.
  139. */
  140. REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_MAX, 0);
  141. REG_SET_BIT(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
  142. esp_rom_delay_us(1); /* RDY needs some time to go low */
  143. while (!GET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY)) {
  144. esp_rom_delay_us(1);
  145. }
  146. }
  147. uint32_t rtc_clk_freq_cal(uint32_t cal_val)
  148. {
  149. if (cal_val == 0) {
  150. return 0; // cal_val will be denominator, return 0 as the symbol of failure.
  151. }
  152. return 1000000ULL * (1 << RTC_CLK_CAL_FRACT) / cal_val;
  153. }