Skip to content

Commit 4f4112b

Browse files
committed
Consolidating timer reads to reduce drift
1 parent 07a2355 commit 4f4112b

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

TESTS/mbed_drivers/wait_us/main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,20 @@ const int ONE_SECOND_US = 1000000;
2727
const int total_ticks = 10;
2828

2929
void test_case_ticker() {
30-
int before_print_us;
30+
int start_time;
3131
int after_print_us;
3232
int wait_time_us = ONE_SECOND_US;
3333

3434
timer.start();
35-
for (int i = 0; i <= total_ticks; ++i) {
35+
start_time = timer.read();
36+
int i = 0;
37+
while (i <= total_ticks) {
3638
wait_us(wait_time_us);
37-
before_print_us = timer.read();
3839
greentea_send_kv("tick", i);
3940
after_print_us = timer.read();
4041

41-
// This won't be 100% exact, but it should be pretty close
42-
wait_time_us = ONE_SECOND_US - (after_print_us - before_print_us);
42+
// This won't be 100% exact, but it should be very close
43+
wait_time_us = after_print_us - start_time - ((++i) * ONE_SECOND_US);
4344
}
4445
timer.stop();
4546
}

0 commit comments

Comments
 (0)