Skip to content

Commit

Permalink
fix(newlib): usleep returning early
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenoonan committed Jan 20, 2025
1 parent b5ac4fb commit c060a85
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions components/newlib/src/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,19 @@ int usleep(useconds_t us)
if (us < us_per_tick) {
esp_rom_delay_us((uint32_t) us);
} else {
/* since vTaskDelay(1) blocks for anywhere between 0 and portTICK_PERIOD_MS,
* round up to compensate.
*/
vTaskDelay((us + us_per_tick - 1) / us_per_tick);
/* vTaskDelay may return up to (n-1) tick periods due to the tick ISR
being asynchronous to the call. We must sleep at least the specified
time, or longer. Checking the monotonic clock allows making an
additional call to vTaskDelay when needed to ensure minimal time is
actually slept. Adding `us_per_tick - 1` prevents ever passing 0 to
vTaskDelay().
*/
uint64_t now_us = esp_time_impl_get_time();
uint64_t target_us = now_us + us;
do {
vTaskDelay((((target_us - now_us) + us_per_tick - 1) / us_per_tick));
now_us = esp_time_impl_get_time();
} while (now_us < target_us);
}
return 0;
}
Expand Down

0 comments on commit c060a85

Please sign in to comment.