esp_clk.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <sys/param.h>
  8. #include <sys/lock.h>
  9. #include "esp_attr.h"
  10. #include "soc/rtc.h"
  11. #if CONFIG_IDF_TARGET_ESP32
  12. #include "esp32/rom/rtc.h"
  13. #include "esp32/clk.h"
  14. #include "esp32/rtc.h"
  15. #elif CONFIG_IDF_TARGET_ESP32S2
  16. #include "esp32s2/rom/rtc.h"
  17. #include "esp32s2/clk.h"
  18. #include "esp32s2/rtc.h"
  19. #elif CONFIG_IDF_TARGET_ESP32S3
  20. #include "esp32s3/rom/rtc.h"
  21. #include "esp32s3/clk.h"
  22. #include "esp32s3/rtc.h"
  23. #include "esp32s3/rom/ets_sys.h"
  24. #elif CONFIG_IDF_TARGET_ESP32C3
  25. #include "esp32c3/rom/rtc.h"
  26. #include "esp32c3/clk.h"
  27. #include "esp32c3/rtc.h"
  28. #elif CONFIG_IDF_TARGET_ESP32H2
  29. #include "esp32h2/rom/rtc.h"
  30. #include "esp32h2/clk.h"
  31. #include "esp32h2/rtc.h"
  32. #endif
  33. #define MHZ (1000000)
  34. // g_ticks_us defined in ROMs for PRO and APP CPU
  35. extern uint32_t g_ticks_per_us_pro;
  36. #if CONFIG_IDF_TARGET_ESP32
  37. #ifndef CONFIG_FREERTOS_UNICORE
  38. extern uint32_t g_ticks_per_us_app;
  39. #endif
  40. #endif
  41. static _lock_t s_esp_rtc_time_lock;
  42. static RTC_DATA_ATTR uint64_t s_esp_rtc_time_us = 0, s_rtc_last_ticks = 0;
  43. inline static int IRAM_ATTR s_get_cpu_freq_mhz(void)
  44. {
  45. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2
  46. return ets_get_cpu_frequency();
  47. #else
  48. return g_ticks_per_us_pro;
  49. #endif
  50. }
  51. int IRAM_ATTR esp_clk_cpu_freq(void)
  52. {
  53. return s_get_cpu_freq_mhz() * MHZ;
  54. }
  55. int IRAM_ATTR esp_clk_apb_freq(void)
  56. {
  57. return MIN(s_get_cpu_freq_mhz(), 80) * MHZ;
  58. }
  59. int IRAM_ATTR esp_clk_xtal_freq(void)
  60. {
  61. return rtc_clk_xtal_freq_get() * MHZ;
  62. }
  63. #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2
  64. void IRAM_ATTR ets_update_cpu_frequency(uint32_t ticks_per_us)
  65. {
  66. /* Update scale factors used by esp_rom_delay_us */
  67. g_ticks_per_us_pro = ticks_per_us;
  68. #if CONFIG_IDF_TARGET_ESP32
  69. #ifndef CONFIG_FREERTOS_UNICORE
  70. g_ticks_per_us_app = ticks_per_us;
  71. #endif
  72. #endif
  73. }
  74. #endif
  75. uint64_t esp_rtc_get_time_us(void)
  76. {
  77. _lock_acquire(&s_esp_rtc_time_lock);
  78. const uint32_t cal = esp_clk_slowclk_cal_get();
  79. const uint64_t rtc_this_ticks = rtc_time_get();
  80. const uint64_t ticks = rtc_this_ticks - s_rtc_last_ticks;
  81. /* RTC counter result is up to 2^48, calibration factor is up to 2^24,
  82. * for a 32kHz clock. We need to calculate (assuming no overflow):
  83. * (ticks * cal) >> RTC_CLK_CAL_FRACT
  84. *
  85. * An overflow in the (ticks * cal) multiplication would cause time to
  86. * wrap around after approximately 13 days, which is probably not enough
  87. * for some applications.
  88. * Therefore multiplication is split into two terms, for the lower 32-bit
  89. * and the upper 16-bit parts of "ticks", i.e.:
  90. * ((ticks_low + 2^32 * ticks_high) * cal) >> RTC_CLK_CAL_FRACT
  91. */
  92. const uint64_t ticks_low = ticks & UINT32_MAX;
  93. const uint64_t ticks_high = ticks >> 32;
  94. const uint64_t delta_time_us = ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) +
  95. ((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT));
  96. s_esp_rtc_time_us += delta_time_us;
  97. s_rtc_last_ticks = rtc_this_ticks;
  98. _lock_release(&s_esp_rtc_time_lock);
  99. return s_esp_rtc_time_us;
  100. }
  101. void esp_clk_slowclk_cal_set(uint32_t new_cal)
  102. {
  103. #if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
  104. /* To force monotonic time values even when clock calibration value changes,
  105. * we adjust esp_rtc_time
  106. */
  107. esp_rtc_get_time_us();
  108. #endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  109. REG_WRITE(RTC_SLOW_CLK_CAL_REG, new_cal);
  110. }
  111. uint32_t esp_clk_slowclk_cal_get(void)
  112. {
  113. return REG_READ(RTC_SLOW_CLK_CAL_REG);
  114. }
  115. uint64_t esp_clk_rtc_time(void)
  116. {
  117. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  118. return esp_rtc_get_time_us();
  119. #else
  120. return 0;
  121. #endif
  122. }