Skip to content

Commit 3b6c00b

Browse files
committed
Timer: add a function to read the timer in a 64bit variable.
The Timer implementation has been refactored to take advantage of this implementation as the base for all functions reading the time elapsed.
1 parent 15327ca commit 3b6c00b

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

drivers/Timer.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,27 @@ void Timer::stop() {
4545
}
4646

4747
int Timer::read_us() {
48-
core_util_critical_section_enter();
49-
us_timestamp_t time = _time + slicetime();
50-
core_util_critical_section_exit();
51-
return time;
48+
return read_high_resolution_us();
5249
}
5350

5451
float Timer::read() {
55-
core_util_critical_section_enter();
56-
us_timestamp_t time = _time + slicetime();
57-
core_util_critical_section_exit();
58-
time = time / 1000;
59-
return (float)read_ms() / 1000.0f;
52+
return (float)read_us() / 1000000.0f;
6053
}
6154

6255
int Timer::read_ms() {
56+
return read_high_resolution_us() / 1000;
57+
}
58+
59+
us_timestamp_t Timer::read_high_resolution_us() {
6360
core_util_critical_section_enter();
6461
us_timestamp_t time = _time + slicetime();
6562
core_util_critical_section_exit();
66-
return time / 1000;
63+
return time;
6764
}
6865

6966
us_timestamp_t Timer::slicetime() {
70-
core_util_critical_section_enter();
7167
us_timestamp_t ret = 0;
68+
core_util_critical_section_enter();
7269
if (_running) {
7370
ret = ticker_read_us(_ticker_data) - _start;
7471
}

drivers/Timer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class Timer {
8282
*/
8383
operator float();
8484

85+
/** Get in a high resolution type the time passed in micro-seconds.
86+
*/
87+
us_timestamp_t read_high_resolution_us();
88+
8589
protected:
8690
us_timestamp_t slicetime();
8791
int _running; // whether the timer is running

0 commit comments

Comments
 (0)