Skip to content

Commit dc93eb5

Browse files
committed
fix(esp_hw_support): add timer wakeup sleep duration check
Closes #15255
1 parent 85ec6a4 commit dc93eb5

File tree

24 files changed

+119
-46
lines changed

24 files changed

+119
-46
lines changed

components/esp_hw_support/include/esp_sleep.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -174,9 +174,13 @@ esp_err_t esp_sleep_enable_ulp_wakeup(void);
174174
/**
175175
* @brief Enable wakeup by timer
176176
* @param time_in_us time before wakeup, in microseconds
177+
* @note The valid `time_in_us` value depends on the bit width of the lp_timer/rtc_timer counter and the
178+
* current slow clock source selection (Refer RTC clock source configuration in menuconfig).
179+
* Valid values should be positive values less than RTC slow clock period * (2 ^ RTC timer bitwidth).
180+
*
177181
* @return
178182
* - ESP_OK on success
179-
* - ESP_ERR_INVALID_ARG if value is out of range (TBD)
183+
* - ESP_ERR_INVALID_ARG if value is out of range.
180184
*/
181185
esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us);
182186

components/esp_hw_support/port/esp32/rtc_time.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -145,9 +145,9 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
145145
uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
146146
{
147147
assert(period);
148-
/* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
149-
* TODO: fix overflow.
150-
*/
148+
if (time_in_us > (UINT64_MAX >> RTC_CLK_CAL_FRACT)) {
149+
return ((time_in_us / period) << RTC_CLK_CAL_FRACT) + ((time_in_us % period) << RTC_CLK_CAL_FRACT) / period;
150+
}
151151
return (time_in_us << RTC_CLK_CAL_FRACT) / period;
152152
}
153153

components/esp_hw_support/port/esp32c2/rtc_time.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -159,9 +159,9 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
159159
uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
160160
{
161161
assert(period);
162-
/* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
163-
* TODO: fix overflow.
164-
*/
162+
if (time_in_us > (UINT64_MAX >> RTC_CLK_CAL_FRACT)) {
163+
return ((time_in_us / period) << RTC_CLK_CAL_FRACT) + ((time_in_us % period) << RTC_CLK_CAL_FRACT) / period;
164+
}
165165
return (time_in_us << RTC_CLK_CAL_FRACT) / period;
166166
}
167167

components/esp_hw_support/port/esp32c3/rtc_time.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -162,9 +162,9 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
162162
uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
163163
{
164164
assert(period);
165-
/* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
166-
* TODO: fix overflow.
167-
*/
165+
if (time_in_us > (UINT64_MAX >> RTC_CLK_CAL_FRACT)) {
166+
return ((time_in_us / period) << RTC_CLK_CAL_FRACT) + ((time_in_us % period) << RTC_CLK_CAL_FRACT) / period;
167+
}
168168
return (time_in_us << RTC_CLK_CAL_FRACT) / period;
169169
}
170170

components/esp_hw_support/port/esp32c5/rtc_time.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -172,9 +172,9 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
172172
uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
173173
{
174174
assert(period);
175-
/* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
176-
* TODO: fix overflow.
177-
*/
175+
if (time_in_us > (UINT64_MAX >> RTC_CLK_CAL_FRACT)) {
176+
return ((time_in_us / period) << RTC_CLK_CAL_FRACT) + ((time_in_us % period) << RTC_CLK_CAL_FRACT) / period;
177+
}
178178
return (time_in_us << RTC_CLK_CAL_FRACT) / period;
179179
}
180180

components/esp_hw_support/port/esp32c6/rtc_time.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -247,9 +247,9 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
247247
uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
248248
{
249249
assert(period);
250-
/* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
251-
* TODO: fix overflow.
252-
*/
250+
if (time_in_us > (UINT64_MAX >> RTC_CLK_CAL_FRACT)) {
251+
return ((time_in_us / period) << RTC_CLK_CAL_FRACT) + ((time_in_us % period) << RTC_CLK_CAL_FRACT) / period;
252+
}
253253
return (time_in_us << RTC_CLK_CAL_FRACT) / period;
254254
}
255255

components/esp_hw_support/port/esp32c61/rtc_time.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -172,9 +172,9 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
172172
uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
173173
{
174174
assert(period);
175-
/* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
176-
* TODO: fix overflow.
177-
*/
175+
if (time_in_us > (UINT64_MAX >> RTC_CLK_CAL_FRACT)) {
176+
return ((time_in_us / period) << RTC_CLK_CAL_FRACT) + ((time_in_us % period) << RTC_CLK_CAL_FRACT) / period;
177+
}
178178
return (time_in_us << RTC_CLK_CAL_FRACT) / period;
179179
}
180180

components/esp_hw_support/port/esp32h2/rtc_time.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -247,9 +247,9 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
247247
uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
248248
{
249249
assert(period);
250-
/* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
251-
* TODO: fix overflow.
252-
*/
250+
if (time_in_us > (UINT64_MAX >> RTC_CLK_CAL_FRACT)) {
251+
return ((time_in_us / period) << RTC_CLK_CAL_FRACT) + ((time_in_us % period) << RTC_CLK_CAL_FRACT) / period;
252+
}
253253
return (time_in_us << RTC_CLK_CAL_FRACT) / period;
254254
}
255255

components/esp_hw_support/port/esp32h21/rtc_time.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -247,9 +247,9 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
247247
uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
248248
{
249249
assert(period);
250-
/* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
251-
* TODO: fix overflow.
252-
*/
250+
if (time_in_us > (UINT64_MAX >> RTC_CLK_CAL_FRACT)) {
251+
return ((time_in_us / period) << RTC_CLK_CAL_FRACT) + ((time_in_us % period) << RTC_CLK_CAL_FRACT) / period;
252+
}
253253
return (time_in_us << RTC_CLK_CAL_FRACT) / period;
254254
}
255255

components/esp_hw_support/port/esp32p4/rtc_time.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -198,9 +198,9 @@ uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles)
198198
uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period)
199199
{
200200
assert(period);
201-
/* Overflow will happen in this function if time_in_us >= 2^45, which is about 400 days.
202-
* TODO: fix overflow.
203-
*/
201+
if (time_in_us > (UINT64_MAX >> RTC_CLK_CAL_FRACT)) {
202+
return ((time_in_us / period) << RTC_CLK_CAL_FRACT) + ((time_in_us % period) << RTC_CLK_CAL_FRACT) / period;
203+
}
204204
return (time_in_us << RTC_CLK_CAL_FRACT) / period;
205205
}
206206

0 commit comments

Comments
 (0)