64 bit Timer Class.

Committer:
tedoyle
Date:
Mon Mar 28 16:31:42 2016 +0000
Revision:
3:8396d3e6eb62
Parent:
2:e89b02820e93
Child:
4:9ca673a83acb
Child:
6:100cf27b43aa
Added code to make Timer64 thread safe;

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 3:8396d3e6eb62 18 #define TIMER64_DEFAULT_ROLLOVER_CHECK_IN_MSECS 30000
tedoyle 3:8396d3e6eb62 19 #define TIMER64_MIN_ROLLOVER_CHECK_IN_MSECS 1000
tedoyle 3:8396d3e6eb62 20 #define TIMER64_MAX_ROLLOVER_CHECK_IN_MSECS 65535
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 3:8396d3e6eb62 51 /** Get the time passed in micro-seconds
tedoyle 0:1e0e79e82839 52 */
tedoyle 3:8396d3e6eb62 53 int read_us(uint64_t* timeInUsec = NULL);
tedoyle 0:1e0e79e82839 54
tedoyle 1:497fba179833 55 /** Get the time passed in milli-seconds
tedoyle 0:1e0e79e82839 56 */
tedoyle 3:8396d3e6eb62 57 int read_ms(uint64_t* timeInMsec = NULL);
tedoyle 0:1e0e79e82839 58
tedoyle 3:8396d3e6eb62 59 /** Get the time passed in seconds
tedoyle 0:1e0e79e82839 60 */
tedoyle 3:8396d3e6eb62 61 int read(double* timeInSec = 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 3:8396d3e6eb62 71 Semaphore* _sem;
tedoyle 3:8396d3e6eb62 72
tedoyle 0:1e0e79e82839 73 static void _rollOverCheck(void const* args);
tedoyle 3:8396d3e6eb62 74 static uint64_t _read_us(void const* args);
tedoyle 0:1e0e79e82839 75 };
tedoyle 0:1e0e79e82839 76
tedoyle 0:1e0e79e82839 77 #endif