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
Timer64Simple.h@9:79633fe7d95b, 2017-06-09 (annotated)
- Committer:
- andriym
- Date:
- Fri Jun 09 13:32:48 2017 +0000
- Revision:
- 9:79633fe7d95b
- Parent:
- Timer64.h@7:381bafbb2218
Simplified version (removed semaphore and rollover check timer).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tedoyle | 4:9ca673a83acb | 1 | /* mbed Microcontroller Library |
tedoyle | 4:9ca673a83acb | 2 | * Copyright (c) 2006-2016 ARM Limited |
tedoyle | 4:9ca673a83acb | 3 | * |
tedoyle | 4:9ca673a83acb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
tedoyle | 4:9ca673a83acb | 5 | * you may not use this file except in compliance with the License. |
tedoyle | 4:9ca673a83acb | 6 | * You may obtain a copy of the License at |
tedoyle | 4:9ca673a83acb | 7 | * |
tedoyle | 4:9ca673a83acb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
tedoyle | 4:9ca673a83acb | 9 | * |
tedoyle | 4:9ca673a83acb | 10 | * Unless required by applicable law or agreed to in writing, software |
tedoyle | 4:9ca673a83acb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
tedoyle | 4:9ca673a83acb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
tedoyle | 4:9ca673a83acb | 13 | * See the License for the specific language governing permissions and |
tedoyle | 4:9ca673a83acb | 14 | * limitations under the License. |
tedoyle | 4:9ca673a83acb | 15 | * |
andriym | 9:79633fe7d95b | 16 | * Modified by Tom Doyle, 2016 |
andriym | 9:79633fe7d95b | 17 | * <ThomasEDoyle@gmail.com> |
tedoyle | 4:9ca673a83acb | 18 | * |
andriym | 9:79633fe7d95b | 19 | * Modified by Andriy Makukha, 2017 |
andriym | 9:79633fe7d95b | 20 | * <andriy@mzjtechnology.com> |
andriym | 9:79633fe7d95b | 21 | * Shenzhen MZJ Technology, Co. |
tedoyle | 4:9ca673a83acb | 22 | */ |
tedoyle | 4:9ca673a83acb | 23 | |
andriym | 9:79633fe7d95b | 24 | #ifndef __TIMER64SIMPLE__ |
andriym | 9:79633fe7d95b | 25 | #define __TIMER64SIMPLE__ |
tedoyle | 0:1e0e79e82839 | 26 | |
tedoyle | 0:1e0e79e82839 | 27 | #include "platform.h" |
tedoyle | 0:1e0e79e82839 | 28 | #include "ticker_api.h" |
tedoyle | 0:1e0e79e82839 | 29 | #include <stdint.h> |
tedoyle | 0:1e0e79e82839 | 30 | |
andriym | 9:79633fe7d95b | 31 | #define TIMER64_TOTAL_TIME_INIT 0llu |
tedoyle | 0:1e0e79e82839 | 32 | |
andriym | 9:79633fe7d95b | 33 | /** Timer64Simple class |
andriym | 9:79633fe7d95b | 34 | * Used to define a 64 bit timer that is NOT thread safe. The Timer64Simple |
andriym | 9:79633fe7d95b | 35 | * behaves in a similiar fashion to the mbed Timer class with the most notable |
andriym | 9:79633fe7d95b | 36 | * exception that the Timer64Simple will not rollover after 2^31 microseconds |
andriym | 9:79633fe7d95b | 37 | * (approximately 35.8 minutes). The Timer64Simple extends the rollover time |
andriym | 9:79633fe7d95b | 38 | * to 2^64 microseconds (more than 584,000 years!) |
tedoyle | 4:9ca673a83acb | 39 | * |
andriym | 9:79633fe7d95b | 40 | * Unlike the Timer64 class by Tom Doyle, Timer64Simple class is simplified to |
andriym | 9:79633fe7d95b | 41 | * be NOT thread safe. It is also assumed that read(), read_ms() or read_us() |
andriym | 9:79633fe7d95b | 42 | * functions are called at least once per 17 minutes (preferably faster!) |
andriym | 9:79633fe7d95b | 43 | * while the timer is running (since this class lacks internal rollover check |
andriym | 9:79633fe7d95b | 44 | * timer). In fact, Timer64Simple is simplifed to the point where it's almost |
andriym | 9:79633fe7d95b | 45 | * trivial. |
tedoyle | 4:9ca673a83acb | 46 | * |
andriym | 9:79633fe7d95b | 47 | * If you need smarter version, check out the original code here: |
andriym | 9:79633fe7d95b | 48 | * https://developer.mbed.org/users/tedoyle/code/Timer64/ |
tedoyle | 4:9ca673a83acb | 49 | * |
andriym | 9:79633fe7d95b | 50 | * Demo program: |
tedoyle | 4:9ca673a83acb | 51 | * @code |
tedoyle | 4:9ca673a83acb | 52 | * #include "mbed.h" |
andriym | 9:79633fe7d95b | 53 | * #include "Timer64Simple.h" |
tedoyle | 4:9ca673a83acb | 54 | * |
andriym | 9:79633fe7d95b | 55 | * Timer64Simple timer64; |
tedoyle | 4:9ca673a83acb | 56 | * |
andriym | 9:79633fe7d95b | 57 | * int main() |
andriym | 9:79633fe7d95b | 58 | * { |
tedoyle | 4:9ca673a83acb | 59 | * timer64.start(); |
andriym | 9:79633fe7d95b | 60 | * wait_ms(100); |
tedoyle | 4:9ca673a83acb | 61 | * timer64.stop(); |
andriym | 9:79633fe7d95b | 62 | * wait_ms(100); |
andriym | 9:79633fe7d95b | 63 | * printf("Test - Elapsed time = %llu usec\r\n", timer64.read_us()); // %llu -- unsigned long long int |
andriym | 9:79633fe7d95b | 64 | * |
tedoyle | 4:9ca673a83acb | 65 | * timer64.reset(); |
andriym | 9:79633fe7d95b | 66 | * timer64.start(); |
tedoyle | 4:9ca673a83acb | 67 | * while (1) |
tedoyle | 4:9ca673a83acb | 68 | * { |
andriym | 9:79633fe7d95b | 69 | * wait(60); // wait 1 minute |
andriym | 9:79633fe7d95b | 70 | * printf("Time = %llu ms\r\n", timer64.read_ms()); |
tedoyle | 4:9ca673a83acb | 71 | * } |
tedoyle | 4:9ca673a83acb | 72 | * } |
tedoyle | 4:9ca673a83acb | 73 | * @endcode |
tedoyle | 4:9ca673a83acb | 74 | */ |
tedoyle | 7:381bafbb2218 | 75 | |
andriym | 9:79633fe7d95b | 76 | class Timer64Simple |
tedoyle | 0:1e0e79e82839 | 77 | { |
tedoyle | 0:1e0e79e82839 | 78 | |
tedoyle | 0:1e0e79e82839 | 79 | public: |
andriym | 9:79633fe7d95b | 80 | Timer64Simple(); |
andriym | 9:79633fe7d95b | 81 | ~Timer64Simple(); |
andriym | 9:79633fe7d95b | 82 | |
andriym | 9:79633fe7d95b | 83 | /** Start the timer */ |
andriym | 9:79633fe7d95b | 84 | void start(void); |
tedoyle | 0:1e0e79e82839 | 85 | |
andriym | 9:79633fe7d95b | 86 | /** Stop the timer */ |
andriym | 9:79633fe7d95b | 87 | void stop(void); |
tedoyle | 0:1e0e79e82839 | 88 | |
andriym | 9:79633fe7d95b | 89 | /** Reset the timer */ |
andriym | 9:79633fe7d95b | 90 | void reset(void); |
andriym | 9:79633fe7d95b | 91 | |
andriym | 9:79633fe7d95b | 92 | /** Read the timer value in microseconds |
tedoyle | 4:9ca673a83acb | 93 | * |
tedoyle | 4:9ca673a83acb | 94 | * @returns |
andriym | 9:79633fe7d95b | 95 | * timeInUsec - current time in microseconds |
tedoyle | 0:1e0e79e82839 | 96 | */ |
andriym | 9:79633fe7d95b | 97 | uint64_t read_us(void); |
tedoyle | 0:1e0e79e82839 | 98 | |
andriym | 9:79633fe7d95b | 99 | /** Read the timer value in milliseconds |
tedoyle | 4:9ca673a83acb | 100 | * |
tedoyle | 4:9ca673a83acb | 101 | * @returns |
andriym | 9:79633fe7d95b | 102 | * timeInMsec - current time in milliseconds |
tedoyle | 0:1e0e79e82839 | 103 | */ |
andriym | 9:79633fe7d95b | 104 | uint64_t read_ms(void); |
tedoyle | 0:1e0e79e82839 | 105 | |
andriym | 9:79633fe7d95b | 106 | /** Read the timer value in seconds |
tedoyle | 4:9ca673a83acb | 107 | * |
tedoyle | 4:9ca673a83acb | 108 | * @returns |
andriym | 9:79633fe7d95b | 109 | * timeInMsec - current time in seconds |
tedoyle | 0:1e0e79e82839 | 110 | */ |
andriym | 9:79633fe7d95b | 111 | double read(void); |
andriym | 9:79633fe7d95b | 112 | |
andriym | 9:79633fe7d95b | 113 | /** Check to see if timer is running |
tedoyle | 4:9ca673a83acb | 114 | * |
tedoyle | 4:9ca673a83acb | 115 | * @returns |
andriym | 9:79633fe7d95b | 116 | * boolean running status |
tedoyle | 0:1e0e79e82839 | 117 | */ |
andriym | 9:79633fe7d95b | 118 | bool is_running(); |
tedoyle | 0:1e0e79e82839 | 119 | |
tedoyle | 0:1e0e79e82839 | 120 | private: |
tedoyle | 1:497fba179833 | 121 | bool _timerRunning; // whether the timer is running |
andriym | 9:79633fe7d95b | 122 | timestamp_t _tickerLastTimeUsec; // the start time of the latest slice |
tedoyle | 0:1e0e79e82839 | 123 | uint64_t _totalTimeUsec; // any accumulated time from previous slices |
tedoyle | 0:1e0e79e82839 | 124 | const ticker_data_t *_ticker_data; |
tedoyle | 3:8396d3e6eb62 | 125 | |
andriym | 9:79633fe7d95b | 126 | uint64_t _read_us(void); |
tedoyle | 0:1e0e79e82839 | 127 | }; |
tedoyle | 0:1e0e79e82839 | 128 | |
andriym | 9:79633fe7d95b | 129 | #endif //__TIMER64SIMPLE__ |