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