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
Timer64.h@0:1e0e79e82839, 2016-03-26 (annotated)
- Committer:
- tedoyle
- Date:
- Sat Mar 26 21:44:41 2016 +0000
- Revision:
- 0:1e0e79e82839
- Child:
- 1:497fba179833
Updated;
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:1e0e79e82839 | 9 | #define TIMER64_OK 0 |
tedoyle | 0:1e0e79e82839 | 10 | |
tedoyle | 0:1e0e79e82839 | 11 | #define TIMER64_ERROR_NOT_INITIALIZED -1 |
tedoyle | 0:1e0e79e82839 | 12 | |
tedoyle | 0:1e0e79e82839 | 13 | #define TIMER64_WARNING_ALREADY_INITIALIZED 1 |
tedoyle | 0:1e0e79e82839 | 14 | #define TIMER64_WARNING_ALREADY_RUNNING 2 |
tedoyle | 0:1e0e79e82839 | 15 | #define TIMER64_WARNING_ALREADY_STOPPED 3 |
tedoyle | 0:1e0e79e82839 | 16 | |
tedoyle | 0:1e0e79e82839 | 17 | #define TIMER64_15MINUTE_ROLLOVER_CHECK_IN_MSECS 900000 |
tedoyle | 0:1e0e79e82839 | 18 | #define TIMER64_MIN_ROLLOVER_CHECK_IN_MSECS 60000 // 0:01:00.000 |
tedoyle | 0:1e0e79e82839 | 19 | #define TIMER64_MAX_ROLLOVER_CHECK_IN_MSECS 2147483 // 0:35:47.483 |
tedoyle | 0:1e0e79e82839 | 20 | |
tedoyle | 0:1e0e79e82839 | 21 | class Timer64 |
tedoyle | 0:1e0e79e82839 | 22 | { |
tedoyle | 0:1e0e79e82839 | 23 | |
tedoyle | 0:1e0e79e82839 | 24 | public: |
tedoyle | 0:1e0e79e82839 | 25 | Timer64(); |
tedoyle | 0:1e0e79e82839 | 26 | Timer64(const ticker_data_t *data); |
tedoyle | 0:1e0e79e82839 | 27 | |
tedoyle | 0:1e0e79e82839 | 28 | /** Initialize the timer |
tedoyle | 0:1e0e79e82839 | 29 | */ |
tedoyle | 0:1e0e79e82839 | 30 | int init(uint32_t rolloverCheckTimeInMsecc = TIMER64_15MINUTE_ROLLOVER_CHECK_IN_MSECS); |
tedoyle | 0:1e0e79e82839 | 31 | |
tedoyle | 0:1e0e79e82839 | 32 | /** Release the timer |
tedoyle | 0:1e0e79e82839 | 33 | */ |
tedoyle | 0:1e0e79e82839 | 34 | int release(void); |
tedoyle | 0:1e0e79e82839 | 35 | |
tedoyle | 0:1e0e79e82839 | 36 | /** Start the timer |
tedoyle | 0:1e0e79e82839 | 37 | */ |
tedoyle | 0:1e0e79e82839 | 38 | int start(void); |
tedoyle | 0:1e0e79e82839 | 39 | |
tedoyle | 0:1e0e79e82839 | 40 | /** Stop the timer |
tedoyle | 0:1e0e79e82839 | 41 | */ |
tedoyle | 0:1e0e79e82839 | 42 | int stop(void); |
tedoyle | 0:1e0e79e82839 | 43 | |
tedoyle | 0:1e0e79e82839 | 44 | /** Reset the timer to 0. |
tedoyle | 0:1e0e79e82839 | 45 | * |
tedoyle | 0:1e0e79e82839 | 46 | * If it was already counting, it will continue |
tedoyle | 0:1e0e79e82839 | 47 | */ |
tedoyle | 0:1e0e79e82839 | 48 | int reset(void); |
tedoyle | 0:1e0e79e82839 | 49 | |
tedoyle | 0:1e0e79e82839 | 50 | /** Get the time passed in seconds |
tedoyle | 0:1e0e79e82839 | 51 | */ |
tedoyle | 0:1e0e79e82839 | 52 | double read(int* status = NULL); |
tedoyle | 0:1e0e79e82839 | 53 | |
tedoyle | 0:1e0e79e82839 | 54 | /** Get the time passed in mili-seconds |
tedoyle | 0:1e0e79e82839 | 55 | */ |
tedoyle | 0:1e0e79e82839 | 56 | uint64_t read_ms(int* status = NULL); |
tedoyle | 0:1e0e79e82839 | 57 | |
tedoyle | 0:1e0e79e82839 | 58 | /** Get the time passed in micro-seconds |
tedoyle | 0:1e0e79e82839 | 59 | */ |
tedoyle | 0:1e0e79e82839 | 60 | uint64_t read_us(int* status = NULL); |
tedoyle | 0:1e0e79e82839 | 61 | |
tedoyle | 0:1e0e79e82839 | 62 | #ifdef MBED_OPERATORS |
tedoyle | 0:1e0e79e82839 | 63 | operator double(); |
tedoyle | 0:1e0e79e82839 | 64 | operator uint64_t(); |
tedoyle | 0:1e0e79e82839 | 65 | operator uint32_t(); |
tedoyle | 0:1e0e79e82839 | 66 | #endif |
tedoyle | 0:1e0e79e82839 | 67 | |
tedoyle | 0:1e0e79e82839 | 68 | private: |
tedoyle | 0:1e0e79e82839 | 69 | bool _timerInitialized; |
tedoyle | 0:1e0e79e82839 | 70 | bool _timerRunning; // whether the timer is running |
tedoyle | 0:1e0e79e82839 | 71 | timestamp_t _tickerStartTimeUsec; // the start time of the latest slice |
tedoyle | 0:1e0e79e82839 | 72 | uint64_t _totalTimeUsec; // any accumulated time from previous slices |
tedoyle | 0:1e0e79e82839 | 73 | const ticker_data_t *_ticker_data; |
tedoyle | 0:1e0e79e82839 | 74 | RtosTimer* _rollOverCheckTimer; |
tedoyle | 0:1e0e79e82839 | 75 | uint32_t _rollOverCheckTimerPeriodInMsec; |
tedoyle | 0:1e0e79e82839 | 76 | static void _rollOverCheck(void const* args); |
tedoyle | 0:1e0e79e82839 | 77 | }; |
tedoyle | 0:1e0e79e82839 | 78 | |
tedoyle | 0:1e0e79e82839 | 79 | #endif |