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 The function \b main is a special thread function that may be started at system initialization. In this case it has the
16 initial priority \a osPriorityNormal.
18 When reaching \b main, it is necessary to:
19 -# Call osKernelInitialize() to initialize the CMSIS-RTOS Kernel
20 -# Setup device peripherals and create other RTOS objects using the \b os*New functions.
21 -# Start the Kernel and begin thread switching by calling osKernelStart().
26 osKernelInitialize (); // initialize CMSIS-RTOS
28 // initialize peripherals here
30 // create 'thread' functions that start executing,
31 // example: tid_name = osThreadNew(thread, NULL, NULL);
33 osKernelStart (); // start thread execution
38 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
42 Identifies the underlying RTOS kernel and API version number.
43 The Version is represented in a combined decimal number in the format: major.minor.rev: mmnnnrrrr
45 Use \ref osKernelGetInfo to retrieve the version numbers
48 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
52 State of the Kernel. Can be retrieved by \ref osKernelGetState.
54 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
56 \fn osStatus_t osKernelInitialize (void)
59 \return Returns \ref osStatus_t with \ref osOK in case of success.
60 \return Returns \ref osError if an internal error prevents the kernel initialization.
62 Initialize the RTOS Kernel. Before osKernelInitialize is successfully executed no RTOS function may be called.
64 The RTOS kernel does not start thread switching until the function osKernelStart is called.
67 #include "RTE_Components.h"
68 #include CMSIS_device_header
69 #include "cmsis_os2.h"
71 /*----------------------------------------------------------------------------
72 * Application main thread
73 *---------------------------------------------------------------------------*/
74 void app_main (void *argument) {
82 // System Initialization
83 SystemCoreClockUpdate();
86 osKernelInitialize(); // Initialize CMSIS-RTOS
87 osThreadNew(app_main, NULL, NULL); // Create application main thread
88 osKernelStart(); // Start thread execution
94 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
96 \fn osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size)
98 Retrieve API and kernel version of the underlying RTOS kernel and a human readable identifier string for the kernel.
107 status = osKernelGetInfo(&osv, infobuf, 100);
109 printf("Kernel Information: %s\r\n", infobuf);
110 printf("Kernel Version : %d\r\n", osv.kernel);
111 printf("Kernel API Version: %d\r\n", osv.api);
116 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
118 \fn osKernelState_t osKernelGetState (void)
120 \return The state of the kernel is represented in the \ref osKernelState_t enum. \ref osKernelGetState can be safely called before the RTOS is initialized.
122 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
123 Calling \ref osKernelGetState from an ISR will return \ref osKernelError.
128 // System Initialization
129 SystemCoreClockUpdate();
131 if(osKernelGetState() == osKernelInactive) { // Is the kernel initialized?
132 osKernelInitialize(); // Initialize CMSIS-RTOS kernel
139 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
141 \fn osStatus_t osKernelStart (void)
143 Start the RTOS Kernel and begin thread switching.
144 The function osKernelStart will not return to its calling function in case of success.
146 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines". Calling \ref osKernelStart from an ISR will return \ref osErrorISR.
151 // System Initialization
152 SystemCoreClockUpdate();
154 if(osKernelGetState() == osKernelInactive) {
155 osKernelInitialize();
157 ; // ... Start Threads
158 if (osKernelGetState() == osKernelReady) { // If kernel is ready to run...
159 osKernelStart(); // ... start thread execution
162 while(1); // only reached in case of error
167 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
169 \fn uint32_t osKernelLock (void)
171 Allows to lock all task switches.
176 lock = osKernelLock();
184 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
186 \fn void osKernelUnlock (void)
188 Resumes from \ref osKernelLock.
190 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
192 \fn uint32_t osKernelSuspend (void)
194 CMSIS-RTOS provides extension for tick-less operation which is useful for applications that use extensively low-power modes 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 timer intervals. The functions osKernelSuspend and osKernelResume control the tick-less operation.
198 void os_idle_demon (void) {
199 /* The idle demon is a system thread, running when no other thread is */
204 /* HERE: include optional user code to be executed when no task runs.*/
205 sleep = os_suspend(); /* Suspend RTX thread scheduler */
207 if (sleep) { /* How long can we sleep? */
208 /* "sleep" is in RTX Timer Ticks which is 1ms in this configuration */
210 /* Setup wake-up e.g. watchdog */
212 /* Enter Power-down mode */
213 __WFE(); /* Enter Power-down mode */
216 sleep = tc; /* Adjust with cycles slept */
219 os_resume(sleep); /* Resume thread scheduler */
225 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
227 \fn void osKernelResume (uint32_t sleep_ticks)
229 See \ref osKernelSuspend.
232 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
234 \fn uint64_t osKernelGetTickCount (void)
238 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
240 \fn uint32_t osKernelGetTickFreq (void)
244 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
246 \fn uint32_t osKernelGetSysTimerCount (void)
250 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
252 \fn uint32_t osKernelGetSysTimerFreq (void)
258 // these struct members must stay outside the group to avoid double entries in documentation
260 \var osKernelState_t::osKernelInactive
262 The kernel is not ready yet. \ref osKernelInitialize needs to be executed successfully.
264 \var osKernelState_t::osKernelReady
266 The kernel is not yet running. \ref osKernelStart transfers the kernel to the running state.
268 \var osKernelState_t::osKernelRunning
270 The kernel is initialized and running.
272 \var osKernelState_t::osKernelLocked
274 The kernel was locked with \ref osKernelLock. The function \ref osKernelUnlock unlocks it.
276 \var osKernelState_t::osKernelSuspended
278 The kernel was suspended using \ref osKernelSuspend. The function \ref osKernelResume returns to normal operation.
280 \var osKernelState_t::osKernelError
282 An error occurred. The kernel error handler should have been called in this state.
284 \var osKernelState_t::osKernelReserved