clk_ctrl_os.c 3.6 KB

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