⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c23.htm

📁 ST20 Embedded Toolset R2.0.5用于开发基于ST20芯片机顶盒软件的开发平台,2.0.5版本,国内找不到的.在国外论坛上花了N天才找到!
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Quadralay WebWorks Publisher Professional Edition 6.0.5">
<meta name="TEMPLATEBASE" content="book_html">
<meta name="LASTUPDATED" content="12/01/03 14:15:48">
<title>14.2 Channels</title>

<STYLE TYPE="text/css">
<!--
	span.Signal { text-transform: uppercase; font-family: Verdana }
-->
</STYLE>

</head>

<body link="#3366CC" vlink="#9999CC" text="#000000" alink="#0000CC" bgcolor="#FFFFFF"
background="images/backgrnd.gif">

<p><img src="images/stlogo.gif" width="106" height="83" align="left"
alt="logo here!"> </p>

<table width="331" border="0" align="right" cellpadding="0" cellspacing="0">
  <tr>
    <td><a href="os20toc.htm"><img src="images/navtoc.gif" width="84" height="23"
    border="0" alt="TOC"> </a></td>
    <td><a href="c22.htm"><img src="images/navprev.gif" width="80" height="23"
    border="0" alt="PREV"> </a></td>
    <td><a href="c24.htm"><img src="images/navnext.gif" width="83" height="23"
    border="0" alt="NEXT"> </a></td>
    <td><a href="os20ix.htm"><img src="images/navidx.gif" width="84" height="23"
    border="0" alt="INDEX"> </a></td>
  </tr>
</table>

<p><br clear="all">
</p>

<hr align="left">

<blockquote>
<h2>
  <a name="1204450"> </a><font color="#003366"  face="Verdana, Arial, Helvetica, sans-serif">14.2 	 Channels</font>
</h2><hr>


<p>
  <a name="1204454"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">OS20 supports the use of channels by all tasks (both normal/low and high priority).</font>
</p>


<p>
  <a name="1204455"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Channels are a way of transferring data from one task to another, and they also provide a way of synchronizing the actions of tasks. If one task needs to wait for another to reach a particular state, then a channel is a suitable way of ensuring that happens.</font>
</p>


<p>
  <a name="1204456"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">If one task is sending and one receiving on the same channel then whichever tries to communicate first waits until the other communicates. The data is copied from the memory of the sending task to the memory of the receiving task and both tasks then continue. If only one task attempts to communicate then it will wait forever.</font>
</p>


<p>
  <a name="1204457"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">A channel communicates in one direction, so if two tasks need bidirectional communication, then two channels are needed, one in each direction. Any data can be passed down a channel, but the user must ensure that the tasks agree a protocol in order to interpret the data correctly.</font>
</p>


<p>
  <a name="1204458"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">It is the responsibility of the programmer to ensure that:</font>
</p>

<ul>
<p>  <font size=2  face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1204459"> </a>data sent by one task is received by another,</font></p>
<p>  <font size=2  face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1204460"> </a>there is never more than one task sending on one channel,</font></p>
<p>  <font size=2  face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1204461"> </a>there is never more than one task receiving on one channel,</font></p>
<p>  <font size=2  face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1204462"> </a>the amounts of data sent and received are the same,</font></p>
<p>  <font size=2  face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1204463"> </a>the types of data sent and received are the same.</font></p>
</ul>

<p>
  <a name="1204464"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">If any of these rules are broken then the effect is not defined.</font>
</p>


<p>
  <a name="1204465"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Channels between tasks are created by using the data structure <font size=2 face=Courier><strong>chan_t</strong></font> and initializing it by calling a library function. Channel input and output functions are then used to pass data. Separate functions exist for input and output and the two must be paired for communication between two tasks to take place. The header file <font size=2 face=Courier><strong>chan.h</strong></font> declares the <font size=2 face=Courier><strong>chan_t</strong></font> data type and channel library functions.</font>
</p>


<p>
  <a name="1204467"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">If one task has exclusive access to a particular resource and acts as a server for the other tasks, then channels can also act as a queuing mechanism for the server to wait for the next of several possible inputs and handle them in turn.</font>
</p>


<p>
  <a name="1204468"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">A channel used to communicate between two tasks on the same processor is known as a "soft燾hannel". A channel used to communicate with a hardware peripheral is known as a "hard燾hannel".</font>
</p>


<p>
  <a name="1204472"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">When the OS20 scheduler is enabled (by calling <font size=2 face=Courier><strong>kernel_start</strong></font>), channel communication results in traps to the kernel, which ensure that correct scheduling semantics are maintained.</font>
</p>


<h3>
  <a name="1204490"> </a><font color="#003366"  face="Verdana, Arial, Helvetica, sans-serif">14.2.1 	 Creating a channel</font>
</h3>


<p>
  <a name="1204494"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">OS20 refers to channels using a <font size=2 face=Courier><strong>chan_t</strong></font> structure. This needs to be initialized before it can be used, by using one of the following functions:</font>
</p>


<a name="1204495"> </a><font size=2 face=Courier><strong>chan_t *chan_create(void)<br>chan_t *chan_create_address(void <font size=2 face=Courier><strong><em>*address</em></strong></font>)<br>chan_init(chan_t *<font size=2 face=Courier><strong><em>chan</em></strong></font>);<br>void chan_init_address(chan_t <font size=2 face=Courier><strong><em>*chan</em></strong></font>, void <font size=2 face=Courier><strong><em>*address</em></strong></font>);<br></strong></font>



