1 <!-- HTML header for doxygen 1.9.6-->
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
5 <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
6 <meta http-equiv="X-UA-Compatible" content="IE=11"/>
7 <meta name="viewport" content="width=device-width, initial-scale=1"/>
8 <title>CMSIS-RTOS2: Semaphores</title>
9 <link href="doxygen.css" rel="stylesheet" type="text/css"/>
10 <link href="tabs.css" rel="stylesheet" type="text/css"/>
11 <link href="extra_navtree.css" rel="stylesheet" type="text/css"/>
12 <link href="extra_stylesheet.css" rel="stylesheet" type="text/css"/>
13 <link href="extra_search.css" rel="stylesheet" type="text/css"/>
14 <script type="text/javascript" src="jquery.js"></script>
15 <script type="text/javascript" src="dynsections.js"></script>
16 <script type="text/javascript" src="printComponentTabs.js"></script>
17 <script type="text/javascript" src="footer.js"></script>
18 <script type="text/javascript" src="navtree.js"></script>
19 <link href="navtree.css" rel="stylesheet" type="text/css"/>
20 <script type="text/javascript" src="resize.js"></script>
21 <script type="text/javascript" src="navtreedata.js"></script>
22 <script type="text/javascript" src="navtree.js"></script>
23 <link href="search/search.css" rel="stylesheet" type="text/css"/>
24 <script type="text/javascript" src="search/searchdata.js"></script>
25 <script type="text/javascript" src="search/search.js"></script>
26 <script type="text/javascript">
27 /* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
28 $(document).ready(function() { init_search(); });
31 <script type="text/javascript" src="darkmode_toggle.js"></script>
32 <link href="extra_stylesheet.css" rel="stylesheet" type="text/css"/>
33 <link href="extra_navtree.css" rel="stylesheet" type="text/css"/>
34 <link href="extra_search.css" rel="stylesheet" type="text/css"/>
35 <link href="version.css" rel="stylesheet" type="text/css" />
36 <script type="text/javascript" src="../../version.js"></script>
39 <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
41 <table cellspacing="0" cellpadding="0">
43 <tr style="height: 55px;">
44 <td id="projectlogo" style="padding: 1.5em;"><img alt="Logo" src="cmsis_logo_white_small.png"/></td>
45 <td style="padding-left: 1em; padding-bottom: 1em;padding-top: 1em;">
46 <div id="projectname">CMSIS-RTOS2
47  <span id="projectnumber"><script type="text/javascript">
49 writeHeader.call(this);
50 writeVersionDropdown.call(this, "CMSIS-RTOS2");
55 <div id="projectbrief">Real-Time Operating System API</div>
57 <td> <div id="MSearchBox" class="MSearchBoxInactive">
59 <span id="MSearchSelect" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()"> </span>
60 <input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
61 onfocus="searchBox.OnSearchFieldFocus(true)"
62 onblur="searchBox.OnSearchFieldFocus(false)"
63 onkeyup="searchBox.OnSearchFieldChange(event)"/>
64 </span><span class="right">
65 <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
69 <!--END !PROJECT_NAME-->
74 <!-- end header part -->
75 <div id="CMSISnav" class="tabs1">
77 <script type="text/javascript">
78 writeComponentTabs.call(this);
82 <!-- Generated by Doxygen 1.9.6 -->
83 <script type="text/javascript">
84 /* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
85 var searchBox = new SearchBox("searchBox", "search/",'.html');
89 <div id="side-nav" class="ui-resizable side-nav-resizable">
91 <div id="nav-tree-contents">
92 <div id="nav-sync" class="sync"></div>
95 <div id="splitbar" style="-moz-user-select:none;"
96 class="ui-resizable-handle">
99 <script type="text/javascript">
100 /* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
101 $(document).ready(function(){initNavTree('group__CMSIS__RTOS__SemaphoreMgmt.html',''); initResizable(); });
104 <div id="doc-content">
105 <!-- window showing the filter options -->
106 <div id="MSearchSelectWindow"
107 onmouseover="return searchBox.OnSearchSelectShow()"
108 onmouseout="return searchBox.OnSearchSelectHide()"
109 onkeydown="return searchBox.OnSearchSelectKey(event)">
112 <!-- iframe showing the search results (closed by default) -->
113 <div id="MSearchResultsWindow">
114 <div id="MSearchResults">
117 <div id="SRResults"></div>
118 <div class="SRStatus" id="Loading">Loading...</div>
119 <div class="SRStatus" id="Searching">Searching...</div>
120 <div class="SRStatus" id="NoMatches">No Matches</div>
127 <div class="summary">
128 <a href="#nested-classes">Data Structures</a> |
129 <a href="#typedef-members">Typedefs</a> |
130 <a href="#func-members">Functions</a> </div>
131 <div class="headertitle"><div class="title">Semaphores<div class="ingroups"><a class="el" href="group__CMSIS__RTOS.html">CMSIS-RTOS API v2</a></div></div></div>
133 <div class="contents">
135 <p>Access shared resources simultaneously from different threads.
136 <a href="#details">More...</a></p>
137 <table class="memberdecls">
138 <tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="nested-classes" name="nested-classes"></a>
139 Data Structures</h2></td></tr>
140 <tr class="memitem:structosSemaphoreAttr__t"><td class="memItemLeft" align="right" valign="top">struct  </td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#structosSemaphoreAttr__t">osSemaphoreAttr_t</a></td></tr>
141 <tr class="memdesc:structosSemaphoreAttr__t"><td class="mdescLeft"> </td><td class="mdescRight">Attributes structure for semaphore. <a href="group__CMSIS__RTOS__SemaphoreMgmt.html#structosSemaphoreAttr__t">More...</a><br /></td></tr>
142 <tr class="separator:structosSemaphoreAttr__t"><td class="memSeparator" colspan="2"> </td></tr>
143 </table><table class="memberdecls">
144 <tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="typedef-members" name="typedef-members"></a>
145 Typedefs</h2></td></tr>
146 <tr class="memitem:ga6e1c1c4b04175bb92b013c8f37249f40"><td class="memItemLeft" align="right" valign="top">typedef void * </td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a></td></tr>
147 <tr class="separator:ga6e1c1c4b04175bb92b013c8f37249f40"><td class="memSeparator" colspan="2"> </td></tr>
148 </table><table class="memberdecls">
149 <tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
150 Functions</h2></td></tr>
151 <tr class="memitem:ga2a39806ace781a0008a4374ca701b14a"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> </td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a> (uint32_t max_count, uint32_t initial_count, const <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#structosSemaphoreAttr__t">osSemaphoreAttr_t</a> *attr)</td></tr>
152 <tr class="memdesc:ga2a39806ace781a0008a4374ca701b14a"><td class="mdescLeft"> </td><td class="mdescRight">Create and Initialize a Semaphore object. <br /></td></tr>
153 <tr class="separator:ga2a39806ace781a0008a4374ca701b14a"><td class="memSeparator" colspan="2"> </td></tr>
154 <tr class="memitem:ga9586952051f00285f1482dbe6695bbc4"><td class="memItemLeft" align="right" valign="top">const char * </td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga9586952051f00285f1482dbe6695bbc4">osSemaphoreGetName</a> (<a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> semaphore_id)</td></tr>
155 <tr class="memdesc:ga9586952051f00285f1482dbe6695bbc4"><td class="mdescLeft"> </td><td class="mdescRight">Get name of a Semaphore object. <br /></td></tr>
156 <tr class="separator:ga9586952051f00285f1482dbe6695bbc4"><td class="memSeparator" colspan="2"> </td></tr>
157 <tr class="memitem:ga7e94c8b242a0c81f2cc79ec22895c87b"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> </td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a> (<a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> semaphore_id, uint32_t timeout)</td></tr>
158 <tr class="memdesc:ga7e94c8b242a0c81f2cc79ec22895c87b"><td class="mdescLeft"> </td><td class="mdescRight">Acquire a Semaphore token or timeout if no tokens are available. <br /></td></tr>
159 <tr class="separator:ga7e94c8b242a0c81f2cc79ec22895c87b"><td class="memSeparator" colspan="2"> </td></tr>
160 <tr class="memitem:ga0abcee1b5449d7a6928fb9248c690bb6"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> </td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a> (<a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> semaphore_id)</td></tr>
161 <tr class="memdesc:ga0abcee1b5449d7a6928fb9248c690bb6"><td class="mdescLeft"> </td><td class="mdescRight">Release a Semaphore token up to the initial maximum count. <br /></td></tr>
162 <tr class="separator:ga0abcee1b5449d7a6928fb9248c690bb6"><td class="memSeparator" colspan="2"> </td></tr>
163 <tr class="memitem:ga7559d4dff3cda9992fc5ab5de3e74c70"><td class="memItemLeft" align="right" valign="top">uint32_t </td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7559d4dff3cda9992fc5ab5de3e74c70">osSemaphoreGetCount</a> (<a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> semaphore_id)</td></tr>
164 <tr class="memdesc:ga7559d4dff3cda9992fc5ab5de3e74c70"><td class="mdescLeft"> </td><td class="mdescRight">Get current Semaphore token count. <br /></td></tr>
165 <tr class="separator:ga7559d4dff3cda9992fc5ab5de3e74c70"><td class="memSeparator" colspan="2"> </td></tr>
166 <tr class="memitem:ga81258ce9c67fa89f07cc49d2e136cd88"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> </td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga81258ce9c67fa89f07cc49d2e136cd88">osSemaphoreDelete</a> (<a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> semaphore_id)</td></tr>
167 <tr class="memdesc:ga81258ce9c67fa89f07cc49d2e136cd88"><td class="mdescLeft"> </td><td class="mdescRight">Delete a Semaphore object. <br /></td></tr>
168 <tr class="separator:ga81258ce9c67fa89f07cc49d2e136cd88"><td class="memSeparator" colspan="2"> </td></tr>
170 <a name="details" id="details"></a><h2 class="groupheader">Description</h2>
171 <p>Semaphores are used to manage and protect access to shared resources. Semaphores are very similar to <a class="el" href="group__CMSIS__RTOS__MutexMgmt.html">Mutexes</a>. Whereas a Mutex permits just one thread to access a shared resource at a time, a semaphore can be used to permit a fixed number of threads/ISRs to access a pool of shared resources. Using semaphores, access to a group of identical peripherals can be managed (for example multiple DMA channels).</p>
173 <img src="Semaphore.png" alt=""/>
174 <div class="caption">
175 CMSIS-RTOS Semaphore</div></div>
176 <p>A semaphore object should be initialized to the maximum number of available tokens. This number of available resources is specified as parameter of the <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a> function. Each time a semaphore token is obtained with <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a> (in <em>available</em> state), the semaphore count is decremented. When the semaphore count is 0 (i.e. <em>depleted</em> state), no more semaphore tokens can be obtained. The thread/ISR that tries to obtain the semaphore token needs to wait until the next token is free. Semaphores are released with <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a> incrementing the semaphore count.</p>
178 <img src="semaphore_states.png" alt=""/>
179 <div class="caption">
180 CMSIS-RTOS Semaphore States</div></div>
181 <dl class="section note"><dt>Note</dt><dd>The functions <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a>, <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7559d4dff3cda9992fc5ab5de3e74c70">osSemaphoreGetCount</a>, and <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a> can be called from Interrupt Service Routines.</dd>
183 Refer to semaphoreConfig for RTX5 configuration options.</dd></dl>
184 <h2><a class="anchor" id="autotoc_md4"></a>
185 Semaphore Use Cases</h2>
186 <p>Due to their flexibility, semaphores cover a wide range of synchronizing applications. At the same time, they are perhaps the most challenging RTOS object to understand. The following explains a use case for semaphores, taken from the book <a href="http://www.greenteapress.com/semaphores/" target="_blank">The Little Book Of Semaphores</a> by Allen B. Downey which is available for free download.</p>
187 <p><b>Non-binary Semaphore (Multiplex)</b></p>
188 <p>A multiplex limits the number of threads that can access a critical section of code. For example, this could be a function accessing DMA resources which can only support a limited number of calls.</p>
189 <p>To allow multiple threads to run the function, initialize a semaphore to the maximum number of threads that can be allowed. The number of tokens in the semaphore represents the number of additional threads that may enter. If this number is zero, then the next thread trying to access the function will have to wait until one of the other threads exits and releases its token. When all threads have exited the token number is back to n. The following example shows the code for one of the threads that might access the resource:</p>
190 <div class="fragment"><div class="line"><a class="code hl_typedef" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> multiplex_id;</div>
191 <div class="line"> </div>
192 <div class="line"><span class="keywordtype">void</span> thread_n (<span class="keywordtype">void</span>) {</div>
193 <div class="line"> </div>
194 <div class="line"> multiplex_id = <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>(3U, 3U, NULL);</div>
195 <div class="line"> <span class="keywordflow">while</span>(1) {</div>
196 <div class="line"> <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a>(multiplex_id, <a class="code hl_define" href="group__CMSIS__RTOS__Definitions.html#ga9eb9a7a797a42e4b55eb171ecc609ddb">osWaitForever</a>);</div>
197 <div class="line"> <span class="comment">// do something</span></div>
198 <div class="line"> <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a>(multiplex_id);</div>
199 <div class="line"> }</div>
200 <div class="line">}</div>
201 <div class="ttc" id="agroup__CMSIS__RTOS__Definitions_html_ga9eb9a7a797a42e4b55eb171ecc609ddb"><div class="ttname"><a href="group__CMSIS__RTOS__Definitions.html#ga9eb9a7a797a42e4b55eb171ecc609ddb">osWaitForever</a></div><div class="ttdeci">#define osWaitForever</div><div class="ttdoc">Wait forever timeout value.</div><div class="ttdef"><b>Definition:</b> cmsis_os2.h:189</div></div>
202 <div class="ttc" id="agroup__CMSIS__RTOS__SemaphoreMgmt_html_ga0abcee1b5449d7a6928fb9248c690bb6"><div class="ttname"><a href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a></div><div class="ttdeci">osStatus_t osSemaphoreRelease(osSemaphoreId_t semaphore_id)</div><div class="ttdoc">Release a Semaphore token up to the initial maximum count.</div></div>
203 <div class="ttc" id="agroup__CMSIS__RTOS__SemaphoreMgmt_html_ga2a39806ace781a0008a4374ca701b14a"><div class="ttname"><a href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a></div><div class="ttdeci">osSemaphoreId_t osSemaphoreNew(uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr)</div><div class="ttdoc">Create and Initialize a Semaphore object.</div></div>
204 <div class="ttc" id="agroup__CMSIS__RTOS__SemaphoreMgmt_html_ga6e1c1c4b04175bb92b013c8f37249f40"><div class="ttname"><a href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a></div><div class="ttdeci">void * osSemaphoreId_t</div><div class="ttdef"><b>Definition:</b> cmsis_os2.h:265</div></div>
205 <div class="ttc" id="agroup__CMSIS__RTOS__SemaphoreMgmt_html_ga7e94c8b242a0c81f2cc79ec22895c87b"><div class="ttname"><a href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a></div><div class="ttdeci">osStatus_t osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout)</div><div class="ttdoc">Acquire a Semaphore token or timeout if no tokens are available.</div></div>
206 </div><!-- fragment --><p><b>Producer/Consumer Semaphore</b></p>
207 <p>The producer-consumer problem can be solved using two semaphores.</p>
208 <p>A first semaphore (<span class="XML-Token">empty_id</span>) counts down the available (empty) buffers, i.e. the producer thread can wait for available buffer slots by acquiring from this one.</p>
209 <p>A second semaphore (<span class="XML-Token">filled_id</span>) counts up the used (filled) buffers, i.e. the consumer thread can wait for available data by acquiring from this one.</p>
210 <p>It is crucial for the correct behaviour that the threads acquire and release on both semaphores in the given sequence. According to this example one can have multiple producer and/or consumer threads running concurrently.</p>
211 <div class="fragment"><div class="line"><span class="preprocessor">#define BUFFER_SIZE 10U</span></div>
212 <div class="line"> </div>
213 <div class="line"><a class="code hl_typedef" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> empty_id = <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>(BUFFER_SIZE, BUFFER_SIZE, NULL);</div>
214 <div class="line"><a class="code hl_typedef" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> filled_id = <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>(BUFFER_SIZE, 0U, NULL);</div>
215 <div class="line"> </div>
216 <div class="line"><span class="keywordtype">void</span> producer_thread (<span class="keywordtype">void</span>) {</div>
217 <div class="line"> <span class="keywordflow">while</span>(1) {</div>
218 <div class="line"> <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a>(empty_id, <a class="code hl_define" href="group__CMSIS__RTOS__Definitions.html#ga9eb9a7a797a42e4b55eb171ecc609ddb">osWaitForever</a>);</div>
219 <div class="line"> <span class="comment">// produce data</span></div>
220 <div class="line"> <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a>(filled_id);</div>
221 <div class="line"> }</div>
222 <div class="line">}</div>
223 <div class="line"> </div>
224 <div class="line"><span class="keywordtype">void</span> consumer_thread (<span class="keywordtype">void</span>) {</div>
225 <div class="line"> </div>
226 <div class="line"> <span class="keywordflow">while</span>(1){</div>
227 <div class="line"> <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a>(filled_id, <a class="code hl_define" href="group__CMSIS__RTOS__Definitions.html#ga9eb9a7a797a42e4b55eb171ecc609ddb">osWaitForever</a>);</div>
228 <div class="line"> <span class="comment">// consume data</span></div>
229 <div class="line"> <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a>(empty_id);</div>
230 <div class="line"> }</div>
231 <div class="line">}</div>
232 </div><!-- fragment --> <hr/><h2 class="groupheader">Data Structure Documentation</h2>
233 <a name="structosSemaphoreAttr__t" id="structosSemaphoreAttr__t"></a>
234 <h2 class="memtitle"><span class="permalink"><a href="#structosSemaphoreAttr__t">◆ </a></span>osSemaphoreAttr_t</h2>
236 <div class="memitem">
237 <div class="memproto">
238 <table class="memname">
240 <td class="memname">struct osSemaphoreAttr_t</td>
243 </div><div class="memdoc">
244 <div class="textblock"><p>Specifies the following attributes for the <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a> function. </p>
245 </div><table class="fieldtable">
246 <tr><th colspan="3">Data Fields</th></tr>
247 <tr><td class="fieldtype">
248 <a id="ab74e6bf80237ddc4109968cedc58c151" name="ab74e6bf80237ddc4109968cedc58c151"></a>const char *</td>
249 <td class="fieldname">
251 <td class="fielddoc">
252 name of the semaphore <p>Pointer to a constant string with a human readable name (displayed during debugging) of the semaphore object.</p>
253 <p>Default: <span class="XML-Token">NULL</span> no name specified. </p>
255 <tr><td class="fieldtype">
256 <a id="a6e93b49cb79c12f768d72580c7731f30" name="a6e93b49cb79c12f768d72580c7731f30"></a>uint32_t</td>
257 <td class="fieldname">
259 <td class="fielddoc">
260 attribute bits <p>Reserved for future use (must be set to '0' for future compatibility). </p>
262 <tr><td class="fieldtype">
263 <a id="a1e100dc33d403841ed3c344e3397868e" name="a1e100dc33d403841ed3c344e3397868e"></a>void *</td>
264 <td class="fieldname">
266 <td class="fielddoc">
267 memory for control block <p>Pointer to a memory for the semaphore control block object. Refer to StaticObjectMemory for more information.</p>
268 <p>Default: <span class="XML-Token">NULL</span> to use <a class="el" href="group__CMSIS__RTOS__MemoryMgmt.html#CMSIS_RTOS_MemoryMgmt_Automatic">Automatic Dynamic Allocation</a> for the semaphore control block. </p>
270 <tr><td class="fieldtype">
271 <a id="aa55a4335d12dc2785dc00fdc292d1159" name="aa55a4335d12dc2785dc00fdc292d1159"></a>uint32_t</td>
272 <td class="fieldname">
274 <td class="fielddoc">
275 size of provided memory for control block <p>The size (in bytes) of memory block passed with <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#a1e100dc33d403841ed3c344e3397868e">cb_mem</a>. For RTX, the minimum value is defined with osRtxSemaphoreCbSize (higher values are permitted).</p>
276 <p>Default: <span class="XML-Token">0</span> as the default is no memory provided with <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#a1e100dc33d403841ed3c344e3397868e">cb_mem</a>. </p>
282 <h2 class="groupheader">Typedef Documentation</h2>
283 <a id="ga6e1c1c4b04175bb92b013c8f37249f40" name="ga6e1c1c4b04175bb92b013c8f37249f40"></a>
284 <h2 class="memtitle"><span class="permalink"><a href="#ga6e1c1c4b04175bb92b013c8f37249f40">◆ </a></span>osSemaphoreId_t</h2>
286 <div class="memitem">
287 <div class="memproto">
288 <table class="memname">
290 <td class="memname"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a></td>
293 </div><div class="memdoc">
294 <p>Semaphore ID identifies the semaphore.</p>
295 <p>Returned by:</p><ul>
296 <li><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a> </li>
301 <h2 class="groupheader">Function Documentation</h2>
302 <a id="ga2a39806ace781a0008a4374ca701b14a" name="ga2a39806ace781a0008a4374ca701b14a"></a>
303 <h2 class="memtitle"><span class="permalink"><a href="#ga2a39806ace781a0008a4374ca701b14a">◆ </a></span>osSemaphoreNew()</h2>
305 <div class="memitem">
306 <div class="memproto">
307 <table class="memname">
309 <td class="memname"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> osSemaphoreNew </td>
311 <td class="paramtype">uint32_t </td>
312 <td class="paramname"><em>max_count</em>, </td>
315 <td class="paramkey"></td>
317 <td class="paramtype">uint32_t </td>
318 <td class="paramname"><em>initial_count</em>, </td>
321 <td class="paramkey"></td>
323 <td class="paramtype">const <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#structosSemaphoreAttr__t">osSemaphoreAttr_t</a> * </td>
324 <td class="paramname"><em>attr</em> </td>
332 </div><div class="memdoc">
333 <dl class="params"><dt>Parameters</dt><dd>
334 <table class="params">
335 <tr><td class="paramdir">[in]</td><td class="paramname">max_count</td><td>maximum number of available tokens. </td></tr>
336 <tr><td class="paramdir">[in]</td><td class="paramname">initial_count</td><td>initial number of available tokens. </td></tr>
337 <tr><td class="paramdir">[in]</td><td class="paramname">attr</td><td>semaphore attributes; NULL: default values. </td></tr>
341 <dl class="section return"><dt>Returns</dt><dd>semaphore ID for reference by other functions or NULL in case of error.</dd></dl>
342 <p>The function <b>osSemaphoreNew</b> creates and initializes a semaphore object that is used to manage access to shared resources and returns the pointer to the semaphore object identifier or <span class="XML-Token">NULL</span> in case of an error. It can be safely called before the RTOS is started (call to <a class="el" href="group__CMSIS__RTOS__KernelCtrl.html#ga9ae2cc00f0d89d7b6a307bba942b5221">osKernelStart</a>), but not before it is initialized (call to <a class="el" href="group__CMSIS__RTOS__KernelCtrl.html#gae818f6611d25ba3140bede410a52d659">osKernelInitialize</a>).</p>
343 <p>The parameter <em>max_count</em> specifies the maximum number of available tokens. A <em>max_count</em> value of 1 creates a binary semaphore.</p>
344 <p>The parameter <em>initial_count</em> sets the initial number of available tokens.</p>
345 <p>The parameter <em>attr</em> specifies additional semaphore attributes. Default attributes will be used if set to <span class="XML-Token">NULL</span>.</p>
346 <dl class="section note"><dt>Note</dt><dd>This function <b>cannot</b> be called from Interrupt Service Routines.</dd></dl>
347 <p><b>Code Example</b> </p><div class="fragment"><div class="line"><span class="preprocessor">#include "<a class="code" href="cmsis__os2_8h.html">cmsis_os2.h</a>"</span> <span class="comment">// CMSIS RTOS header file</span></div>
348 <div class="line"> </div>
349 <div class="line"><a class="code hl_typedef" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> sid_Semaphore; <span class="comment">// semaphore id</span></div>
350 <div class="line"> </div>
351 <div class="line"><a class="code hl_typedef" href="group__CMSIS__RTOS__ThreadMgmt.html#gaa6c32fe2a3e0a2e01f212d55b02e51c7">osThreadId_t</a> tid_Thread_Semaphore; <span class="comment">// thread id</span></div>
352 <div class="line"> </div>
353 <div class="line"><span class="keywordtype">void</span> Thread_Semaphore (<span class="keywordtype">void</span> *argument); <span class="comment">// thread function</span></div>
354 <div class="line"> </div>
355 <div class="line"><span class="keywordtype">int</span> Init_Semaphore (<span class="keywordtype">void</span>) {</div>
356 <div class="line"> </div>
357 <div class="line"> sid_Semaphore = <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>(2U, 2U, NULL);</div>
358 <div class="line"> <span class="keywordflow">if</span> (sid_Semaphore == NULL) {</div>
359 <div class="line"> ; <span class="comment">// Semaphore object not created, handle failure</span></div>
360 <div class="line"> }</div>
361 <div class="line"> </div>
362 <div class="line"> tid_Thread_Semaphore = <a class="code hl_function" href="group__CMSIS__RTOS__ThreadMgmt.html#ga48d68b8666d99d28fa646ee1d2182b8f">osThreadNew</a>(Thread_Semaphore, NULL, NULL);</div>
363 <div class="line"> <span class="keywordflow">if</span> (tid_Thread_Semaphore == NULL) {</div>
364 <div class="line"> <span class="keywordflow">return</span>(-1);</div>
365 <div class="line"> }</div>
366 <div class="line"> </div>
367 <div class="line"> <span class="keywordflow">return</span>(0);</div>
368 <div class="line">}</div>
369 <div class="line"> </div>
370 <div class="line"><span class="keywordtype">void</span> Thread_Semaphore (<span class="keywordtype">void</span> *argument) {</div>
371 <div class="line"> <a class="code hl_enumeration" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> val;</div>
372 <div class="line"> </div>
373 <div class="line"> <span class="keywordflow">while</span> (1) {</div>
374 <div class="line"> ; <span class="comment">// Insert thread code here...</span></div>
375 <div class="line"> </div>
376 <div class="line"> val = <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a>(sid_Semaphore, 10U); <span class="comment">// wait for max. 10 ticks for semaphore token to get available</span></div>
377 <div class="line"> <span class="keywordflow">switch</span> (val) {</div>
378 <div class="line"> <span class="keywordflow">case</span> <a class="code hl_enumvalue" href="cmsis__os2_8h.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813ea9e1c9e2550bb4de8969a935acffc968f">osOK</a>:</div>
379 <div class="line"> ; <span class="comment">// Use protected code here...</span></div>
380 <div class="line"> <a class="code hl_function" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a>(sid_Semaphore); <span class="comment">// return a token back to a semaphore</span></div>
381 <div class="line"> <span class="keywordflow">break</span>;</div>
382 <div class="line"> <span class="keywordflow">case</span> <a class="code hl_enumvalue" href="cmsis__os2_8h.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813ea8fc5801e8b0482bdf22ad63a77f0155d">osErrorResource</a>:</div>
383 <div class="line"> <span class="keywordflow">break</span>;</div>
384 <div class="line"> <span class="keywordflow">case</span> <a class="code hl_enumvalue" href="cmsis__os2_8h.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813eac24adca6a5d072c9f01c32178ba0d109">osErrorParameter</a>:</div>
385 <div class="line"> <span class="keywordflow">break</span>;</div>
386 <div class="line"> <span class="keywordflow">default</span>:</div>
387 <div class="line"> <span class="keywordflow">break</span>;</div>
388 <div class="line"> }</div>
389 <div class="line"> </div>
390 <div class="line"> <a class="code hl_function" href="group__CMSIS__RTOS__ThreadMgmt.html#gad01c7ec26535b1de6b018bb9466720e2">osThreadYield</a>(); <span class="comment">// suspend thread</span></div>
391 <div class="line"> }</div>
392 <div class="line">}</div>
393 <div class="ttc" id="acmsis__os2_8h_html"><div class="ttname"><a href="cmsis__os2_8h.html">cmsis_os2.h</a></div></div>
394 <div class="ttc" id="acmsis__os2_8h_html_ga6c0dbe6069e4e7f47bb4cd32ae2b813ea8fc5801e8b0482bdf22ad63a77f0155d"><div class="ttname"><a href="cmsis__os2_8h.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813ea8fc5801e8b0482bdf22ad63a77f0155d">osErrorResource</a></div><div class="ttdeci">@ osErrorResource</div><div class="ttdoc">Resource not available.</div><div class="ttdef"><b>Definition:</b> cmsis_os2.h:243</div></div>
395 <div class="ttc" id="acmsis__os2_8h_html_ga6c0dbe6069e4e7f47bb4cd32ae2b813ea9e1c9e2550bb4de8969a935acffc968f"><div class="ttname"><a href="cmsis__os2_8h.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813ea9e1c9e2550bb4de8969a935acffc968f">osOK</a></div><div class="ttdeci">@ osOK</div><div class="ttdoc">Operation completed successfully.</div><div class="ttdef"><b>Definition:</b> cmsis_os2.h:240</div></div>
396 <div class="ttc" id="acmsis__os2_8h_html_ga6c0dbe6069e4e7f47bb4cd32ae2b813eac24adca6a5d072c9f01c32178ba0d109"><div class="ttname"><a href="cmsis__os2_8h.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813eac24adca6a5d072c9f01c32178ba0d109">osErrorParameter</a></div><div class="ttdeci">@ osErrorParameter</div><div class="ttdoc">Parameter error.</div><div class="ttdef"><b>Definition:</b> cmsis_os2.h:244</div></div>
397 <div class="ttc" id="agroup__CMSIS__RTOS__Definitions_html_ga6c0dbe6069e4e7f47bb4cd32ae2b813e"><div class="ttname"><a href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a></div><div class="ttdeci">osStatus_t</div><div class="ttdoc">Status code values returned by CMSIS-RTOS functions.</div><div class="ttdef"><b>Definition:</b> cmsis_os2.h:239</div></div>
398 <div class="ttc" id="agroup__CMSIS__RTOS__ThreadMgmt_html_ga48d68b8666d99d28fa646ee1d2182b8f"><div class="ttname"><a href="group__CMSIS__RTOS__ThreadMgmt.html#ga48d68b8666d99d28fa646ee1d2182b8f">osThreadNew</a></div><div class="ttdeci">osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)</div><div class="ttdoc">Create a thread and add it to Active Threads.</div></div>
399 <div class="ttc" id="agroup__CMSIS__RTOS__ThreadMgmt_html_gaa6c32fe2a3e0a2e01f212d55b02e51c7"><div class="ttname"><a href="group__CMSIS__RTOS__ThreadMgmt.html#gaa6c32fe2a3e0a2e01f212d55b02e51c7">osThreadId_t</a></div><div class="ttdeci">void * osThreadId_t</div><div class="ttdef"><b>Definition:</b> cmsis_os2.h:253</div></div>
400 <div class="ttc" id="agroup__CMSIS__RTOS__ThreadMgmt_html_gad01c7ec26535b1de6b018bb9466720e2"><div class="ttname"><a href="group__CMSIS__RTOS__ThreadMgmt.html#gad01c7ec26535b1de6b018bb9466720e2">osThreadYield</a></div><div class="ttdeci">osStatus_t osThreadYield(void)</div><div class="ttdoc">Pass control to next thread that is in state READY.</div></div>
401 </div><!-- fragment -->
404 <a id="ga9586952051f00285f1482dbe6695bbc4" name="ga9586952051f00285f1482dbe6695bbc4"></a>
405 <h2 class="memtitle"><span class="permalink"><a href="#ga9586952051f00285f1482dbe6695bbc4">◆ </a></span>osSemaphoreGetName()</h2>
407 <div class="memitem">
408 <div class="memproto">
409 <table class="memname">
411 <td class="memname">const char * osSemaphoreGetName </td>
413 <td class="paramtype"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> </td>
414 <td class="paramname"><em>semaphore_id</em></td><td>)</td>
418 </div><div class="memdoc">
419 <dl class="params"><dt>Parameters</dt><dd>
420 <table class="params">
421 <tr><td class="paramdir">[in]</td><td class="paramname">semaphore_id</td><td>semaphore ID obtained by <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </td></tr>
425 <dl class="section return"><dt>Returns</dt><dd>name as null-terminated string.</dd></dl>
426 <p>The function <b>osSemaphoreGetName</b> returns the pointer to the name string of the semaphore identified by parameter <em>semaphore_id</em> or <span class="XML-Token">NULL</span> in case of an error.</p>
427 <dl class="section note"><dt>Note</dt><dd>This function may be called from Interrupt Service Routines. </dd></dl>
431 <a id="ga7e94c8b242a0c81f2cc79ec22895c87b" name="ga7e94c8b242a0c81f2cc79ec22895c87b"></a>
432 <h2 class="memtitle"><span class="permalink"><a href="#ga7e94c8b242a0c81f2cc79ec22895c87b">◆ </a></span>osSemaphoreAcquire()</h2>
434 <div class="memitem">
435 <div class="memproto">
436 <table class="memname">
438 <td class="memname"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> osSemaphoreAcquire </td>
440 <td class="paramtype"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> </td>
441 <td class="paramname"><em>semaphore_id</em>, </td>
444 <td class="paramkey"></td>
446 <td class="paramtype">uint32_t </td>
447 <td class="paramname"><em>timeout</em> </td>
455 </div><div class="memdoc">
456 <dl class="params"><dt>Parameters</dt><dd>
457 <table class="params">
458 <tr><td class="paramdir">[in]</td><td class="paramname">semaphore_id</td><td>semaphore ID obtained by <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </td></tr>
459 <tr><td class="paramdir">[in]</td><td class="paramname">timeout</td><td>CMSIS_RTOS_TimeOutValue or 0 in case of no time-out. </td></tr>
463 <dl class="section return"><dt>Returns</dt><dd>status code that indicates the execution status of the function.</dd></dl>
464 <p>The blocking function <b>osSemaphoreAcquire</b> waits until a token of the semaphore object specified by parameter <em>semaphore_id</em> becomes available. If a token is available, the function instantly returns and decrements the token count.</p>
465 <p>The parameter <em>timeout</em> specifies how long the system waits to acquire the token. While the system waits, the thread that is calling this function is put into the <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ThreadStates">BLOCKED</a> state. The parameter timeout can have the following values:</p><ul>
466 <li>when <em>timeout</em> is <span class="XML-Token">0</span>, the function returns instantly (i.e. try semantics).</li>
467 <li>when <em>timeout</em> is set to <b>osWaitForever</b> the function will wait for an infinite time until the semaphore becomes available (i.e. wait semantics).</li>
468 <li>all other values specify a time in kernel ticks for a timeout (i.e. timed-wait semantics).</li>
470 <p>Possible <a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> return values:</p><ul>
471 <li><em>osOK:</em> the token has been obtained and the token count decremented.</li>
472 <li><em>osErrorTimeout:</em> the token could not be obtained in the given time.</li>
473 <li><em>osErrorResource:</em> the token could not be obtained when no <em>timeout</em> was specified.</li>
474 <li><em>osErrorParameter:</em> the parameter <em>semaphore_id</em> is <span class="XML-Token">NULL</span> or invalid.</li>
475 <li><em>osErrorSafetyClass:</em> the calling thread safety class is lower than the safety class of the specified semaphore.</li>
477 <dl class="section note"><dt>Note</dt><dd>May be called from Interrupt Service Routines if the parameter <em>timeout</em> is set to <span class="XML-Token">0</span>.</dd></dl>
478 <p><b>Code Example</b></p>
479 <p>Refer to <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </p>
483 <a id="ga0abcee1b5449d7a6928fb9248c690bb6" name="ga0abcee1b5449d7a6928fb9248c690bb6"></a>
484 <h2 class="memtitle"><span class="permalink"><a href="#ga0abcee1b5449d7a6928fb9248c690bb6">◆ </a></span>osSemaphoreRelease()</h2>
486 <div class="memitem">
487 <div class="memproto">
488 <table class="memname">
490 <td class="memname"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> osSemaphoreRelease </td>
492 <td class="paramtype"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> </td>
493 <td class="paramname"><em>semaphore_id</em></td><td>)</td>
497 </div><div class="memdoc">
498 <dl class="params"><dt>Parameters</dt><dd>
499 <table class="params">
500 <tr><td class="paramdir">[in]</td><td class="paramname">semaphore_id</td><td>semaphore ID obtained by <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </td></tr>
504 <dl class="section return"><dt>Returns</dt><dd>status code that indicates the execution status of the function.</dd></dl>
505 <p>The function <b>osSemaphoreRelease</b> releases a token of the semaphore object specified by parameter <em>semaphore_id</em>. Tokens can only be released up to the maximum count specified at creation time, see <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. Other threads that currently wait for a token of this semaphore object will be put into the <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ThreadStates">READY</a> state.</p>
506 <p>Possible <a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> return values:</p><ul>
507 <li><em>osOK:</em> the token has been released and the count incremented.</li>
508 <li><em>osErrorResource:</em> the token could not be released (maximum token count has been reached).</li>
509 <li><em>osErrorParameter:</em> the parameter <em>semaphore_id</em> is <span class="XML-Token">NULL</span> or invalid.</li>
510 <li><em>osErrorSafetyClass:</em> the calling thread safety class is lower than the safety class of the specified semaphore.</li>
512 <dl class="section note"><dt>Note</dt><dd>This function may be called from Interrupt Service Routines.</dd></dl>
513 <p><b>Code Example</b></p>
514 <p>Refer to <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </p>
518 <a id="ga7559d4dff3cda9992fc5ab5de3e74c70" name="ga7559d4dff3cda9992fc5ab5de3e74c70"></a>
519 <h2 class="memtitle"><span class="permalink"><a href="#ga7559d4dff3cda9992fc5ab5de3e74c70">◆ </a></span>osSemaphoreGetCount()</h2>
521 <div class="memitem">
522 <div class="memproto">
523 <table class="memname">
525 <td class="memname">uint32_t osSemaphoreGetCount </td>
527 <td class="paramtype"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> </td>
528 <td class="paramname"><em>semaphore_id</em></td><td>)</td>
532 </div><div class="memdoc">
533 <dl class="params"><dt>Parameters</dt><dd>
534 <table class="params">
535 <tr><td class="paramdir">[in]</td><td class="paramname">semaphore_id</td><td>semaphore ID obtained by <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </td></tr>
539 <dl class="section return"><dt>Returns</dt><dd>number of tokens available.</dd></dl>
540 <p>The function <b>osSemaphoreGetCount</b> returns the number of available tokens of the semaphore object specified by parameter <em>semaphore_id</em>. In case of an error it returns <span class="XML-Token">0</span>.</p>
541 <dl class="section note"><dt>Note</dt><dd>This function may be called from Interrupt Service Routines. </dd></dl>
545 <a id="ga81258ce9c67fa89f07cc49d2e136cd88" name="ga81258ce9c67fa89f07cc49d2e136cd88"></a>
546 <h2 class="memtitle"><span class="permalink"><a href="#ga81258ce9c67fa89f07cc49d2e136cd88">◆ </a></span>osSemaphoreDelete()</h2>
548 <div class="memitem">
549 <div class="memproto">
550 <table class="memname">
552 <td class="memname"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> osSemaphoreDelete </td>
554 <td class="paramtype"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> </td>
555 <td class="paramname"><em>semaphore_id</em></td><td>)</td>
559 </div><div class="memdoc">
560 <dl class="params"><dt>Parameters</dt><dd>
561 <table class="params">
562 <tr><td class="paramdir">[in]</td><td class="paramname">semaphore_id</td><td>semaphore ID obtained by <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </td></tr>
566 <dl class="section return"><dt>Returns</dt><dd>status code that indicates the execution status of the function.</dd></dl>
567 <p>The function <b>osSemaphoreDelete</b> deletes a semaphore object specified by parameter <em>semaphore_id</em>. It releases internal memory obtained for semaphore handling. After this call, the <em>semaphore_id</em> is no longer valid and cannot be used. The semaphore may be created again using the function <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>.</p>
568 <p>Possible <a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> return values:</p><ul>
569 <li><em>osOK:</em> the semaphore object has been deleted.</li>
570 <li><em>osErrorParameter:</em> the parameter <em>semaphore_id</em> is <span class="XML-Token">NULL</span> or invalid.</li>
571 <li><em>osErrorResource:</em> the semaphore is in an invalid state.</li>
572 <li><em>osErrorISR:</em> <b>osSemaphoreDelete</b> cannot be called from interrupt service routines.</li>
573 <li><em>osErrorSafetyClass:</em> the calling thread safety class is lower than the safety class of the specified semaphore.</li>
575 <dl class="section note"><dt>Note</dt><dd>This function <b>cannot</b> be called from Interrupt Service Routines. </dd></dl>
579 </div><!-- contents -->
580 </div><!-- doc-content -->
581 <!-- start footer part -->
582 <div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
585 <script type="text/javascript">
587 writeFooter.call(this);