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 Provide version/system information and start 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, no RTOS function may be called.
72 Possible \ref osStatus_t return values:
73 - \em osOK in case of success.
74 - \em osError if an unspecific error occurred.
75 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
76 - \em osErrorNoMemory if no memory could be reserved for the operation.
78 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
82 #include "RTE_Components.h"
83 #include CMSIS_device_header
84 #include "cmsis_os2.h"
86 /*----------------------------------------------------------------------------
87 * Application main thread
88 *---------------------------------------------------------------------------*/
89 void app_main (void *argument) {
97 // System Initialization
98 SystemCoreClockUpdate();
101 osKernelInitialize(); // Initialize CMSIS-RTOS
102 osThreadNew(app_main, NULL, NULL); // Create application main thread
103 osKernelStart(); // Start thread execution
109 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
111 \fn osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size)
113 The function \b osKernelGetInfo retrieves the API and kernel version of the underlying RTOS kernel and a human readable identifier string for the kernel.
115 Possible \ref osStatus_t return values:
116 - \em osOK in case of success.
117 - \em osError if an unspecific error occurred.
118 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
120 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
129 status = osKernelGetInfo(&osv, infobuf, sizeof(infobuf));
131 printf("Kernel Information: %s\r\n", infobuf);
132 printf("Kernel Version : %d\r\n", osv.kernel);
133 printf("Kernel API Version: %d\r\n", osv.api);
139 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
141 \fn osKernelState_t osKernelGetState (void)
143 The function \b osKernelGetState returns the current state of the kernel and can be safely called before the RTOS is
144 initialized. In case it fails or if it is called from an ISR, it will return \c osKernelError, otherwise it returns the
145 kernel state (refer to \ref osKernelState_t for the list of kernel states).
147 Possible \ref osKernelState_t return values:
148 - \ref osKernelError if an unspecific error occurred or it has been called from an
149 \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
150 - the actual kernel state otherwise.
152 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
157 // System Initialization
158 SystemCoreClockUpdate();
160 if(osKernelGetState() == osKernelInactive) { // Is the kernel initialized?
161 osKernelInitialize(); // Initialize CMSIS-RTOS kernel
168 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
170 \fn osStatus_t osKernelStart (void)
172 The function \b osKernelStart starts the RTOS kernel and begins thread switching. It will not return to its calling function in case of success.
174 Possible \ref osStatus_t return values:
175 - \em osOK in case of success.
176 - \em osError if an unspecific error occurred.
177 - \em osErrorISR if called from an \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routine".
179 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
184 // System Initialization
185 SystemCoreClockUpdate();
187 if(osKernelGetState() == osKernelInactive) {
188 osKernelInitialize();
190 ; // ... Start Threads
191 if (osKernelGetState() == osKernelReady) { // If kernel is ready to run...
192 osKernelStart(); // ... start thread execution
195 while(1); // only reached in case of error
200 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
202 \fn int32_t osKernelLock (void)
204 The function \b osKernelLock allows to lock all task switches. It returns the previous value of the lock state (\token{1} if
205 it was locked, \token{0} if it was unlocked), or a negative number representing an error code otherwise (refer to
208 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
212 uint32_t state = osKernelLock();
214 osKernelRestore(state);
218 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
220 \fn int32_t osKernelUnlock (void)
222 The function \b osKernelUnlock resumes from \ref osKernelLock. It returns the previous value of the lock state (\token{1} if
223 it was locked, \token{0} if it was unlocked), or a negative number representing an error code otherwise (refer to
226 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
230 uint32_t sl = osKernelLock();
233 unint32_t su = osKernelUnlock();
234 // ... uncritical code
235 osKernelRestoreLock(su);
238 osKernelRestoreLock(sl);
242 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
244 \fn int32_t osKernelRestoreLock (int32_t lock)
246 The function \b osKernelRestoreLock restores the previous lock state after \ref osKernelLock or \ref osKernelUnlock.
248 The argument \a lock specifies the lock state as obtained by \ref osKernelLock or \ref osKernelUnlock.
250 The function returns the new value of the lock state (\token{1} if it was locked, \token{0} if it was unlocked), or a
251 negative number representing an error code otherwise (refer to \ref osStatus_t).
253 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
257 uint32_t sl = osKernelLock();
260 unint32_t su = osKernelUnlock();
261 // ... uncritical code
262 osKernelRestoreLock(su);
265 osKernelRestoreLock(sl);
269 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
271 \fn uint32_t osKernelSuspend (void)
273 CMSIS-RTOS provides extension for tick-less operation which is useful for applications that use extensively low-power modes
274 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
275 timer intervals. The function \b osKernelSuspend suspends the RTX kernel scheduler and thus enables sleep modes.
277 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
281 void osRtxIdleThread (void) {
282 /* The idle thread is running
283 when no other thread is ready
288 /* HERE: include optional user
289 code to be executed when no
291 sleep = osKernelSuspend(); /* Suspend RTX thread scheduler */
293 if (sleep) { /* How long can we sleep? */
294 /* "sleep" is in RTX Timer Ticks
298 /* Setup wake-up e.g. watchdog */
300 __WFE(); /* Enter Power-down mode */
303 sleep = tc; /* Adjust with cycles slept */
306 osKernelResume(sleep); /* Resume thread scheduler */
312 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
314 \fn void osKernelResume (uint32_t sleep_ticks)
316 CMSIS-RTOS provides extension for tick-less operation which is useful for applications that use extensively low-power modes
317 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
318 timer intervals. The function \b osKernelResume enables the RTX kernel scheduler and thus wakes up the system from sleep
321 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
325 void osRtxIdleThread (void) {
326 /* The idle thread is running
327 when no other thread is ready
332 /* HERE: include optional user
333 code to be executed when no
335 sleep = osKernelSuspend(); /* Suspend RTX thread scheduler */
337 if (sleep) { /* How long can we sleep? */
338 /* "sleep" is in RTX Timer Ticks
342 /* Setup wake-up e.g. watchdog */
344 __WFE(); /* Enter Power-down mode */
347 sleep = tc; /* Adjust with cycles slept */
350 osKernelResume(sleep); /* Resume thread scheduler */
356 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
358 \fn uint64_t osKernelGetTickCount (void)
360 The function \b osKernelGetTickCount returns the current RTOS kernel tick count or \token{0} in case of a failure.
362 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
365 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
367 \fn uint32_t osKernelGetTickFreq (void)
369 The function \b osKernelGetTickFreq returns the frequency of the current RTOS kernel tick or \token{0} in case of a failure.
371 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
374 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
376 \fn uint32_t osKernelGetSysTimerCount (void)
378 The function \b osKernelGetSysTimerCount returns the current RTOS kernel system timer as a 32-bit value or \token{0} in case of a failure.
380 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
383 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
385 \fn uint32_t osKernelGetSysTimerFreq (void)
387 The function \b osKernelGetSysTimerFreq returns the frequency of the current RTOS kernel system timer or \token{0} in case of a failure.
389 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
393 // these struct members must stay outside the group to avoid double entries in documentation
395 \var osKernelState_t::osKernelInactive
397 The kernel is not ready yet. \ref osKernelInitialize needs to be executed successfully.
399 \var osKernelState_t::osKernelReady
401 The kernel is not yet running. \ref osKernelStart transfers the kernel to the running state.
403 \var osKernelState_t::osKernelRunning
405 The kernel is initialized and running.
407 \var osKernelState_t::osKernelLocked
409 The kernel was locked with \ref osKernelLock. The functions \ref osKernelUnlock or \ref osKernelRestoreLock unlocks it.
411 \var osKernelState_t::osKernelSuspended
413 The kernel was suspended using \ref osKernelSuspend. The function \ref osKernelResume returns to normal operation.
415 \var osKernelState_t::osKernelError
419 \var osKernelState_t::osKernelReserved