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