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.
121 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
123 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
132 status = osKernelGetInfo(&osv, infobuf, sizeof(infobuf));
134 printf("Kernel Information: %s\r\n", infobuf);
135 printf("Kernel Version : %d\r\n", osv.kernel);
136 printf("Kernel API Version: %d\r\n", osv.api);
142 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
144 \fn osKernelState_t osKernelGetState (void)
146 The function \b osKernelGetState returns the current state of the kernel and can be safely called before the RTOS is
147 initialized or started (call to \ref osKernelInitialize or \ref osKernelStart). In case it fails or if it is called from an
148 ISR, it will return \c osKernelError, otherwise it returns the kernel state (refer to \ref osKernelState_t for the list of
151 Possible \ref osKernelState_t return values:
152 - \ref osKernelError if an unspecific error occurred or it has been called from an
153 \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
154 - the actual kernel state otherwise.
156 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
161 // System Initialization
162 SystemCoreClockUpdate();
164 if(osKernelGetState() == osKernelInactive) { // Is the kernel initialized?
165 osKernelInitialize(); // Initialize CMSIS-RTOS kernel
172 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
174 \fn osStatus_t osKernelStart (void)
176 The function \b osKernelStart starts the RTOS kernel and begins thread switching. It will not return to its calling function
177 in case of success. Before it is successfully executed, only the functions \ref osKernelGetInfo, \ref osKernelGetState, and
178 object creation functions (\b osXxxNew) may be called.
180 At least one initial thread should be created prior osKernelStart, see \ref osThreadNew.
182 Possible \ref osStatus_t return values:
183 - \em osError if an unspecific error occurred.
184 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
186 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
191 // System Initialization
192 SystemCoreClockUpdate();
194 if(osKernelGetState() == osKernelInactive) {
195 osKernelInitialize();
197 ; // ... Start Threads
198 if (osKernelGetState() == osKernelReady) { // If kernel is ready to run...
199 osKernelStart(); // ... start thread execution
202 while(1); // only reached in case of error
207 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
209 \fn int32_t osKernelLock (void)
211 The function \b osKernelLock allows to lock all task switches. It returns the previous value of the lock state (\token{1} if
212 it was locked, \token{0} if it was unlocked), or a negative number representing an error code otherwise (refer to
215 Possible \ref osStatus_t return values:
216 - \em osError if an unspecific error occurred.
217 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
219 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
223 uint32_t state = osKernelLock();
225 osKernelRestore(state);
229 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
231 \fn int32_t osKernelUnlock (void)
233 The function \b osKernelUnlock resumes from \ref osKernelLock. It returns the previous value of the lock state (\token{1} if
234 it was locked, \token{0} if it was unlocked), or a negative number representing an error code otherwise (refer to
237 Possible \ref osStatus_t return values:
238 - \em osError if an unspecific error occurred.
239 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
241 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
245 uint32_t sl = osKernelLock();
248 unint32_t su = osKernelUnlock();
249 // ... uncritical code
250 osKernelRestoreLock(su);
253 osKernelRestoreLock(sl);
257 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
259 \fn int32_t osKernelRestoreLock (int32_t lock)
261 The function \b osKernelRestoreLock restores the previous lock state after \ref osKernelLock or \ref osKernelUnlock.
263 The argument \a lock specifies the lock state as obtained by \ref osKernelLock or \ref osKernelUnlock.
265 The function returns the new value of the lock state (\token{1} if it was locked, \token{0} if it was unlocked), or a
266 negative number representing an error code otherwise (refer to \ref osStatus_t).
268 Possible \ref osStatus_t return values:
269 - \em osError if an unspecific error occurred.
270 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
272 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
276 uint32_t sl = osKernelLock();
279 unint32_t su = osKernelUnlock();
280 // ... uncritical code
281 osKernelRestoreLock(su);
284 osKernelRestoreLock(sl);
288 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
290 \fn uint32_t osKernelSuspend (void)
292 CMSIS-RTOS provides extension for tick-less operation which is useful for applications that use extensively low-power modes
293 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
294 timer intervals. The function \b osKernelSuspend suspends the RTX kernel scheduler and thus enables sleep modes.
296 The return value can be used to determine the amount of system ticks until the next tick-based kernel event will occure, i.e.
297 a delayed thread becomed ready again. It is recommended to set up the low power timer to generate a wake-up interrupt based
298 on this return value.
300 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
304 void osRtxIdleThread (void) {
305 /* The idle thread is running
306 when no other thread is ready
311 /* HERE: include optional user
312 code to be executed when no
314 sleep = osKernelSuspend(); /* Suspend RTX thread scheduler */
316 if (sleep) { /* How long can we sleep? */
317 /* "sleep" is in RTX Timer Ticks
321 /* Setup wake-up e.g. watchdog */
323 __WFE(); /* Enter Power-down mode */
326 sleep = tc; /* Adjust with cycles slept */
329 osKernelResume(sleep); /* Resume thread scheduler */
335 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
337 \fn void osKernelResume (uint32_t sleep_ticks)
339 CMSIS-RTOS provides extension for tick-less operation which is useful for applications that use extensively low-power modes
340 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
341 timer intervals. The function \b osKernelResume enables the RTX kernel scheduler and thus wakes up the system from sleep
344 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
348 void osRtxIdleThread (void) {
349 /* The idle thread is running
350 when no other thread is ready
355 /* HERE: include optional user
356 code to be executed when no
358 sleep = osKernelSuspend(); /* Suspend RTX thread scheduler */
360 if (sleep) { /* How long can we sleep? */
361 /* "sleep" is in RTX Timer Ticks
365 /* Setup wake-up e.g. watchdog */
367 __WFE(); /* Enter Power-down mode */
370 sleep = tc; /* Adjust with cycles slept */
373 osKernelResume(sleep); /* Resume thread scheduler */
379 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
381 \fn uint64_t osKernelGetTickCount (void)
383 The function \b osKernelGetTickCount returns the current RTOS kernel tick count or \token{0} in case of a failure.
385 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
389 #include "cmsis_os2.h"
391 void Thread_1 (void *arg) { // Thread function
394 tick = osKernelGetTickCount(); // retrieve the number of system ticks
396 tick += 1000; // delay 1000 ticks periodically
403 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
405 \fn uint32_t osKernelGetTickFreq (void)
407 The function \b osKernelGetTickFreq returns the frequency of the current RTOS kernel tick or \token{0} in case of a failure.
409 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
412 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
414 \fn uint32_t osKernelGetSysTimerCount (void)
416 The function \b osKernelGetSysTimerCount returns the current RTOS kernel system timer as a 32-bit value or \token{0} in case of a failure.
418 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
421 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
423 \fn uint32_t osKernelGetSysTimerFreq (void)
425 The function \b osKernelGetSysTimerFreq returns the frequency of the current RTOS kernel system timer or \token{0} in case of a failure.
427 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
431 // these struct members must stay outside the group to avoid double entries in documentation
433 \var osKernelState_t::osKernelInactive
435 The kernel is not ready yet. \ref osKernelInitialize needs to be executed successfully.
437 \var osKernelState_t::osKernelReady
439 The kernel is not yet running. \ref osKernelStart transfers the kernel to the running state.
441 \var osKernelState_t::osKernelRunning
443 The kernel is initialized and running.
445 \var osKernelState_t::osKernelLocked
447 The kernel was locked with \ref osKernelLock. The functions \ref osKernelUnlock or \ref osKernelRestoreLock unlocks it.
449 \var osKernelState_t::osKernelSuspended
451 The kernel was suspended using \ref osKernelSuspend. The function \ref osKernelResume returns to normal operation.
453 \var osKernelState_t::osKernelError
457 \var osKernelState_t::osKernelReserved