1 // ==== Timer Management Functions ====
3 \addtogroup CMSIS_RTOS_TimerMgmt Timer Management
5 \brief Create and control timer and timer callback functions.
7 In addition to the \ref CMSIS_RTOS_Wait CMSIS-RTOS also supports virtual timer objects. These timer objects can trigger the execution of a function (not threads). When a timer expires, a callback function is executed to run associated code with the timer. Each timer can be configured as a one-shot or a periodic timer. A periodic timer repeats its operation until it is \ref osTimerDelete "deleted" or \ref osTimerStop "stopped". All timers can be \ref osTimerStart "started, restarted", or \ref osTimerStop "stopped".
9 \note Timer management functions cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
11 The figure below shows the behavior of a periodic timer. For one-shot timers, the timer stops after execution of the callback function.
13 \image html "Timer.png" "Behavior of a Periodic Timer"
17 The following steps are required to use a software timer:
20 osTimerId_t one_shot_id, periodic_id;
22 -# Define callback functions:
24 static void one_shot_Callback (void *argument) {
25 int32_t arg = (int32_t)argument; // cast back argument '0'
26 // do something, i.e. set thread/event flags
28 static void periodic_Callback (void *argument) {
29 int32_t arg = (int32_t)argument; // cast back argument '5'
30 // do something, i.e. set thread/event flags
33 -# Instantiate and start the timers:
35 // creates a one-shot timer:
36 one_shot_id = osTimerNew(one_shot_Callback, osTimerOnce, (void *)0, NULL); // (void*)0 is passed as an argument
37 // to the callback function
38 // creates a periodic timer:
39 periodic_id = osTimerNew(periodic_Callback, osTimerPeriodic, (void *)5, NULL); // (void*)5 is passed as an argument
40 // to the callback function
41 osTimerStart(one_shot_id, 500U);
42 osTimerStart(periodic_id, 1500U);
44 // start the one-shot timer again after it has triggered the first time:
45 osTimerStart(one_shot_id, 500U);
47 // when timers are not needed any longer free the resources:
48 osTimerDelete(one_shot_id);
49 osTimerDelete(periodic_id);
54 \addtogroup CMSIS_RTOS_TimerMgmt
59 \typedef osTimerType_t
61 The \b osTimerType_t specifies the timer type as repeating (periodic) or one-shot. Used in the function \ref osTimerNew.
63 \var osTimerType_t::osTimerOnce
65 The timer is not automatically restarted once it has elapsed. It can be restarted manually using \ref osTimerStart as needed.
67 \var osTimerType_t::osTimerPeriodic
69 The timer repeats automatically and triggers the callback continuously while running, see \ref osTimerStart and \ref osTimerStop.
75 Instances of this type hold a reference to a timer object. \n
81 \typedef void (*osTimerFunc_t) (void *argument)
83 The timer callback function is called every time the timer elapses.
85 The callback might be executed either in a dedicated timer thread or in interrupt context. Thus it is recommended to only
86 use ISR callable functions from the timer callback.
88 \param[in] argument The argument provided to \ref osTimerNew.
94 Specifies the following attributes for the \ref osTimerNew function.
100 \fn osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
102 The function \b osTimerNew creates an one-shot or periodic timer and associates it with a callback function with \a argument.
103 The timer is in stopped state until it is started with \ref osTimerStart. The function can be safely called before the RTOS
104 is started (call to \ref osKernelStart), but not before it is initialized (call to \ref osKernelInitialize).
106 The function \b osTimerNew returns the pointer to the timer object identifier or \token{NULL} in case of an error.
108 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
112 #include "cmsis_os2.h"
114 void Timer1_Callback (void *arg); // prototypes for timer callback function
115 void Timer2_Callback (void *arg); // prototypes for timer callback function
117 uint32_t exec1; // argument for the timer call back function
118 uint32_t exec2; // argument for the timer call back function
120 void TimerCreate_example (void) {
121 osTimerId_t id1; // timer id
122 osTimerId_t id2; // timer id
124 // Create one-shoot timer
126 id1 = osTimerNew(Timer1_Callback, osTimerOnce, &exec1, NULL);
128 // One-shoot timer created
131 // Create periodic timer
133 id2 = osTimerNew(Timer2_Callback, osTimerPeriodic, &exec2, NULL);
135 // Periodic timer created
143 \fn const char *osTimerGetName (osTimerId_t timer_id)
145 The function \b osTimerGetName returns the pointer to the name string of the timer identified by parameter \a timer_id or
146 \token{NULL} in case of an error.
148 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
152 \fn osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks)
154 The function \b osTimerStart starts or restarts a timer specified by the parameter \a timer_id. The parameter \a ticks
155 specifies the value of the timer in \ref CMSIS_RTOS_TimeOutValue "time ticks".
157 Possible \ref osStatus_t return values:
158 - \em osOK: the specified timer has been started or restarted.
159 - \em osErrorISR: \b osTimerStart cannot be called from interrupt service routines.
160 - \em osErrorParameter: parameter \a timer_id is either \token{NULL} or invalid or \a ticks is incorrect.
161 - \em osErrorResource: the timer is in an invalid state.
162 - \em osErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified timer.
164 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
168 #include "cmsis_os2.h"
170 void Timer_Callback (void *arg) { // timer callback function
171 // arg contains &exec
172 // called every second after osTimerStart
175 uint32_t exec; // argument for the timer call back function
177 void TimerStart_example (void) {
178 osTimerId_t id; // timer id
179 uint32_t timerDelay; // timer value
180 osStatus_t status; // function return status
182 // Create periodic timer
184 id = osTimerNew(Timer_Callback, osTimerPeriodic, &exec, NULL);
187 status = osTimerStart(id, timerDelay); // start timer
188 if (status != osOK) {
189 // Timer could not be started
198 \fn osStatus_t osTimerStop (osTimerId_t timer_id)
200 The function \b osTimerStop stops a timer specified by the parameter \a timer_id.
202 Possible \ref osStatus_t return values:
203 - \em osOK: the specified timer has been stopped.
204 - \em osErrorISR: \b osTimerStop cannot be called from interrupt service routines.
205 - \em osErrorParameter: parameter \a timer_id is either \token{NULL} or invalid.
206 - \em osErrorResource: the timer is not running (you can only stop a running timer).
207 - \em osErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified timer.
209 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
213 #include "cmsis_os2.h"
215 void Timer_Callback (void *arg); // prototype for timer callback function
217 uint32_t exec; // argument for the timer call back function
219 void TimerStop_example (void) {
220 osTimerId_t id; // timer id
221 osStatus_t status; // function return status
223 // Create periodic timer
225 id = osTimerNew(Timer_Callback, osTimerPeriodic, &exec, NULL);
226 osTimerStart(id, 1000U); // start timer
228 status = osTimerStop(id); // stop timer
229 if (status != osOK) {
230 // Timer could not be stopped
233 osTimerStart(id, 1000U); // start timer again
240 \fn uint32_t osTimerIsRunning (osTimerId_t timer_id)
242 The function \b osTimerIsRunning checks whether a timer specified by parameter \a timer_id is running. It returns \token{1}
243 if the timer is running and \token{0} if the timer is stopped or an error occurred.
245 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
249 \fn osStatus_t osTimerDelete (osTimerId_t timer_id)
251 The function \b osTimerDelete deletes the timer specified by parameter \a timer_id.
253 Possible \ref osStatus_t return values:
254 - \em osOK: the specified timer has been deleted.
255 - \em osErrorISR: \b osTimerDelete cannot be called from interrupt service routines.
256 - \em osErrorParameter: parameter \a timer_id is either \token{NULL} or invalid.
257 - \em osErrorResource: the timer is in an invalid state.
258 - \em osErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified timer.
260 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
264 #include "cmsis_os2.h"
266 void Timer_Callback (void *arg); // prototype for timer callback function
268 uint32_t exec; // argument for the timer call back function
270 void TimerDelete_example (void) {
271 osTimerId_t id; // timer id
272 osStatus_t status; // function return status
274 // Create periodic timer
276 id = osTimerNew(Timer_Callback, osTimerPeriodic, &exec, NULL);
277 osTimerStart(id, 1000U); // start timer
279 status = osTimerDelete(id); // stop and delete timer
280 if (status != osOK) {
281 // Timer could not be deleted
292 // these struct members must stay outside the group to avoid double entries in documentation
294 \var osTimerAttr_t::attr_bits
296 Reserved for future use (must be set to '0' for future compatibility).
298 \var osTimerAttr_t::cb_mem
300 Pointer to a memory for the timer control block object. Refer to \ref CMSIS_RTOS_MemoryMgmt_Manual for more information.
302 Default: \token{NULL} to use \ref CMSIS_RTOS_MemoryMgmt_Automatic for the timer control block.
304 \var osTimerAttr_t::cb_size
306 The size (in bytes) of memory block passed with \ref cb_mem. Required value depends on the underlying kernel implementation.
308 Default: \token{0} as the default is no memory provided with \ref cb_mem.
310 \var osTimerAttr_t::name
312 Pointer to a constant string with a human readable name (displayed during debugging) of the timer object.
314 Default: \token{NULL} no name specified.