Mbed OS Reference
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
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
24namespace mbed {
25
26class CriticalSectionLock;
27
28/**
29 * \file
30 * \defgroup drivers_Timer Timer classes
31 * \ingroup drivers-public-api-ticker
32 * @{
33 */
34
35/**
36 * @brief Base class for Timer and LowPowerTimer
37 *
38 * @note Synchronization level: Interrupt safe
39 */
40class TimerBase {
41
42public:
43 /**
44 * @brief Start the timer.
45 *
46 * Will lock deep sleep if this is a \c Timer instance
47 */
48 void start();
49
50 /**
51 * @brief Stop the timer
52 *
53 * Will release the deep sleep lock if this is a \c Timer instance
54 */
55 void stop();
56
57 /**
58 * @brief Reset the timer to 0.
59 *
60 * If it was already running, it will continue
61 */
62 void reset();
63
64 /**
65 * @brief Get the time passed in seconds
66 *
67 * @returns Time passed in floating-point seconds
68 */
69 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()`")
70 float read() const;
71
72 /**
73 * @brief Get the time passed in milliseconds
74 *
75 * @returns Time passed in integer milliseconds
76 */
77 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()`")
78 int read_ms() const;
79
80 /**
81 * @brief Get the time passed in microseconds
82 *
83 * @returns Time passed in integer microseconds
84 *
85 * \warning Limited to 31 bits -- will not return correct values for times larger than about 35 minutes.
86 * Use #read_high_resolution_us or #elapsed_time instead as they are not vulnerable to this problem.
87 */
88 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()`")
89 int read_us() const;
90
91 /**
92 * @brief An operator shorthand for read()
93 *
94 * @returns Time passed in floating-point seconds
95 */
96 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()`")
97 operator float() const;
98
99 /**
100 * @brief Get (in a high resolution type) the time passed in microseconds.
101 *
102 * @returns Time passed as 64-bit microseconds.
103 */
104 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()`")
105 us_timestamp_t read_high_resolution_us() const;
106
107 /**
108 * @brief Get the time passed as chrono microseconds.
109 *
110 * @returns Time passed \c as std::chrono::microseconds
111 */
112 std::chrono::microseconds elapsed_time() const;
113
114#if !defined(DOXYGEN_ONLY)
115protected:
116 TimerBase(const ticker_data_t *data);
117 TimerBase(const ticker_data_t *data, bool lock_deepsleep);
118 TimerBase(const TimerBase &t);
119 TimerBase(TimerBase &&t);
120 ~TimerBase();
121
122 const TimerBase &operator=(const TimerBase &) = delete;
123
124 std::chrono::microseconds slicetime() const;
125 TickerDataClock::time_point _start{}; // the start time of the latest slice
126 std::chrono::microseconds _time{}; // any accumulated time from previous slices
127 TickerDataClock _ticker_data;
128 bool _lock_deepsleep; // flag that indicates if deep sleep should be disabled
129 bool _running = false; // whether the timer is running
130
131private:
132 // Copy storage while a lock is held
133 TimerBase(const TimerBase &t, const CriticalSectionLock &) : TimerBase(t, false) {}
134 // Copy storage only - used by delegating constructors
135 TimerBase(const TimerBase &t, bool) : _start(t._start), _time(t._time), _ticker_data(t._ticker_data), _lock_deepsleep(t._lock_deepsleep), _running(t._running) {}
136#endif
137};
138
139/**
140 * @brief Timer implementation using the us ticker.
141 *
142 * Locks deep sleep while actively running.
143 *
144 * Example:
145 * \code{.cpp}
146 * // Count the time to toggle an LED
147 *
148 * #include "mbed.h"
149 * #include <cinttypes>
150 *
151 * Timer timer;
152 * DigitalOut led(LED1);
153 *
154 * int main() {
155 * timer.start();
156 * const auto begin = timer.elapsed_time();
157 * led = !led;
158 * const auto end = timer.elapsed_time();
159 * printf("Toggling the led takes %" PRIi64 " us", (end - begin).count());
160 * }
161 * \endcode
162 *
163 * @note When constructed, a Timer is stopped and reset to zero time.
164 *
165 * @note Synchronization level: Interrupt safe
166 */
167class Timer : public TimerBase {
168public:
169 Timer();
170};
171/** @}*/
172
173} // namespace mbed
174
175#endif
Base class for Timer and LowPowerTimer.
Definition Timer.h:40
void start()
Start the timer.
int read_us() const
Get the time passed in microseconds.
void stop()
Stop the timer.
std::chrono::microseconds elapsed_time() const
Get the time passed as chrono microseconds.
int read_ms() const
Get the time passed in milliseconds.
float read() const
Get the time passed in seconds.
void reset()
Reset the timer to 0.
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...