1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 #include <stdint.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 /**
15  * @file esp_clk.h
16  *
17  * This file contains declarations of clock related functions.
18  */
19 
20 /**
21  * @brief Get the calibration value of RTC slow clock
22  *
23  * The value is in the same format as returned by rtc_clk_cal (microseconds,
24  * in Q13.19 fixed-point format).
25  *
26  * @return the calibration value obtained using rtc_clk_cal, at startup time
27  */
28 uint32_t esp_clk_slowclk_cal_get(void);
29 
30 /**
31  * @brief Update the calibration value of RTC slow clock
32  *
33  * The value has to be in the same format as returned by rtc_clk_cal (microseconds,
34  * in Q13.19 fixed-point format).
35  * This value is used by timekeeping functions (such as gettimeofday) to
36  * calculate current time based on RTC counter value.
37  * @param value calibration value obtained using rtc_clk_cal
38  */
39 void esp_clk_slowclk_cal_set(uint32_t value);
40 
41 /**
42  * @brief Return current CPU clock frequency
43  * When frequency switching is performed, this frequency may change.
44  * However it is guaranteed that the frequency never changes with a critical
45  * section.
46  *
47  * @return CPU clock frequency, in Hz
48  */
49 int esp_clk_cpu_freq(void);
50 
51 /**
52  * @brief Return current APB clock frequency
53  *
54  * When frequency switching is performed, this frequency may change.
55  * However it is guaranteed that the frequency never changes with a critical
56  * section.
57  *
58  * @return APB clock frequency, in Hz
59  */
60 int esp_clk_apb_freq(void);
61 
62 /**
63  * @brief Return frequency of the main XTAL
64  *
65  * Frequency of the main XTAL can be either auto-detected or set at compile
66  * time (see CONFIG_XTAL_FREQ_SEL sdkconfig option). In both cases, this
67  * function returns the actual value at run time.
68  *
69  * @return XTAL frequency, in Hz
70  */
71 int esp_clk_xtal_freq(void);
72 
73 
74 /**
75  * @brief Read value of RTC counter, converting it to microseconds
76  * @attention The value returned by this function may change abruptly when
77  * calibration value of RTC counter is updated via esp_clk_slowclk_cal_set
78  * function. This should not happen unless application calls esp_clk_slowclk_cal_set.
79  * In ESP-IDF, esp_clk_slowclk_cal_set is only called in startup code.
80  *
81  * @return Value or RTC counter, expressed in microseconds
82  */
83 uint64_t esp_clk_rtc_time(void);
84 
85 /**
86  * @brief obtain internal critical section used esp_clk implementation.
87  *
88  * This is used by the esp_light_sleep_start() to avoid deadlocking when it
89  * calls esp_clk related API after stalling the other CPU.
90  */
91 void esp_clk_private_lock(void);
92 
93 /**
94  * @brief counterpart of esp_clk_private_lock
95  */
96 void esp_clk_private_unlock(void);
97 
98 #ifdef __cplusplus
99 }
100 #endif
101