clk_ctrl_os.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <freertos/FreeRTOS.h>
  15. #include "soc/clk_ctrl_os.h"
  16. #define DELAY_RTC_CLK_SWITCH 5
  17. static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
  18. static uint8_t s_periph_ref_counts = 0;
  19. static uint32_t s_rtc_clk_freq = 0; // Frequency of the 8M/256 clock in Hz
  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. 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. }
  31. s_periph_ref_counts++;
  32. portEXIT_CRITICAL(&periph_spinlock);
  33. return true;
  34. }
  35. uint32_t periph_rtc_dig_clk8m_get_freq(void)
  36. {
  37. return s_rtc_clk_freq * 256;
  38. }
  39. void periph_rtc_dig_clk8m_disable(void)
  40. {
  41. portENTER_CRITICAL(&periph_spinlock);
  42. assert(s_periph_ref_counts > 0);
  43. s_periph_ref_counts--;
  44. if (s_periph_ref_counts == 0) {
  45. s_rtc_clk_freq = 0;
  46. rtc_dig_clk8m_disable();
  47. }
  48. portEXIT_CRITICAL(&periph_spinlock);
  49. }