clk_ctrl_os.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 "sdkconfig.h"
  9. #define DELAY_RTC_CLK_SWITCH 5
  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. bool periph_rtc_dig_clk8m_enable(void)
  14. {
  15. portENTER_CRITICAL(&periph_spinlock);
  16. if (s_periph_ref_counts == 0) {
  17. rtc_dig_clk8m_enable();
  18. #if CONFIG_IDF_TARGET_ESP32H2
  19. s_rtc_clk_freq = rtc_clk_freq_cal(rtc_clk_cal(RTC_CAL_RC32K, 100));
  20. #else
  21. s_rtc_clk_freq = rtc_clk_freq_cal(rtc_clk_cal(RTC_CAL_8MD256, 100));
  22. #endif
  23. if (s_rtc_clk_freq == 0) {
  24. portEXIT_CRITICAL(&periph_spinlock);
  25. return false;
  26. }
  27. }
  28. s_periph_ref_counts++;
  29. portEXIT_CRITICAL(&periph_spinlock);
  30. return true;
  31. }
  32. uint32_t periph_rtc_dig_clk8m_get_freq(void)
  33. {
  34. return s_rtc_clk_freq * 256;
  35. }
  36. void periph_rtc_dig_clk8m_disable(void)
  37. {
  38. portENTER_CRITICAL(&periph_spinlock);
  39. assert(s_periph_ref_counts > 0);
  40. s_periph_ref_counts--;
  41. if (s_periph_ref_counts == 0) {
  42. s_rtc_clk_freq = 0;
  43. rtc_dig_clk8m_disable();
  44. }
  45. portEXIT_CRITICAL(&periph_spinlock);
  46. }