rtc_time.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <assert.h>
  8. #include "esp32p4/rom/ets_sys.h"
  9. #include "soc/rtc.h"
  10. #include "soc/lp_timer_reg.h"
  11. #include "hal/lp_timer_hal.h"
  12. #include "hal/clk_tree_ll.h"
  13. #include "hal/timer_ll.h"
  14. #include "soc/timer_group_reg.h"
  15. #include "esp_rom_sys.h"
  16. #include "esp_private/periph_ctrl.h"
  17. static const char *TAG = "rtc_time";
  18. /* Calibration of RTC_SLOW_CLK is performed using a special feature of TIMG0.
  19. * This feature counts the number of XTAL clock cycles within a given number of
  20. * RTC_SLOW_CLK cycles.
  21. *
  22. * Slow clock calibration feature has two modes of operation: one-off and cycling.
  23. * In cycling mode (which is enabled by default on SoC reset), counting of XTAL
  24. * cycles within RTC_SLOW_CLK cycle is done continuously. Cycling mode is enabled
  25. * using TIMG_RTC_CALI_START_CYCLING bit. In one-off mode counting is performed
  26. * once, and TIMG_RTC_CALI_RDY bit is set when counting is done. One-off mode is
  27. * enabled using TIMG_RTC_CALI_START bit.
  28. */
  29. /* On ESP32P4, TIMG_RTC_CALI_CLK_SEL can config to 0, 1, 2, 3
  30. * 0 or 3: calibrate RC_SLOW clock
  31. * 1: calibrate RC_FAST clock
  32. * 2: calibrate 32K clock, which 32k depends on reg_32k_sel: 0: Internal 32 kHz RC oscillator, 1: External 32 kHz XTAL, 2: External 32kHz clock input by lp_pad_gpio0
  33. */
  34. #define TIMG_RTC_CALI_CLK_SEL_RC_SLOW 0
  35. #define TIMG_RTC_CALI_CLK_SEL_RC_FAST 1
  36. #define TIMG_RTC_CALI_CLK_SEL_32K 2
  37. uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
  38. {
  39. assert(slowclk_cycles < TIMG_RTC_CALI_MAX_V);
  40. uint32_t cali_clk_sel = 0;
  41. soc_rtc_slow_clk_src_t slow_clk_src = rtc_clk_slow_src_get();
  42. soc_rtc_slow_clk_src_t old_32k_cal_clk_sel = clk_ll_32k_calibration_get_target();
  43. if (cal_clk == RTC_CAL_RTC_MUX) {
  44. cal_clk = (rtc_cal_sel_t)slow_clk_src;
  45. }
  46. if (cal_clk == RTC_CAL_RC_FAST) {
  47. cali_clk_sel = TIMG_RTC_CALI_CLK_SEL_RC_FAST;
  48. } else if (cal_clk == RTC_CAL_RC_SLOW) {
  49. cali_clk_sel = TIMG_RTC_CALI_CLK_SEL_RC_SLOW;
  50. } else {
  51. cali_clk_sel = TIMG_RTC_CALI_CLK_SEL_32K;
  52. clk_ll_32k_calibration_set_target((soc_rtc_slow_clk_src_t)cal_clk);
  53. }
  54. /* Enable requested clock (150k clock is always on) */
  55. // All clocks on/off takes time to be stable, so we shouldn't frequently enable/disable the clock
  56. // Only enable if orignally was disabled, and set back to the disable state after calibration is done
  57. // If the clock is already on, then do nothing
  58. bool dig_32k_xtal_enabled = clk_ll_xtal32k_digi_is_enabled();
  59. if (cal_clk == RTC_CAL_32K_XTAL && !dig_32k_xtal_enabled) {
  60. clk_ll_xtal32k_digi_enable();
  61. }
  62. bool rc_fast_enabled = clk_ll_rc_fast_is_enabled();
  63. bool dig_rc_fast_enabled = clk_ll_rc_fast_digi_is_enabled();
  64. if (cal_clk == RTC_CAL_RC_FAST) {
  65. if (!rc_fast_enabled) {
  66. rtc_clk_8m_enable(true);
  67. }
  68. if (!dig_rc_fast_enabled) {
  69. rtc_dig_clk8m_enable();
  70. }
  71. }
  72. bool rc32k_enabled = clk_ll_rc32k_is_enabled();
  73. bool dig_rc32k_enabled = clk_ll_rc32k_digi_is_enabled();
  74. if (cal_clk == RTC_CAL_RC32K) {
  75. if (!rc32k_enabled) {
  76. rtc_clk_rc32k_enable(true);
  77. }
  78. if (!dig_rc32k_enabled) {
  79. clk_ll_rc32k_digi_enable();
  80. }
  81. }
  82. /* There may be another calibration process already running during we call this function,
  83. * so we should wait the last process is done.
  84. */
  85. if (GET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START_CYCLING)) {
  86. /**
  87. * Set a small timeout threshold to accelerate the generation of timeout.
  88. * The internal circuit will be reset when the timeout occurs and will not affect the next calibration.
  89. */
  90. REG_SET_FIELD(TIMG_RTCCALICFG2_REG(0), TIMG_RTC_CALI_TIMEOUT_THRES, 1);
  91. while (!GET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY)
  92. && !GET_PERI_REG_MASK(TIMG_RTCCALICFG2_REG(0), TIMG_RTC_CALI_TIMEOUT));
  93. }
  94. /* Prepare calibration */
  95. REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_CLK_SEL, cali_clk_sel);
  96. CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START_CYCLING);
  97. REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_MAX, slowclk_cycles);
  98. /* Figure out how long to wait for calibration to finish */
  99. /* Set timeout reg and expect time delay*/
  100. uint32_t expected_freq;
  101. if (cali_clk_sel == TIMG_RTC_CALI_CLK_SEL_32K) {
  102. REG_SET_FIELD(TIMG_RTCCALICFG2_REG(0), TIMG_RTC_CALI_TIMEOUT_THRES, RTC_SLOW_CLK_32K_CAL_TIMEOUT_THRES(slowclk_cycles));
  103. expected_freq = SOC_CLK_XTAL32K_FREQ_APPROX;
  104. } else if (cali_clk_sel == TIMG_RTC_CALI_CLK_SEL_RC_FAST) {
  105. REG_SET_FIELD(TIMG_RTCCALICFG2_REG(0), TIMG_RTC_CALI_TIMEOUT_THRES, RTC_FAST_CLK_20M_CAL_TIMEOUT_THRES(slowclk_cycles));
  106. expected_freq = SOC_CLK_RC_FAST_FREQ_APPROX;
  107. } else {
  108. REG_SET_FIELD(TIMG_RTCCALICFG2_REG(0), TIMG_RTC_CALI_TIMEOUT_THRES, RTC_SLOW_CLK_150K_CAL_TIMEOUT_THRES(slowclk_cycles));
  109. expected_freq = SOC_CLK_RC_SLOW_FREQ_APPROX;
  110. }
  111. uint32_t us_time_estimate = (uint32_t) (((uint64_t) slowclk_cycles) * MHZ / expected_freq);
  112. /* Start calibration */
  113. CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
  114. SET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
  115. /* Wait for calibration to finish up to another us_time_estimate */
  116. esp_rom_delay_us(us_time_estimate);
  117. uint32_t cal_val;
  118. while (true) {
  119. if (GET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY)) {
  120. cal_val = REG_GET_FIELD(TIMG_RTCCALICFG1_REG(0), TIMG_RTC_CALI_VALUE);
  121. break;
  122. }
  123. if (GET_PERI_REG_MASK(TIMG_RTCCALICFG2_REG(0), TIMG_RTC_CALI_TIMEOUT)) {
  124. cal_val = 0;
  125. break;
  126. }
  127. }
  128. CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
  129. /* if dig_32k_xtal was originally off and enabled due to calibration, then set back to off state */
  130. if (cal_clk == RTC_CAL_32K_XTAL && !dig_32k_xtal_enabled) {
  131. clk_ll_xtal32k_digi_disable();
  132. }
  133. if (cal_clk == RTC_CAL_RC_FAST) {
  134. if (!dig_rc_fast_enabled) {
  135. rtc_dig_clk8m_disable();
  136. }
  137. if (!rc_fast_enabled) {
  138. rtc_clk_8m_enable(false);
  139. }
  140. }
  141. if (cal_clk == RTC_CAL_RC32K) {
  142. if (!dig_rc32k_enabled) {
  143. clk_ll_rc32k_digi_disable();
  144. }
  145. if (!rc32k_enabled) {
  146. rtc_clk_rc32k_enable(false);
  147. }
  148. }
  149. // Always set back the calibration 32kHz clock selection
  150. if (old_32k_cal_clk_sel != SOC_RTC_SLOW_CLK_SRC_INVALID) {
  151. clk_ll_32k_calibration_set_target(old_32k_cal_clk_sel);
  152. }
  153. return cal_val;
  154. }
  155. static bool rtc_clk_cal_32k_valid(rtc_xtal_freq_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
  156. {
  157. uint64_t expected_xtal_cycles = (xtal_freq * 1000000ULL * slowclk_cycles) >> 15; // xtal_freq(hz) * slowclk_cycles / 32768
  158. uint64_t delta = expected_xtal_cycles / 2000; // 5/10000 = 0.05% error range
  159. return (actual_xtal_cycles >= (expected_xtal_cycles - delta)) && (actual_xtal_cycles <= (expected_xtal_cycles + delta));
  160. }
  161. uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
  162. {
  163. rtc_xtal_freq_t xtal_freq = rtc_clk_xtal_freq_get();
  164. uint64_t xtal_cycles = rtc_clk_cal_internal(cal_clk, slowclk_cycles);
  165. if (cal_clk == RTC_CAL_32K_XTAL && !rtc_clk_cal_32k_valid(xtal_freq, slowclk_cycles, xtal_cycles)) {
  166. return 0;
  167. }
  168. uint64_t divider = ((uint64_t)xtal_freq) * slowclk_cycles;
  169. uint64_t period_64 = ((xtal_cycles << RTC_CLK_CAL_FRACT) + divider / 2 - 1) / divider;
  170. uint32_t period = (uint32_t)(period_64 & UINT32_MAX);
  171. return period;
  172. }
  173. uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
  174. {
  175. /* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
  176. * TODO: fix overflow.
  177. */
  178. return (time_in_us << RTC_CLK_CAL_FRACT) / period;
  179. }
  180. uint64_t rtc_time_slowclk_to_us(uint64_t rtc_cycles, uint32_t period)
  181. {
  182. return (rtc_cycles * period) >> RTC_CLK_CAL_FRACT;
  183. }
  184. uint64_t rtc_time_get(void)
  185. {
  186. // return lp_timer_hal_get_cycle_count(0);
  187. ESP_EARLY_LOGE(TAG, "rtc_time_get has not been implemented yet");
  188. return 0;
  189. }
  190. void rtc_clk_wait_for_slow_cycle(void) //This function may not by useful any more
  191. {
  192. // TODO: IDF-5781
  193. ESP_EARLY_LOGW(TAG, "rtc_clk_wait_for_slow_cycle() has not been implemented yet");
  194. }
  195. uint32_t rtc_clk_freq_cal(uint32_t cal_val)
  196. {
  197. if (cal_val == 0) {
  198. return 0; // cal_val will be denominator, return 0 as the symbol of failure.
  199. }
  200. return 1000000ULL * (1 << RTC_CLK_CAL_FRACT) / cal_val;
  201. }
  202. /// @brief if the calibration is used, we need to enable the timer group0 first
  203. __attribute__((constructor))
  204. static void enable_timer_group0_for_calibration(void)
  205. {
  206. PERIPH_RCC_ACQUIRE_ATOMIC(PERIPH_TIMG0_MODULE, ref_count) {
  207. if (ref_count == 0) {
  208. timer_ll_enable_bus_clock(0, true);
  209. timer_ll_reset_register(0);
  210. }
  211. }
  212. }