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