[Driver] Improve timer precision for ms and seconds.

pull/4094/head
Vincent Coubard 2017-03-31 15:10:57 +01:00
parent abb0a65ac5
commit 15327ca607
2 changed files with 19 additions and 12 deletions

View File

@ -31,7 +31,7 @@ Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker
void Timer::start() {
core_util_critical_section_enter();
if (!_running) {
_start = ticker_read(_ticker_data);
_start = ticker_read_us(_ticker_data);
_running = 1;
}
core_util_critical_section_exit();
@ -46,24 +46,31 @@ void Timer::stop() {
int Timer::read_us() {
core_util_critical_section_enter();
int time = _time + slicetime();
us_timestamp_t time = _time + slicetime();
core_util_critical_section_exit();
return time;
}
float Timer::read() {
return (float)read_us() / 1000000.0f;
core_util_critical_section_enter();
us_timestamp_t time = _time + slicetime();
core_util_critical_section_exit();
time = time / 1000;
return (float)read_ms() / 1000.0f;
}
int Timer::read_ms() {
return read_us() / 1000;
core_util_critical_section_enter();
us_timestamp_t time = _time + slicetime();
core_util_critical_section_exit();
return time / 1000;
}
int Timer::slicetime() {
us_timestamp_t Timer::slicetime() {
core_util_critical_section_enter();
int ret = 0;
us_timestamp_t ret = 0;
if (_running) {
ret = ticker_read(_ticker_data) - _start;
ret = ticker_read_us(_ticker_data) - _start;
}
core_util_critical_section_exit();
return ret;
@ -71,7 +78,7 @@ int Timer::slicetime() {
void Timer::reset() {
core_util_critical_section_enter();
_start = ticker_read(_ticker_data);
_start = ticker_read_us(_ticker_data);
_time = 0;
core_util_critical_section_exit();
}

View File

@ -83,10 +83,10 @@ public:
operator float();
protected:
int slicetime();
int _running; // whether the timer is running
unsigned int _start; // the start time of the latest slice
int _time; // any accumulated time from previous slices
us_timestamp_t slicetime();
int _running; // whether the timer is running
us_timestamp_t _start; // the start time of the latest slice
us_timestamp_t _time; // any accumulated time from previous slices
const ticker_data_t *_ticker_data;
};