Mistake on this page?
Report an issue in GitHub or email us
RtosTimer.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2012 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 "cmsis_os2.h"
27 #include "mbed_rtos_storage.h"
28 #include "platform/Callback.h"
29 #include "platform/NonCopyable.h"
30 #include "platform/mbed_toolchain.h"
31 #include "mbed_rtos1_types.h"
32 
33 namespace rtos {
34 /** \addtogroup rtos */
35 /** @{*/
36 /**
37  * \defgroup rtos_RtosTimer RtosTimer class
38  * @{
39  */
40 
41 /** The RtosTimer class allow creating and and controlling of timer functions in the system.
42  A timer function is called when a time period expires whereby both on-shot and
43  periodic timers are possible. A timer can be started, restarted, or stopped.
44 
45  Timers are handled in the thread osTimerThread.
46  Callback functions run under control of this thread and may use CMSIS-RTOS API calls.
47 
48  @deprecated
49  The RtosTimer has been superseded by the EventQueue. The RtosTimer and EventQueue duplicate
50  the functionality of timing events outside of interrupt context, however the EventQueue
51  has additional features to handle deferring other events to multiple contexts.
52 
53  For an example, the following code shows a simple use of the RtosTimer:
54  @code
55  DigitalOut led(LED1);
56  void blink() {
57  led = !led;
58  }
59 
60  RtosTimer timer(&blink);
61  int main() {
62  timer.start(1000); // call blink every 1s
63  wait_ms(5000);
64  timer.stop(); // stop after 5s
65  }
66  @endcode
67 
68  This is the above example rewritten to use the EventQueue:
69  @code
70  DigitalOut led(LED1);
71  void blink() {
72  led = !led;
73  }
74 
75  EventQueue queue(4*EVENTS_EVENT_SIZE);
76  int main() {
77  int blink_id = queue.call_every(1000, &blink); // call blink every 1s
78  queue.dispatch(5000);
79  queue.cancel(blink_id); // stop after 5s
80  }
81  @endcode
82 
83  @note
84  Memory considerations: The timer control structures will be created on current thread's stack, both for the mbed OS
85  and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
86 */
87 class RtosTimer : private mbed::NonCopyable<RtosTimer> {
88 public:
89  /** Create timer.
90  @param func function to be executed by this timer.
91  @param type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic)
92  @param argument argument to the timer call back function. (default: NULL)
93  @deprecated Replaced with RtosTimer(Callback<void()>, os_timer_type)
94  @deprecated
95  The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
96 
97  @note You cannot call this function from ISR context.
98  */
99  MBED_DEPRECATED_SINCE("mbed-os-5.1",
100  "Replaced with RtosTimer(Callback<void()>, os_timer_type)")
101  MBED_DEPRECATED_SINCE("mbed-os-5.2",
102  "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
103  RtosTimer(void (*func)(void const *argument), os_timer_type type = osTimerPeriodic, void *argument = NULL)
104  {
105  constructor(mbed::callback((void (*)(void *))func, argument), type);
106  }
107 
108  /** Create timer.
109  @param func function to be executed by this timer.
110  @param type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic)
111  @deprecated
112  The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
113 
114  @note You cannot call this function from ISR context.
115  */
116  MBED_DEPRECATED_SINCE("mbed-os-5.2",
117  "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
118  RtosTimer(mbed::Callback<void()> func, os_timer_type type = osTimerPeriodic)
119  {
120  constructor(func, type);
121  }
122 
123  /** Create timer.
124  @param obj pointer to the object to call the member function on.
125  @param method member function to be executed by this timer.
126  @param type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic)
127  @deprecated
128  The RtosTimer constructor does not support cv-qualifiers. Replaced by
129  RtosTimer(callback(obj, method), os_timer_type).
130  @deprecated
131  The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details
132 
133  @note You cannot call this function from ISR context.
134  */
135  template <typename T, typename M>
136  MBED_DEPRECATED_SINCE("mbed-os-5.1",
137  "The RtosTimer constructor does not support cv-qualifiers. Replaced by "
138  "RtosTimer(callback(obj, method), os_timer_type).")
139  MBED_DEPRECATED_SINCE("mbed-os-5.2",
140  "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details")
141  RtosTimer(T *obj, M method, os_timer_type type = osTimerPeriodic)
142  {
143  constructor(mbed::callback(obj, method), type);
144  }
145 
146  /** Stop the timer.
147  @return status code that indicates the execution status of the function:
148  @a osOK the timer has been stopped.
149  @a osErrorISR @a stop cannot be called from interrupt service routines.
150  @a osErrorParameter internal error.
151  @a osErrorResource the timer is not running.
152 
153  @note You cannot call this function from ISR context.
154  */
155  osStatus stop(void);
156 
157  /** Start or restart the timer.
158  @param millisec non-zero value of the timer.
159  @return status code that indicates the execution status of the function:
160  @a osOK the timer has been started or restarted.
161  @a osErrorISR @a start cannot be called from interrupt service routines.
162  @a osErrorParameter internal error or incorrect parameter value.
163  @a osErrorResource internal error (the timer is in an invalid timer state).
164 
165  @note You cannot call this function from ISR context.
166  */
167  osStatus start(uint32_t millisec);
168 
169  /** RtosTimer destructor
170  *
171  * @note You cannot call this function from ISR context.
172  */
173  ~RtosTimer();
174 
175 private:
176  // Required to share definitions without
177  // delegated constructors
178  void constructor(mbed::Callback<void()> func, os_timer_type type);
179 
180  osTimerId_t _id;
181  mbed_rtos_storage_timer_t _obj_mem;
182  mbed::Callback<void()> _function;
183 };
184 /** @}*/
185 /** @}*/
186 
187 }
188 
189 #endif
190 
191 
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:168
The RtosTimer class allow creating and and controlling of timer functions in the system.
Definition: RtosTimer.h:87
Callback< R()> callback(R(*func)()=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:3848
osStatus stop(void)
Stop the timer.
~RtosTimer()
RtosTimer destructor.
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.