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".
128 #include "cmsis_os2.h"
130 void Timer_Callback (void const *arg) { // timer callback function
131 // arg contains &exec
132 // called every second after osTimerStart
135 uint32_t exec; // argument for the timer call back function
137 void TimerStart_example (void) {
138 osTimerId_t id; // timer id
139 uint32_t timerDelay; // timer value
140 osStatus_t status; // function return status
142 // Create periodic timer
144 id = osTimerNew ((os_timer_func_t)&Timer_Callback, osTimerPeriodic, &exec);
147 status = osTimerStart (id, timerDelay); // start timer
148 if (status != osOK) {
149 // Timer could not be started
156 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
158 \fn osStatus_t osTimerStop (osTimerId_t timer_id)
162 \ref osStatus_t return values:
163 - \em osOK: the specified timer has been stopped.
164 - \em osErrorISR: \ref osTimerStop cannot be called from interrupt service routines.
165 - \em osErrorParameter: \a timer_id is incorrect.
166 - \em osErrorResource: the timer is not started.
168 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
172 #include "cmsis_os2.h"
174 void Timer_Callback (void const *arg); // prototype for timer callback function
176 void TimerStop_example (void) {
177 osTimerId_t id; // timer id
178 osStatus_t status; // function return status
180 // Create periodic timer
182 id = osTimerCreate ((os_timer_func_t)&Timer_Callback, osTimerPeriodic, NULL);
183 osTimerStart (id, 1000); // start timer
185 status = osTimerStop (id); // stop timer
186 if (status != osOK) {
187 // Timer could not be stopped
190 osTimerStart (id, 1000); // start timer again
195 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
197 \fn uint32_t osTimerIsRunning (osTimerId_t timer_id)
199 Test if timer is running.
200 Returns 0 if timer is stopped.
201 Returns 1 if timer is running.
204 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
206 \fn osStatus_t osTimerDelete (osTimerId_t timer_id)
208 Delete the timer object.
210 \ref osStatus_t return values:
211 - \em osOK: the specified timer has been deleted.
212 - \em osErrorISR: \ref osTimerDelete cannot be called from interrupt service routines.
213 - \em osErrorParameter: \a timer_id is incorrect.
215 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
219 #include "cmsis_os2.h"
221 void Timer_Callback (void const *arg); // prototype for timer callback function
223 void TimerDelete_example (void) {
224 osTimerId_t id; // timer id
225 osStatus_t status; // function return status
227 // Create periodic timer
229 id = osTimerCreate (osTimer(Timer2), osTimerPeriodic, &exec);
230 osTimerStart (id, 1000UL); // start timer
232 status = osTimerDelete (id); // stop and delete timer
233 if (status != osOK) {
234 // Timer could not be deleted