rtc_time.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #define MHZ (1000000)
  20. /* Calibration of RTC_SLOW_CLK is performed using a special feature of TIMG0.
  21. * This feature counts the number of XTAL clock cycles within a given number of
  22. * RTC_SLOW_CLK cycles.
  23. *
  24. * Slow clock calibration feature has two modes of operation: one-off and cycling.
  25. * In cycling mode (which is enabled by default on SoC reset), counting of XTAL
  26. * cycles within RTC_SLOW_CLK cycle is done continuously. Cycling mode is enabled
  27. * using TIMG_RTC_CALI_START_CYCLING bit. In one-off mode counting is performed
  28. * once, and TIMG_RTC_CALI_RDY bit is set when counting is done. One-off mode is
  29. * enabled using TIMG_RTC_CALI_START bit.
  30. */
  31. uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
  32. {
  33. rtc_xtal_freq_t xtal_freq = rtc_clk_xtal_freq_get();
  34. /* Enable requested clock (150k clock is always on) */
  35. if (cal_clk == RTC_CAL_32K_XTAL) {
  36. SET_PERI_REG_MASK(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_DIG_XTAL32K_EN);
  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 = 8 * MHZ / 256;
  54. } else {
  55. expected_freq = 150000; /* 150k internal oscillator */
  56. }
  57. uint32_t us_time_estimate = slowclk_cycles * MHZ / expected_freq;
  58. /* Start calibration */
  59. CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
  60. SET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
  61. /* Wait the expected time calibration should take.
  62. * TODO: if running under RTOS, and us_time_estimate > RTOS tick, use the
  63. * RTOS delay function.
  64. */
  65. ets_delay_us(us_time_estimate);
  66. /* Wait for calibration to finish up to another us_time_estimate */
  67. int timeout_us = us_time_estimate;
  68. while (!GET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY) &&
  69. timeout_us-- > 0) {
  70. ets_delay_us(1);
  71. }
  72. if (cal_clk == RTC_CAL_32K_XTAL) {
  73. CLEAR_PERI_REG_MASK(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_DIG_XTAL32K_EN);
  74. }
  75. if (cal_clk == RTC_CAL_8MD256) {
  76. CLEAR_PERI_REG_MASK(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_DIG_CLK8M_D256_EN);
  77. }
  78. if (timeout_us == 0) {
  79. /* timed out waiting for calibration */
  80. return 0;
  81. }
  82. uint64_t xtal_cycles = REG_GET_FIELD(TIMG_RTCCALICFG1_REG(0), TIMG_RTC_CALI_VALUE);
  83. uint64_t divider = ((uint64_t)xtal_freq) * slowclk_cycles;
  84. uint64_t period_64 = (xtal_cycles << RTC_CLK_CAL_FRACT) / divider;
  85. uint32_t period = (uint32_t)(period_64 & UINT32_MAX);
  86. return period;
  87. }
  88. uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
  89. {
  90. /* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
  91. * TODO: fix overflow.
  92. */
  93. return (time_in_us << RTC_CLK_CAL_FRACT) / period;
  94. }
  95. uint64_t rtc_time_slowclk_to_us(uint64_t rtc_cycles, uint32_t period)
  96. {
  97. return (rtc_cycles * period) >> RTC_CLK_CAL_FRACT;
  98. }
  99. uint64_t rtc_time_get()
  100. {
  101. SET_PERI_REG_MASK(RTC_CNTL_TIME_UPDATE_REG, RTC_CNTL_TIME_UPDATE);
  102. while (GET_PERI_REG_MASK(RTC_CNTL_TIME_UPDATE_REG, RTC_CNTL_TIME_VALID) == 0) {
  103. ets_delay_us(1); // might take 1 RTC slowclk period, don't flood RTC bus
  104. }
  105. SET_PERI_REG_MASK(RTC_CNTL_INT_CLR_REG, RTC_CNTL_TIME_VALID_INT_CLR);
  106. uint64_t t = READ_PERI_REG(RTC_CNTL_TIME0_REG);
  107. t |= ((uint64_t) READ_PERI_REG(RTC_CNTL_TIME1_REG)) << 32;
  108. return t;
  109. }