Pinned to some recent date
drivers/Timer.h@0:fb7af294d5d9, 2016-11-17 (annotated)
- Committer:
- Simon Cooksey
- Date:
- Thu Nov 17 16:43:53 2016 +0000
- Revision:
- 0:fb7af294d5d9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Simon Cooksey |
0:fb7af294d5d9 | 1 | /* mbed Microcontroller Library |
Simon Cooksey |
0:fb7af294d5d9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Simon Cooksey |
0:fb7af294d5d9 | 3 | * |
Simon Cooksey |
0:fb7af294d5d9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Simon Cooksey |
0:fb7af294d5d9 | 5 | * you may not use this file except in compliance with the License. |
Simon Cooksey |
0:fb7af294d5d9 | 6 | * You may obtain a copy of the License at |
Simon Cooksey |
0:fb7af294d5d9 | 7 | * |
Simon Cooksey |
0:fb7af294d5d9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Simon Cooksey |
0:fb7af294d5d9 | 9 | * |
Simon Cooksey |
0:fb7af294d5d9 | 10 | * Unless required by applicable law or agreed to in writing, software |
Simon Cooksey |
0:fb7af294d5d9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Simon Cooksey |
0:fb7af294d5d9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Simon Cooksey |
0:fb7af294d5d9 | 13 | * See the License for the specific language governing permissions and |
Simon Cooksey |
0:fb7af294d5d9 | 14 | * limitations under the License. |
Simon Cooksey |
0:fb7af294d5d9 | 15 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 16 | #ifndef MBED_TIMER_H |
Simon Cooksey |
0:fb7af294d5d9 | 17 | #define MBED_TIMER_H |
Simon Cooksey |
0:fb7af294d5d9 | 18 | |
Simon Cooksey |
0:fb7af294d5d9 | 19 | #include "platform/platform.h" |
Simon Cooksey |
0:fb7af294d5d9 | 20 | #include "hal/ticker_api.h" |
Simon Cooksey |
0:fb7af294d5d9 | 21 | |
Simon Cooksey |
0:fb7af294d5d9 | 22 | namespace mbed { |
Simon Cooksey |
0:fb7af294d5d9 | 23 | /** \addtogroup drivers */ |
Simon Cooksey |
0:fb7af294d5d9 | 24 | /** @{*/ |
Simon Cooksey |
0:fb7af294d5d9 | 25 | |
Simon Cooksey |
0:fb7af294d5d9 | 26 | /** A general purpose timer |
Simon Cooksey |
0:fb7af294d5d9 | 27 | * |
Simon Cooksey |
0:fb7af294d5d9 | 28 | * @Note Synchronization level: Interrupt safe |
Simon Cooksey |
0:fb7af294d5d9 | 29 | * |
Simon Cooksey |
0:fb7af294d5d9 | 30 | * Example: |
Simon Cooksey |
0:fb7af294d5d9 | 31 | * @code |
Simon Cooksey |
0:fb7af294d5d9 | 32 | * // Count the time to toggle a LED |
Simon Cooksey |
0:fb7af294d5d9 | 33 | * |
Simon Cooksey |
0:fb7af294d5d9 | 34 | * #include "mbed.h" |
Simon Cooksey |
0:fb7af294d5d9 | 35 | * |
Simon Cooksey |
0:fb7af294d5d9 | 36 | * Timer timer; |
Simon Cooksey |
0:fb7af294d5d9 | 37 | * DigitalOut led(LED1); |
Simon Cooksey |
0:fb7af294d5d9 | 38 | * int begin, end; |
Simon Cooksey |
0:fb7af294d5d9 | 39 | * |
Simon Cooksey |
0:fb7af294d5d9 | 40 | * int main() { |
Simon Cooksey |
0:fb7af294d5d9 | 41 | * timer.start(); |
Simon Cooksey |
0:fb7af294d5d9 | 42 | * begin = timer.read_us(); |
Simon Cooksey |
0:fb7af294d5d9 | 43 | * led = !led; |
Simon Cooksey |
0:fb7af294d5d9 | 44 | * end = timer.read_us(); |
Simon Cooksey |
0:fb7af294d5d9 | 45 | * printf("Toggle the led takes %d us", end - begin); |
Simon Cooksey |
0:fb7af294d5d9 | 46 | * } |
Simon Cooksey |
0:fb7af294d5d9 | 47 | * @endcode |
Simon Cooksey |
0:fb7af294d5d9 | 48 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 49 | class Timer { |
Simon Cooksey |
0:fb7af294d5d9 | 50 | |
Simon Cooksey |
0:fb7af294d5d9 | 51 | public: |
Simon Cooksey |
0:fb7af294d5d9 | 52 | Timer(); |
Simon Cooksey |
0:fb7af294d5d9 | 53 | Timer(const ticker_data_t *data); |
Simon Cooksey |
0:fb7af294d5d9 | 54 | |
Simon Cooksey |
0:fb7af294d5d9 | 55 | /** Start the timer |
Simon Cooksey |
0:fb7af294d5d9 | 56 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 57 | void start(); |
Simon Cooksey |
0:fb7af294d5d9 | 58 | |
Simon Cooksey |
0:fb7af294d5d9 | 59 | /** Stop the timer |
Simon Cooksey |
0:fb7af294d5d9 | 60 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 61 | void stop(); |
Simon Cooksey |
0:fb7af294d5d9 | 62 | |
Simon Cooksey |
0:fb7af294d5d9 | 63 | /** Reset the timer to 0. |
Simon Cooksey |
0:fb7af294d5d9 | 64 | * |
Simon Cooksey |
0:fb7af294d5d9 | 65 | * If it was already counting, it will continue |
Simon Cooksey |
0:fb7af294d5d9 | 66 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 67 | void reset(); |
Simon Cooksey |
0:fb7af294d5d9 | 68 | |
Simon Cooksey |
0:fb7af294d5d9 | 69 | /** Get the time passed in seconds |
Simon Cooksey |
0:fb7af294d5d9 | 70 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 71 | float read(); |
Simon Cooksey |
0:fb7af294d5d9 | 72 | |
Simon Cooksey |
0:fb7af294d5d9 | 73 | /** Get the time passed in mili-seconds |
Simon Cooksey |
0:fb7af294d5d9 | 74 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 75 | int read_ms(); |
Simon Cooksey |
0:fb7af294d5d9 | 76 | |
Simon Cooksey |
0:fb7af294d5d9 | 77 | /** Get the time passed in micro-seconds |
Simon Cooksey |
0:fb7af294d5d9 | 78 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 79 | int read_us(); |
Simon Cooksey |
0:fb7af294d5d9 | 80 | |
Simon Cooksey |
0:fb7af294d5d9 | 81 | /** An operator shorthand for read() |
Simon Cooksey |
0:fb7af294d5d9 | 82 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 83 | operator float(); |
Simon Cooksey |
0:fb7af294d5d9 | 84 | |
Simon Cooksey |
0:fb7af294d5d9 | 85 | protected: |
Simon Cooksey |
0:fb7af294d5d9 | 86 | int slicetime(); |
Simon Cooksey |
0:fb7af294d5d9 | 87 | int _running; // whether the timer is running |
Simon Cooksey |
0:fb7af294d5d9 | 88 | unsigned int _start; // the start time of the latest slice |
Simon Cooksey |
0:fb7af294d5d9 | 89 | int _time; // any accumulated time from previous slices |
Simon Cooksey |
0:fb7af294d5d9 | 90 | const ticker_data_t *_ticker_data; |
Simon Cooksey |
0:fb7af294d5d9 | 91 | }; |
Simon Cooksey |
0:fb7af294d5d9 | 92 | |
Simon Cooksey |
0:fb7af294d5d9 | 93 | } // namespace mbed |
Simon Cooksey |
0:fb7af294d5d9 | 94 | |
Simon Cooksey |
0:fb7af294d5d9 | 95 | #endif |
Simon Cooksey |
0:fb7af294d5d9 | 96 | |
Simon Cooksey |
0:fb7af294d5d9 | 97 | /** @}*/ |