Skip to content

Commit 49606cd

Browse files
author
Bogdan Marinescu
committed
Improved accuracy for microsecond delays
1 parent 1a1507a commit 49606cd

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

hal/common/mbed_wait_api_rtos.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,8 @@ void wait_ms(int ms) {
3131
wait_us(ms * 1000);
3232
}
3333

34-
// Wait for the given number of microseconds using a hardware timer
35-
// in a busy wait loop
36-
static void wait_us_busy(int us) {
37-
if (us > 0) {
38-
uint32_t start = us_ticker_read();
39-
while ((us_ticker_read() - start) < (uint32_t)us);
40-
}
41-
}
42-
4334
void wait_us(int us) {
35+
uint32_t start = us_ticker_read();
4436
// Use the RTOS to wait for millisecond delays if possible
4537
int ms = us / 1000;
4638
if ((ms > 0) && core_util_are_interrupts_enabled()) {
@@ -49,7 +41,9 @@ void wait_us(int us) {
4941
}
5042
// Use busy waiting for sub-millisecond delays, or for the whole
5143
// interval if interrupts are not enabled
52-
wait_us_busy(us);
44+
if (us > 0) {
45+
while((us_ticker_read() - start) < (uint32_t)us);
46+
}
5347
}
5448

5549
#endif // #if MBED_CONF_RTOS_PRESENT

0 commit comments

Comments
 (0)