mirror of https://github.com/ARMmbed/mbed-os.git
[Driver] Improve timer precision for ms and seconds.
parent
abb0a65ac5
commit
15327ca607
|
@ -31,7 +31,7 @@ Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker
|
||||||
void Timer::start() {
|
void Timer::start() {
|
||||||
core_util_critical_section_enter();
|
core_util_critical_section_enter();
|
||||||
if (!_running) {
|
if (!_running) {
|
||||||
_start = ticker_read(_ticker_data);
|
_start = ticker_read_us(_ticker_data);
|
||||||
_running = 1;
|
_running = 1;
|
||||||
}
|
}
|
||||||
core_util_critical_section_exit();
|
core_util_critical_section_exit();
|
||||||
|
@ -46,24 +46,31 @@ void Timer::stop() {
|
||||||
|
|
||||||
int Timer::read_us() {
|
int Timer::read_us() {
|
||||||
core_util_critical_section_enter();
|
core_util_critical_section_enter();
|
||||||
int time = _time + slicetime();
|
us_timestamp_t time = _time + slicetime();
|
||||||
core_util_critical_section_exit();
|
core_util_critical_section_exit();
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Timer::read() {
|
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() {
|
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();
|
core_util_critical_section_enter();
|
||||||
int ret = 0;
|
us_timestamp_t ret = 0;
|
||||||
if (_running) {
|
if (_running) {
|
||||||
ret = ticker_read(_ticker_data) - _start;
|
ret = ticker_read_us(_ticker_data) - _start;
|
||||||
}
|
}
|
||||||
core_util_critical_section_exit();
|
core_util_critical_section_exit();
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -71,7 +78,7 @@ int Timer::slicetime() {
|
||||||
|
|
||||||
void Timer::reset() {
|
void Timer::reset() {
|
||||||
core_util_critical_section_enter();
|
core_util_critical_section_enter();
|
||||||
_start = ticker_read(_ticker_data);
|
_start = ticker_read_us(_ticker_data);
|
||||||
_time = 0;
|
_time = 0;
|
||||||
core_util_critical_section_exit();
|
core_util_critical_section_exit();
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,10 +83,10 @@ public:
|
||||||
operator float();
|
operator float();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int slicetime();
|
us_timestamp_t slicetime();
|
||||||
int _running; // whether the timer is running
|
int _running; // whether the timer is running
|
||||||
unsigned int _start; // the start time of the latest slice
|
us_timestamp_t _start; // the start time of the latest slice
|
||||||
int _time; // any accumulated time from previous slices
|
us_timestamp_t _time; // any accumulated time from previous slices
|
||||||
const ticker_data_t *_ticker_data;
|
const ticker_data_t *_ticker_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue