Select one of the symbols to view example projects that use it.
 
Outline
#include <freertos/FreeRTOS.h>
#include "clk_ctrl_os.h"
#include "soc/rtc.h"
#include "esp_ldo_regulator.h"
#include "esp_private/esp_clk_tree_common.h"
#include "esp_check.h"
#include "hal/clk_tree_hal.h"
#include "hal/clk_tree_ll.h"
#include "rtc_clk.h"
TAG
periph_spinlock
s_periph_ref_counts
s_rc_fast_freq
s_cur_apll_freq
s_apll_ref_cnt
periph_rtc_dig_clk8m_enable()
periph_rtc_dig_clk8m_get_freq()
periph_rtc_dig_clk8m_disable()
periph_rtc_apll_acquire()
periph_rtc_apll_release()
periph_rtc_apll_freq_set(uint32_t, uint32_t *)
Files
ESP-IDF
components
app_trace
app_update
bootloader_support
bt
cmock
console
cxx
driver
efuse
esp_adc
esp_app_format
esp_bootloader_format
esp_coex
esp_common
esp_driver_ana_cmpr
esp_driver_cam
esp_driver_dac
esp_driver_gpio
esp_driver_gptimer
esp_driver_i2c
esp_driver_i2s
esp_driver_jpeg
esp_driver_ledc
esp_driver_mcpwm
esp_driver_parlio
esp_driver_pcnt
esp_driver_rmt
esp_driver_sdio
esp_driver_sdm
esp_driver_sdmmc
esp_driver_sdspi
esp_driver_spi
esp_driver_tsens
esp_driver_uart
esp_driver_usb_serial_jtag
esp_eth
esp_event
esp_gdbstub
esp_hid
esp_http_client
esp_http_server
esp_https_ota
esp_https_server
esp_hw_support
dma
include
ldo
port
esp_lcd
esp_local_ctrl
esp_mm
esp_netif
esp_partition
esp_phy
esp_pm
esp_psram
esp_ringbuf
esp_rom
esp_security
esp_system
esp_timer
esp_vfs_console
esp_wifi
esp-tls
espcoredump
hal
heap
http_parser
ieee802154
log
mqtt
newlib
nvs_flash
nvs_sec_provider
openthread
perfmon
protobuf-c
protocomm
pthread
rt
sdmmc
soc
spi_flash
spiffs
tcp_transport
ulp
unity
vfs
wear_levelling
wifi_provisioning
wpa_supplicant
xtensa
examples
lwIP
FreeRTOS
cJSON
mbedTLS
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/esp_hw_support/clk_ctrl_os.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <freertos/FreeRTOS.h> #include "clk_ctrl_os.h" #include "soc/rtc.h" #include "esp_ldo_regulator.h" #include "esp_private/esp_clk_tree_common.h" #include "esp_check.h" #include "hal/clk_tree_hal.h" #include "hal/clk_tree_ll.h"8 includes #if SOC_CLK_MPLL_SUPPORTED #include "rtc_clk.h" #endif #if SOC_CLK_APLL_SUPPORTED || SOC_CLK_MPLL_SUPPORTED static const char *TAG = "clk_ctrl_os"; #endif static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED; static uint8_t s_periph_ref_counts = 0; static uint32_t s_rc_fast_freq = 0; // Frequency of the RC_FAST clock in Hz #if SOC_CLK_APLL_SUPPORTED // Current APLL frequency, in HZ. Zero if APLL is not enabled. static uint32_t s_cur_apll_freq = 0; static int s_apll_ref_cnt = 0;/* ... */ #endif #if SOC_CLK_MPLL_SUPPORTED static uint32_t s_cur_mpll_freq = 0; static int s_mpll_ref_cnt = 0; static esp_ldo_channel_handle_t s_ldo_chan = NULL;/* ... */ #endif bool periph_rtc_dig_clk8m_enable(void) { portENTER_CRITICAL(&periph_spinlock); if (s_periph_ref_counts == 0) { rtc_dig_clk8m_enable(); #if SOC_CLK_RC_FAST_SUPPORT_CALIBRATION s_rc_fast_freq = esp_clk_tree_rc_fast_get_freq_hz(ESP_CLK_TREE_SRC_FREQ_PRECISION_EXACT); if (s_rc_fast_freq == 0) { rtc_dig_clk8m_disable(); portEXIT_CRITICAL(&periph_spinlock); return false; }{...} #endif/* ... */ //SOC_CLK_RC_FAST_SUPPORT_CALIBRATION }{...} s_periph_ref_counts++; portEXIT_CRITICAL(&periph_spinlock); return true; }{ ... } uint32_t periph_rtc_dig_clk8m_get_freq(void) { #if !SOC_CLK_RC_FAST_SUPPORT_CALIBRATION /* Workaround: CLK8M calibration cannot be performed, we can only return its theoretic value */ return SOC_CLK_RC_FAST_FREQ_APPROX;/* ... */ #else return s_rc_fast_freq; #endif }{ ... } 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_rc_fast_freq = 0; rtc_dig_clk8m_disable(); }{...} portEXIT_CRITICAL(&periph_spinlock); }{ ... } #if SOC_CLK_APLL_SUPPORTED void periph_rtc_apll_acquire(void) { portENTER_CRITICAL(&periph_spinlock); s_apll_ref_cnt++; if (s_apll_ref_cnt == 1) { // For the first time enable APLL, need to set power up rtc_clk_apll_enable(true); }{...} portEXIT_CRITICAL(&periph_spinlock); }{ ... } void periph_rtc_apll_release(void) { portENTER_CRITICAL(&periph_spinlock); assert(s_apll_ref_cnt > 0); s_apll_ref_cnt--; if (s_apll_ref_cnt == 0) { // If there is no peripheral using APLL, shut down the power s_cur_apll_freq = 0; rtc_clk_apll_enable(false); }{...} portEXIT_CRITICAL(&periph_spinlock); }{ ... } esp_err_t periph_rtc_apll_freq_set(uint32_t expt_freq, uint32_t *real_freq) { uint32_t o_div = 0; uint32_t sdm0 = 0; uint32_t sdm1 = 0; uint32_t sdm2 = 0; // Guarantee 'periph_rtc_apll_acquire' has been called before set apll freq assert(s_apll_ref_cnt > 0); uint32_t apll_freq = rtc_clk_apll_coeff_calc(expt_freq, &o_div, &sdm0, &sdm1, &sdm2); ESP_RETURN_ON_FALSE(apll_freq, ESP_ERR_INVALID_ARG, TAG, "APLL coefficients calculate failed"); bool need_config = true; portENTER_CRITICAL(&periph_spinlock); /* If APLL is not in use or only one peripheral in use, its frequency can be changed as will * But when more than one peripheral refers APLL, its frequency is not allowed to change once it is set *//* ... */ if (s_cur_apll_freq == 0 || s_apll_ref_cnt < 2) { s_cur_apll_freq = apll_freq; }{...} else { apll_freq = s_cur_apll_freq; need_config = false; }{...} portEXIT_CRITICAL(&periph_spinlock); *real_freq = apll_freq; if (need_config) { ESP_LOGD(TAG, "APLL will working at %"PRIu32" Hz with coefficients [sdm0] %"PRIu32" [sdm1] %"PRIu32" [sdm2] %"PRIu32" [o_div] %"PRIu32"", apll_freq, sdm0, sdm1, sdm2, o_div); /* Set coefficients for APLL, notice that it doesn't mean APLL will start */ rtc_clk_apll_coeff_set(o_div, sdm0, sdm1, sdm2); }{...} else { return ESP_ERR_INVALID_STATE; }{...} return ESP_OK; }{ ... } #endif/* ... */ // SOC_CLK_APLL_SUPPORTED #if SOC_CLK_MPLL_SUPPORTED esp_err_t IRAM_ATTR periph_rtc_mpll_acquire(void) { // power up LDO for the MPLL #if CONFIG_ESP_LDO_RESERVE_PSRAM esp_ldo_channel_config_t ldo_mpll_config = { .chan_id = CONFIG_ESP_LDO_CHAN_PSRAM_DOMAIN, .voltage_mv = CONFIG_ESP_LDO_VOLTAGE_PSRAM_DOMAIN, }{...}; ESP_RETURN_ON_ERROR(esp_ldo_acquire_channel(&ldo_mpll_config, &s_ldo_chan), TAG, "acquire internal LDO for MPLL failed");/* ... */ #endif portENTER_CRITICAL(&periph_spinlock); s_mpll_ref_cnt++; if (s_mpll_ref_cnt == 1) { // For the first time enable MPLL, need to set power up rtc_clk_mpll_enable(); }{...} portEXIT_CRITICAL(&periph_spinlock); return ESP_OK; }{...} void periph_rtc_mpll_release(void) { #if defined(CONFIG_ESP_LDO_CHAN_PSRAM_DOMAIN) && CONFIG_ESP_LDO_CHAN_PSRAM_DOMAIN != -1 if (s_ldo_chan) { esp_ldo_release_channel(s_ldo_chan); }{...} /* ... */#endif portENTER_CRITICAL(&periph_spinlock); assert(s_mpll_ref_cnt > 0); s_mpll_ref_cnt--; if (s_mpll_ref_cnt == 0) { // If there is no peripheral using MPLL, shut down the power s_cur_mpll_freq = 0; rtc_clk_mpll_disable(); }{...} portEXIT_CRITICAL(&periph_spinlock); }{...} esp_err_t IRAM_ATTR periph_rtc_mpll_freq_set(uint32_t expt_freq, uint32_t *real_freq) { esp_err_t ret = ESP_OK; // Guarantee 'periph_rtc_apll_acquire' has been called before set apll freq assert(s_mpll_ref_cnt > 0); portENTER_CRITICAL(&periph_spinlock); if (s_cur_mpll_freq == expt_freq) { goto end; }{...} /* If MPLL is not in use or only one peripheral in use, its frequency can be changed as will * But when more than one peripheral refers MPLL, its frequency is not allowed to change once it is set *//* ... */ if (s_cur_mpll_freq == 0 || s_mpll_ref_cnt < 2) { uint32_t xtal_freq_mhz = clk_ll_xtal_load_freq_mhz(); rtc_clk_mpll_configure(xtal_freq_mhz, expt_freq / MHZ); s_cur_mpll_freq = clk_ll_mpll_get_freq_mhz(xtal_freq_mhz); }{...} else { ret = ESP_ERR_INVALID_STATE; }{...} end: if (real_freq != NULL) { *real_freq = s_cur_mpll_freq; }{...} portEXIT_CRITICAL(&periph_spinlock); return ret; }{...} /* ... */#endif // SOC_CLK_MPLL_SUPPORTED
Details