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. The timer number is passed as a parameter to the callback function. Each timer can be configured as a
13 one-shot or a periodic timer. A periodic timer repeats its operation until it is \ref osTimerDelete "deleted" or
14 \ref osTimerStop "stopped". All timers can be \ref osTimerStart "started, restarted", or \ref osTimerStop "stopped".
16 \note RTX handles Timers in the thread \b os_TimerThread. Callback functions run under control of this thread and may use other
17 CMSIS-RTOS API calls. The thread is configured in \ref timerConfig.
19 The figure below shows the behavior of a periodic timer. For one-shot timers, the timer stops after execution of the
22 \image html "Timer.png" "Behavior of a Periodic Timer"
25 Refer to \ref timerConfig for RTX5 configuration options.
30 The following steps are required to use a timer:
33 osTimerId one_shot_id, periodic_id;
35 -# Instantiate and start the timers:
37 one_shot_id = osTimerNew((os_timer_func_t)&one_shot_Callback, osTimerOnce, (void *)0); // creates a one-shot timer;
38 // (void*)0 is passed as an argument
39 // to the callback function
40 periodic_id = osTimerNew((os_timer_func_t)&periodic_Callback, osTimerPeriodic, (void *)5); // creates a periodic timer;
41 // (void*)5 is passed as an argument
42 // to the callback function
43 osTimerStart(one_shot_id, 500);
44 osTimerStart(periodic, 1500);
49 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
53 The \ref osTimerType_t specifies the a repeating (periodic) or one-shot timer for the function \ref osTimerNew.
56 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
60 Instances of this type hold a reference to a timer object.
63 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
65 \typedef void (*os_timer_func_t) (void *argument)
69 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
77 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
79 \fn osTimerId_t osTimerNew (os_timer_func_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
81 Create a one-shot or periodic timer and associate it with a callback function argument.
82 The timer is in stopped until it is started with \ref osTimerStart.
84 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
88 #include "cmsis_os2.h"
90 void Timer1_Callback (void const *arg); // prototypes for timer callback function
91 void Timer2_Callback (void const *arg);
93 uint32_t exec1; // argument for the timer call back function
94 uint32_t exec2; // argument for the timer call back function
96 void TimerCreate_example (void) {
97 osTimerId_t id1; // timer id
98 osTimerId_t id2; // timer id
100 // Create one-shoot timer
102 id1 = osTimerNew ((os_timer_func_t)&Timer1_Callback, osTimerOnce, &exec1);
104 // One-shoot timer created
107 // Create periodic timer
109 id2 = osTimerNew ((os_timer_func_t)&Timer2_Callback, osTimerPeriodic, &exec2);
111 // Periodic timer created
119 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
121 \fn const char *osTimerGetName (osTimerId_t timer_id)
126 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
128 \fn osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks)
130 Start or restart the timer.
132 \ref osStatus_t return values:
133 - \em osOK: the specified timer has been started or restarted.
134 - \em osErrorISR: \ref osTimerStart cannot be called from interrupt service routines.
135 - \em osErrorParameter: \a timer_id is incorrect.
137 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
138 Calling \ref osTimerStart from an ISR will return \ref osErrorISR.
142 #include "cmsis_os2.h"
144 void Timer_Callback (void const *arg) { // timer callback function
145 // arg contains &exec
146 // called every second after osTimerStart
149 uint32_t exec; // argument for the timer call back function
151 void TimerStart_example (void) {
152 osTimerId_t id; // timer id
153 uint32_t timerDelay; // timer value
154 osStatus_t status; // function return status
156 // Create periodic timer
158 id = osTimerNew ((os_timer_func_t)&Timer_Callback, osTimerPeriodic, &exec);
161 status = osTimerStart (id, timerDelay); // start timer
162 if (status != osOK) {
163 // Timer could not be started
170 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
172 \fn osStatus_t osTimerStop (osTimerId_t timer_id)
176 \ref osStatus_t return values:
177 - \em osOK: the specified timer has been stopped.
178 - \em osErrorISR: \ref osTimerStop cannot be called from interrupt service routines.
179 - \em osErrorParameter: \a timer_id is incorrect.
180 - \em osErrorResource: the timer is not started.
182 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
183 Calling \ref osTimerStop from an ISR will return \ref osErrorISR.
187 #include "cmsis_os2.h"
189 void Timer_Callback (void const *arg); // prototype for timer callback function
191 void TimerStop_example (void) {
192 osTimerId_t id; // timer id
193 osStatus_t status; // function return status
195 // Create periodic timer
197 id = osTimerCreate ((os_timer_func_t)&Timer_Callback, osTimerPeriodic, NULL);
198 osTimerStart (id, 1000); // start timer
200 status = osTimerStop (id); // stop timer
201 if (status != osOK) {
202 // Timer could not be stopped
205 osTimerStart (id, 1000); // start timer again
210 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
212 \fn uint32_t osTimerIsRunning (osTimerId_t timer_id)
214 Test if timer is running.
215 Returns 0 if timer is stopped.
216 Returns 1 if timer is running.
219 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
221 \fn osStatus_t osTimerDelete (osTimerId_t timer_id)
223 Delete the timer object.
225 \ref osStatus_t return values:
226 - \em osOK: the specified timer has been deleted.
227 - \em osErrorISR: \ref osTimerDelete cannot be called from interrupt service routines.
228 - \em osErrorParameter: \a timer_id is incorrect.
230 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
231 Calling \ref osTimerDelete from an ISR will return \ref osErrorISR.
235 #include "cmsis_os2.h"
237 void Timer_Callback (void const *arg); // prototype for timer callback function
239 void TimerDelete_example (void) {
240 osTimerId_t id; // timer id
241 osStatus_t status; // function return status
243 // Create periodic timer
245 id = osTimerCreate (osTimer(Timer2), osTimerPeriodic, &exec);
246 osTimerStart (id, 1000UL); // start timer
248 status = osTimerDelete (id); // stop and delete timer
249 if (status != osOK) {
250 // Timer could not be deleted