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 "drivers/TickerDataClock.h"
22 #include "platform/NonCopyable.h"
23 
24 namespace mbed {
25 
26 class CriticalSectionLock;
27 
28 /**
29  * \defgroup drivers_Timer Timer class
30  * \ingroup drivers-public-api-ticker
31  * @{
32  */
33 
34 /** A general purpose timer
35  *
36  * @note Synchronization level: Interrupt safe
37  *
38  * Example:
39  * @code
40  * // Count the time to toggle an LED
41  *
42  * #include "mbed.h"
43  *
44  * Timer timer;
45  * DigitalOut led(LED1);
46  * int begin, end;
47  *
48  * int main() {
49  * timer.start();
50  * begin = timer.read_us();
51  * led = !led;
52  * end = timer.read_us();
53  * printf("Toggle the led takes %d us", end - begin);
54  * }
55  * @endcode
56  */
57 class TimerBase {
58 
59 public:
60  /** Start the timer
61  */
62  void start();
63 
64  /** Stop the timer
65  */
66  void stop();
67 
68  /** Reset the timer to 0.
69  *
70  * If it was already running, it will continue
71  */
72  void reset();
73 
74  /** Get the time passed in seconds
75  *
76  * @returns Time passed in seconds
77  */
78  MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Floating point operators should normally be avoided for code size. If really needed, you can use `duration<float>{elapsed_time()}.count()`")
79  float read() const;
80 
81  /** Get the time passed in milliseconds
82  *
83  * @returns Time passed in milliseconds
84  */
85  MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Use the Chrono-based elapsed_time method. If integer milliseconds are needed, you can use `duration_cast<milliseconds>(elapsed_time()).count()`")
86  int read_ms() const;
87 
88  /** Get the time passed in microseconds
89  *
90  * @returns Time passed in microseconds
91  */
92  MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Use the Chrono-based elapsed_time method. If integer microseconds are needed, you can use `elapsed_time().count()`")
93  int read_us() const;
94 
95  /** An operator shorthand for read()
96  */
97  MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Floating point operators should normally be avoided for code size. If really needed, you can use `duration<float>{elapsed_time()}.count()`")
98  operator float() const;
99 
100  /** Get in a high resolution type the time passed in microseconds.
101  * Returns a 64 bit integer.
102  */
103  MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Use the Chrono-based elapsed_time method. If integer microseconds are needed, you can use `elapsed_time().count()`")
104  us_timestamp_t read_high_resolution_us() const;
105 
106  /** Get in a high resolution type the time passed in microseconds.
107  * Returns a 64 bit integer chrono duration.
108  */
109  std::chrono::microseconds elapsed_time() const;
110 
111 #if !defined(DOXYGEN_ONLY)
112 protected:
113  TimerBase(const ticker_data_t *data);
114  TimerBase(const ticker_data_t *data, bool lock_deepsleep);
115  TimerBase(const TimerBase &t);
116  TimerBase(TimerBase &&t);
117  ~TimerBase();
118 
119  const TimerBase &operator=(const TimerBase &) = delete;
120 
121  std::chrono::microseconds slicetime() const;
122  TickerDataClock::time_point _start{}; // the start time of the latest slice
123  std::chrono::microseconds _time{}; // any accumulated time from previous slices
124  TickerDataClock _ticker_data;
125  bool _lock_deepsleep; // flag that indicates if deep sleep should be disabled
126  bool _running = false; // whether the timer is running
127 
128 private:
129  // Copy storage while a lock is held
130  TimerBase(const TimerBase &t, const CriticalSectionLock &) : TimerBase(t, false) {}
131  // Copy storage only - used by delegating constructors
132  TimerBase(const TimerBase &t, bool) : _start(t._start), _time(t._time), _ticker_data(t._ticker_data), _lock_deepsleep(t._lock_deepsleep), _running(t._running) {}
133 };
134 #endif
135 
136 class Timer : public TimerBase {
137 public:
138  Timer();
139 };
140 /** @}*/
141 
142 } // namespace mbed
143 
144 #endif
Definition: ATHandler.h:46
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
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.