// Copyright 2020 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include #include "soc/clk_ctrl_os.h" #define DELAY_RTC_CLK_SWITCH 5 static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED; static uint8_t s_periph_ref_counts = 0; static uint32_t s_rtc_clk_freq = 0; // Frequency of the 8M/256 clock in Hz bool periph_rtc_dig_clk8m_enable(void) { portENTER_CRITICAL(&periph_spinlock); if (s_periph_ref_counts == 0) { rtc_dig_clk8m_enable(); s_rtc_clk_freq = rtc_clk_freq_cal(rtc_clk_cal(RTC_CAL_8MD256, 100)); if (s_rtc_clk_freq == 0) { portEXIT_CRITICAL(&periph_spinlock); return false; } } s_periph_ref_counts++; portEXIT_CRITICAL(&periph_spinlock); return true; } uint32_t periph_rtc_dig_clk8m_get_freq(void) { return s_rtc_clk_freq * 256; } void periph_rtc_dig_clk8m_disable(void) { portENTER_CRITICAL(&periph_spinlock); assert(s_periph_ref_counts > 0); s_periph_ref_counts--; if (s_periph_ref_counts == 0) { s_rtc_clk_freq = 0; rtc_dig_clk8m_disable(); } portEXIT_CRITICAL(&periph_spinlock); }