File tree Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Original file line number Diff line number Diff line change @@ -207,10 +207,19 @@ int usleep(useconds_t us)
207
207
if (us < us_per_tick ) {
208
208
esp_rom_delay_us ((uint32_t ) us );
209
209
} 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
+ } while (now_us < target_us );
214
223
}
215
224
return 0 ;
216
225
}
You can’t perform that action at this time.
0 commit comments