Simple 64-bit timer for mbed. It is a drastically simplified version of Timer64 class by Tom Doyle. It is meant to be used in single-thread applications, where timer is used frequently, while standard 32-bit Timer is not enough.
Fork of Timer64 by
Diff: Timer64.h
- Revision:
- 0:1e0e79e82839
- Child:
- 1:497fba179833
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Timer64.h Sat Mar 26 21:44:41 2016 +0000 @@ -0,0 +1,79 @@ +#ifndef __NEW_TIMER64__ +#define __NEW_TIMER64__ + +#include "platform.h" +#include "ticker_api.h" +#include <stdint.h> +#include "rtos.h" + +#define TIMER64_OK 0 + +#define TIMER64_ERROR_NOT_INITIALIZED -1 + +#define TIMER64_WARNING_ALREADY_INITIALIZED 1 +#define TIMER64_WARNING_ALREADY_RUNNING 2 +#define TIMER64_WARNING_ALREADY_STOPPED 3 + +#define TIMER64_15MINUTE_ROLLOVER_CHECK_IN_MSECS 900000 +#define TIMER64_MIN_ROLLOVER_CHECK_IN_MSECS 60000 // 0:01:00.000 +#define TIMER64_MAX_ROLLOVER_CHECK_IN_MSECS 2147483 // 0:35:47.483 + +class Timer64 +{ + +public: + Timer64(); + Timer64(const ticker_data_t *data); + + /** Initialize the timer + */ + int init(uint32_t rolloverCheckTimeInMsecc = TIMER64_15MINUTE_ROLLOVER_CHECK_IN_MSECS); + + /** Release the timer + */ + int release(void); + + /** Start the timer + */ + int start(void); + + /** Stop the timer + */ + int stop(void); + + /** Reset the timer to 0. + * + * If it was already counting, it will continue + */ + int reset(void); + + /** Get the time passed in seconds + */ + double read(int* status = NULL); + + /** Get the time passed in mili-seconds + */ + uint64_t read_ms(int* status = NULL); + + /** Get the time passed in micro-seconds + */ + uint64_t read_us(int* status = NULL); + +#ifdef MBED_OPERATORS + operator double(); + operator uint64_t(); + operator uint32_t(); +#endif + +private: + bool _timerInitialized; + bool _timerRunning; // whether the timer is running + timestamp_t _tickerStartTimeUsec; // the start time of the latest slice + uint64_t _totalTimeUsec; // any accumulated time from previous slices + const ticker_data_t *_ticker_data; + RtosTimer* _rollOverCheckTimer; + uint32_t _rollOverCheckTimerPeriodInMsec; + static void _rollOverCheck(void const* args); +}; + +#endif