Skip to content

Commit 15c349b

Browse files
Merge branch 'contrib/github_pr_15132' into 'master'
fix(newlib): usleep returning early (GitHub PR) Closes IDFGH-14342 See merge request espressif/esp-idf!38483
2 parents 3054a72 + ec07f61 commit 15c349b

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
idf_component_register(SRCS "test_cxx_general.cpp"
2-
PRIV_REQUIRES unity driver)
2+
PRIV_REQUIRES unity driver esp_timer)

components/cxx/test_apps/general/main/test_cxx_general.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -14,6 +14,9 @@
1414
#include "unity.h"
1515
#include "unity_test_utils.h"
1616
#include "soc/soc.h"
17+
#include <chrono>
18+
#include <thread>
19+
#include "esp_timer.h"
1720

1821
extern "C" void setUp()
1922
{
@@ -319,6 +322,29 @@ TEST_CASE("stack smashing protection CXX", "[stack_smash]")
319322
recur_and_smash_cxx();
320323
}
321324

325+
TEST_CASE("test std::this_thread::sleep_for basic functionality", "[misc]")
326+
{
327+
const int us_per_tick = portTICK_PERIOD_MS * 1000;
328+
329+
// Test sub-tick sleep
330+
const auto short_sleep = std::chrono::microseconds(us_per_tick / 4);
331+
int64_t start = esp_timer_get_time();
332+
std::this_thread::sleep_for(short_sleep);
333+
int64_t end = esp_timer_get_time();
334+
int64_t elapsed_us = end - start;
335+
printf("short sleep: %lld us\n", elapsed_us);
336+
TEST_ASSERT_GREATER_OR_EQUAL(short_sleep.count(), elapsed_us);
337+
338+
// Test multi-tick sleep
339+
const auto long_sleep = std::chrono::microseconds(us_per_tick * 2);
340+
start = esp_timer_get_time();
341+
std::this_thread::sleep_for(long_sleep);
342+
end = esp_timer_get_time();
343+
elapsed_us = end - start;
344+
printf("long sleep: %lld us\n", elapsed_us);
345+
TEST_ASSERT_GREATER_OR_EQUAL(long_sleep.count(), elapsed_us);
346+
}
347+
322348
extern "C" void app_main(void)
323349
{
324350
s_testTLS.foo(); /* allocates memory that will be reused */

components/newlib/src/time.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,25 @@ int usleep(useconds_t us)
207207
if (us < us_per_tick) {
208208
esp_rom_delay_us((uint32_t) us);
209209
} else {
210-
/* since vTaskDelay(1) blocks for anywhere between 0 and portTICK_PERIOD_MS,
211-
* round up to compensate.
212-
*/
213-
vTaskDelay((us + us_per_tick - 1) / us_per_tick);
210+
/* vTaskDelay may return up to (n-1) tick periods due to the tick ISR
211+
being asynchronous to the call. We must sleep at least the specified
212+
time, or longer. Checking the monotonic clock allows making an
213+
additional call to vTaskDelay when needed to ensure minimal time is
214+
actually slept. Adding `us_per_tick - 1` prevents ever passing 0 to
215+
vTaskDelay().
216+
*/
217+
uint64_t now_us = esp_time_impl_get_time();
218+
uint64_t target_us = now_us + us;
219+
do {
220+
vTaskDelay((((target_us - now_us) + us_per_tick - 1) / us_per_tick));
221+
now_us = esp_time_impl_get_time();
222+
/* If the time left until the target is less than 1 tick, then we use ROM delay to fill the gap */
223+
uint64_t time_left = target_us - now_us;
224+
if (time_left != 0 && time_left < us_per_tick) {
225+
esp_rom_delay_us(time_left);
226+
break;
227+
}
228+
} while (now_us < target_us);
214229
}
215230
return 0;
216231
}

components/newlib/test_apps/newlib/main/test_time.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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
*/
66
#include <stdio.h>
77
#include <string.h>
88
#include <math.h>
9+
#include <unistd.h>
910
#include "unity.h"
1011
#include <time.h>
1112
#include <sys/time.h>
@@ -77,6 +78,27 @@ TEST_CASE("Reading RTC registers on APP CPU doesn't affect clock", "[newlib]")
7778

7879
#endif // (CONFIG_FREERTOS_NUMBER_OF_CORES == 2) && CONFIG_IDF_TARGET_ARCH_XTENSA
7980

81+
TEST_CASE("test usleep basic functionality", "[newlib]")
82+
{
83+
const int us_per_tick = portTICK_PERIOD_MS * 1000;
84+
85+
// Test sub-tick sleep such that usleep() uses ROM delay path
86+
const int short_sleep_us = us_per_tick / 4;
87+
int64_t start = esp_timer_get_time();
88+
TEST_ASSERT_EQUAL(0, usleep(short_sleep_us));
89+
int64_t end = esp_timer_get_time();
90+
printf("short sleep: %lld us\n", end - start);
91+
TEST_ASSERT_GREATER_OR_EQUAL(short_sleep_us, end - start);
92+
93+
// Test multi-tick sleep using vTaskDelay path
94+
const int long_sleep_us = us_per_tick * 2;
95+
start = esp_timer_get_time();
96+
TEST_ASSERT_EQUAL(0, usleep(long_sleep_us));
97+
end = esp_timer_get_time();
98+
printf("long sleep: %lld us\n", end - start);
99+
TEST_ASSERT_GREATER_OR_EQUAL(long_sleep_us, end - start);
100+
}
101+
80102
TEST_CASE("test adjtime function", "[newlib]")
81103
{
82104
struct timeval tv_time;

0 commit comments

Comments
 (0)