<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>CMSIS-RTOS RTX: Theory of Operation</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="cmsis.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="stylsheetf" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 46px;">
  <td id="projectlogo"><img alt="Logo" src="CMSIS_Logo_Final.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">CMSIS-RTOS RTX
   &#160;<span id="projectnumber">Version 4.75</span>
   </div>
   <div id="projectbrief">CMSIS-RTOS RTX: Real-Time Operating System for Cortex-M processor-based devices</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<div id="CMSISnav" class="tabs1">
    <ul class="tablist">
      <li class="current"><a href="./index.html"><span>RTX</span></a></li>
    </ul>
</div>
<!-- Generated by Doxygen 1.8.2 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Usage&#160;and&#160;Description</span></a></li>
      <li><a href="modules.html"><span>Reference</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('_theory.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Theory of Operation </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>This section describes how CMSIS-RTOS RTX manages the resources of the target system. Many aspects of the CMSIS-RTOS RTX kernel can be configured. Information about configuration options is mentioned where applicable.</p>
<h1><a class="anchor" id="KernelTimer"></a>
RTX Kernel Timer Tick and Thread Management</h1>
<p>The CMSIS-RTOS RTX uses, by default, the Cortex-M SysTick timer to generate periodic interrupts for the RTX kernel timer tick. CMSIS-RTOS provides <a class="el" href="group___c_m_s_i_s___r_t_o_s___timer_mgmt.html">Timer Management</a> functions and several CMSIS-RTOS functions have a timeout parameter. This periodic RTX kernel timer tick interrupt is used to derive the required time interval. CMSIS-RTOS RTX also provides configuration options for a alternative timer and tick-less operation. Refer to <a class="el" href="_timer_tick.html">RTX Kernel Tick Timer Configuration</a> for more information.</p>
<p>To handle timeout and time delays for threads, the CMSIS-RTOS RTX thread management is controlled by the RTX kernel timer tick interrupt. The thread context switch itself is implemented in the HAL_CMx.x hardware abstraction layer source files. The thread context contains all CPU registers (R0 - R12), the return address (LR), the program counter (PC), and the processor status register (xPSR). For the Cortex-M4 FPU and Cortex-M7 FPU the floating point status and registers (S0 - S32, FPSCR) are also part of the thread context.</p>
<p>When a thread switch occurs:</p>
<ul>
<li>the thread context of the current running thread is stored on the local stack of this thread.</li>
<li>the stack pointer is switched to the next running thread.</li>
<li>the thread context of this next running thread is restored and this thread starts to run.</li>
</ul>
<dl class="section note"><dt>Note</dt><dd>For Cortex-M0, Cortex-M3, Cortex-M4, and Cortex-M7 the thread context requires 64 bytes on the local stack. </dd>
<dd>
For Cortex-M4 FPU and Cortex-M7 FPU the thread context requires 200 bytes on the local stack. For devices with Cortex-M4 FPU and Cortex M7 FPU the default stack space should be increased to a minimum of 300 bytes.</dd></dl>
<p>Each thread is provided with an separate stack that holds the thread context and stack space for automatic variables and return addresses for function call nesting. The stack sizes of the RTX threads are flexible configurable as explained in the section <a class="el" href="_thread_config.html">Thread Configuration</a>. RTX even offers a configurable checking for stack overflows. Refer to <a class="el" href="_thread_config.html#StackCheck">Stack Overflow Checking</a> for more information.</p>
<h1><a class="anchor" id="RTX_Threads"></a>
CMSIS-RTOS RTX Threads</h1>
<p>At startup time, the CMSIS-RTOS RTX creates the following threads:</p>
<ul>
<li><b>main</b> : the 'main' function of the application code is started as thread with the <a class="el" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa45a2895ad30c79fb97de18cac7cc19f1">osPriorityNormal</a>.</li>
</ul>
<ul>
<li><b>os_idle_demon</b> : this thread executes when no other thread is in <b>RUNNING</b> state. The code of that thread is provided in the <a class="el" href="_r_t_x__conf__c_m.html">RTX_Conf_CM.c Configuration File</a> and is typically used to put the system into a power-saving mode.</li>
</ul>
<ul>
<li><b>osTimerThread</b> : this thread executes the <a class="el" href="group___c_m_s_i_s___r_t_o_s___timer_mgmt.html">Timer Management</a> callback functions. This thread can be disabled; refer to <a class="el" href="_system_config.html#UserTimer">Settings for CMSIS-RTOS User Timer Management</a> for configuration options.</li>
</ul>
<h1><a class="anchor" id="PriorityInversion"></a>
Priority Inversion on Resource Sharing</h1>
<p>The CMSIS-RTOS RTX employs a priority-based preemptive scheduler which ensures that from all the threads that are in the <b>READY</b> state, the thread with the highest priority gets executed and becomes the <b>RUNNING</b> thread. Because threads share resources, events that are outside of the control of the RTX scheduler can prevent the highest priority thread from running when it should. If this happens, a critical deadline could be missed, causing the system to fail. Priority inversion is the term of a scenario in which the highest-priority ready task fails to run when it should.</p>
<p>Threads typically share resources to communicate and process data by using the CMSIS-RTOS <a class="el" href="group___c_m_s_i_s___r_t_o_s___mutex_mgmt.html">Mutex Management</a>. At any time, two or more threads share a resource, such as a memory buffer or a serial port, one of them may have a higher priority. It is expected that the higher-priority thread runs as soon as it is in the <b>READY</b> state. However, if the lower-priority thread is using a shared resource of a higher-priority thread, this higher-priority thread must wait until the lower-priority thread releases the shared resource.</p>
<p>To prevent priority inversions, the CMSIS-RTOS RTX implements a <b>priority inheritance</b> method for the <a class="el" href="group___c_m_s_i_s___r_t_o_s___mutex_mgmt.html">Mutex Management</a>. A lower-priority thread inherit the priority of any higher-priority thread that is waiting with <a class="el" href="group___c_m_s_i_s___r_t_o_s___mutex_mgmt.html#ga5e1752b73f573ee015dbd9ef1edaba13">osMutexWait</a> on a shared resource. During the time the higher-priority thread is in <b>WAITING</b> state, the lower-priority thread runs at the same priority of a higher-priority pending thread. When the lower-priority thread stops to share a resource with <a class="el" href="group___c_m_s_i_s___r_t_o_s___mutex_mgmt.html#ga006e4744d741e8e132c3d5bbc295afe1">osMutexRelease</a>, the original priority is assigned to this thread again. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="_overview.html">Overview</a></li>
    <li class="footer">Generated on Wed Sep 24 2014 12:17:11 for CMSIS-RTOS RTX by ARM Ltd. All rights reserved.
	<!--
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.2 
	-->
	</li>
  </ul>
</div>
</body>
</html>
