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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [usb/] [slave/] [v2_0/] [doc/] [usbs-start-rx.html] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
<!-- Copyright (C) 2002 Red Hat, Inc.                                -->
2
<!-- This material may be distributed only subject to the terms      -->
3
<!-- and conditions set forth in the Open Publication License, v1.0  -->
4
<!-- or later (the latest version is presently available at          -->
5
<!-- http://www.opencontent.org/openpub/).                           -->
6
<!-- Distribution of substantively modified versions of this         -->
7
<!-- document is prohibited without the explicit permission of the   -->
8
<!-- copyright holder.                                               -->
9
<!-- Distribution of the work or derivative of the work in any       -->
10
<!-- standard (paper) book form is prohibited unless prior           -->
11
<!-- permission is obtained from the copyright holder.               -->
12
<HTML
13
><HEAD
14
><TITLE
15
>Receiving Data from the Host</TITLE
16
><meta name="MSSmartTagsPreventParsing" content="TRUE">
17
<META
18
NAME="GENERATOR"
19
CONTENT="Modular DocBook HTML Stylesheet Version 1.64
20
"><LINK
21
REL="HOME"
22
TITLE="eCos USB Slave Support"
23
HREF="io-usb-slave.html"><LINK
24
REL="PREVIOUS"
25
TITLE="Devtab Entries"
26
HREF="usbs-devtab.html"><LINK
27
REL="NEXT"
28
TITLE="Sending Data to the Host"
29
HREF="usbs-start-tx.html"></HEAD
30
><BODY
31
CLASS="REFENTRY"
32
BGCOLOR="#FFFFFF"
33
TEXT="#000000"
34
LINK="#0000FF"
35
VLINK="#840084"
36
ALINK="#0000FF"
37
><DIV
38
CLASS="NAVHEADER"
39
><TABLE
40
WIDTH="100%"
41
BORDER="0"
42
CELLPADDING="0"
43
CELLSPACING="0"
44
><TR
45
><TH
46
COLSPAN="3"
47
ALIGN="center"
48
>eCos USB Slave Support</TH
49
></TR
50
><TR
51
><TD
52
WIDTH="10%"
53
ALIGN="left"
54
VALIGN="bottom"
55
><A
56
HREF="usbs-devtab.html"
57
>Prev</A
58
></TD
59
><TD
60
WIDTH="80%"
61
ALIGN="center"
62
VALIGN="bottom"
63
></TD
64
><TD
65
WIDTH="10%"
66
ALIGN="right"
67
VALIGN="bottom"
68
><A
69
HREF="usbs-start-tx.html"
70
>Next</A
71
></TD
72
></TR
73
></TABLE
74
><HR
75
ALIGN="LEFT"
76
WIDTH="100%"></DIV
77
><H1
78
><A
79
NAME="USBS-START-RX"
80
>Receiving Data from the Host</A
81
></H1
82
><DIV
83
CLASS="REFNAMEDIV"
84
><A
85
NAME="AEN277"
86
></A
87
><H2
88
>Name</H2
89
><TT
90
CLASS="FUNCTION"
91
>usbs_start_rx_buffer</TT
92
>&nbsp;--&nbsp;Receiving Data from the Host</DIV
93
><DIV
94
CLASS="REFSYNOPSISDIV"
95
><A
96
NAME="AEN281"
97
></A
98
><H2
99
>Synopsis</H2
100
><DIV
101
CLASS="FUNCSYNOPSIS"
102
><A
103
NAME="AEN282"
104
></A
105
><P
106
></P
107
><TABLE
108
BORDER="0"
109
BGCOLOR="#E0E0E0"
110
WIDTH="100%"
111
><TR
112
><TD
113
><PRE
114
CLASS="FUNCSYNOPSISINFO"
115
>#include &lt;cyg/io/usb/usbs.h&gt;</PRE
116
></TD
117
></TR
118
></TABLE
119
><P
120
><CODE
121
><CODE
122
CLASS="FUNCDEF"
123
>void usbs_start_rx_buffer</CODE
124
>(usbs_rx_endpoint* ep, unsigned char* buffer, int length, void (*)(void*,int) complete_fn, void * complete_data);</CODE
125
></P
126
><P
127
><CODE
128
><CODE
129
CLASS="FUNCDEF"
130
>void usbs_start_rx</CODE
131
>(usbs_rx_endpoint* ep);</CODE
132
></P
133
><P
134
></P
135
></DIV
136
></DIV
137
><DIV
138
CLASS="REFSECT1"
139
><A
140
NAME="AEN302"
141
></A
142
><H2
143
><TT
144
CLASS="FUNCTION"
145
>Description</TT
146
></H2
147
><P
148
><TT
149
CLASS="FUNCTION"
150
>usbs_start_rx_buffer</TT
151
> is a USB-specific function
152
to accept a transfer from host to peripheral. It can be used for bulk,
153
interrupt or isochronous transfers, but not for control messages.
154
Instead those involve manipulating the <A
155
HREF="usbs-control.html"
156
><SPAN
157
CLASS="STRUCTNAME"
158
>usbs_control_endpoint</SPAN
159
></A
160
>
161
data structure directly. The function takes five arguments:</P
162
><P
163
></P
164
><OL
165
TYPE="1"
166
><LI
167
><P
168
>The first argument identifies the specific endpoint that should be
169
used. Different USB devices will support different sets of endpoints
170
and the device driver will provide appropriate data structures. The
171
device driver's documentation should be consulted for details of which
172
endpoints are available.</P
173
></LI
174
><LI
175
><P
176
>The <TT
177
CLASS="PARAMETER"
178
><I
179
>buffer</I
180
></TT
181
> and <TT
182
CLASS="PARAMETER"
183
><I
184
>length</I
185
></TT
186
>
187
arguments control the actual transfer. USB device drivers are not
188
expected to perform any buffering or to support partial transfers, so
189
the length specified should correspond to the maximum transfer that is
190
currently possible and the buffer should be at least this large. For
191
isochronous transfers the USB specification imposes an upper bound of
192
1023 bytes, and a smaller limit may be set in the <A
193
HREF="usbs-enum.html#AEN142"
194
>enumeration data</A
195
>. Interrupt
196
transfers are similarly straightforward with an upper bound of 64
197
bytes, or less as per the enumeration data. Bulk transfers are more
198
complicated because they can involve multiple 64-byte packets plus a
199
terminating packet of less than 64 bytes, so there is no predefined
200
limit on the transfer size. Instead it is left to higher-level
201
protocols to specify an appropriate upper bound.</P
202
><P
203
>One technique that may work for bulk transfers is to exploit the fact
204
that such transfers happen in 64-byte packets: it may be possible to
205
receive an initial 64 bytes, corresponding to the first packet in the
206
transfer; these 64 bytes can then be examined to determine the total
207
transfer size, and the remaining data can be transferred in another
208
receive operation. This technique is not guaranteed to work with all
209
USB hardware. Also, if the delay between accepting the first packet and
210
the remainder of the transfer is excessive then this could cause
211
timeout problems for the host-side software. For these reasons this
212
technique should be avoided.</P
213
></LI
214
><LI
215
><P
216
><TT
217
CLASS="FUNCTION"
218
>usbs_start_rx_buffer</TT
219
> is non-blocking. It merely
220
starts the receive operation, and does not wait for completion. At
221
some later point the USB device driver will invoke the completion
222
function parameter with two arguments: the completion data defined by
223
the last parameter and a result field. A result &gt;=
224
<TT
225
CLASS="LITERAL"
226
>0</TT
227
> indicates a successful transfer of that many
228
bytes, which may be less than the upper bound imposed by the
229
<TT
230
CLASS="PARAMETER"
231
><I
232
>length</I
233
></TT
234
> argument. A result &lt;
235
<TT
236
CLASS="LITERAL"
237
>0</TT
238
> indicates an error. The most likely errors are
239
<TT
240
CLASS="LITERAL"
241
>-EPIPE</TT
242
> to indicate that the connection between the
243
host and the target has been broken, and <TT
244
CLASS="LITERAL"
245
>-EAGAIN</TT
246
>
247
for when the endpoint has been <A
248
HREF="usbs-halt.html"
249
>halted</A
250
>. Specific USB device drivers may
251
specify additional error conditions.</P
252
></LI
253
></OL
254
><P
255
>The normal sequence of events is that the USB device driver will
256
update the appropriate hardware registers. At some point after that
257
the host will attempt to send data by transmitting an OUT token
258
followed by a data packet, and since a receive operation is now in
259
progress the data will be accepted and ACK'd. If there were no receive
260
operation then the peripheral would instead generate a NAK. The USB
261
hardware will generate an interrupt once the whole packet has been
262
received, and the USB device driver will service this interrupt and
263
arrange for a DSR to be called. Isochronous and interrupt transfers
264
involve just a single packet. However, bulk transfers may involve
265
multiple packets so the device driver has to check whether the packet
266
was a full 64 bytes or whether it was a terminating packet of less
267
than this. When the device driver DSR detects a complete transfer it
268
will inform higher-level code by invoking the supplied completion
269
function.</P
270
><P
271
>This means that the completion function will normally be invoked by a
272
DSR and not in thread context - although some USB device drivers may
273
have a different implementation. Therefore the completion function is
274
restricted in what it can do. In particular it must not make any
275
calls that will or may block such as locking a mutex or allocating
276
memory. The kernel documentation should be consulted for more details
277
of DSR's and interrupt handling generally.</P
278
><P
279
>It is possible that the completion function will be invoked before
280
<TT
281
CLASS="FUNCTION"
282
>usbs_start_rx_buffer</TT
283
> returns. Such an event would
284
be unusual because the transfer cannot happen until the next time the
285
host tries to send data to this peripheral, but it may happen if for
286
example another interrupt happens and a higher priority thread is
287
scheduled to run. Also, if the endpoint is currently halted then the
288
completion function will be invoked immediately with
289
<TT
290
CLASS="LITERAL"
291
>-EAGAIN</TT
292
>: typically this will happen in the current
293
thread rather than in a separate DSR. The completion function is
294
allowed to start another transfer immediately by calling
295
<TT
296
CLASS="FUNCTION"
297
>usbs_start_rx_buffer</TT
298
> again.</P
299
><P
300
>USB device drivers are not expected to perform any locking. It is the
301
responsibility of higher-level code to ensure that there is only one
302
receive operation for a given endpoint in progress at any one time. If
303
there are concurrent calls to
304
<TT
305
CLASS="FUNCTION"
306
>usbs_start_rx_buffer</TT
307
> then the resulting behaviour
308
is undefined. For typical USB applications this does not present any
309
problems, because only one piece of code will access a given endpoint
310
at any particular time.</P
311
><P
312
>The following code fragment illustrates a very simple use of
313
<TT
314
CLASS="FUNCTION"
315
>usbs_start_rx_buffer</TT
316
> to implement a blocking
317
receive, using a semaphore to synchronise between the foreground
318
thread and the DSR. For a simple example like this no completion data
319
is needed.</P
320
><TABLE
321
BORDER="0"
322
BGCOLOR="#E0E0E0"
323
WIDTH="100%"
324
><TR
325
><TD
326
><PRE
327
CLASS="PROGRAMLISTING"
328
>static int error_code = 0;
329
static cyg_sem_t completion_wait;
330
 
331
static void
332
completion_fn(void* data, int result)
333
{
334
    error_code = result;
335
    cyg_semaphore_post(&amp;completion_wait);
336
}
337
 
338
int
339
blocking_receive(usbs_rx_endpoint* ep, unsigned char* buf, int len)
340
{
341
    error_code = 0;
342
    usbs_start_rx_buffer(ep, buf, len, &amp;completion_fn, NULL);
343
    cyg_semaphore_wait(&amp;completion_wait);
344
    return error_code;
345
}</PRE
346
></TD
347
></TR
348
></TABLE
349
><P
350
>There is also a utility function <TT
351
CLASS="FUNCTION"
352
>usbs_start_rx</TT
353
>. This
354
can be used by code that wants to manipulate <A
355
HREF="usbs-data.html"
356
>data endpoints</A
357
> directly, specifically the
358
<TT
359
CLASS="STRUCTFIELD"
360
><I
361
>complete_fn</I
362
></TT
363
>,
364
<TT
365
CLASS="STRUCTFIELD"
366
><I
367
>complete_data</I
368
></TT
369
>,
370
<TT
371
CLASS="STRUCTFIELD"
372
><I
373
>buffer</I
374
></TT
375
> and
376
<TT
377
CLASS="STRUCTFIELD"
378
><I
379
>buffer_size</I
380
></TT
381
> fields.
382
<TT
383
CLASS="FUNCTION"
384
>usbs_start_tx</TT
385
> just invokes a function
386
supplied by the device driver.</P
387
></DIV
388
><DIV
389
CLASS="NAVFOOTER"
390
><HR
391
ALIGN="LEFT"
392
WIDTH="100%"><TABLE
393
WIDTH="100%"
394
BORDER="0"
395
CELLPADDING="0"
396
CELLSPACING="0"
397
><TR
398
><TD
399
WIDTH="33%"
400
ALIGN="left"
401
VALIGN="top"
402
><A
403
HREF="usbs-devtab.html"
404
>Prev</A
405
></TD
406
><TD
407
WIDTH="34%"
408
ALIGN="center"
409
VALIGN="top"
410
><A
411
HREF="io-usb-slave.html"
412
>Home</A
413
></TD
414
><TD
415
WIDTH="33%"
416
ALIGN="right"
417
VALIGN="top"
418
><A
419
HREF="usbs-start-tx.html"
420
>Next</A
421
></TD
422
></TR
423
><TR
424
><TD
425
WIDTH="33%"
426
ALIGN="left"
427
VALIGN="top"
428
>Devtab Entries</TD
429
><TD
430
WIDTH="34%"
431
ALIGN="center"
432
VALIGN="top"
433
>&nbsp;</TD
434
><TD
435
WIDTH="33%"
436
ALIGN="right"
437
VALIGN="top"
438
>Sending Data to the Host</TD
439
></TR
440
></TABLE
441
></DIV
442
></BODY
443
></HTML
444
>

powered by: WebSVN 2.1.0

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