Skip to content

Commit 15327ca

Browse files
committed
[Driver] Improve timer precision for ms and seconds.
1 parent abb0a65 commit 15327ca

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

drivers/Timer.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker
3131
void Timer::start() {
3232
core_util_critical_section_enter();
3333
if (!_running) {
34-
_start = ticker_read(_ticker_data);
34+
_start = ticker_read_us(_ticker_data);
3535
_running = 1;
3636
}
3737
core_util_critical_section_exit();
@@ -46,32 +46,39 @@ void Timer::stop() {
4646

4747
int Timer::read_us() {
4848
core_util_critical_section_enter();
49-
int time = _time + slicetime();
49+
us_timestamp_t time = _time + slicetime();
5050
core_util_critical_section_exit();
5151
return time;
5252
}
5353

5454
float Timer::read() {
55-
return (float)read_us() / 1000000.0f;
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;
5660
}
5761

5862
int Timer::read_ms() {
59-
return read_us() / 1000;
63+
core_util_critical_section_enter();
64+
us_timestamp_t time = _time + slicetime();
65+
core_util_critical_section_exit();
66+
return time / 1000;
6067
}
6168

62-
int Timer::slicetime() {
69+
us_timestamp_t Timer::slicetime() {
6370
core_util_critical_section_enter();
64-
int ret = 0;
71+
us_timestamp_t ret = 0;
6572
if (_running) {
66-
ret = ticker_read(_ticker_data) - _start;
73+
ret = ticker_read_us(_ticker_data) - _start;
6774
}
6875
core_util_critical_section_exit();
6976
return ret;
7077
}
7178

7279
void Timer::reset() {
7380
core_util_critical_section_enter();
74-
_start = ticker_read(_ticker_data);
81+
_start = ticker_read_us(_ticker_data);
7582
_time = 0;
7683
core_util_critical_section_exit();
7784
}

drivers/Timer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ class Timer {
8383
operator float();
8484

8585
protected:
86-
int slicetime();
87-
int _running; // whether the timer is running
88-
unsigned int _start; // the start time of the latest slice
89-
int _time; // any accumulated time from previous slices
86+
us_timestamp_t slicetime();
87+
int _running; // whether the timer is running
88+
us_timestamp_t _start; // the start time of the latest slice
89+
us_timestamp_t _time; // any accumulated time from previous slices
9090
const ticker_data_t *_ticker_data;
9191
};
9292

0 commit comments

Comments
 (0)