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