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 Timers are handled in the thread \b osTimerThread. Callback functions run under control of this thread and may use other
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"
26 The following steps are required to use a timer:
29 osTimerId one_shot_id, periodic_id;
31 -# Instantiate and start the timers:
33 one_shot_id = osTimerNew((os_timer_func_t)&one_shot_Callback, osTimerOnce, (void *)0); // creates a one-shot timer;
34 // (void*)0 is passed as an argument
35 // to the callback function
36 periodic_id = osTimerNew((os_timer_func_t)&periodic_Callback, osTimerPeriodic, (void *)5); // creates a periodic timer;
37 // (void*)5 is passed as an argument
38 // to the callback function
39 osTimerStart(one_shot_id, 500);
40 osTimerStart(periodic, 1500);
45 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
49 The \ref osTimerType_t specifies the a repeating (periodic) or one-shot timer for the function \ref osTimerNew.
52 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
56 Instances of this type hold a reference to a timer object.
59 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
61 \typedef void (*os_timer_func_t) (void *argument)
65 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
73 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
75 \fn osTimerId_t osTimerNew (os_timer_func_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
77 Create a one-shot or periodic timer and associate it with a callback function argument.
78 The timer is in stopped until it is started with \ref osTimerStart.
80 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
84 #include "cmsis_os2.h"
86 void Timer1_Callback (void const *arg); // prototypes for timer callback function
87 void Timer2_Callback (void const *arg);
89 uint32_t exec1; // argument for the timer call back function
90 uint32_t exec2; // argument for the timer call back function
92 void TimerCreate_example (void) {
93 osTimerId_t id1; // timer id
94 osTimerId_t id2; // timer id
96 // Create one-shoot timer
98 id1 = osTimerNew ((os_timer_func_t)&Timer1_Callback, osTimerOnce, &exec1);
100 // One-shoot timer created
103 // Create periodic timer
105 id2 = osTimerNew ((os_timer_func_t)&Timer2_Callback, osTimerPeriodic, &exec2);
107 // Periodic timer created
113 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
115 \fn osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks)
117 Start or restart the timer.
119 \ref osStatus_t return values:
120 - \em osOK: the specified timer has been started or restarted.
121 - \em osErrorISR: \ref osTimerStart cannot be called from interrupt service routines.
122 - \em osErrorParameter: \a timer_id is incorrect.
124 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
125 Calling \ref osTimerStart from an ISR will return \ref osErrorISR.
129 #include "cmsis_os2.h"
131 void Timer_Callback (void const *arg) { // timer callback function
132 // arg contains &exec
133 // called every second after osTimerStart
136 uint32_t exec; // argument for the timer call back function
138 void TimerStart_example (void) {
139 osTimerId_t id; // timer id
140 uint32_t timerDelay; // timer value
141 osStatus_t status; // function return status
143 // Create periodic timer
145 id = osTimerNew ((os_timer_func_t)&Timer_Callback, osTimerPeriodic, &exec);
148 status = osTimerStart (id, timerDelay); // start timer
149 if (status != osOK) {
150 // Timer could not be started
157 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
159 \fn osStatus_t osTimerStop (osTimerId_t timer_id)
163 \ref osStatus_t return values:
164 - \em osOK: the specified timer has been stopped.
165 - \em osErrorISR: \ref osTimerStop cannot be called from interrupt service routines.
166 - \em osErrorParameter: \a timer_id is incorrect.
167 - \em osErrorResource: the timer is not started.
169 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
170 Calling \ref osTimerStop from an ISR will return \ref osErrorISR.
174 #include "cmsis_os2.h"
176 void Timer_Callback (void const *arg); // prototype for timer callback function
178 void TimerStop_example (void) {
179 osTimerId_t id; // timer id
180 osStatus_t status; // function return status
182 // Create periodic timer
184 id = osTimerCreate ((os_timer_func_t)&Timer_Callback, osTimerPeriodic, NULL);
185 osTimerStart (id, 1000); // start timer
187 status = osTimerStop (id); // stop timer
188 if (status != osOK) {
189 // Timer could not be stopped
192 osTimerStart (id, 1000); // start timer again
197 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
199 \fn uint32_t osTimerIsRunning (osTimerId_t timer_id)
201 Test if timer is running.
202 Returns 0 if timer is stopped.
203 Returns 1 if timer is running.
206 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
208 \fn osStatus_t osTimerDelete (osTimerId_t timer_id)
210 Delete the timer object.
212 \ref osStatus_t return values:
213 - \em osOK: the specified timer has been deleted.
214 - \em osErrorISR: \ref osTimerDelete cannot be called from interrupt service routines.
215 - \em osErrorParameter: \a timer_id is incorrect.
217 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
218 Calling \ref osTimerDelete from an ISR will return \ref osErrorISR.
222 #include "cmsis_os2.h"
224 void Timer_Callback (void const *arg); // prototype for timer callback function
226 void TimerDelete_example (void) {
227 osTimerId_t id; // timer id
228 osStatus_t status; // function return status
230 // Create periodic timer
232 id = osTimerCreate (osTimer(Timer2), osTimerPeriodic, &exec);
233 osTimerStart (id, 1000UL); // start timer
235 status = osTimerDelete (id); // stop and delete timer
236 if (status != osOK) {
237 // Timer could not be deleted