esp_clk.c 3.8 KB

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