<p>
  <a name="1204496"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">The <font size=2 face=Courier><strong>_create</strong></font> versions allocate memory for the data structure from the system partition and initialize the channel to their default state. <font size=2 face=Courier><strong>chan_create</strong></font> creates a "soft" channel, <font size=2 face=Courier><strong>chan_create_address</strong></font> creates a "hard" channel.</font>
</p>


<p>
  <a name="1204497"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">The <font size=2 face=Courier><strong>_init</strong></font> versions also initialize a channel, but the allocation of memory for <font size=2 face=Courier><strong>chan_t</strong></font> is left to the user. <font size=2 face=Courier><strong>chan_init</strong></font> initializes a "soft" channel and <font size=2 face=Courier><strong>chan_init_address</strong></font> initializes a "hard" channel:</font>
</p>


<p>
  <a name="1204498"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">For example:</font>
</p>


<a name="1204499"> </a><font size=2 face=Courier><strong>#include &lt;chan.h&gt; <br><font size=2 face=Courier><em>/* Initialize a soft channel */<br></em></font>chan_t soft_chan;<br>chan_init(&amp;soft_chan);<br><br></strong></font>



<a name="1204502"> </a><font size=2 face=Courier><strong><font size=2 face=Courier><em>/* Initialize a hard channel to link 0 input channel */<br></em></font>chan_t chan0;<br>chan_init_address(&amp;chan0, (void*)0x80000010);<br></strong></font>



<h3>
  <a name="1204505"> </a><font color="#003366"  face="Verdana, Arial, Helvetica, sans-serif">14.2.2 	 Communications over channels</font>
</h3>


<p>
  <a name="1204506"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Once a channel has been initialized, there are several functions available for communications:</font>
</p>


<a name="1204508"> </a><font size=2 face=Courier><strong>void chan_in(chan_t <font size=2 face=Courier><strong><em>*chan</em></strong></font>, void* <font size=2 face=Courier><strong><em>cp</em></strong></font>, int <font size=2 face=Courier><strong><em>count</em></strong></font>);<br>void chan_out(chan_t <font size=2 face=Courier><strong><em>*chan</em></strong></font>, const void* <font size=2 face=Courier><strong><em>cp</em></strong></font>, int <font size=2 face=Courier><strong><em>count</em></strong></font>);<br>int chan_in_int(chan_t <font size=2 face=Courier><strong><em>*chan</em></strong></font>);<br>void chan_out_int(chan_t <font size=2 face=Courier><strong><em>*chan</em></strong></font>, int <font size=2 face=Courier><strong><em>data</em></strong></font>);<br>char chan_in_char(chan_t <font size=2 face=Courier><strong><em>*chan</em></strong></font>);<br>void chan_out_char(chan_t <font size=2 face=Courier><strong><em>*chan</em></strong></font>, char <font size=2 face=Courier><strong><em>data</em></strong></font>);<br></strong></font>



<p>
  <a name="1204516"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">These functions transfer a block of data (<font size=2 face=Courier><strong>chan_in</strong></font> and <font size=2 face=Courier><strong>chan_out</strong></font>), an integer (<font size=2 face=Courier><strong>chan_in_int</strong></font> and <font size=2 face=Courier><strong>chan_out_int</strong></font>) or a character (<font size=2 face=Courier><strong>chan_in_char</strong></font> and <font size=2 face=Courier><strong>chan_out_char</strong></font>).</font>
</p>


<p>
  <a name="1204517"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Each communications function call represents a single communication. The task does not continue until the transfer is complete.</font>
</p>


<p>
  <a name="1204518"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Take care to ensure that data is only transferred in one direction across the channel, and that the sending and receiving data is the same length, as this is not checked for at run time.</font>
</p>


<p>
  <a name="1204519"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">For example, the following code uses channel <font size=2 face=Courier><strong>my_chan</strong></font> to send a character followed by an integer followed by a string:</font>
</p>


<a name="1204520"> </a><font size=2 face=Courier><strong>#include &lt;chan.h&gt;<br>char ch1;<br>int n1;<br><br></strong></font>



<a name="1207671"> </a><font size=2 face=Courier><strong>chan_out_char (my_chan, ch1);<br>chan_out_int (my_chan, n1);<br>chan_out (my_chan, "Hello", 5);<br></strong></font>



<p>
  <a name="1204521"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">To receive this data on channel <font size=2 face=Courier><strong>my_chan</strong></font>, the following code could be used:</font>
</p>


<a name="1204522"> </a><font size=2 face=Courier><strong>#include &lt;chan.h&gt;<br>char ch, buffer[5];<br>int n;<br><br></strong></font>



<a name="1207672"> </a><font size=2 face=Courier><strong>ch = chan_in_char (my_chan);<br>n = chan_in_int (my_chan);<br>chan_in (my_chan, buffer, 5);<br></strong></font>



<h3>
  <a name="1204524"> </a><font color="#003366"  face="Verdana, Arial, Helvetica, sans-serif">14.2.3 	 Reading from several channels</font>
</h3>


<p>
  <a name="1204525"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">There are many cases where a receiving task needs to listen to several channels and wishes to detect which one has data ready first. The ST20C2 micro-kernel provides a mechanism to handle this situation called an alternative input. This is implemented in OS20 by the following function:</font>
</p>


<a name="1204530"> </a><font size=2 face=Courier><strong>int chan_alt( chan_t ** <font size=2 face=Courier><strong><em>chanlist</em></strong></font>,<br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -