Moved all source to the trunk directory.
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Pipe.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Functions, macros and enums related to pipe management when in USB Host mode. This
34 * module contains the pipe management macros, as well as pipe interrupt and data
35 * send/recieve functions for various datatypes.
36 */
37
38 #ifndef __PIPE_H__
39 #define __PIPE_H__
40
41 /* Includes: */
42 #include <avr/io.h>
43 #include <stdbool.h>
44
45 #include "../../../Common/Common.h"
46 #include "../HighLevel/USBTask.h"
47
48 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
49 #include "StreamCallbacks.h"
50 #endif
51 /* Enable C linkage for C++ Compilers: */
52 #if defined(__cplusplus)
53 extern "C" {
54 #endif
55
56 /* Public Interface - May be used in end-application: */
57 /* Macros: */
58 /** Mask for Pipe_GetErrorFlags(), indicating that a CRC error occurred in the pipe on the received data. */
59 #define PIPE_ERRORFLAG_CRC16 (1 << 4)
60
61 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware timeout error occurred in the pipe. */
62 #define PIPE_ERRORFLAG_TIMEOUT (1 << 3)
63
64 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware PID error occurred in the pipe. */
65 #define PIPE_ERRORFLAG_PID (1 << 2)
66
67 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware data PID error occurred in the pipe. */
68 #define PIPE_ERRORFLAG_DATAPID (1 << 1)
69
70 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware data toggle error occurred in the pipe. */
71 #define PIPE_ERRORFLAG_DATATGL (1 << 0)
72
73 /** Token mask for Pipe_ConfigurePipe(). This sets the pipe as a SETUP token (for CONTROL type pipes),
74 * which will trigger a control request on the attached device when data is written to the pipe.
75 */
76 #define PIPE_TOKEN_SETUP (0b00 << PTOKEN0)
77
78 /** Token mask for Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
79 * indicating that the pipe data will flow from device to host.
80 */
81 #define PIPE_TOKEN_IN (0b01 << PTOKEN0)
82
83 /** Token mask for Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
84 * indicating that the pipe data will flow from host to device.
85 */
86 #define PIPE_TOKEN_OUT (0b10 << PTOKEN0)
87
88 /** Mask for the bank mode selection for the Pipe_ConfigurePipe() macro. This indicates that the pipe
89 * should have one single bank, which requires less USB FIFO memory but results in slower transfers as
90 * only one USB device (the AVR or the attached device) can access the pipe's bank at the one time.
91 */
92 #define PIPE_BANK_SINGLE 0
93
94 /** Mask for the bank mode selection for the Pipe_ConfigurePipe() macro. This indicates that the pipe
95 * should have two banks, which requires more USB FIFO memory but results in faster transfers as one
96 * USB device (the AVR or the attached device) can access one bank while the other accesses the second
97 * bank.
98 */
99 #define PIPE_BANK_DOUBLE (1 << EPBK0)
100
101 /** Pipe address for the default control pipe, which always resides in address 0. This is
102 * defined for convenience to give more readable code when used with the pipe macros.
103 */
104 #define PIPE_CONTROLPIPE 0
105
106 /** Default size of the default control pipe's bank, until altered by the Endpoint0Size value
107 * in the device descriptor of the attached device.
108 */
109 #define PIPE_CONTROLPIPE_DEFAULT_SIZE 8
110
111 /** Pipe number mask, for masking against pipe addresses to retrieve the pipe's numerical address
112 * in the device.
113 */
114 #define PIPE_PIPENUM_MASK 0x07
115
116 /** Total number of pipes (including the default control pipe at address 0) which may be used in
117 * the device. Different USB AVR models support different amounts of pipes, this value reflects
118 * the maximum number of pipes for the currently selected AVR model.
119 */
120 #define PIPE_TOTAL_PIPES 7
121
122 /** Size in bytes of the largest pipe bank size possible in the device. Not all banks on each AVR
123 * model supports the largest bank size possible on the device; different pipe numbers support
124 * different maximum bank sizes. This value reflects the largest possible bank of any pipe on the
125 * currently selected USB AVR model.
126 */
127 #define PIPE_MAX_SIZE 256
128
129 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
130 * numerical address in the attached device.
131 */
132 #define PIPE_EPNUM_MASK 0x07
133
134 /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
135 * bank size in the attached device.
136 */
137 #define PIPE_EPSIZE_MASK 0x7FF
138
139 /** Interrupt definition for the pipe IN interrupt (for INTERRUPT type pipes). Should be used with
140 * the USB_INT_* macros located in USBInterrupt.h.
141 *
142 * This interrupt will fire if enabled on an INTERRUPT type pipe if the pipe interrupt period has
143 * elapsed and the pipe is ready for the next packet from the attached device to be read out from its
144 * FIFO buffer (if received).
145 *
146 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
147 * will fire the common pipe interrupt vector.
148 *
149 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
150 */
151 #define PIPE_INT_IN UPIENX, (1 << RXINE) , UPINTX, (1 << RXINI)
152
153 /** Interrupt definition for the pipe OUT interrupt (for INTERRUPT type pipes). Should be used with
154 * the USB_INT_* macros located in USBInterrupt.h.
155 *
156 * This interrupt will fire if enabled on an INTERRUPT type endpoint if a the pipe interrupt period
157 * has elapsed and the pipe is ready for a packet to be written to the pipe's FIFO buffer and sent
158 * to the attached device (if required).
159 *
160 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
161 * will fire the common pipe interrupt vector.
162 *
163 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector. */
164 #define PIPE_INT_OUT UPIENX, (1 << TXOUTE), UPINTX, (1 << TXOUTI)
165
166 /** Interrupt definition for the pipe SETUP bank ready interrupt (for CONTROL type pipes). Should be
167 * used with the USB_INT_* macros located in USBInterrupt.h.
168 *
169 * This interrupt will fire if enabled on an CONTROL type pipe when the pipe is ready for a new
170 * control request.
171 *
172 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
173 * will fire the common pipe interrupt vector.
174 *
175 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
176 */
177 #define PIPE_INT_SETUP UPIENX, (1 << TXSTPE) , UPINTX, (1 << TXSTPI)
178
179 /** Interrupt definition for the pipe error interrupt. Should be used with the USB_INT_* macros
180 * located in USBInterrupt.h.
181 *
182 * This interrupt will fire if enabled on a particular pipe if an error occurs on that pipe, such
183 * as a CRC mismatch error.
184 *
185 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
186 * will fire the common pipe interrupt vector.
187 *
188 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
189 *
190 * \see Pipe_GetErrorFlags() for more information on the pipe errors.
191 */
192 #define PIPE_INT_ERROR UPIENX, (1 << PERRE), UPINTX, (1 << PERRI)
193
194 /** Interrupt definition for the pipe NAK received interrupt. Should be used with the USB_INT_* macros
195 * located in USBInterrupt.h.
196 *
197 * This interrupt will fire if enabled on a particular pipe if an attached device returns a NAK in
198 * response to a sent packet.
199 *
200 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
201 * will fire the common pipe interrupt vector.
202 *
203 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
204 *
205 * \see Pipe_IsNAKReceived() for more information on pipe NAKs.
206 */
207 #define PIPE_INT_NAK UPIENX, (1 << NAKEDE), UPINTX, (1 << NAKEDI)
208
209 /** Interrupt definition for the pipe STALL received interrupt. Should be used with the USB_INT_* macros
210 * located in USBInterrupt.h.
211 *
212 * This interrupt will fire if enabled on a particular pipe if an attached device returns a STALL on the
213 * currently selected pipe. This will also fire if the pipe is an isochronous pipe and a CRC error occurs.
214 *
215 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
216 * will fire the common pipe interrupt vector.
217 *
218 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
219 */
220 #define PIPE_INT_STALL UPIENX, (1 << RXSTALLE), UPINTX, (1 << RXSTALLI)
221
222 /** Indicates the number of bytes currently stored in the current pipe's selected bank. */
223 #define Pipe_BytesInPipe() UPBCX
224
225 /** Resets the desired pipe, including the pipe banks and flags. */
226 #define Pipe_ResetPipe(pipenum) MACROS{ UPRST = (1 << pipenum); UPRST = 0; }MACROE
227
228 /** Selects the given pipe number. Any pipe operations which do not require the pipe number to be
229 * indicated will operate on the currently selected pipe.
230 */
231 #define Pipe_SelectPipe(pipenum) MACROS{ UPNUM = pipenum; }MACROE
232
233 /** Returns the pipe address of the currently selected pipe. This is typically used to save the
234 * currently selected pipe number so that it can be restored after another pipe has been manipulated.
235 */
236 #define Pipe_GetCurrentPipe() (UPNUM & PIPE_PIPENUM_MASK)
237
238 /** Enables the currently selected pipe so that data can be sent and received through it to and from
239 * an attached device.
240 *
241 * \note Pipes must first be configured properly rather than just being enabled via the
242 * Pipe_ConfigurePipe() macro, which calls Pipe_EnablePipe() automatically.
243 */
244 #define Pipe_EnablePipe() MACROS{ UPCONX |= (1 << PEN); }MACROE
245
246 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
247 * from an attached device.
248 */
249 #define Pipe_DisablePipe() MACROS{ UPCONX &= ~(1 << PEN); }MACROE
250
251 /** Returns true if the currently selected pipe is enabled, false otherwise. */
252 #define Pipe_IsEnabled() ((UPCONX & (1 << PEN)) ? true : false)
253
254 /** Sets the token for the currently selected endpoint to one of the tokens specified by the PIPE_TOKEN_*
255 * masks. This should only be used on CONTROL type endpoints, to allow for bidirectional transfer of
256 * data during control requests.
257 */
258 #define Pipe_SetToken(token) MACROS{ UPCFG0X = ((UPCFG0X & ~PIPE_TOKEN_MASK) | token); }MACROE
259
260 /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */
261 #define Pipe_SetInfiniteINRequests() MACROS{ UPCONX |= (1 << INMODE); }MACROE
262
263 /** Configures the currently selected pipe to only allow the specified number of IN requests to be
264 * accepted by the pipe before it is automatically frozen.
265 */
266 #define Pipe_SetFiniteINRequests(n) MACROS{ UPCONX &= ~(1 << INMODE); UPINRQX = n; }MACROE
267
268 /** Returns true if the currently selected pipe is configured, false otherwise. */
269 #define Pipe_IsConfigured() ((UPSTAX & (1 << CFGOK)) ? true : false)
270
271 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds. */
272 #define Pipe_SetInterruptPeriod(ms) MACROS{ UPCFG2X = ms; }MACROE
273
274 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
275 * be serviced.
276 */
277 #define Pipe_GetPipeInterrupts() UPINT
278
279 /** Clears the interrupt flag for the specified pipe number. */
280 #define Pipe_ClearPipeInterrupt(n) MACROS{ UPINT &= ~(1 << n); }MACROE
281
282 /** Returns true if the specified pipe's interrupt period has elapsed, false otherwise. */
283 #define Pipe_HasPipeInterrupted(n) ((UPINT & (1 << n)) ? true : false)
284
285 /** Clears the pipe bank, and switches to the alternate bank if the currently selected pipe is
286 * dual-banked. When cleared, this either frees the bank up for the next packet from the host
287 * (if the endpoint is of the OUT direction) or sends the packet contents to the host (if the
288 * pipe is of the IN direction).
289 */
290 #define Pipe_ClearCurrentBank() MACROS{ UPINTX &= ~(1 << FIFOCON); }MACROE
291
292 /** Unfreezes the pipe, allowing it to communicate with an attached device. */
293 #define Pipe_Unfreeze() MACROS{ UPCONX &= ~(1 << PFREEZE); }MACROE
294
295 /** Freezes the pipe, preventing it from communicating with an attached device. */
296 #define Pipe_Freeze() MACROS{ UPCONX |= (1 << PFREEZE); }MACROE
297
298 /** Clears the master pipe error flag. */
299 #define Pipe_ClearError() MACROS{ UPINTX &= ~(1 << PERRI); }MACROE
300
301 /** Returns true if the master pipe error flag is set for the currently selected pipe, indicating that
302 * some sort of hardware error has occurred on the pipe.
303 *
304 * \see Pipe_GetErrorFlags() macro for information on retreiving the exact error flag.
305 */
306 #define Pipe_IsError() ((UPINTX & (1 << PERRI)) ? true : false)
307
308 /** Clears all the currently selected pipe's hardware error flags, but does not clear the master error
309 * flag for the pipe. */
310 #define Pipe_ClearErrorFlags() MACROS{ UPERRX = 0; }MACROE
311
312 /** Returns a mask of the hardware error flags which have occured on the currently selected pipe. This
313 * value can then be masked against the PIPE_ERRORFLAG_* masks to determine what error has occurred.
314 */
315 #define Pipe_GetErrorFlags() UPERRX
316
317 /** Returns true if the currently selected pipe may be read from (if data is waiting in the pipe
318 * bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT
319 * direction). This function will return false if an error has occured in the pipe, or if the pipe
320 * is an IN direction and no packet has been received, or if the pipe is an OUT direction and the
321 * pipe bank is full.
322 */
323 #define Pipe_ReadWriteAllowed() ((UPINTX & (1 << RWAL)) ? true : false)
324
325 /** Clears the flag indicating that a SETUP request has been sent to the attached device from the
326 * currently selected CONTROL type pipe.
327 */
328 #define Pipe_ClearSetupSent() MACROS{ UPINTX &= ~(1 << TXSTPI); }MACROE
329
330 /** Returns true if no SETUP request is currently being sent to the attached device, false otherwise. */
331 #define Pipe_IsSetupSent() ((UPINTX & (1 << TXSTPI)) ? true : false)
332
333 /** Returns true if the currently selected pipe has been stalled by the attached device, false otherwise. */
334 #define Pipe_IsStalled() ((UPINTX & (1 << RXSTALLI)) ? true : false)
335
336 /** Clears the stall condition on the currently selected pipe. */
337 #define Pipe_ClearStall() MACROS{ UPINTX &= ~(1 << RXSTALLI); }MACROE
338
339 /** Returns true if an IN request has been received on the currently selected CONTROL type pipe, false
340 * otherwise.
341 */
342 #define Pipe_IsSetupINReceived() ((UPINTX & (1 << RXINI)) ? true : false)
343
344 /** Returns true if the currently selected CONTROL type pipe is ready to send an OUT request, false
345 * otherwise.
346 */
347 #define Pipe_IsSetupOUTReady() ((UPINTX & (1 << TXOUTI)) ? true : false)
348
349 /** Acknowedges the reception of a setup IN request from the attached device on the currently selected
350 * CONTROL type endpoint, allowing for the transmission of a setup OUT packet, or the reception of
351 * another setup IN packet.
352 */
353 #define Pipe_ClearSetupIN() MACROS{ UPINTX &= ~(1 << RXINI); UPINTX &= ~(1 << FIFOCON); }MACROE
354
355 /** Sends the currently selected CONTROL type pipe's contents to the device as a setup OUT packet. */
356 #define Pipe_ClearSetupOUT() MACROS{ UPINTX &= ~(1 << TXOUTI); UPINTX &= ~(1 << FIFOCON); }MACROE
357
358 /** Returns true if the device sent a NAK (Negative Acknowedge) in response to the last sent packet on
359 * the currently selected pipe. This ocurrs when the host sends a packet to the device, but the device
360 * is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been
361 * received, it must be cleard using Pipe_ClearNAKReceived() before the previous (or any other) packet
362 * can be re-sent.
363 */
364 #define Pipe_IsNAKReceived() ((UPINTX & (1 << NAKEDI)) ? true : false)
365
366 /** Clears the NAK condition on the currently selected pipe.
367 *
368 * \see Pipe_IsNAKReceived() for more details.
369 */
370 #define Pipe_ClearNAKReceived() MACROS{ UPINTX &= ~(1 << NAKEDI); }MACROE
371
372 /* Enums: */
373 /** Enum for the possible error return codes of the Pipe_WaitUntilReady function */
374 enum Pipe_WaitUntilReady_ErrorCodes_t
375 {
376 PIPE_READYWAIT_NoError = 0, /**< Pipe ready for next packet, no error */
377 PIPE_READYWAIT_PipeStalled = 1, /**< The device stalled the pipe while waiting. */
378 PIPE_READYWAIT_DeviceDisconnected = 2, /**< Device was disconnected from the host while waiting. */
379 PIPE_READYWAIT_Timeout = 3, /**< The device failed to accept or send the next packet
380 * within the software timeout period set by the
381 * USB_STREAM_TIMEOUT_MS macro.
382 */
383 };
384
385 /** Enum for the possible error return codes of the Pipe_*_Stream_* functions. */
386 enum Pipe_Stream_RW_ErrorCodes_t
387 {
388 PIPE_RWSTREAM_ERROR_NoError = 0, /**< Command completed successfully, no error. */
389 PIPE_RWSTREAM_ERROR_PipeStalled = 1, /**< The device stalled the pipe during the transfer. */
390 PIPE_RWSTREAM_ERROR_DeviceDisconnected = 2, /**< Device was disconnected from the host during
391 * the transfer.
392 */
393 PIPE_RWSTREAM_ERROR_Timeout = 3, /**< The device failed to accept or send the next packet
394 * within the software timeout period set by the
395 * USB_STREAM_TIMEOUT_MS macro.
396 */
397 PIPE_RWSTREAM_ERROR_CallbackAborted = 4, /**< Indicates that the stream's callback function aborted
398 * the transfer early.
399 */
400 };
401
402 /* Inline Functions: */
403 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes. */
404 static inline uint8_t Pipe_Read_Byte(void) ATTR_WARN_UNUSED_RESULT;
405 static inline uint8_t Pipe_Read_Byte(void)
406 {
407 return UPDATX;
408 }
409
410 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes. */
411 static inline void Pipe_Write_Byte(const uint8_t Byte)
412 {
413 UPDATX = Byte;
414 }
415
416 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes. */
417 static inline void Pipe_Discard_Byte(void)
418 {
419 uint8_t Dummy;
420
421 Dummy = UPDATX;
422 }
423
424 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
425 * direction pipes.
426 */
427 static inline uint16_t Pipe_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT;
428 static inline uint16_t Pipe_Read_Word_LE(void)
429 {
430 uint16_t Data;
431
432 Data = UPDATX;
433 Data |= (((uint16_t)UPDATX) << 8);
434
435 return Data;
436 }
437
438 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
439 * direction pipes.
440 */
441 static inline uint16_t Pipe_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT;
442 static inline uint16_t Pipe_Read_Word_BE(void)
443 {
444 uint16_t Data;
445
446 Data = (((uint16_t)UPDATX) << 8);
447 Data |= UPDATX;
448
449 return Data;
450 }
451
452 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
453 * direction pipes.
454 */
455 static inline void Pipe_Write_Word_LE(const uint16_t Word)
456 {
457 UPDATX = (Word & 0xFF);
458 UPDATX = (Word >> 8);
459 }
460
461 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
462 * direction pipes.
463 */
464 static inline void Pipe_Write_Word_BE(const uint16_t Word)
465 {
466 UPDATX = (Word >> 8);
467 UPDATX = (Word & 0xFF);
468 }
469
470 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes. */
471 static inline void Pipe_Ignore_Word(void)
472 {
473 uint8_t Dummy;
474
475 Dummy = UPDATX;
476 Dummy = UPDATX;
477 }
478
479 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
480 * direction pipes.
481 */
482 static inline uint32_t Pipe_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT;
483 static inline uint32_t Pipe_Read_DWord_LE(void)
484 {
485 union
486 {
487 uint32_t DWord;
488 uint8_t Bytes[4];
489 } Data;
490
491 Data.Bytes[0] = UPDATX;
492 Data.Bytes[1] = UPDATX;
493 Data.Bytes[2] = UPDATX;
494 Data.Bytes[3] = UPDATX;
495
496 return Data.DWord;
497 }
498
499 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
500 * direction pipes.
501 */
502 static inline uint32_t Pipe_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT;
503 static inline uint32_t Pipe_Read_DWord_BE(void)
504 {
505 union
506 {
507 uint32_t DWord;
508 uint8_t Bytes[4];
509 } Data;
510
511 Data.Bytes[3] = UPDATX;
512 Data.Bytes[2] = UPDATX;
513 Data.Bytes[1] = UPDATX;
514 Data.Bytes[0] = UPDATX;
515
516 return Data.DWord;
517 }
518
519 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
520 * direction pipes.
521 */
522 static inline void Pipe_Write_DWord_LE(const uint32_t DWord)
523 {
524 Pipe_Write_Word_LE(DWord);
525 Pipe_Write_Word_LE(DWord >> 16);
526 }
527
528 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
529 * direction pipes.
530 */
531 static inline void Pipe_Write_DWord_BE(const uint32_t DWord)
532 {
533 Pipe_Write_Word_BE(DWord >> 16);
534 Pipe_Write_Word_BE(DWord);
535 }
536
537 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes. */
538 static inline void Pipe_Ignore_DWord(void)
539 {
540 uint8_t Dummy;
541
542 Dummy = UPDATX;
543 Dummy = UPDATX;
544 Dummy = UPDATX;
545 Dummy = UPDATX;
546 }
547
548 /* External Variables: */
549 /** Global indicating the maximum packet size of the default control pipe located at address
550 * 0 in the device. This value is set to the value indicated in the attached device's device
551 * descriptor once the USB interface is initialized into host mode and a device is attached
552 * to the USB bus.
553 *
554 * \note This variable should be treated as read-only in the user application, and never manually
555 * changed in value.
556 */
557 extern uint8_t USB_ControlPipeSize;
558
559 /* Function Prototypes: */
560 /** Configures the specified pipe number with the given pipe type, token, target endpoint number in the
561 * attached device, bank size and banking mode. Pipes should be allocated in ascending order by their
562 * address in the device (i.e. pipe 1 should be configured before pipe 2 and so on).
563 *
564 * The pipe type may be one of the EP_TYPE_* macros listed in LowLevel.h, the token may be one of the
565 * PIPE_TOKEN_* masks.
566 *
567 * The bank size must indicate the maximum packet size that the pipe can handle. Different pipe
568 * numbers can handle different maximum packet sizes - refer to the chosen USB AVR's datasheet to
569 * determine the maximum bank size for each pipe.
570 *
571 * The banking mode may be either PIPE_BANK_SINGLE or PIPE_BANK_DOUBLE.
572 *
573 * A newly configured pipe is frozen by default, and must be unfrozen before use via the Pipe_Unfreeze() macro.
574 *
575 * \note This routine will select the specified pipe, and the pipe will remain selected once the
576 * routine completes regardless of if the pipe configuration succeeds.
577 *
578 * \return Boolean true if the configuration is successful, false otherwise
579 */
580 bool Pipe_ConfigurePipe(const uint8_t Number, const uint8_t Type, const uint8_t Token, const uint8_t EndpointNumber,
581 const uint16_t Size, const uint8_t Banks);
582
583 /** Spinloops until the currently selected non-control pipe is ready for the next packed of data
584 * to be read or written to it.
585 *
586 * \note This routine should not be called on CONTROL type pipes.
587 *
588 * \return A value from the Pipe_WaitUntilReady_ErrorCodes_t enum.
589 */
590 uint8_t Pipe_WaitUntilReady(void);
591
592 /** Writes the given number of bytes to the pipe from the given buffer in little endian,
593 * sending full packets to the device as needed. The last packet filled is not automatically sent;
594 * the user is responsible for manually sending the last written packet to the host via the
595 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
596 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
597 *
598 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
599 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
600 * and this function has the Callback parameter ommitted.
601 *
602 * \param Buffer Pointer to the source data buffer to read from.
603 * \param Length Number of bytes to read for the currently selected pipe into the buffer.
604 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
605 *
606 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
607 */
608 uint8_t Pipe_Write_Stream_LE(const void* Buffer, uint16_t Length
609 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
610 , uint8_t (* const Callback)(void)
611 #endif
612 ) ATTR_NON_NULL_PTR_ARG(1);
613
614 /** Writes the given number of bytes to the pipe from the given buffer in big endian,
615 * sending full packets to the device as needed. The last packet filled is not automatically sent;
616 * the user is responsible for manually sending the last written packet to the host via the
617 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
618 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
619 *
620 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
621 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
622 * and this function has the Callback parameter ommitted.
623 *
624 * \param Buffer Pointer to the source data buffer to read from.
625 * \param Length Number of bytes to read for the currently selected pipe into the buffer.
626 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
627 *
628 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
629 */
630 uint8_t Pipe_Write_Stream_BE(const void* Buffer, uint16_t Length
631 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
632 , uint8_t (* const Callback)(void)
633 #endif
634 ) ATTR_NON_NULL_PTR_ARG(1);
635
636 /** Reads and discards the given number of bytes from the pipe, discarding fully read packets from the host
637 * as needed. The last packet is not automatically discarded once the remaining bytes has been read; the
638 * user is responsible for manually discarding the last packet from the host via the Pipe_ClearCurrentBank() macro.
639 * Between each USB packet, the given stream callback function is executed repeatedly until the next packet is ready,
640 * allowing for early aborts of stream transfers.
641 *
642 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
643 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
644 * and this function has the Callback parameter ommitted.
645 *
646 * \param Length Number of bytes to send via the currently selected pipe.
647 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
648 *
649 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
650 */
651 uint8_t Pipe_Discard_Stream(uint16_t Length
652 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
653 , uint8_t (* const Callback)(void)
654 #endif
655 );
656
657 /** Reads the given number of bytes from the pipe into the given buffer in little endian,
658 * sending full packets to the device as needed. The last packet filled is not automatically sent;
659 * the user is responsible for manually sending the last written packet to the host via the
660 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
661 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
662 *
663 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
664 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
665 * and this function has the Callback parameter ommitted.
666 *
667 * \param Buffer Pointer to the source data buffer to write to.
668 * \param Length Number of bytes to read for the currently selected pipe to read from.
669 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
670 *
671 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
672 */
673 uint8_t Pipe_Read_Stream_LE(void* Buffer, uint16_t Length
674 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
675 , uint8_t (* const Callback)(void)
676 #endif
677 ) ATTR_NON_NULL_PTR_ARG(1);
678
679 /** Reads the given number of bytes from the pipe into the given buffer in big endian,
680 * sending full packets to the device as needed. The last packet filled is not automatically sent;
681 * the user is responsible for manually sending the last written packet to the host via the
682 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
683 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
684 *
685 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
686 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
687 * and this function has the Callback parameter ommitted.
688 *
689 * \param Buffer Pointer to the source data buffer to write to.
690 * \param Length Number of bytes to read for the currently selected pipe to read from.
691 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
692 *
693 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
694 */
695 uint8_t Pipe_Read_Stream_BE(void* Buffer, uint16_t Length
696 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
697 , uint8_t (* const Callback)(void)
698 #endif
699 ) ATTR_NON_NULL_PTR_ARG(1);
700
701 /* Function Aliases: */
702 /** Alias for Pipe_Discard_Byte().
703 */
704 #define Pipe_Ignore_Byte() Pipe_Discard_Byte()
705
706 /** Alias for Pipe_Discard_Word().
707 */
708 #define Pipe_Ignore_Word() Pipe_Discard_Word()
709
710 /** Alias for Pipe_Discard_DWord().
711 */
712 #define Pipe_Ignore_DWord() Pipe_Discard_DWord()
713
714 /** Alias for Pipe_Read_Word_LE(). By default USB transfers use little endian format, thus
715 * the command with no endianness specifier indicates little endian mode.
716 */
717 #define Pipe_Read_Word() Pipe_Read_Word_LE()
718
719 /** Alias for Pipe_Write_Word_LE(). By default USB transfers use little endian format, thus
720 * the command with no endianness specifier indicates little endian mode.
721 */
722 #define Pipe_Write_Word(Word) Pipe_Write_Word_LE(Word)
723
724 /** Alias for Pipe_Read_DWord_LE(). By default USB transfers use little endian format, thus
725 * the command with no endianness specifier indicates little endian mode.
726 */
727 #define Pipe_Read_DWord() Pipe_Read_DWord_LE()
728
729 /** Alias for Pipe_Write_DWord_LE(). By default USB transfers use little endian format, thus
730 * the command with no endianness specifier indicates little endian mode.
731 */
732 #define Pipe_Write_DWord(DWord) Pipe_Write_DWord_LE(DWord)
733
734 /** Alias for Pipe_Read_Stream_LE(). By default USB transfers use little endian format, thus
735 * the command with no endianness specifier indicates little endian mode.
736 */
737 #if !defined(NO_STREAM_CALLBACKS)
738 #define Pipe_Read_Stream(Buffer, Length, Callback) Pipe_Read_Stream_LE(Buffer, Length, Callback)
739 #else
740 #define Pipe_Read_Stream(Buffer, Length) Pipe_Read_Stream_LE(Buffer, Length)
741 #endif
742
743 /** Alias for Pipe_Write_Stream_LE(). By default USB transfers use little endian format, thus
744 * the command with no endianness specifier indicates little endian mode.
745 */
746 #if !defined(NO_STREAM_CALLBACKS)
747 #define Pipe_Write_Stream(Buffer, Length, Callback) Pipe_Read_Stream_LE(Buffer, Length, Callback)
748 #else
749 #define Pipe_Write_Stream(Buffer, Length) Pipe_Read_Stream_LE(Buffer, Length)
750 #endif
751
752 /* Private Interface - For use in library only: */
753 #if !defined(__DOXYGEN__)
754 /* Macros: */
755 #define PIPE_TOKEN_MASK (0x03 << PTOKEN0)
756
757 #define Pipe_AllocateMemory() MACROS{ UPCFG1X |= (1 << ALLOC); }MACROE
758 #define Pipe_DeallocateMemory() MACROS{ UPCFG1X &= ~(1 << ALLOC); }MACROE
759
760 /* Function Prototypes: */
761 void Pipe_ClearPipes(void);
762
763 /* Inline Functions: */
764 static inline uint8_t Pipe_BytesToEPSizeMask(uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYSINLINE;
765 static inline uint8_t Pipe_BytesToEPSizeMask(uint16_t Bytes)
766 {
767 if (Bytes <= 8)
768 return (0 << EPSIZE0);
769 else if (Bytes <= 16)
770 return (1 << EPSIZE0);
771 else if (Bytes <= 32)
772 return (2 << EPSIZE0);
773 else if (Bytes <= 64)
774 return (3 << EPSIZE0);
775 else if (Bytes <= (8 << 4))
776 return (4 << EPSIZE0);
777 else
778 return (5 << EPSIZE0);
779 };
780
781 #endif
782
783 /* Disable C linkage for C++ Compilers: */
784 #if defined(__cplusplus)
785 }
786 #endif
787
788 #endif