1 /*
2  * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdbool.h>
8 #include "soc/soc_caps.h"
9 #include "esp_err.h"
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /**
16  * @brief This function is used to enable the digital RC_FAST clock,
17  *        to support the peripherals.
18  *
19  * @note If this function is called a number of times, the `periph_rtc_dig_clk8m_disable`
20  *       function needs to be called same times to disable.
21  *
22  * @return true: success for enable the RC_FAST clock, false: RC_FAST clock enable failed
23  */
24 bool periph_rtc_dig_clk8m_enable(void);
25 
26 /**
27  * @brief This function is used to disable the digital RC_FAST clock, which should be called
28  *        with the `periph_rtc_dig_clk8m_enable` pairedly
29  *
30  * @note If this function is called a number of times, the `periph_rtc_dig_clk8m_disable`
31  *       function needs to be called same times to disable.
32  */
33 void periph_rtc_dig_clk8m_disable(void);
34 
35 /**
36  * @brief This function is used to get the real clock frequency value of RC_FAST clock
37  *
38  * @return The real clock value, in Hz
39  */
40 uint32_t periph_rtc_dig_clk8m_get_freq(void);
41 
42 #if SOC_CLK_APLL_SUPPORTED
43 /**
44  * @brief Enable APLL power if it has not enabled
45  */
46 void periph_rtc_apll_acquire(void);
47 
48 /**
49  * @brief Shut down APLL power if no peripherals using APLL
50  */
51 void periph_rtc_apll_release(void);
52 
53 /**
54  * @brief Calculate and set APLL coefficients by given frequency
55  * @note  Have to call 'periph_rtc_apll_acquire' to enable APLL power before setting frequency
56  * @note  This calculation is based on the inequality:
57  *        xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536) >= SOC_APLL_MULTIPLIER_OUT_MIN_HZ(350 MHz)
58  *        It will always calculate the minimum coefficients that can satisfy the inequality above, instead of loop them one by one.
59  *        which means more appropriate coefficients are likely to exist.
60  *        But this algorithm can meet almost all the cases and the accuracy can be guaranteed as well.
61  * @note  The APLL frequency is only allowed to set when there is only one peripheral refer to it.
62  *        If APLL is already set by another peripheral, this function will return `ESP_ERR_INVALID_STATE`
63  *        and output the current frequency by parameter `real_freq`.
64  *
65  * @param expt_freq Expected APLL frequency (unit: Hz)
66  * @param real_freq APLL real working frequency [output] (unit: Hz)
67  * @return
68  *      - ESP_OK: APLL frequency set success
69  *      - ESP_ERR_INVALID_ARG: The input expt_freq is out of APLL support range
70  *      - ESP_ERR_INVALID_STATE: APLL is refered by more than one peripherals, not allowed to change its frequency now
71  */
72 esp_err_t periph_rtc_apll_freq_set(uint32_t expt_freq, uint32_t *real_freq);
73 #endif // SOC_CLK_APLL_SUPPORTED
74 
75 #ifdef __cplusplus
76 }
77 #endif
78