OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [doc/] [html/] [ref/] [usbs-start-rx.html] - Diff between revs 28 and 174

Only display areas with differences | Details | Blame | View Log

Rev 28 Rev 174
<!-- Copyright (C) 2003 Red Hat, Inc.                                -->
<!-- Copyright (C) 2003 Red Hat, Inc.                                -->
<!-- This material may be distributed only subject to the terms      -->
<!-- This material may be distributed only subject to the terms      -->
<!-- and conditions set forth in the Open Publication License, v1.0  -->
<!-- and conditions set forth in the Open Publication License, v1.0  -->
<!-- or later (the latest version is presently available at          -->
<!-- or later (the latest version is presently available at          -->
<!-- http://www.opencontent.org/openpub/).                           -->
<!-- http://www.opencontent.org/openpub/).                           -->
<!-- Distribution of the work or derivative of the work in any       -->
<!-- Distribution of the work or derivative of the work in any       -->
<!-- standard (paper) book form is prohibited unless prior           -->
<!-- standard (paper) book form is prohibited unless prior           -->
<!-- permission is obtained from the copyright holder.               -->
<!-- permission is obtained from the copyright holder.               -->
<HTML
<HTML
><HEAD
><HEAD
><TITLE
><TITLE
>Receiving Data from the Host</TITLE
>Receiving Data from the Host</TITLE
><meta name="MSSmartTagsPreventParsing" content="TRUE">
><meta name="MSSmartTagsPreventParsing" content="TRUE">
<META
<META
NAME="GENERATOR"
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
"><LINK
REL="HOME"
REL="HOME"
TITLE="eCos Reference Manual"
TITLE="eCos Reference Manual"
HREF="ecos-ref.html"><LINK
HREF="ecos-ref.html"><LINK
REL="UP"
REL="UP"
TITLE="eCos USB Slave Support"
TITLE="eCos USB Slave Support"
HREF="io-usb-slave.html"><LINK
HREF="io-usb-slave.html"><LINK
REL="PREVIOUS"
REL="PREVIOUS"
TITLE="Devtab Entries"
TITLE="Devtab Entries"
HREF="usbs-devtab.html"><LINK
HREF="usbs-devtab.html"><LINK
REL="NEXT"
REL="NEXT"
TITLE="Sending Data to the Host"
TITLE="Sending Data to the Host"
HREF="usbs-start-tx.html"></HEAD
HREF="usbs-start-tx.html"></HEAD
><BODY
><BODY
CLASS="REFENTRY"
CLASS="REFENTRY"
BGCOLOR="#FFFFFF"
BGCOLOR="#FFFFFF"
TEXT="#000000"
TEXT="#000000"
LINK="#0000FF"
LINK="#0000FF"
VLINK="#840084"
VLINK="#840084"
ALINK="#0000FF"
ALINK="#0000FF"
><DIV
><DIV
CLASS="NAVHEADER"
CLASS="NAVHEADER"
><TABLE
><TABLE
SUMMARY="Header navigation table"
SUMMARY="Header navigation table"
WIDTH="100%"
WIDTH="100%"
BORDER="0"
BORDER="0"
CELLPADDING="0"
CELLPADDING="0"
CELLSPACING="0"
CELLSPACING="0"
><TR
><TR
><TH
><TH
COLSPAN="3"
COLSPAN="3"
ALIGN="center"
ALIGN="center"
>eCos Reference Manual</TH
>eCos Reference Manual</TH
></TR
></TR
><TR
><TR
><TD
><TD
WIDTH="10%"
WIDTH="10%"
ALIGN="left"
ALIGN="left"
VALIGN="bottom"
VALIGN="bottom"
><A
><A
HREF="usbs-devtab.html"
HREF="usbs-devtab.html"
ACCESSKEY="P"
ACCESSKEY="P"
>Prev</A
>Prev</A
></TD
></TD
><TD
><TD
WIDTH="80%"
WIDTH="80%"
ALIGN="center"
ALIGN="center"
VALIGN="bottom"
VALIGN="bottom"
></TD
></TD
><TD
><TD
WIDTH="10%"
WIDTH="10%"
ALIGN="right"
ALIGN="right"
VALIGN="bottom"
VALIGN="bottom"
><A
><A
HREF="usbs-start-tx.html"
HREF="usbs-start-tx.html"
ACCESSKEY="N"
ACCESSKEY="N"
>Next</A
>Next</A
></TD
></TD
></TR
></TR
></TABLE
></TABLE
><HR
><HR
ALIGN="LEFT"
ALIGN="LEFT"
WIDTH="100%"></DIV
WIDTH="100%"></DIV
><H1
><H1
><A
><A
NAME="USBS-START-RX">Receiving Data from the Host</H1
NAME="USBS-START-RX">Receiving Data from the Host</H1
><DIV
><DIV
CLASS="REFNAMEDIV"
CLASS="REFNAMEDIV"
><A
><A
NAME="AEN16314"
NAME="AEN16314"
></A
></A
><H2
><H2
>Name</H2
>Name</H2
><TT
><TT
CLASS="FUNCTION"
CLASS="FUNCTION"
>usbs_start_rx_buffer</TT
>usbs_start_rx_buffer</TT
>&nbsp;--&nbsp;Receiving Data from the Host</DIV
>&nbsp;--&nbsp;Receiving Data from the Host</DIV
><DIV
><DIV
CLASS="REFSYNOPSISDIV"
CLASS="REFSYNOPSISDIV"
><A
><A
NAME="AEN16318"><H2
NAME="AEN16318"><H2
>Synopsis</H2
>Synopsis</H2
><DIV
><DIV
CLASS="FUNCSYNOPSIS"
CLASS="FUNCSYNOPSIS"
><A
><A
NAME="AEN16319"><P
NAME="AEN16319"><P
></P
></P
><TABLE
><TABLE
BORDER="5"
BORDER="5"
BGCOLOR="#E0E0F0"
BGCOLOR="#E0E0F0"
WIDTH="70%"
WIDTH="70%"
><TR
><TR
><TD
><TD
><PRE
><PRE
CLASS="FUNCSYNOPSISINFO"
CLASS="FUNCSYNOPSISINFO"
>#include &lt;cyg/io/usb/usbs.h&gt;</PRE
>#include &lt;cyg/io/usb/usbs.h&gt;</PRE
></TD
></TD
></TR
></TR
></TABLE
></TABLE
><P
><P
><CODE
><CODE
><CODE
><CODE
CLASS="FUNCDEF"
CLASS="FUNCDEF"
>void usbs_start_rx_buffer</CODE
>void usbs_start_rx_buffer</CODE
>(usbs_rx_endpoint* ep, unsigned char* buffer, int length, void (*)(void*,int) complete_fn, void * complete_data);</CODE
>(usbs_rx_endpoint* ep, unsigned char* buffer, int length, void (*)(void*,int) complete_fn, void * complete_data);</CODE
></P
></P
><P
><P
><CODE
><CODE
><CODE
><CODE
CLASS="FUNCDEF"
CLASS="FUNCDEF"
>void usbs_start_rx</CODE
>void usbs_start_rx</CODE
>(usbs_rx_endpoint* ep);</CODE
>(usbs_rx_endpoint* ep);</CODE
></P
></P
><P
><P
></P
></P
></DIV
></DIV
></DIV
></DIV
><DIV
><DIV
CLASS="REFSECT1"
CLASS="REFSECT1"
><A
><A
NAME="AEN16339"
NAME="AEN16339"
></A
></A
><H2
><H2
><TT
><TT
CLASS="FUNCTION"
CLASS="FUNCTION"
>Description</TT
>Description</TT
></H2
></H2
><P
><P
><TT
><TT
CLASS="FUNCTION"
CLASS="FUNCTION"
>usbs_start_rx_buffer</TT
>usbs_start_rx_buffer</TT
> is a USB-specific function
> is a USB-specific function
to accept a transfer from host to peripheral. It can be used for bulk,
to accept a transfer from host to peripheral. It can be used for bulk,
interrupt or isochronous transfers, but not for control messages.
interrupt or isochronous transfers, but not for control messages.
Instead those involve manipulating the <A
Instead those involve manipulating the <A
HREF="usbs-control.html"
HREF="usbs-control.html"
><SPAN
><SPAN
CLASS="STRUCTNAME"
CLASS="STRUCTNAME"
>usbs_control_endpoint</SPAN
>usbs_control_endpoint</SPAN
></A
></A
>
>
data structure directly. The function takes five arguments:</P
data structure directly. The function takes five arguments:</P
><P
><P
></P
></P
><OL
><OL
TYPE="1"
TYPE="1"
><LI
><LI
><P
><P
>The first argument identifies the specific endpoint that should be
>The first argument identifies the specific endpoint that should be
used. Different USB devices will support different sets of endpoints
used. Different USB devices will support different sets of endpoints
and the device driver will provide appropriate data structures. The
and the device driver will provide appropriate data structures. The
device driver's documentation should be consulted for details of which
device driver's documentation should be consulted for details of which
endpoints are available.</P
endpoints are available.</P
></LI
></LI
><LI
><LI
><P
><P
>The <TT
>The <TT
CLASS="PARAMETER"
CLASS="PARAMETER"
><I
><I
>buffer</I
>buffer</I
></TT
></TT
> and <TT
> and <TT
CLASS="PARAMETER"
CLASS="PARAMETER"
><I
><I
>length</I
>length</I
></TT
></TT
>
>
arguments control the actual transfer. USB device drivers are not
arguments control the actual transfer. USB device drivers are not
expected to perform any buffering or to support partial transfers, so
expected to perform any buffering or to support partial transfers, so
the length specified should correspond to the maximum transfer that is
the length specified should correspond to the maximum transfer that is
currently possible and the buffer should be at least this large. For
currently possible and the buffer should be at least this large. For
isochronous transfers the USB specification imposes an upper bound of
isochronous transfers the USB specification imposes an upper bound of
1023 bytes, and a smaller limit may be set in the <A
1023 bytes, and a smaller limit may be set in the <A
HREF="usbs-enum.html#AEN16179"
HREF="usbs-enum.html#AEN16179"
>enumeration data</A
>enumeration data</A
>. Interrupt
>. Interrupt
transfers are similarly straightforward with an upper bound of 64
transfers are similarly straightforward with an upper bound of 64
bytes, or less as per the enumeration data. Bulk transfers are more
bytes, or less as per the enumeration data. Bulk transfers are more
complicated because they can involve multiple 64-byte packets plus a
complicated because they can involve multiple 64-byte packets plus a
terminating packet of less than 64 bytes, so there is no predefined
terminating packet of less than 64 bytes, so there is no predefined
limit on the transfer size. Instead it is left to higher-level
limit on the transfer size. Instead it is left to higher-level
protocols to specify an appropriate upper bound.</P
protocols to specify an appropriate upper bound.</P
><P
><P
>One technique that may work for bulk transfers is to exploit the fact
>One technique that may work for bulk transfers is to exploit the fact
that such transfers happen in 64-byte packets: it may be possible to
that such transfers happen in 64-byte packets: it may be possible to
receive an initial 64 bytes, corresponding to the first packet in the
receive an initial 64 bytes, corresponding to the first packet in the
transfer; these 64 bytes can then be examined to determine the total
transfer; these 64 bytes can then be examined to determine the total
transfer size, and the remaining data can be transferred in another
transfer size, and the remaining data can be transferred in another
receive operation. This technique is not guaranteed to work with all
receive operation. This technique is not guaranteed to work with all
USB hardware. Also, if the delay between accepting the first packet and
USB hardware. Also, if the delay between accepting the first packet and
the remainder of the transfer is excessive then this could cause
the remainder of the transfer is excessive then this could cause
timeout problems for the host-side software. For these reasons this
timeout problems for the host-side software. For these reasons this
technique should be avoided.</P
technique should be avoided.</P
></LI
></LI
><LI
><LI
><P
><P
><TT
><TT
CLASS="FUNCTION"
CLASS="FUNCTION"
>usbs_start_rx_buffer</TT
>usbs_start_rx_buffer</TT
> is non-blocking. It merely
> is non-blocking. It merely
starts the receive operation, and does not wait for completion. At
starts the receive operation, and does not wait for completion. At
some later point the USB device driver will invoke the completion
some later point the USB device driver will invoke the completion
function parameter with two arguments: the completion data defined by
function parameter with two arguments: the completion data defined by
the last parameter and a result field. A result &gt;=
the last parameter and a result field. A result &gt;=
<TT
<TT
CLASS="LITERAL"
CLASS="LITERAL"
>0</TT
>0</TT
> indicates a successful transfer of that many
> indicates a successful transfer of that many
bytes, which may be less than the upper bound imposed by the
bytes, which may be less than the upper bound imposed by the
<TT
<TT
CLASS="PARAMETER"
CLASS="PARAMETER"
><I
><I
>length</I
>length</I
></TT
></TT
> argument. A result &lt;
> argument. A result &lt;
<TT
<TT
CLASS="LITERAL"
CLASS="LITERAL"
>0</TT
>0</TT
> indicates an error. The most likely errors are
> indicates an error. The most likely errors are
<TT
<TT
CLASS="LITERAL"
CLASS="LITERAL"
>-EPIPE</TT
>-EPIPE</TT
> to indicate that the connection between the
> to indicate that the connection between the
host and the target has been broken, and <TT
host and the target has been broken, and <TT
CLASS="LITERAL"
CLASS="LITERAL"
>-EAGAIN</TT
>-EAGAIN</TT
>
>
for when the endpoint has been <A
for when the endpoint has been <A
HREF="usbs-halt.html"
HREF="usbs-halt.html"
>halted</A
>halted</A
>. Specific USB device drivers may
>. Specific USB device drivers may
specify additional error conditions.</P
specify additional error conditions.</P
></LI
></LI
></OL
></OL
><P
><P
>The normal sequence of events is that the USB device driver will
>The normal sequence of events is that the USB device driver will
update the appropriate hardware registers. At some point after that
update the appropriate hardware registers. At some point after that
the host will attempt to send data by transmitting an OUT token
the host will attempt to send data by transmitting an OUT token
followed by a data packet, and since a receive operation is now in
followed by a data packet, and since a receive operation is now in
progress the data will be accepted and ACK'd. If there were no receive
progress the data will be accepted and ACK'd. If there were no receive
operation then the peripheral would instead generate a NAK. The USB
operation then the peripheral would instead generate a NAK. The USB
hardware will generate an interrupt once the whole packet has been
hardware will generate an interrupt once the whole packet has been
received, and the USB device driver will service this interrupt and
received, and the USB device driver will service this interrupt and
arrange for a DSR to be called. Isochronous and interrupt transfers
arrange for a DSR to be called. Isochronous and interrupt transfers
involve just a single packet. However, bulk transfers may involve
involve just a single packet. However, bulk transfers may involve
multiple packets so the device driver has to check whether the packet
multiple packets so the device driver has to check whether the packet
was a full 64 bytes or whether it was a terminating packet of less
was a full 64 bytes or whether it was a terminating packet of less
than this. When the device driver DSR detects a complete transfer it
than this. When the device driver DSR detects a complete transfer it
will inform higher-level code by invoking the supplied completion
will inform higher-level code by invoking the supplied completion
function.</P
function.</P
><P
><P
>This means that the completion function will normally be invoked by a
>This means that the completion function will normally be invoked by a
DSR and not in thread context - although some USB device drivers may
DSR and not in thread context - although some USB device drivers may
have a different implementation. Therefore the completion function is
have a different implementation. Therefore the completion function is
restricted in what it can do. In particular it must not make any
restricted in what it can do. In particular it must not make any
calls that will or may block such as locking a mutex or allocating
calls that will or may block such as locking a mutex or allocating
memory. The kernel documentation should be consulted for more details
memory. The kernel documentation should be consulted for more details
of DSR's and interrupt handling generally.</P
of DSR's and interrupt handling generally.</P
><P
><P
>It is possible that the completion function will be invoked before
>It is possible that the completion function will be invoked before
<TT
<TT
CLASS="FUNCTION"
CLASS="FUNCTION"
>usbs_start_rx_buffer</TT
>usbs_start_rx_buffer</TT
> returns. Such an event would
> returns. Such an event would
be unusual because the transfer cannot happen until the next time the
be unusual because the transfer cannot happen until the next time the
host tries to send data to this peripheral, but it may happen if for
host tries to send data to this peripheral, but it may happen if for
example another interrupt happens and a higher priority thread is
example another interrupt happens and a higher priority thread is
scheduled to run. Also, if the endpoint is currently halted then the
scheduled to run. Also, if the endpoint is currently halted then the
completion function will be invoked immediately with
completion function will be invoked immediately with
<TT
<TT
CLASS="LITERAL"
CLASS="LITERAL"
>-EAGAIN</TT
>-EAGAIN</TT
>: typically this will happen in the current
>: typically this will happen in the current
thread rather than in a separate DSR. The completion function is
thread rather than in a separate DSR. The completion function is
allowed to start another transfer immediately by calling
allowed to start another transfer immediately by calling
<TT
<TT
CLASS="FUNCTION"
CLASS="FUNCTION"
>usbs_start_rx_buffer</TT
>usbs_start_rx_buffer</TT
> again.</P
> again.</P
><P
><P
>USB device drivers are not expected to perform any locking. It is the
>USB device drivers are not expected to perform any locking. It is the
responsibility of higher-level code to ensure that there is only one
responsibility of higher-level code to ensure that there is only one
receive operation for a given endpoint in progress at any one time. If
receive operation for a given endpoint in progress at any one time. If
there are concurrent calls to
there are concurrent calls to
<TT
<TT
CLASS="FUNCTION"
CLASS="FUNCTION"
>usbs_start_rx_buffer</TT
>usbs_start_rx_buffer</TT
> then the resulting behaviour
> then the resulting behaviour
is undefined. For typical USB applications this does not present any
is undefined. For typical USB applications this does not present any
problems, because only one piece of code will access a given endpoint
problems, because only one piece of code will access a given endpoint
at any particular time.</P
at any particular time.</P
><P
><P
>The following code fragment illustrates a very simple use of
>The following code fragment illustrates a very simple use of
<TT
<TT
CLASS="FUNCTION"
CLASS="FUNCTION"
>usbs_start_rx_buffer</TT
>usbs_start_rx_buffer</TT
> to implement a blocking
> to implement a blocking
receive, using a semaphore to synchronise between the foreground
receive, using a semaphore to synchronise between the foreground
thread and the DSR. For a simple example like this no completion data
thread and the DSR. For a simple example like this no completion data
is needed.</P
is needed.</P
><TABLE
><TABLE
BORDER="5"
BORDER="5"
BGCOLOR="#E0E0F0"
BGCOLOR="#E0E0F0"
WIDTH="70%"
WIDTH="70%"
><TR
><TR
><TD
><TD
><PRE
><PRE
CLASS="PROGRAMLISTING"
CLASS="PROGRAMLISTING"
>static int error_code = 0;
>static int error_code = 0;
static cyg_sem_t completion_wait;
static cyg_sem_t completion_wait;
 
 
static void
static void
completion_fn(void* data, int result)
completion_fn(void* data, int result)
{
{
    error_code = result;
    error_code = result;
    cyg_semaphore_post(&amp;completion_wait);
    cyg_semaphore_post(&amp;completion_wait);
}
}
 
 
int
int
blocking_receive(usbs_rx_endpoint* ep, unsigned char* buf, int len)
blocking_receive(usbs_rx_endpoint* ep, unsigned char* buf, int len)
{
{
    error_code = 0;
    error_code = 0;
    usbs_start_rx_buffer(ep, buf, len, &amp;completion_fn, NULL);
    usbs_start_rx_buffer(ep, buf, len, &amp;completion_fn, NULL);
    cyg_semaphore_wait(&amp;completion_wait);
    cyg_semaphore_wait(&amp;completion_wait);
    return error_code;
    return error_code;
}</PRE
}</PRE
></TD
></TD
></TR
></TR
></TABLE
></TABLE
><P
><P
>There is also a utility function <TT
>There is also a utility function <TT
CLASS="FUNCTION"
CLASS="FUNCTION"
>usbs_start_rx</TT
>usbs_start_rx</TT
>. This
>. This
can be used by code that wants to manipulate <A
can be used by code that wants to manipulate <A
HREF="usbs-data.html"
HREF="usbs-data.html"
>data endpoints</A
>data endpoints</A
> directly, specifically the
> directly, specifically the
<TT
<TT
CLASS="STRUCTFIELD"
CLASS="STRUCTFIELD"
><I
><I
>complete_fn</I
>complete_fn</I
></TT
></TT
>,
>,
<TT
<TT
CLASS="STRUCTFIELD"
CLASS="STRUCTFIELD"
><I
><I
>complete_data</I
>complete_data</I
></TT
></TT
>,
>,
<TT
<TT
CLASS="STRUCTFIELD"
CLASS="STRUCTFIELD"
><I
><I
>buffer</I
>buffer</I
></TT
></TT
> and
> and
<TT
<TT
CLASS="STRUCTFIELD"
CLASS="STRUCTFIELD"
><I
><I
>buffer_size</I
>buffer_size</I
></TT
></TT
> fields.
> fields.
<TT
<TT
CLASS="FUNCTION"
CLASS="FUNCTION"
>usbs_start_tx</TT
>usbs_start_tx</TT
> just invokes a function
> just invokes a function
supplied by the device driver.</P
supplied by the device driver.</P
></DIV
></DIV
><DIV
><DIV
CLASS="NAVFOOTER"
CLASS="NAVFOOTER"
><HR
><HR
ALIGN="LEFT"
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
SUMMARY="Footer navigation table"
WIDTH="100%"
WIDTH="100%"
BORDER="0"
BORDER="0"
CELLPADDING="0"
CELLPADDING="0"
CELLSPACING="0"
CELLSPACING="0"
><TR
><TR
><TD
><TD
WIDTH="33%"
WIDTH="33%"
ALIGN="left"
ALIGN="left"
VALIGN="top"
VALIGN="top"
><A
><A
HREF="usbs-devtab.html"
HREF="usbs-devtab.html"
ACCESSKEY="P"
ACCESSKEY="P"
>Prev</A
>Prev</A
></TD
></TD
><TD
><TD
WIDTH="34%"
WIDTH="34%"
ALIGN="center"
ALIGN="center"
VALIGN="top"
VALIGN="top"
><A
><A
HREF="ecos-ref.html"
HREF="ecos-ref.html"
ACCESSKEY="H"
ACCESSKEY="H"
>Home</A
>Home</A
></TD
></TD
><TD
><TD
WIDTH="33%"
WIDTH="33%"
ALIGN="right"
ALIGN="right"
VALIGN="top"
VALIGN="top"
><A
><A
HREF="usbs-start-tx.html"
HREF="usbs-start-tx.html"
ACCESSKEY="N"
ACCESSKEY="N"
>Next</A
>Next</A
></TD
></TD
></TR
></TR
><TR
><TR
><TD
><TD
WIDTH="33%"
WIDTH="33%"
ALIGN="left"
ALIGN="left"
VALIGN="top"
VALIGN="top"
>Devtab Entries</TD
>Devtab Entries</TD
><TD
><TD
WIDTH="34%"
WIDTH="34%"
ALIGN="center"
ALIGN="center"
VALIGN="top"
VALIGN="top"
><A
><A
HREF="io-usb-slave.html"
HREF="io-usb-slave.html"
ACCESSKEY="U"
ACCESSKEY="U"
>Up</A
>Up</A
></TD
></TD
><TD
><TD
WIDTH="33%"
WIDTH="33%"
ALIGN="right"
ALIGN="right"
VALIGN="top"
VALIGN="top"
>Sending Data to the Host</TD
>Sending Data to the Host</TD
></TR
></TR
></TABLE
></TABLE
></DIV
></DIV
></BODY
></BODY
></HTML
></HTML
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.