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 Tom Doyle

Committer:
tedoyle
Date:
Sun Mar 27 20:44:54 2016 +0000
Revision:
2:e89b02820e93
Parent:
1:497fba179833
Child:
3:8396d3e6eb62
Reorganized code to make it easier to read.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tedoyle 0:1e0e79e82839 1 #ifndef __NEW_TIMER64__
tedoyle 0:1e0e79e82839 2 #define __NEW_TIMER64__
tedoyle 0:1e0e79e82839 3
tedoyle 0:1e0e79e82839 4 #include "platform.h"
tedoyle 0:1e0e79e82839 5 #include "ticker_api.h"
tedoyle 0:1e0e79e82839 6 #include <stdint.h>
tedoyle 0:1e0e79e82839 7 #include "rtos.h"
tedoyle 0:1e0e79e82839 8
tedoyle 1:497fba179833 9 #define TIMER64_OK 0
tedoyle 0:1e0e79e82839 10
tedoyle 1:497fba179833 11 #define TIMER64_ERROR_NOT_INITIALIZED -1
tedoyle 0:1e0e79e82839 12
tedoyle 1:497fba179833 13 #define TIMER64_WARNING_ALREADY_INITIALIZED 1
tedoyle 1:497fba179833 14 #define TIMER64_WARNING_ALREADY_RUNNING 2
tedoyle 1:497fba179833 15 #define TIMER64_WARNING_ALREADY_STOPPED 3
tedoyle 2:e89b02820e93 16 #define TIMER64_WARNING_ALREADY_RELEASED 4
tedoyle 0:1e0e79e82839 17
tedoyle 0:1e0e79e82839 18 #define TIMER64_15MINUTE_ROLLOVER_CHECK_IN_MSECS 900000
tedoyle 0:1e0e79e82839 19 #define TIMER64_MIN_ROLLOVER_CHECK_IN_MSECS 60000 // 0:01:00.000
tedoyle 0:1e0e79e82839 20 #define TIMER64_MAX_ROLLOVER_CHECK_IN_MSECS 2147483 // 0:35:47.483
tedoyle 0:1e0e79e82839 21
tedoyle 0:1e0e79e82839 22 class Timer64
tedoyle 0:1e0e79e82839 23 {
tedoyle 0:1e0e79e82839 24
tedoyle 0:1e0e79e82839 25 public:
tedoyle 0:1e0e79e82839 26 Timer64();
tedoyle 1:497fba179833 27 ~Timer64();
tedoyle 0:1e0e79e82839 28
tedoyle 1:497fba179833 29 /** Initialize the timer - this must be called before the timer can be used
tedoyle 0:1e0e79e82839 30 */
tedoyle 0:1e0e79e82839 31 int init(uint32_t rolloverCheckTimeInMsecc = TIMER64_15MINUTE_ROLLOVER_CHECK_IN_MSECS);
tedoyle 0:1e0e79e82839 32
tedoyle 0:1e0e79e82839 33 /** Release the timer
tedoyle 0:1e0e79e82839 34 */
tedoyle 0:1e0e79e82839 35 int release(void);
tedoyle 0:1e0e79e82839 36
tedoyle 0:1e0e79e82839 37 /** Start the timer
tedoyle 0:1e0e79e82839 38 */
tedoyle 0:1e0e79e82839 39 int start(void);
tedoyle 0:1e0e79e82839 40
tedoyle 0:1e0e79e82839 41 /** Stop the timer
tedoyle 0:1e0e79e82839 42 */
tedoyle 0:1e0e79e82839 43 int stop(void);
tedoyle 0:1e0e79e82839 44
tedoyle 0:1e0e79e82839 45 /** Reset the timer to 0.
tedoyle 0:1e0e79e82839 46 *
tedoyle 0:1e0e79e82839 47 * If it was already counting, it will continue
tedoyle 0:1e0e79e82839 48 */
tedoyle 0:1e0e79e82839 49 int reset(void);
tedoyle 0:1e0e79e82839 50
tedoyle 0:1e0e79e82839 51 /** Get the time passed in seconds
tedoyle 0:1e0e79e82839 52 */
tedoyle 0:1e0e79e82839 53 double read(int* status = NULL);
tedoyle 0:1e0e79e82839 54
tedoyle 1:497fba179833 55 /** Get the time passed in milli-seconds
tedoyle 0:1e0e79e82839 56 */
tedoyle 0:1e0e79e82839 57 uint64_t read_ms(int* status = NULL);
tedoyle 0:1e0e79e82839 58
tedoyle 0:1e0e79e82839 59 /** Get the time passed in micro-seconds
tedoyle 0:1e0e79e82839 60 */
tedoyle 0:1e0e79e82839 61 uint64_t read_us(int* status = NULL);
tedoyle 0:1e0e79e82839 62
tedoyle 0:1e0e79e82839 63 private:
tedoyle 0:1e0e79e82839 64 bool _timerInitialized;
tedoyle 1:497fba179833 65 bool _timerRunning; // whether the timer is running
tedoyle 0:1e0e79e82839 66 timestamp_t _tickerStartTimeUsec; // the start time of the latest slice
tedoyle 0:1e0e79e82839 67 uint64_t _totalTimeUsec; // any accumulated time from previous slices
tedoyle 0:1e0e79e82839 68 const ticker_data_t *_ticker_data;
tedoyle 0:1e0e79e82839 69 RtosTimer* _rollOverCheckTimer;
tedoyle 0:1e0e79e82839 70 uint32_t _rollOverCheckTimerPeriodInMsec;
tedoyle 0:1e0e79e82839 71 static void _rollOverCheck(void const* args);
tedoyle 0:1e0e79e82839 72 };
tedoyle 0:1e0e79e82839 73
tedoyle 0:1e0e79e82839 74 #endif