Skip to content

Commit 1980701

Browse files
committed
rtc: update sources to allow proper calibration
Update rtc_time.c sources to guarantee proper clock calibration. Signed-off-by: Sylvio Alves <sylvio.alves@espressif.com>
1 parent 5154425 commit 1980701

File tree

7 files changed

+63
-50
lines changed

7 files changed

+63
-50
lines changed

components/esp_hw_support/port/esp32/rtc_time.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
#include "esp_rom_sys.h"
99
#include "hal/clk_tree_ll.h"
1010
#include "hal/rtc_cntl_ll.h"
11+
#include "hal/timer_ll.h"
1112
#include "soc/rtc.h"
1213
#include "soc/timer_periph.h"
1314
#include "esp_hw_log.h"
15+
#include "esp_private/periph_ctrl.h"
1416

15-
static const char* TAG = "rtc_time";
17+
static const char *TAG = "rtc_time";
1618

1719
/* Calibration of RTC_SLOW_CLK is performed using a special feature of TIMG0.
1820
* This feature counts the number of XTAL clock cycles within a given number of
@@ -55,10 +57,10 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
5557
uint32_t expected_freq;
5658
soc_rtc_slow_clk_src_t slow_clk_src = rtc_clk_slow_src_get();
5759
if (cal_clk == RTC_CAL_32K_XTAL ||
58-
(cal_clk == RTC_CAL_RTC_MUX && slow_clk_src == SOC_RTC_SLOW_CLK_SRC_XTAL32K)) {
60+
(cal_clk == RTC_CAL_RTC_MUX && slow_clk_src == SOC_RTC_SLOW_CLK_SRC_XTAL32K)) {
5961
expected_freq = SOC_CLK_XTAL32K_FREQ_APPROX; /* standard 32k XTAL */
6062
} else if (cal_clk == RTC_CAL_8MD256 ||
61-
(cal_clk == RTC_CAL_RTC_MUX && slow_clk_src == SOC_RTC_SLOW_CLK_SRC_RC_FAST_D256)) {
63+
(cal_clk == RTC_CAL_RTC_MUX && slow_clk_src == SOC_RTC_SLOW_CLK_SRC_RC_FAST_D256)) {
6264
expected_freq = SOC_CLK_RC_FAST_D256_FREQ_APPROX;
6365
} else {
6466
expected_freq = SOC_CLK_RC_SLOW_FREQ_APPROX; /* 150k internal oscillator */
@@ -117,7 +119,7 @@ uint32_t rtc_clk_cal_ratio(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
117119
return ratio;
118120
}
119121

120-
static inline bool rtc_clk_cal_32k_valid(rtc_xtal_freq_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
122+
static inline bool rtc_clk_cal_32k_valid(uint32_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
121123
{
122124
uint64_t expected_xtal_cycles = (xtal_freq * 1000000ULL * slowclk_cycles) >> 15; // xtal_freq(hz) * slowclk_cycles / 32768
123125
uint64_t delta = expected_xtal_cycles / 2000; // 5/10000
@@ -130,7 +132,7 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
130132
rtc_xtal_freq_t xtal_freq = rtc_clk_xtal_freq_get();
131133
uint64_t xtal_cycles = rtc_clk_cal_internal(cal_clk, slowclk_cycles);
132134

133-
if ((cal_clk == RTC_CAL_32K_XTAL) && !rtc_clk_cal_32k_valid(xtal_freq, slowclk_cycles, xtal_cycles)) {
135+
if ((cal_clk == RTC_CAL_32K_XTAL) && !rtc_clk_cal_32k_valid((uint32_t)xtal_freq, slowclk_cycles, xtal_cycles)) {
134136
return 0;
135137
}
136138

components/esp_hw_support/port/esp32c2/rtc_time.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
#include "soc/rtc_cntl_reg.h"
1111
#include "hal/clk_tree_ll.h"
1212
#include "hal/rtc_cntl_ll.h"
13+
#include "hal/timer_ll.h"
1314
#include "soc/timer_group_reg.h"
1415
#include "esp_rom_sys.h"
16+
#include "esp_private/periph_ctrl.h"
1517

1618
/* Calibration of RTC_SLOW_CLK is performed using a special feature of TIMG0.
1719
* This feature counts the number of XTAL clock cycles within a given number of
@@ -34,7 +36,7 @@
3436
uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
3537
{
3638
/* On ESP32-C2, choosing RTC_CAL_RTC_MUX results in calibration of
37-
* the 150k RTC clock regardless of the currenlty selected SLOW_CLK.
39+
* the 150k RTC clock regardless of the currently selected SLOW_CLK.
3840
* On the ESP32, it used the currently selected SLOW_CLK.
3941
* The following code emulates ESP32 behavior:
4042
*/
@@ -131,7 +133,7 @@ uint32_t rtc_clk_cal_ratio(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
131133
return ratio;
132134
}
133135

134-
static inline bool rtc_clk_cal_32k_valid(rtc_xtal_freq_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
136+
static inline bool rtc_clk_cal_32k_valid(uint32_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
135137
{
136138
uint64_t expected_xtal_cycles = (xtal_freq * 1000000ULL * slowclk_cycles) >> 15; // xtal_freq(hz) * slowclk_cycles / 32768
137139
uint64_t delta = expected_xtal_cycles / 2000; // 5/10000
@@ -144,7 +146,7 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
144146
rtc_xtal_freq_t xtal_freq = rtc_clk_xtal_freq_get();
145147
uint64_t xtal_cycles = rtc_clk_cal_internal(cal_clk, slowclk_cycles);
146148

147-
if ((cal_clk == RTC_CAL_32K_OSC_SLOW) && !rtc_clk_cal_32k_valid(xtal_freq, slowclk_cycles, xtal_cycles)) {
149+
if ((cal_clk == RTC_CAL_32K_OSC_SLOW) && !rtc_clk_cal_32k_valid((uint32_t)xtal_freq, slowclk_cycles, xtal_cycles)) {
148150
return 0;
149151
}
150152

components/esp_hw_support/port/esp32c3/rtc_time.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
#include "soc/rtc_cntl_reg.h"
1111
#include "hal/clk_tree_ll.h"
1212
#include "hal/rtc_cntl_ll.h"
13+
#include "hal/timer_ll.h"
1314
#include "soc/timer_group_reg.h"
1415
#include "esp_rom_sys.h"
16+
#include "esp_private/periph_ctrl.h"
1517

1618
/* Calibration of RTC_SLOW_CLK is performed using a special feature of TIMG0.
1719
* This feature counts the number of XTAL clock cycles within a given number of
@@ -34,7 +36,7 @@
3436
uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
3537
{
3638
/* On ESP32C3, choosing RTC_CAL_RTC_MUX results in calibration of
37-
* the 150k RTC clock regardless of the currenlty selected SLOW_CLK.
39+
* the 150k RTC clock regardless of the currently selected SLOW_CLK.
3840
* On the ESP32, it used the currently selected SLOW_CLK.
3941
* The following code emulates ESP32 behavior:
4042
*/
@@ -49,7 +51,6 @@ uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
4951
cal_clk = RTC_CAL_RTC_MUX;
5052
}
5153

52-
5354
/* Enable requested clock (150k clock is always on) */
5455
bool dig_32k_xtal_enabled = clk_ll_xtal32k_digi_is_enabled();
5556
if (cal_clk == RTC_CAL_32K_XTAL && !dig_32k_xtal_enabled) {
@@ -72,7 +73,7 @@ uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
7273
*/
7374
REG_SET_FIELD(TIMG_RTCCALICFG2_REG(0), TIMG_RTC_CALI_TIMEOUT_THRES, 1);
7475
while (!GET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY)
75-
&& !GET_PERI_REG_MASK(TIMG_RTCCALICFG2_REG(0), TIMG_RTC_CALI_TIMEOUT));
76+
&& !GET_PERI_REG_MASK(TIMG_RTCCALICFG2_REG(0), TIMG_RTC_CALI_TIMEOUT));
7677
}
7778

7879
/* Prepare calibration */
@@ -135,7 +136,7 @@ uint32_t rtc_clk_cal_ratio(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
135136
return ratio;
136137
}
137138

138-
static bool rtc_clk_cal_32k_valid(rtc_xtal_freq_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
139+
static bool rtc_clk_cal_32k_valid(uint32_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
139140
{
140141
uint64_t expected_xtal_cycles = (xtal_freq * 1000000ULL * slowclk_cycles) >> 15; // xtal_freq(hz) * slowclk_cycles / 32768
141142
uint64_t delta = expected_xtal_cycles / 2000; // 5/10000
@@ -148,8 +149,9 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
148149
rtc_xtal_freq_t xtal_freq = rtc_clk_xtal_freq_get();
149150
uint64_t xtal_cycles = rtc_clk_cal_internal(cal_clk, slowclk_cycles);
150151

151-
if ((cal_clk == RTC_CAL_32K_XTAL) && !rtc_clk_cal_32k_valid(xtal_freq, slowclk_cycles, xtal_cycles))
152+
if ((cal_clk == RTC_CAL_32K_XTAL) && !rtc_clk_cal_32k_valid((uint32_t)xtal_freq, slowclk_cycles, xtal_cycles)) {
152153
return 0;
154+
}
153155

154156
uint64_t divider = ((uint64_t)xtal_freq) * slowclk_cycles;
155157
uint64_t period_64 = ((xtal_cycles << RTC_CLK_CAL_FRACT) + divider / 2 - 1) / divider;

components/esp_hw_support/port/esp32c6/rtc_time.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
#include "esp32c6/rom/ets_sys.h"
99
#include "soc/rtc.h"
1010
#include "soc/pcr_reg.h"
11-
#include "soc/lp_timer_reg.h"
1211
#include "hal/lp_timer_hal.h"
1312
#include "hal/clk_tree_ll.h"
13+
#include "hal/timer_ll.h"
1414
#include "soc/timer_group_reg.h"
1515
#include "esp_rom_sys.h"
1616
#include "assert.h"
1717
#include "hal/efuse_hal.h"
1818
#include "soc/chip_revision.h"
19+
#include "esp_private/periph_ctrl.h"
1920

20-
static const char *TAG = "rtc_time";
21+
__attribute__((unused)) static const char *TAG = "rtc_time";
2122

2223
/* Calibration of RTC_SLOW_CLK is performed using a special feature of TIMG0.
2324
* This feature counts the number of XTAL clock cycles within a given number of
@@ -80,7 +81,7 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
8081

8182
/* Enable requested clock (150k clock is always on) */
8283
// All clocks on/off takes time to be stable, so we shouldn't frequently enable/disable the clock
83-
// Only enable if orignally was disabled, and set back to the disable state after calibration is done
84+
// Only enable if originally was disabled, and set back to the disable state after calibration is done
8485
// If the clock is already on, then do nothing
8586
bool dig_32k_xtal_enabled = clk_ll_xtal32k_digi_is_enabled();
8687
if (cal_clk == RTC_CAL_32K_XTAL && !dig_32k_xtal_enabled) {
@@ -124,6 +125,9 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
124125

125126
/* Prepare calibration */
126127
REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_CLK_SEL, cali_clk_sel);
128+
if (cali_clk_sel == TIMG_RTC_CALI_CLK_SEL_RC_FAST) {
129+
clk_ll_rc_fast_tick_conf();
130+
}
127131
CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START_CYCLING);
128132
REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_MAX, slowclk_cycles);
129133
/* Figure out how long to wait for calibration to finish */
@@ -157,7 +161,9 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
157161

158162
/*The Fosc CLK of calibration circuit is divided by 32 for ECO1.
159163
So we need to multiply the frequency of the Fosc for ECO1 and above chips by 32 times.
160-
And ensure that this modification will not affect ECO0.*/
164+
And ensure that this modification will not affect ECO0.
165+
And the 32-divider belongs to REF_TICK module, so we need to enable its clock during
166+
calibration. */
161167
if (ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 1)) {
162168
if (cal_clk == RTC_CAL_RC_FAST) {
163169
cal_val = cal_val >> 5;
@@ -204,7 +210,7 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
204210
return cal_val;
205211
}
206212

207-
static bool rtc_clk_cal_32k_valid(rtc_xtal_freq_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
213+
static bool rtc_clk_cal_32k_valid(uint32_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
208214
{
209215
uint64_t expected_xtal_cycles = (xtal_freq * 1000000ULL * slowclk_cycles) >> 15; // xtal_freq(hz) * slowclk_cycles / 32768
210216
uint64_t delta = expected_xtal_cycles / 2000; // 5/10000 = 0.05% error range
@@ -228,7 +234,7 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
228234

229235
uint64_t xtal_cycles = rtc_clk_cal_internal(cal_clk, slowclk_cycles);
230236

231-
if (cal_clk == RTC_CAL_32K_XTAL && !rtc_clk_cal_32k_valid(xtal_freq, slowclk_cycles, xtal_cycles)) {
237+
if (cal_clk == RTC_CAL_32K_XTAL && !rtc_clk_cal_32k_valid((uint32_t)xtal_freq, slowclk_cycles, xtal_cycles)) {
232238
return 0;
233239
}
234240

@@ -257,12 +263,6 @@ uint64_t rtc_time_get(void)
257263
return lp_timer_hal_get_cycle_count();
258264
}
259265

260-
void rtc_clk_wait_for_slow_cycle(void) //This function may not by useful any more
261-
{
262-
// TODO: IDF-5781
263-
ESP_EARLY_LOGW(TAG, "rtc_clk_wait_for_slow_cycle() has not been implemented yet");
264-
}
265-
266266
uint32_t rtc_clk_freq_cal(uint32_t cal_val)
267267
{
268268
if (cal_val == 0) {

components/esp_hw_support/port/esp32h2/rtc_time.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
#include <stdint.h>
88
#include "esp32h2/rom/ets_sys.h"
99
#include "soc/rtc.h"
10-
#include "soc/lp_timer_reg.h"
10+
#include "hal/lp_timer_hal.h"
1111
#include "hal/clk_tree_ll.h"
12+
#include "hal/timer_ll.h"
1213
#include "soc/timer_group_reg.h"
14+
#include "soc/pcr_reg.h"
1315
#include "esp_rom_sys.h"
1416
#include "assert.h"
1517
#include "hal/efuse_hal.h"
1618
#include "soc/chip_revision.h"
19+
#include "esp_private/periph_ctrl.h"
1720

18-
19-
static const char *TAG = "rtc_time";
21+
__attribute__((unused)) static const char *TAG = "rtc_time";
2022

2123
/* Calibration of RTC_SLOW_CLK is performed using a special feature of TIMG0.
2224
* This feature counts the number of XTAL clock cycles within a given number of
@@ -79,7 +81,7 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
7981

8082
/* Enable requested clock (150k clock is always on) */
8183
// All clocks on/off takes time to be stable, so we shouldn't frequently enable/disable the clock
82-
// Only enable if orignally was disabled, and set back to the disable state after calibration is done
84+
// Only enable if originally was disabled, and set back to the disable state after calibration is done
8385
// If the clock is already on, then do nothing
8486
bool dig_32k_xtal_enabled = clk_ll_xtal32k_digi_is_enabled();
8587
if (cal_clk == RTC_CAL_32K_XTAL && !dig_32k_xtal_enabled) {
@@ -123,6 +125,9 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
123125

124126
/* Prepare calibration */
125127
REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_CLK_SEL, cali_clk_sel);
128+
if (cali_clk_sel == TIMG_RTC_CALI_CLK_SEL_RC_FAST) {
129+
clk_ll_rc_fast_tick_conf();
130+
}
126131
CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START_CYCLING);
127132
REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_MAX, slowclk_cycles);
128133
/* Figure out how long to wait for calibration to finish */
@@ -135,6 +140,9 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
135140
} else if (cali_clk_sel == TIMG_RTC_CALI_CLK_SEL_RC_FAST) {
136141
REG_SET_FIELD(TIMG_RTCCALICFG2_REG(0), TIMG_RTC_CALI_TIMEOUT_THRES, RTC_FAST_CLK_8M_CAL_TIMEOUT_THRES(slowclk_cycles));
137142
expected_freq = SOC_CLK_RC_FAST_FREQ_APPROX;
143+
if (ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 2)) {
144+
expected_freq = expected_freq >> 5;
145+
}
138146
} else {
139147
REG_SET_FIELD(TIMG_RTCCALICFG2_REG(0), TIMG_RTC_CALI_TIMEOUT_THRES, RTC_SLOW_CLK_150K_CAL_TIMEOUT_THRES(slowclk_cycles));
140148
expected_freq = SOC_CLK_RC_SLOW_FREQ_APPROX;
@@ -153,10 +161,13 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
153161

154162
/*The Fosc CLK of calibration circuit is divided by 32 for ECO2.
155163
So we need to multiply the frequency of the Fosc for ECO2 and above chips by 32 times.
156-
And ensure that this modification will not affect ECO0 and ECO1.*/
164+
And ensure that this modification will not affect ECO0 and ECO1.
165+
And the 32-divider belongs to REF_TICK module, so we need to enable its clock during
166+
calibration. */
157167
if (ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 2)) {
158168
if (cal_clk == RTC_CAL_RC_FAST) {
159169
cal_val = cal_val >> 5;
170+
CLEAR_PERI_REG_MASK(PCR_CTRL_TICK_CONF_REG, PCR_TICK_ENABLE);
160171
}
161172
}
162173
break;
@@ -199,7 +210,7 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
199210
return cal_val;
200211
}
201212

202-
static bool rtc_clk_cal_32k_valid(rtc_xtal_freq_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
213+
static bool rtc_clk_cal_32k_valid(uint32_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
203214
{
204215
uint64_t expected_xtal_cycles = (xtal_freq * 1000000ULL * slowclk_cycles) >> 15; // xtal_freq(hz) * slowclk_cycles / 32768
205216
uint64_t delta = expected_xtal_cycles / 2000; // 5/10000 = 0.05% error range
@@ -217,12 +228,13 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
217228
if (ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 2)) {
218229
if (cal_clk == RTC_CAL_RC_FAST) {
219230
slowclk_cycles = slowclk_cycles >> 5;
231+
SET_PERI_REG_MASK(PCR_CTRL_TICK_CONF_REG, PCR_TICK_ENABLE);
220232
}
221233
}
222234

223235
uint64_t xtal_cycles = rtc_clk_cal_internal(cal_clk, slowclk_cycles);
224236

225-
if (cal_clk == RTC_CAL_32K_XTAL && !rtc_clk_cal_32k_valid(xtal_freq, slowclk_cycles, xtal_cycles)) {
237+
if (cal_clk == RTC_CAL_32K_XTAL && !rtc_clk_cal_32k_valid((uint32_t)xtal_freq, slowclk_cycles, xtal_cycles)) {
226238
return 0;
227239
}
228240

@@ -248,16 +260,7 @@ uint64_t rtc_time_slowclk_to_us(uint64_t rtc_cycles, uint32_t period)
248260

249261
uint64_t rtc_time_get(void)
250262
{
251-
SET_PERI_REG_MASK(LP_TIMER_UPDATE_REG, LP_TIMER_MAIN_TIMER_UPDATE);
252-
uint64_t t = READ_PERI_REG(LP_TIMER_MAIN_BUF0_LOW_REG);
253-
t |= ((uint64_t) READ_PERI_REG(LP_TIMER_MAIN_BUF0_HIGH_REG)) << 32;
254-
return t;
255-
}
256-
257-
void rtc_clk_wait_for_slow_cycle(void) //This function may not by useful any more
258-
{
259-
// TODO: IDF-6254
260-
ESP_EARLY_LOGW(TAG, "rtc_clk_wait_for_slow_cycle() has not been implemented yet");
263+
return lp_timer_hal_get_cycle_count();
261264
}
262265

263266
uint32_t rtc_clk_freq_cal(uint32_t cal_val)

components/esp_hw_support/port/esp32s2/rtc_time.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include "soc/rtc_cntl_reg.h"
1111
#include "hal/clk_tree_ll.h"
1212
#include "hal/rtc_cntl_ll.h"
13+
#include "hal/timer_ll.h"
1314
#include "soc/timer_group_reg.h"
15+
#include "esp_private/periph_ctrl.h"
1416

1517
/* Calibration of RTC_SLOW_CLK is performed using a special feature of TIMG0.
1618
* This feature counts the number of XTAL clock cycles within a given number of
@@ -114,7 +116,7 @@ static uint32_t rtc_clk_cal_internal_cycling(rtc_cal_sel_t cal_clk, uint32_t slo
114116
}
115117

116118
/**
117-
* @brief Slowclk period calculating funtion used by rtc_clk_cal and rtc_clk_cal_cycling
119+
* @brief Slowclk period calculating function used by rtc_clk_cal and rtc_clk_cal_cycling
118120
* @param xtal_cycles number of xtal cycles count
119121
* @param slowclk_cycles number of slow clock cycles to count
120122
* @return slow clock period
@@ -138,7 +140,7 @@ static uint32_t rtc_clk_xtal_to_slowclk(uint64_t xtal_cycles, uint32_t slowclk_c
138140
uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles, uint32_t cal_mode)
139141
{
140142
/* On ESP32S2, choosing RTC_CAL_RTC_MUX results in calibration of
141-
* the 90k RTC clock regardless of the currenlty selected SLOW_CLK.
143+
* the 90k RTC clock regardless of the currently selected SLOW_CLK.
142144
* On the ESP32, it used the currently selected SLOW_CLK.
143145
* The following code emulates ESP32 behavior:
144146
*/
@@ -197,7 +199,7 @@ uint32_t rtc_clk_cal_ratio(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
197199
return ratio;
198200
}
199201

200-
static inline bool rtc_clk_cal_32k_valid(rtc_xtal_freq_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
202+
static inline bool rtc_clk_cal_32k_valid(uint32_t xtal_freq, uint32_t slowclk_cycles, uint64_t actual_xtal_cycles)
201203
{
202204
uint64_t expected_xtal_cycles = (xtal_freq * 1000000ULL * slowclk_cycles) >> 15; // xtal_freq(hz) * slowclk_cycles / 32768
203205
uint64_t delta = expected_xtal_cycles / 2000; // 5/10000
@@ -208,7 +210,7 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
208210
{
209211
uint64_t xtal_cycles = rtc_clk_cal_internal(cal_clk, slowclk_cycles, RTC_TIME_CAL_ONEOFF_MODE);
210212

211-
if ((cal_clk == RTC_CAL_32K_XTAL) && !rtc_clk_cal_32k_valid(rtc_clk_xtal_freq_get(), slowclk_cycles, xtal_cycles)) {
213+
if ((cal_clk == RTC_CAL_32K_XTAL) && !rtc_clk_cal_32k_valid((uint32_t)rtc_clk_xtal_freq_get(), slowclk_cycles, xtal_cycles)) {
212214
return 0;
213215
}
214216

0 commit comments

Comments
 (0)