1 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
2 // ==== Kernel Control ====
4 \addtogroup CMSIS_RTOS_KernelCtrl Kernel Information and Control
6 \brief Provides version/system information and starts/controls the RTOS Kernel.
8 The kernel Information and Control function group allows to:
9 - obtain information about the system and the underlying kernel.
10 - obtain version information about the CMSIS-RTOS API.
11 - initialize of the RTOS kernel for creating objects.
12 - start the RTOS kernel and thread switching.
13 - check the execution status of the RTOS kernel.
15 \note The kernel information and control functions cannot be called from
16 \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
17 \note The kernel initialization for RTX5 is documented in \ref SystemStartup.
21 /*----------------------------------------------------------------------------
22 * Application main thread
23 *---------------------------------------------------------------------------*/
24 void app_main (void *argument) {
32 // System Initialization
33 SystemCoreClockUpdate();
34 #ifdef RTE_Compiler_EventRecorder
35 // Initialize and start Event Recorder
36 EventRecorderInitialize(EventRecordError, 1U);
40 osKernelInitialize(); // Initialize CMSIS-RTOS
41 osThreadNew(app_main, NULL, NULL); // Create application main thread
42 osKernelStart(); // Start thread execution
48 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
52 Identifies the underlying RTOS kernel and API version number. The version is represented in a combined decimal number in the
53 format: major.minor.rev: mmnnnrrrr
55 Use \ref osKernelGetInfo to retrieve the version numbers.
58 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
62 State of the kernel as retrieved by \ref osKernelGetState. In case \b osKernelGetState fails or if it is called from an ISR,
63 it will return \c osKernelError, otherwise it returns the kernel state.
66 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
68 \fn osStatus_t osKernelInitialize (void)
70 The function \b osKernelInitialize initializes the RTOS Kernel. Before it is successfully executed, only the functions
71 \ref osKernelGetInfo and \ref osKernelGetState may be called.
73 Possible \ref osStatus_t return values:
74 - \em osOK in case of success.
75 - \em osError if an unspecific error occurred.
76 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
77 - \em osErrorNoMemory if no memory could be reserved for the operation.
79 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
83 #include "RTE_Components.h"
84 #include CMSIS_device_header
85 #include "cmsis_os2.h"
87 /*----------------------------------------------------------------------------
88 * Application main thread
89 *---------------------------------------------------------------------------*/
90 void app_main (void *argument) {
98 // System Initialization
99 SystemCoreClockUpdate();
102 osKernelInitialize(); // Initialize CMSIS-RTOS
103 osThreadNew(app_main, NULL, NULL); // Create application main thread
104 osKernelStart(); // Start thread execution
110 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
112 \fn osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size)
114 The function \b osKernelGetInfo retrieves the API and kernel version of the underlying RTOS kernel and a human readable
115 identifier string for the kernel. It can be safely called before the RTOS is initialized or started (call to
116 \ref osKernelInitialize or \ref osKernelStart).
118 Possible \ref osStatus_t return values:
119 - \em osOK in case of success.
120 - \em osError if an unspecific error occurred.
122 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
131 status = osKernelGetInfo(&osv, infobuf, sizeof(infobuf));
133 printf("Kernel Information: %s\r\n", infobuf);
134 printf("Kernel Version : %d\r\n", osv.kernel);
135 printf("Kernel API Version: %d\r\n", osv.api);
141 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
143 \fn osKernelState_t osKernelGetState (void)
145 The function \b osKernelGetState returns the current state of the kernel and can be safely called before the RTOS is
146 initialized or started (call to \ref osKernelInitialize or \ref osKernelStart). In case it fails it will return \c osKernelError,
147 otherwise it returns the kernel state (refer to \ref osKernelState_t for the list of kernel states).
149 Possible \ref osKernelState_t return values:
150 - \ref osKernelError if an unspecific error occurred.
151 - the actual kernel state otherwise.
153 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
158 // System Initialization
159 SystemCoreClockUpdate();
161 if(osKernelGetState() == osKernelInactive) { // Is the kernel initialized?
162 osKernelInitialize(); // Initialize CMSIS-RTOS kernel
169 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
171 \fn osStatus_t osKernelStart (void)
173 The function \b osKernelStart starts the RTOS kernel and begins thread switching. It will not return to its calling function
174 in case of success. Before it is successfully executed, only the functions \ref osKernelGetInfo, \ref osKernelGetState, and
175 object creation functions (\b osXxxNew) may be called.
177 At least one initial thread should be created prior osKernelStart, see \ref osThreadNew.
179 Possible \ref osStatus_t return values:
180 - \em osError if an unspecific error occurred.
181 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
183 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
188 // System Initialization
189 SystemCoreClockUpdate();
191 if(osKernelGetState() == osKernelInactive) {
192 osKernelInitialize();
194 ; // ... Start Threads
195 if (osKernelGetState() == osKernelReady) { // If kernel is ready to run...
196 osKernelStart(); // ... start thread execution
199 while(1); // only reached in case of error
204 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
206 \fn int32_t osKernelLock (void)
208 The function \b osKernelLock allows to lock all task switches. It returns the previous value of the lock state (\token{1} if
209 it was locked, \token{0} if it was unlocked), or a negative number representing an error code otherwise (refer to
212 Possible \ref osStatus_t return values:
213 - \em osError if an unspecific error occurred.
214 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
216 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
220 uint32_t state = osKernelLock();
222 osKernelRestore(state);
226 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
228 \fn int32_t osKernelUnlock (void)
230 The function \b osKernelUnlock resumes from \ref osKernelLock. It returns the previous value of the lock state (\token{1} if
231 it was locked, \token{0} if it was unlocked), or a negative number representing an error code otherwise (refer to
234 Possible \ref osStatus_t return values:
235 - \em osError if an unspecific error occurred.
236 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
238 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
242 uint32_t sl = osKernelLock();
245 unint32_t su = osKernelUnlock();
246 // ... uncritical code
247 osKernelRestoreLock(su);
250 osKernelRestoreLock(sl);
254 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
256 \fn int32_t osKernelRestoreLock (int32_t lock)
258 The function \b osKernelRestoreLock restores the previous lock state after \ref osKernelLock or \ref osKernelUnlock.
260 The argument \a lock specifies the lock state as obtained by \ref osKernelLock or \ref osKernelUnlock.
262 The function returns the new value of the lock state (\token{1} if it was locked, \token{0} if it was unlocked), or a
263 negative number representing an error code otherwise (refer to \ref osStatus_t).
265 Possible \ref osStatus_t return values:
266 - \em osError if an unspecific error occurred.
267 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
269 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
273 uint32_t sl = osKernelLock();
276 unint32_t su = osKernelUnlock();
277 // ... uncritical code
278 osKernelRestoreLock(su);
281 osKernelRestoreLock(sl);
285 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
287 \fn uint32_t osKernelSuspend (void)
289 CMSIS-RTOS provides extension for tick-less operation which is useful for applications that use extensively low-power modes
290 where the SysTick timer is also disabled. To provide a time-tick in such power-saving modes a wake-up timer is used to derive
291 timer intervals. The function \b osKernelSuspend suspends the RTX kernel scheduler and thus enables sleep modes.
293 The return value can be used to determine the amount of system ticks until the next tick-based kernel event will occure, i.e.
294 a delayed thread becomed ready again. It is recommended to set up the low power timer to generate a wake-up interrupt based
295 on this return value.
297 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
301 void osRtxIdleThread (void) {
302 /* The idle thread is running
303 when no other thread is ready
308 /* HERE: include optional user
309 code to be executed when no
311 sleep = osKernelSuspend(); /* Suspend RTX thread scheduler */
313 if (sleep) { /* How long can we sleep? */
314 /* "sleep" is in RTX Timer Ticks
318 /* Setup wake-up e.g. watchdog */
320 __WFE(); /* Enter Power-down mode */
323 sleep = tc; /* Adjust with cycles slept */
326 osKernelResume(sleep); /* Resume thread scheduler */
332 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
334 \fn void osKernelResume (uint32_t sleep_ticks)
336 CMSIS-RTOS provides extension for tick-less operation which is useful for applications that use extensively low-power modes
337 where the SysTick timer is also disabled. To provide a time-tick in such power-saving modes a wake-up timer is used to derive
338 timer intervals. The function \b osKernelResume enables the RTX kernel scheduler and thus wakes up the system from sleep
341 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
345 void osRtxIdleThread (void) {
346 /* The idle thread is running
347 when no other thread is ready
352 /* HERE: include optional user
353 code to be executed when no
355 sleep = osKernelSuspend(); /* Suspend RTX thread scheduler */
357 if (sleep) { /* How long can we sleep? */
358 /* "sleep" is in RTX Timer Ticks
362 /* Setup wake-up e.g. watchdog */
364 __WFE(); /* Enter Power-down mode */
367 sleep = tc; /* Adjust with cycles slept */
370 osKernelResume(sleep); /* Resume thread scheduler */
376 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
378 \fn uint32_t osKernelGetTickCount (void)
380 The function \b osKernelGetTickCount returns the current RTOS kernel tick count.
382 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
386 #include "cmsis_os2.h"
388 void Thread_1 (void *arg) { // Thread function
391 tick = osKernelGetTickCount(); // retrieve the number of system ticks
393 tick += 1000; // delay 1000 ticks periodically
399 Due to the limited value range used for the tick count it may overflow during runtime,
400 i.e. after 2<sup>32</sup> ticks which are roughly 49days @ 1ms. Typically one has not to
401 take special care of this unless a monotonic counter is needed. For such a case an additional
402 64bit tick counter can be implemented as follows. The given example needs GetTick() called at
403 least twice per tick overflow to work properly.
407 uint64_t GetTick(void) {
408 static uint32_t tick_h = 0U;
409 static uint32_t tick_l = 0U;
412 tick = osKernelGetTickCount();
418 return (((uint64_t)tick_h << 32) | tick_l);
423 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
425 \fn uint32_t osKernelGetTickFreq (void)
427 The function \b osKernelGetTickFreq returns the frequency of the current RTOS kernel tick.
429 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
432 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
434 \fn uint32_t osKernelGetSysTimerCount (void)
436 The function \b osKernelGetSysTimerCount returns the current RTOS kernel system timer as a 32-bit value.
438 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
441 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
443 \fn uint32_t osKernelGetSysTimerFreq (void)
445 The function \b osKernelGetSysTimerFreq returns the frequency of the current RTOS kernel system timer.
447 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
451 // these struct members must stay outside the group to avoid double entries in documentation
453 \var osKernelState_t::osKernelInactive
455 The kernel is not ready yet. \ref osKernelInitialize needs to be executed successfully.
457 \var osKernelState_t::osKernelReady
459 The kernel is not yet running. \ref osKernelStart transfers the kernel to the running state.
461 \var osKernelState_t::osKernelRunning
463 The kernel is initialized and running.
465 \var osKernelState_t::osKernelLocked
467 The kernel was locked with \ref osKernelLock. The functions \ref osKernelUnlock or \ref osKernelRestoreLock unlocks it.
469 \var osKernelState_t::osKernelSuspended
471 The kernel was suspended using \ref osKernelSuspend. The function \ref osKernelResume returns to normal operation.
473 \var osKernelState_t::osKernelError
477 \var osKernelState_t::osKernelReserved