rtc_time.c 7.0 KB

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