esp_clk.c 4.1 KB

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