clk_ctrl_os.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <freertos/FreeRTOS.h>
  7. #include "clk_ctrl_os.h"
  8. #include "esp_check.h"
  9. #include "sdkconfig.h"
  10. static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
  11. static uint8_t s_periph_ref_counts = 0;
  12. static uint32_t s_rtc_clk_freq = 0; // Frequency of the 8M/256 clock in Hz
  13. #if SOC_CLK_APLL_SUPPORTED
  14. static const char *TAG = "clk_ctrl_os";
  15. // Current APLL frequency, in HZ. Zero if APLL is not enabled.
  16. static uint32_t s_cur_apll_freq = 0;
  17. static int s_apll_ref_cnt = 0;
  18. #endif
  19. bool periph_rtc_dig_clk8m_enable(void)
  20. {
  21. portENTER_CRITICAL(&periph_spinlock);
  22. if (s_periph_ref_counts == 0) {
  23. rtc_dig_clk8m_enable();
  24. #if !CONFIG_IDF_TARGET_ESP32H2
  25. s_rtc_clk_freq = rtc_clk_freq_cal(rtc_clk_cal(RTC_CAL_8MD256, 100));
  26. if (s_rtc_clk_freq == 0) {
  27. portEXIT_CRITICAL(&periph_spinlock);
  28. return false;
  29. }
  30. #endif
  31. }
  32. s_periph_ref_counts++;
  33. portEXIT_CRITICAL(&periph_spinlock);
  34. return true;
  35. }
  36. uint32_t periph_rtc_dig_clk8m_get_freq(void)
  37. {
  38. #if CONFIG_IDF_TARGET_ESP32H2
  39. /* Workaround: H2 doesn't have 8MD256 clk, so calibration cannot be done, we just return its theoretic frequency */
  40. return SOC_CLK_RC_FAST_FREQ_APPROX;
  41. #else
  42. return s_rtc_clk_freq * 256;
  43. #endif
  44. }
  45. void periph_rtc_dig_clk8m_disable(void)
  46. {
  47. portENTER_CRITICAL(&periph_spinlock);
  48. assert(s_periph_ref_counts > 0);
  49. s_periph_ref_counts--;
  50. if (s_periph_ref_counts == 0) {
  51. s_rtc_clk_freq = 0;
  52. rtc_dig_clk8m_disable();
  53. }
  54. portEXIT_CRITICAL(&periph_spinlock);
  55. }
  56. #if SOC_CLK_APLL_SUPPORTED
  57. void periph_rtc_apll_acquire(void)
  58. {
  59. portENTER_CRITICAL(&periph_spinlock);
  60. s_apll_ref_cnt++;
  61. if (s_apll_ref_cnt == 1) {
  62. // For the first time enable APLL, need to set power up
  63. rtc_clk_apll_enable(true);
  64. }
  65. portEXIT_CRITICAL(&periph_spinlock);
  66. }
  67. void periph_rtc_apll_release(void)
  68. {
  69. portENTER_CRITICAL(&periph_spinlock);
  70. assert(s_apll_ref_cnt > 0);
  71. s_apll_ref_cnt--;
  72. if (s_apll_ref_cnt == 0) {
  73. // If there is no peripheral using APLL, shut down the power
  74. s_cur_apll_freq = 0;
  75. rtc_clk_apll_enable(false);
  76. }
  77. portEXIT_CRITICAL(&periph_spinlock);
  78. }
  79. esp_err_t periph_rtc_apll_freq_set(uint32_t expt_freq, uint32_t *real_freq)
  80. {
  81. uint32_t o_div = 0;
  82. uint32_t sdm0 = 0;
  83. uint32_t sdm1 = 0;
  84. uint32_t sdm2 = 0;
  85. // Guarantee 'periph_rtc_apll_acquire' has been called before set apll freq
  86. assert(s_apll_ref_cnt > 0);
  87. uint32_t apll_freq = rtc_clk_apll_coeff_calc(expt_freq, &o_div, &sdm0, &sdm1, &sdm2);
  88. ESP_RETURN_ON_FALSE(apll_freq, ESP_ERR_INVALID_ARG, TAG, "APLL coefficients calculate failed");
  89. bool need_config = true;
  90. portENTER_CRITICAL(&periph_spinlock);
  91. /* If APLL is not in use or only one peripheral in use, its frequency can be changed as will
  92. * But when more than one peripheral refers APLL, its frequency is not allowed to change once it is set */
  93. if (s_cur_apll_freq == 0 || s_apll_ref_cnt < 2) {
  94. s_cur_apll_freq = apll_freq;
  95. } else {
  96. apll_freq = s_cur_apll_freq;
  97. need_config = false;
  98. }
  99. portEXIT_CRITICAL(&periph_spinlock);
  100. *real_freq = apll_freq;
  101. if (need_config) {
  102. ESP_LOGD(TAG, "APLL will working at %d Hz with coefficients [sdm0] %d [sdm1] %d [sdm2] %d [o_div] %d",
  103. apll_freq, sdm0, sdm1, sdm2, o_div);
  104. /* Set coefficients for APLL, notice that it doesn't mean APLL will start */
  105. rtc_clk_apll_coeff_set(o_div, sdm0, sdm1, sdm2);
  106. } else {
  107. return ESP_ERR_INVALID_STATE;
  108. }
  109. return ESP_OK;
  110. }
  111. #endif // SOC_CLK_APLL_SUPPORTED