Mistake on this page?
Report an issue in GitHub or email us
RtosTimer.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 ARM Limited
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef RTOS_TIMER_H
23 #define RTOS_TIMER_H
24 
25 #include <stdint.h>
26 #include "rtos/mbed_rtos_types.h"
27 #include "rtos/mbed_rtos_storage.h"
28 #include "platform/Callback.h"
29 #include "platform/NonCopyable.h"
30 #include "platform/mbed_toolchain.h"
31 #include "rtos/mbed_rtos1_types.h"
32 
33 #if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
34 
35 namespace rtos {
36 /** \addtogroup rtos-public-api */
37 /** @{*/
38 
39 /**
40  * \defgroup rtos_RtosTimer RtosTimer class
41  * @{
42  */
43 
44 /** The RtosTimer class allow creating and and controlling of timer functions in the system.
45  A timer function is called when a time period expires whereby both on-shot and
46  periodic timers are possible. A timer can be started, restarted, or stopped.
47 
48  Timers are handled in the thread osTimerThread.
49  Callback functions run under control of this thread and may use CMSIS-RTOS API calls.
50 
51  @deprecated
52  The RtosTimer has been superseded by the EventQueue. The RtosTimer and EventQueue duplicate
53  the functionality of timing events outside of interrupt context, however the EventQueue
54  has additional features to handle deferring other events to multiple contexts.
55 
56  For an example, the following code shows a simple use of the RtosTimer:
57  @code
58  DigitalOut led(LED1);
59  void blink() {
60  led = !led;
61  }
62 
63  RtosTimer timer(&blink);
64  int main() {
65  timer.start(1000); // call blink every 1s
66  ThisThread::sleep_for(5000);
67  timer.stop(); // stop after 5s
68  }
69  @endcode
70 
71  This is the above example rewritten to use the EventQueue:
72  @code
73  DigitalOut led(LED1);
74  void blink() {
75  led = !led;
76  }
77 
78  EventQueue queue(4*EVENTS_EVENT_SIZE);
79  int main() {
80  int blink_id = queue.call_every(1000, &blink); // call blink every 1s
81  queue.dispatch(5000);
82  queue.cancel(blink_id); // stop after 5s
83  }
84  @endcode
85 
86  @note
87  Memory considerations: The timer control structures will be created on current thread's stack, both for the mbed OS
88  and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
89 */
90 class RtosTimer : private mbed::NonCopyable<RtosTimer> {
91 public:
92  /** Create timer.
93  @param func function to be executed by this timer.
94  @param type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic)
95  @param argument argument to the timer call back function. (default: nullptr)
96  @deprecated Replaced with RtosTimer(Callback<void()>, os_timer_type)
97  @deprecated
98  The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
99 
100  @note You cannot call this function from ISR context.
101  */
102  MBED_DEPRECATED_SINCE("mbed-os-5.1",
103  "Replaced with RtosTimer(Callback<void()>, os_timer_type)")
104  MBED_DEPRECATED_SINCE("mbed-os-5.2",
105  "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
106  RtosTimer(void (*func)(void const *argument), os_timer_type type = osTimerPeriodic, void *argument = nullptr)
107  {
108  constructor(mbed::callback((void (*)(void *))func, argument), type);
109  }
110 
111  /** Create timer.
112  @param func function to be executed by this timer.
113  @param type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic)
114  @deprecated
115  The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
116 
117  @note You cannot call this function from ISR context.
118  */
119  MBED_DEPRECATED_SINCE("mbed-os-5.2",
120  "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
121  RtosTimer(mbed::Callback<void()> func, os_timer_type type = osTimerPeriodic)
122  {
123  constructor(func, type);
124  }
125 
126  /** Create timer.
127  @param obj pointer to the object to call the member function on.
128  @param method member function to be executed by this timer.
129  @param type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic)
130  @deprecated
131  The RtosTimer constructor does not support cv-qualifiers. Replaced by
132  RtosTimer(callback(obj, method), os_timer_type).
133  @deprecated
134  The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
135 
136  @note You cannot call this function from ISR context.
137  */
138  template <typename T, typename M>
139  MBED_DEPRECATED_SINCE("mbed-os-5.1",
140  "The RtosTimer constructor does not support cv-qualifiers. Replaced by "
141  "RtosTimer(callback(obj, method), os_timer_type).")
142  MBED_DEPRECATED_SINCE("mbed-os-5.2",
143  "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
144  RtosTimer(T *obj, M method, os_timer_type type = osTimerPeriodic)
145  {
146  constructor(mbed::callback(obj, method), type);
147  }
148 
149  /** Stop the timer.
150  @return status code that indicates the execution status of the function:
151  @a osOK the timer has been stopped.
152  @a osErrorISR @a stop cannot be called from interrupt service routines.
153  @a osErrorParameter internal error.
154  @a osErrorResource the timer is not running.
155 
156  @note You cannot call this function from ISR context.
157  */
158  osStatus stop(void);
159 
160  /** Start or restart the timer.
161  @param millisec non-zero value of the timer.
162  @return status code that indicates the execution status of the function:
163  @a osOK the timer has been started or restarted.
164  @a osErrorISR @a start cannot be called from interrupt service routines.
165  @a osErrorParameter internal error or incorrect parameter value.
166  @a osErrorResource internal error (the timer is in an invalid timer state).
167 
168  @note You cannot call this function from ISR context.
169  */
170  osStatus start(uint32_t millisec);
171 
172  /** RtosTimer destructor
173  *
174  * @note You cannot call this function from ISR context.
175  */
176  ~RtosTimer();
177 
178 private:
179  // Required to share definitions without
180  // delegated constructors
181  void constructor(mbed::Callback<void()> func, os_timer_type type);
182 
183  osTimerId_t _id;
184  mbed_rtos_storage_timer_t _obj_mem;
185  mbed::Callback<void()> _function;
186 };
187 /** @}*/
188 /** @}*/
189 
190 }
191 
192 #endif
193 
194 #endif
osStatus start(uint32_t millisec)
Start or restart the timer.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:169
The RtosTimer class allow creating and and controlling of timer functions in the system.
Definition: RtosTimer.h:90
osStatus stop(void)
Stop the timer.
~RtosTimer()
RtosTimer destructor.
Timer Control Block.
Definition: rtx_os.h:149
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:709
Definition: TaskBase.h:25
Callback class based on template specialization.
Definition: Callback.h:39
#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.