Mistake on this page?
Report an issue in GitHub or email us
Timer.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 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 
24 namespace mbed {
25 /**
26  * \defgroup drivers_Timer Timer class
27  * \ingroup drivers-public-api-ticker
28  * @{
29  */
30 
31 /** A general purpose timer
32  *
33  * @note Synchronization level: Interrupt safe
34  *
35  * Example:
36  * @code
37  * // Count the time to toggle an LED
38  *
39  * #include "mbed.h"
40  *
41  * Timer timer;
42  * DigitalOut led(LED1);
43  * int begin, end;
44  *
45  * int main() {
46  * timer.start();
47  * begin = timer.read_us();
48  * led = !led;
49  * end = timer.read_us();
50  * printf("Toggle the led takes %d us", end - begin);
51  * }
52  * @endcode
53  */
54 class Timer : private NonCopyable<Timer> {
55 
56 public:
57  Timer();
58  Timer(const ticker_data_t *data);
59  ~Timer();
60 
61  /** Start the timer
62  */
63  void start();
64 
65  /** Stop the timer
66  */
67  void stop();
68 
69  /** Reset the timer to 0.
70  *
71  * If it was already running, it will continue
72  */
73  void reset();
74 
75  /** Get the time passed in seconds
76  *
77  * @returns Time passed in seconds
78  */
79  float read();
80 
81  /** Get the time passed in milliseconds
82  *
83  * @returns Time passed in milliseconds
84  */
85  int read_ms();
86 
87  /** Get the time passed in microseconds
88  *
89  * @returns Time passed in microseconds
90  */
91  int read_us();
92 
93  /** An operator shorthand for read()
94  */
95  operator float();
96 
97  /** Get in a high resolution type the time passed in microseconds.
98  * Returns a 64 bit integer.
99  */
100  us_timestamp_t read_high_resolution_us();
101 
102 #if !defined(DOXYGEN_ONLY)
103 protected:
104  us_timestamp_t slicetime();
105  int _running; // whether the timer is running
106  us_timestamp_t _start; // the start time of the latest slice
107  us_timestamp_t _time; // any accumulated time from previous slices
108  const ticker_data_t *_ticker_data;
109  bool _lock_deepsleep; // flag that indicates if deep sleep should be disabled
110 };
111 #endif
112 
113 /** @}*/
114 
115 } // namespace mbed
116 
117 #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
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.