Download STLite/OS20 Real-Time Kernel Reference Manual

Transcript
13.1 Channels
For example, the following code will use channel my_chan to send a character
followed by an integer followed by a string:
#include <chan.h>
char ch1;
int n1;
chan_out_char (my_chan, ch1);
chan_out_int (my_chan, n1);
chan_out (my_chan, ”Hello”, 5);
To receive this data on channel my_chan, the following code could be used:
#include <chan.h>
char ch, buffer[5];
int n;
ch = chan_in_char (my_chan);
n = chan_in_int (my_chan);
chan_in (my_chan, buffer, 5);
13.1.3 Reading from several channels
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 ST20-C2 micro-kernel provides a
mechanism to handle this situation called an alternative input. This is implemented in
STLite/OS20 by the following function:
int chan_alt(chan_t ** chanlist, int nchans,
const clock_t *timeout);
chan_alt takes as parameters an array of channel pointers, and a count of the
number of elements in the array. It returns the index of the selected channel, starting
at zero for the first channel. The selected channel may then be read, using the input
functions described above in section 13.1.2. Any channels that become ready and are
not read will continue to wait. In addition an optional timeout may be provided, which
allows chan_alt to be used in a polling mode, or wait until a specified time before
returning, whether a channel has become ready for reading or not. Timeouts for
channels are implemented using hardware and so do not increase the application’s
code size.
Normally chan_alt will be used with the time-out value TIMEOUT_INFINITY, in
which case only one of the channels becoming ready (i.e. one of the sending tasks is
trying to send) will cause it to return. When one or more channels are ready then one
will be selected. If no channel becomes ready then the function will wait for ever. Note:
that the header file ostime.h must be included when using this function.
To input from an array of channels, the returned index can be used as an index into
the channel array, for example:
#include <chan.h>
#include <ostime.h>
220