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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.