3 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
4 // ==== Timer Management Functions ====
6 \addtogroup CMSIS_RTOS_TimerMgmt Timer Management
8 \brief Create and control timer and timer callback functions.
10 In addition to the \ref CMSIS_RTOS_Wait CMSIS-RTOS also supports virtual timer objects. These timer objects can
11 trigger the execution of a function (not threads). When a timer expires, a callback function is executed to run associated
12 code with the timer. Each timer can be configured as a one-shot or a periodic timer. A periodic timer repeats its operation
13 until it is \ref osTimerDelete "deleted" or \ref osTimerStop "stopped". All timers can be
14 \ref osTimerStart "started, restarted", or \ref osTimerStop "stopped".
16 \note RTX handles Timers in the thread \b osRtxTimerThread. Callback functions run under control of this thread and may use
17 other CMSIS-RTOS API calls. The \b osRtxTimerThread is configured in \ref timerConfig.
18 \note Timer management functions cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
20 The figure below shows the behavior of a periodic timer. For one-shot timers, the timer stops after execution of the
23 \image html "Timer.png" "Behavior of a Periodic Timer"
28 The following steps are required to use a timer:
31 osTimerId_t one_shot_id, periodic_id;
33 -# Instantiate and start the timers:
35 // creates a one-shot timer:
36 one_shot_id = osTimerNew((osTimerFunc_t)&one_shot_Callback, osTimerOnce, (void *)0); // (void*)0 is passed as an argument
37 // to the callback function
39 // creates a periodic timer:
40 periodic_id = osTimerNew((osTimerFunc_t)&periodic_Callback, osTimerPeriodic, (void *)5); // (void*)5 is passed as an argument
41 // to the callback function
42 osTimerStart(one_shot_id, 500);
43 osTimerStart(periodic_id, 1500);
48 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
52 The \ref osTimerType_t specifies the a repeating (periodic) or one-shot timer for the function \ref osTimerNew.
55 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
59 Instances of this type hold a reference to a timer object.
62 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
64 \typedef void (*osTimerFunc_t) (void *argument)
68 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
74 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
76 \fn osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
78 The function \b osTimerNew creates an one-shot or periodic timer and associates it with a callback function with \a argument.
79 The timer is in stopped state until it is started with \ref osTimerStart. The function can be safely called before the RTOS
80 is started (call to \ref osKernelStart), but not before it is initialized (call to \ref osKernelInitialize).
82 The parameter \a func specifies the start address of the timer callback function.
84 The parameter \a type specifies the type of the timer (refer to \ref osTimerType_t).
86 The parameter \a attr sets the timer attributes (refer to \ref osTimerAttr_t). Default attributes will be used if set to
89 The function \b osTimerNew returns the pointer to the timer object identifier or \token{NULL} in case of an error.
91 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
95 #include "cmsis_os2.h"
97 void Timer1_Callback (void const *arg); // prototypes for timer callback function
98 void Timer2_Callback (void const *arg);
100 uint32_t exec1; // argument for the timer call back function
101 uint32_t exec2; // argument for the timer call back function
103 void TimerCreate_example (void) {
104 osTimerId_t id1; // timer id
105 osTimerId_t id2; // timer id
107 // Create one-shoot timer
109 id1 = osTimerNew ((osTimerFunc_t)&Timer1_Callback, osTimerOnce, &exec1, NULL);
111 // One-shoot timer created
114 // Create periodic timer
116 id2 = osTimerNew ((osTimerFunc_t)&Timer2_Callback, osTimerPeriodic, &exec2, NULL);
118 // Periodic timer created
126 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
128 \fn const char *osTimerGetName (osTimerId_t timer_id)
130 The function \b osTimerGetName returns the pointer to the name string of the timer identified by parameter \a timer_id or
131 \token{NULL} in case of an error.
133 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
136 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
138 \fn osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks)
140 The function \b osTimerStart starts or restarts a timer specified by the parameter \a timer_id. The parameter \a ticks
141 specifies the value of the timer in \ref CMSIS_RTOS_TimeOutValue "time ticks".
143 Possible \ref osStatus_t return values:
144 - \em osOK: the specified timer has been started or restarted.
145 - \em osErrorISR: \b osTimerStart cannot be called from interrupt service routines.
146 - \em osErrorParameter: parameter \a timer_id is either \token{NULL} or invalid or \a ticks is incorrect.
147 - \em osErrorResource: the timer specified by parameter \a timer_id is in an invalid timer state.
149 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
153 #include "cmsis_os2.h"
155 void Timer_Callback (void const *arg) { // timer callback function
156 // arg contains &exec
157 // called every second after osTimerStart
160 uint32_t exec; // argument for the timer call back function
162 void TimerStart_example (void) {
163 osTimerId_t id; // timer id
164 uint32_t timerDelay; // timer value
165 osStatus_t status; // function return status
167 // Create periodic timer
169 id = osTimerNew ((osTimerFunc_t)&Timer_Callback, osTimerPeriodic, &exec, NULL);
172 status = osTimerStart (id, timerDelay); // start timer
173 if (status != osOK) {
174 // Timer could not be started
182 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
184 \fn osStatus_t osTimerStop (osTimerId_t timer_id)
186 The function \b osTimerStop stops a timer specified by the parameter \a timer_id.
188 Possible \ref osStatus_t return values:
189 - \em osOK: the specified timer has been stopped.
190 - \em osErrorISR: \b osTimerStop cannot be called from interrupt service routines.
191 - \em osErrorParameter: parameter \a timer_id is either \token{NULL} or invalid.
192 - \em osErrorResource: the timer specified by parameter \a timer_id is not running (you can only stop a running timer).
194 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
198 #include "cmsis_os2.h"
200 void Timer_Callback (void const *arg); // prototype for timer callback function
202 uint32_t exec; // argument for the timer call back function
204 void TimerStop_example (void) {
205 osTimerId_t id; // timer id
206 osStatus_t status; // function return status
208 // Create periodic timer
210 id = osTimerNew ((osTimerFunc_t)&Timer_Callback, osTimerPeriodic, &exec, NULL);
211 osTimerStart (id, 1000); // start timer
213 status = osTimerStop (id); // stop timer
214 if (status != osOK) {
215 // Timer could not be stopped
218 osTimerStart (id, 1000); // start timer again
224 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
226 \fn uint32_t osTimerIsRunning (osTimerId_t timer_id)
228 the function \b osTimerIsRunning checks whether a timer specified by parameter \a timer_id is running. It returns \token{1}
229 if the timer is running and \token{0} if the timer is stopped or an error occurred.
231 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
234 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
236 \fn osStatus_t osTimerDelete (osTimerId_t timer_id)
238 The function \b osTimerDelete deletes the timer specified by parameter \a timer_id.
240 Possible \ref osStatus_t return values:
241 - \em osOK: the specified timer has been deleted.
242 - \em osErrorISR: \b osTimerDelete cannot be called from interrupt service routines.
243 - \em osErrorParameter: parameter \a timer_id is either \token{NULL} or invalid.
244 - \em osErrorResource: the timer specified by parameter \a timer_id is in an invalid timer state.
246 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
250 #include "cmsis_os2.h"
252 void Timer_Callback (void const *arg); // prototype for timer callback function
254 uint32_t exec; // argument for the timer call back function
256 void TimerDelete_example (void) {
257 osTimerId_t id; // timer id
258 osStatus_t status; // function return status
260 // Create periodic timer
262 id = osTimerNew ((osTimerFunc_t)&Timer_Callback, osTimerPeriodic, &exec, NULL);
263 osTimerStart (id, 1000UL); // start timer
265 status = osTimerDelete (id); // stop and delete timer
266 if (status != osOK) {
267 // Timer could not be deleted