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