3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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.
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
31 /** \ingroup Group_USB
32 * @defgroup Group_PipeManagement Pipe Management
34 * This module contains functions, macros and enums related to pipe management when in USB Host mode. This
35 * module contains the pipe management macros, as well as pipe interrupt and data send/receive functions
36 * for various data types.
41 /** @defgroup Group_PipeRW Pipe Data Reading and Writing
43 * Functions, macros, variables, enums and types related to data reading and writing from and to pipes.
46 /** \ingroup Group_PipeRW
47 * @defgroup Group_PipePrimitiveRW Read/Write of Primitive Data Types
49 * Functions, macros, variables, enums and types related to data reading and writing of primitive data types
53 /** \ingroup Group_PipeRW
54 * @defgroup Group_PipeStreamRW Read/Write of Multi-Byte Streams
56 * Functions, macros, variables, enums and types related to data reading and writing of data streams from
60 /** @defgroup Group_PipePacketManagement Pipe Packet Management
62 * Functions, macros, variables, enums and types related to packet management of pipes.
65 /** @defgroup Group_PipeControlReq Pipe Control Request Management
67 * Module for host mode request processing. This module allows for the transmission of standard, class and
68 * vendor control requests to the default control endpoint of an attached device while in host mode.
70 * \see Chapter 9 of the USB 2.0 specification.
77 #if defined(__AVR32__)
81 #elif defined(__AVR__)
83 #include <avr/pgmspace.h>
84 #include <avr/eeprom.h>
88 #include "../../../Common/Common.h"
90 #include "../HighLevel/USBTask.h"
92 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
93 #include "../HighLevel/StreamCallbacks.h"
96 /* Enable C linkage for C++ Compilers: */
97 #if defined(__cplusplus)
101 /* Preprocessor Checks: */
102 #if !defined(__INCLUDE_FROM_USB_DRIVER)
103 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
106 /* Public Interface - May be used in end-application: */
108 /** Mask for \ref Pipe_GetErrorFlags(), indicating that an overflow error occurred in the pipe on the received data. */
109 #define PIPE_ERRORFLAG_OVERFLOW (1 << 6)
111 /** Mask for \ref Pipe_GetErrorFlags(), indicating that an underflow error occurred in the pipe on the received data. */
112 #define PIPE_ERRORFLAG_UNDERFLOW (1 << 5)
114 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a CRC error occurred in the pipe on the received data. */
115 #define PIPE_ERRORFLAG_CRC16 (1 << 4)
117 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware timeout error occurred in the pipe. */
118 #define PIPE_ERRORFLAG_TIMEOUT (1 << 3)
120 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware PID error occurred in the pipe. */
121 #define PIPE_ERRORFLAG_PID (1 << 2)
123 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data PID error occurred in the pipe. */
124 #define PIPE_ERRORFLAG_DATAPID (1 << 1)
126 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data toggle error occurred in the pipe. */
127 #define PIPE_ERRORFLAG_DATATGL (1 << 0)
129 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a SETUP token (for CONTROL type pipes),
130 * which will trigger a control request on the attached device when data is written to the pipe.
132 #define PIPE_TOKEN_SETUP (0 << PTOKEN0)
134 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
135 * indicating that the pipe data will flow from device to host.
137 #define PIPE_TOKEN_IN (1 << PTOKEN0)
139 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a OUT token (for non-CONTROL type pipes),
140 * indicating that the pipe data will flow from host to device.
142 #define PIPE_TOKEN_OUT (2 << PTOKEN0)
144 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
145 * should have one single bank, which requires less USB FIFO memory but results in slower transfers as
146 * only one USB device (the AVR or the attached device) can access the pipe's bank at the one time.
148 #define PIPE_BANK_SINGLE (0 << EPBK0)
150 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
151 * should have two banks, which requires more USB FIFO memory but results in faster transfers as one
152 * USB device (the AVR or the attached device) can access one bank while the other accesses the second
155 #define PIPE_BANK_DOUBLE (1 << EPBK0)
157 /** Pipe address for the default control pipe, which always resides in address 0. This is
158 * defined for convenience to give more readable code when used with the pipe macros.
160 #define PIPE_CONTROLPIPE 0
162 /** Default size of the default control pipe's bank, until altered by the Endpoint0Size value
163 * in the device descriptor of the attached device.
165 #define PIPE_CONTROLPIPE_DEFAULT_SIZE 64
167 /** Pipe number mask, for masking against pipe addresses to retrieve the pipe's numerical address
170 #define PIPE_PIPENUM_MASK 0x07
172 /** Total number of pipes (including the default control pipe at address 0) which may be used in
173 * the device. Different USB AVR models support different amounts of pipes, this value reflects
174 * the maximum number of pipes for the currently selected AVR model.
176 #define PIPE_TOTAL_PIPES 7
178 /** Size in bytes of the largest pipe bank size possible in the device. Not all banks on each AVR
179 * model supports the largest bank size possible on the device; different pipe numbers support
180 * different maximum bank sizes. This value reflects the largest possible bank of any pipe on the
181 * currently selected USB AVR model.
183 #define PIPE_MAX_SIZE 256
185 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
186 * numerical address in the attached device.
188 #define PIPE_EPNUM_MASK 0x0F
190 /** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's
191 * direction for comparing with the ENDPOINT_DESCRIPTOR_DIR_* masks.
193 #define PIPE_EPDIR_MASK 0x80
195 /* Pseudo-Function Macros: */
196 #if defined(__DOXYGEN__)
197 /** Indicates the number of bytes currently stored in the current pipes's selected bank.
199 * \note The return width of this function may differ, depending on the maximum pipe bank size
200 * of the selected AVR model.
202 * \ingroup Group_PipeRW
204 * \return Total number of bytes in the currently selected Pipe's FIFO buffer
206 static inline uint16_t Pipe_BytesInPipe(void);
208 /** Returns the pipe address of the currently selected pipe. This is typically used to save the
209 * currently selected pipe number so that it can be restored after another pipe has been manipulated.
211 * \return Index of the currently selected pipe
213 static inline uint8_t Pipe_GetCurrentPipe(void);
215 /** Selects the given pipe number. Any pipe operations which do not require the pipe number to be
216 * indicated will operate on the currently selected pipe.
218 * \param[in] PipeNumber Index of the pipe to select
220 static inline void Pipe_SelectPipe(uint8_t PipeNumber
);
222 /** Resets the desired pipe, including the pipe banks and flags.
224 * \param[in] PipeNumber Index of the pipe to reset
226 static inline void Pipe_ResetPipe(uint8_t PipeNumber
);
228 /** Enables the currently selected pipe so that data can be sent and received through it to and from
229 * an attached device.
231 * \note Pipes must first be configured properly via \ref Pipe_ConfigurePipe().
233 static inline void Pipe_EnablePipe(void);
235 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
236 * from an attached device.
238 static inline void Pipe_DisablePipe(void);
240 /** Determines if the currently selected pipe is enabled, but not necessarily configured.
242 * \return Boolean True if the currently selected pipe is enabled, false otherwise
244 static inline bool Pipe_IsEnabled(void);
246 /** Gets the current pipe token, indicating the pipe's data direction and type.
248 * \return The current pipe token, as a PIPE_TOKEN_* mask
250 static inline uint8_t Pipe_GetPipeToken(void);
252 /** Sets the token for the currently selected pipe to one of the tokens specified by the PIPE_TOKEN_*
253 * masks. This can be used on CONTROL type pipes, to allow for bidirectional transfer of data during
254 * control requests, or on regular pipes to allow for half-duplex bidirectional data transfer to devices
255 * which have two endpoints of opposite direction sharing the same endpoint address within the device.
257 * \param[in] Token New pipe token to set the selected pipe to, as a PIPE_TOKEN_* mask
259 static inline void Pipe_SetPipeToken(uint8_t Token
);
261 /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */
262 static inline void Pipe_SetInfiniteINRequests(void);
264 /** Configures the currently selected pipe to only allow the specified number of IN requests to be
265 * accepted by the pipe before it is automatically frozen.
267 * \param[in] TotalINRequests Total number of IN requests that the pipe may receive before freezing
269 static inline void Pipe_SetFiniteINRequests(uint8_t TotalINRequests
);
271 /** Determines if the currently selected pipe is configured.
273 * \return Boolean true if the selected pipe is configured, false otherwise
275 static inline bool Pipe_IsConfigured(void);
277 /** Retrieves the endpoint number of the endpoint within the attached device that the currently selected
280 * \return Endpoint number the currently selected pipe is bound to
282 static inline uint8_t Pipe_BoundEndpointNumber(void);
284 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds.
286 * \param[in] Milliseconds Number of milliseconds between each pipe poll
288 static inline void Pipe_SetInterruptPeriod(uint8_t Milliseconds
);
290 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
293 * \return Mask whose bits indicate which pipes have interrupted
295 static inline uint8_t Pipe_GetPipeInterrupts(void);
297 /** Determines if the specified pipe number has interrupted (valid only for INTERRUPT type
300 * \param[in] PipeNumber Index of the pipe whose interrupt flag should be tested
302 * \return Boolean true if the specified pipe has interrupted, false otherwise
304 static inline bool Pipe_HasPipeInterrupted(uint8_t PipeNumber
);
306 /** Unfreezes the selected pipe, allowing it to communicate with an attached device. */
307 static inline void Pipe_Unfreeze(void);
309 /** Freezes the selected pipe, preventing it from communicating with an attached device. */
310 static inline void Pipe_Freeze(void);
312 /** Determines if the currently selected pipe is frozen, and not able to accept data.
314 * \return Boolean true if the currently selected pipe is frozen, false otherwise
316 static inline bool Pipe_IsFrozen(void);
318 /** Clears the master pipe error flag. */
319 static inline void Pipe_ClearError(void);
321 /** Determines if the master pipe error flag is set for the currently selected pipe, indicating that
322 * some sort of hardware error has occurred on the pipe.
324 * \see \ref Pipe_GetErrorFlags() macro for information on retrieving the exact error flag.
326 * \return Boolean true if an error has occurred on the selected pipe, false otherwise
328 static inline bool Pipe_IsError(void);
330 /** Clears all the currently selected pipe's hardware error flags, but does not clear the master error
333 static inline void Pipe_ClearErrorFlags(void);
335 /** Gets a mask of the hardware error flags which have occurred on the currently selected pipe. This
336 * value can then be masked against the PIPE_ERRORFLAG_* masks to determine what error has occurred.
338 * \return Mask comprising of PIPE_ERRORFLAG_* bits indicating what error has occurred on the selected pipe
340 static inline uint8_t Pipe_GetErrorFlags(void);
342 /** Determines if the currently selected pipe may be read from (if data is waiting in the pipe
343 * bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT
344 * direction). This function will return false if an error has occurred in the pipe, or if the pipe
345 * is an IN direction and no packet (or an empty packet) has been received, or if the pipe is an OUT
346 * direction and the pipe bank is full.
348 * \note This function is not valid on CONTROL type pipes.
350 * \ingroup Group_PipePacketManagement
352 * \return Boolean true if the currently selected pipe may be read from or written to, depending on its direction
354 static inline bool Pipe_IsReadWriteAllowed(void);
356 /** Determines if an IN request has been received on the currently selected pipe.
358 * \ingroup Group_PipePacketManagement
360 * \return Boolean true if the current pipe has received an IN packet, false otherwise.
362 static inline bool Pipe_IsINReceived(void);
364 /** Determines if the currently selected pipe is ready to send an OUT request.
366 * \ingroup Group_PipePacketManagement
368 * \return Boolean true if the current pipe is ready for an OUT packet, false otherwise.
370 static inline bool Pipe_IsOUTReady(void);
372 /** Determines if no SETUP request is currently being sent to the attached device on the selected
375 * \ingroup Group_PipePacketManagement
377 * \return Boolean true if the current pipe is ready for a SETUP packet, false otherwise.
379 static inline bool Pipe_IsSETUPSent(void);
381 /** Sends the currently selected CONTROL type pipe's contents to the device as a SETUP packet.
383 * \ingroup Group_PipePacketManagement
385 static inline void Pipe_ClearSETUP(void);
387 /** Acknowledges the reception of a setup IN request from the attached device on the currently selected
388 * pipe, freeing the bank ready for the next packet.
390 * \ingroup Group_PipePacketManagement
392 static inline void Pipe_ClearIN(void);
394 /** Sends the currently selected pipe's contents to the device as an OUT packet on the selected pipe, freeing
395 * the bank ready for the next packet.
397 * \ingroup Group_PipePacketManagement
399 static inline void Pipe_ClearOUT(void);
401 /** Determines if the device sent a NAK (Negative Acknowledge) in response to the last sent packet on
402 * the currently selected pipe. This occurs when the host sends a packet to the device, but the device
403 * is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been
404 * received, it must be cleared using \ref Pipe_ClearNAKReceived() before the previous (or any other) packet
407 * \ingroup Group_PipePacketManagement
409 * \return Boolean true if an NAK has been received on the current pipe, false otherwise
411 static inline bool Pipe_IsNAKReceived(void);
413 /** Clears the NAK condition on the currently selected pipe.
415 * \ingroup Group_PipePacketManagement
417 * \see \ref Pipe_IsNAKReceived() for more details.
419 static inline void Pipe_ClearNAKReceived(void);
421 /** Determines if the currently selected pipe has had the STALL condition set by the attached device.
423 * \ingroup Group_PipePacketManagement
425 * \return Boolean true if the current pipe has been stalled by the attached device, false otherwise
427 static inline bool Pipe_IsStalled(void);
429 /** Clears the STALL condition detection flag on the currently selected pipe, but does not clear the
430 * STALL condition itself (this must be done via a ClearFeature control request to the device).
432 * \ingroup Group_PipePacketManagement
434 static inline void Pipe_ClearStall(void);
436 #define Pipe_BytesInPipe() UPBCX
438 #define Pipe_GetCurrentPipe() (UPNUM & PIPE_PIPENUM_MASK)
440 #define Pipe_SelectPipe(pipenum) MACROS{ UPNUM = (pipenum); }MACROE
442 #define Pipe_ResetPipe(pipenum) MACROS{ UPRST = (1 << (pipenum)); UPRST = 0; }MACROE
444 #define Pipe_EnablePipe() MACROS{ UPCONX |= (1 << PEN); }MACROE
446 #define Pipe_DisablePipe() MACROS{ UPCONX &= ~(1 << PEN); }MACROE
448 #define Pipe_IsEnabled() ((UPCONX & (1 << PEN)) ? true : false)
450 #define Pipe_GetPipeToken() (UPCFG0X & PIPE_TOKEN_MASK)
452 #define Pipe_SetPipeToken(token) MACROS{ UPCFG0X = ((UPCFG0X & ~PIPE_TOKEN_MASK) | (token)); }MACROE
454 #define Pipe_SetInfiniteINRequests() MACROS{ UPCONX |= (1 << INMODE); }MACROE
456 #define Pipe_SetFiniteINRequests(n) MACROS{ UPCONX &= ~(1 << INMODE); UPINRQX = (n); }MACROE
458 #define Pipe_IsConfigured() ((UPSTAX & (1 << CFGOK)) ? true : false)
460 #define Pipe_BoundEndpointNumber() ((UPCFG0X >> PEPNUM0) & PIPE_EPNUM_MASK)
462 #define Pipe_SetInterruptPeriod(ms) MACROS{ UPCFG2X = (ms); }MACROE
464 #define Pipe_GetPipeInterrupts() UPINT
466 #define Pipe_HasPipeInterrupted(n) ((UPINT & (1 << (n))) ? true : false)
468 #define Pipe_Unfreeze() MACROS{ UPCONX &= ~(1 << PFREEZE); }MACROE
470 #define Pipe_Freeze() MACROS{ UPCONX |= (1 << PFREEZE); }MACROE
472 #define Pipe_IsFrozen() ((UPCONX & (1 << PFREEZE)) ? true : false)
474 #define Pipe_ClearError() MACROS{ UPINTX &= ~(1 << PERRI); }MACROE
476 #define Pipe_IsError() ((UPINTX & (1 << PERRI)) ? true : false)
478 #define Pipe_ClearErrorFlags() MACROS{ UPERRX = 0; }MACROE
480 #define Pipe_GetErrorFlags() ((UPERRX & (PIPE_ERRORFLAG_CRC16 | PIPE_ERRORFLAG_TIMEOUT | \
481 PIPE_ERRORFLAG_PID | PIPE_ERRORFLAG_DATAPID | \
482 PIPE_ERRORFLAG_DATATGL)) | \
483 (UPSTAX & PIPE_ERRORFLAG_OVERFLOW | PIPE_ERRORFLAG_UNDERFLOW))
485 #define Pipe_IsReadWriteAllowed() ((UPINTX & (1 << RWAL)) ? true : false)
487 #define Pipe_IsINReceived() ((UPINTX & (1 << RXINI)) ? true : false)
489 #define Pipe_IsOUTReady() ((UPINTX & (1 << TXOUTI)) ? true : false)
491 #define Pipe_IsSETUPSent() ((UPINTX & (1 << TXSTPI)) ? true : false)
493 #define Pipe_ClearIN() MACROS{ uint8_t Temp = UPINTX; UPINTX = (Temp & ~(1 << RXINI)); \
494 UPINTX = (Temp & ~(1 << FIFOCON)); }MACROE
496 #define Pipe_ClearOUT() MACROS{ uint8_t Temp = UPINTX; UPINTX = (Temp & ~(1 << TXOUTI)); \
497 UPINTX = (Temp & ~(1 << FIFOCON)); }MACROE
499 #define Pipe_ClearSETUP() MACROS{ uint8_t Temp = UPINTX; UPINTX = (Temp & ~(1 << TXSTPI)); \
500 UPINTX = (Temp & ~(1 << FIFOCON)); }MACROE
502 #define Pipe_IsNAKReceived() ((UPINTX & (1 << NAKEDI)) ? true : false)
504 #define Pipe_ClearNAKReceived() MACROS{ UPINTX &= ~(1 << NAKEDI); }MACROE
506 #define Pipe_IsStalled() ((UPINTX & (1 << RXSTALLI)) ? true : false)
508 #define Pipe_ClearStall() MACROS{ UPINTX &= ~(1 << RXSTALLI); }MACROE
512 /** Enum for the possible error return codes of the Pipe_WaitUntilReady function
514 * \ingroup Group_PipeRW
516 enum Pipe_WaitUntilReady_ErrorCodes_t
518 PIPE_READYWAIT_NoError
= 0, /**< Pipe ready for next packet, no error */
519 PIPE_READYWAIT_PipeStalled
= 1, /**< The device stalled the pipe while waiting. */
520 PIPE_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while waiting. */
521 PIPE_READYWAIT_Timeout
= 3, /**< The device failed to accept or send the next packet
522 * within the software timeout period set by the
523 * \ref USB_STREAM_TIMEOUT_MS macro.
527 /** Enum for the possible error return codes of the Pipe_*_Stream_* functions.
529 * \ingroup Group_PipeRW
531 enum Pipe_Stream_RW_ErrorCodes_t
533 PIPE_RWSTREAM_NoError
= 0, /**< Command completed successfully, no error. */
534 PIPE_RWSTREAM_PipeStalled
= 1, /**< The device stalled the pipe during the transfer. */
535 PIPE_RWSTREAM_DeviceDisconnected
= 2, /**< Device was disconnected from the host during
538 PIPE_RWSTREAM_Timeout
= 3, /**< The device failed to accept or send the next packet
539 * within the software timeout period set by the
540 * \ref USB_STREAM_TIMEOUT_MS macro.
542 PIPE_RWSTREAM_CallbackAborted
= 4, /**< Indicates that the stream's callback function aborted
543 * the transfer early.
547 /* Inline Functions: */
548 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes.
550 * \ingroup Group_PipePrimitiveRW
552 * \return Next byte in the currently selected pipe's FIFO buffer
554 static inline uint8_t Pipe_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
555 static inline uint8_t Pipe_Read_Byte(void)
557 #if defined(__AVR32__)
558 return __AVR32_EPREG_X(UEDAT0
);
559 #elif defined(__AVR__)
564 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes.
566 * \ingroup Group_PipePrimitiveRW
568 * \param[in] Byte Next byte to write into the the currently selected pipe's FIFO buffer
570 static inline void Pipe_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
571 static inline void Pipe_Write_Byte(const uint8_t Byte
)
573 #if defined(__AVR32__)
574 __AVR32_EPREG_X(UEDAT0
) = Byte
;
575 #elif defined(__AVR__)
580 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes.
582 * \ingroup Group_PipePrimitiveRW
584 static inline void Pipe_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
585 static inline void Pipe_Discard_Byte(void)
589 #if defined(__AVR32__)
590 Dummy
= __AVR32_EPREG_X(UEDAT0
);
591 #elif defined(__AVR__)
596 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
599 * \ingroup Group_PipePrimitiveRW
601 * \return Next word in the currently selected pipe's FIFO buffer
603 static inline uint16_t Pipe_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
604 static inline uint16_t Pipe_Read_Word_LE(void)
612 #if defined(__AVR32__)
613 Data
.Bytes
[0] = __AVR32_EPREG_X(UEDAT0
);
614 Data
.Bytes
[1] = __AVR32_EPREG_X(UEDAT0
);
615 #elif defined(__AVR__)
616 Data
.Bytes
[0] = UPDATX
;
617 Data
.Bytes
[1] = UPDATX
;
623 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
626 * \ingroup Group_PipePrimitiveRW
628 * \return Next word in the currently selected pipe's FIFO buffer
630 static inline uint16_t Pipe_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
631 static inline uint16_t Pipe_Read_Word_BE(void)
639 #if defined(__AVR32__)
640 Data
.Bytes
[1] = __AVR32_EPREG_X(UEDAT0
);
641 Data
.Bytes
[0] = __AVR32_EPREG_X(UEDAT0
);
642 #elif defined(__AVR__)
643 Data
.Bytes
[1] = UPDATX
;
644 Data
.Bytes
[0] = UPDATX
;
650 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
653 * \ingroup Group_PipePrimitiveRW
655 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer
657 static inline void Pipe_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
658 static inline void Pipe_Write_Word_LE(const uint16_t Word
)
660 #if defined(__AVR32__)
661 __AVR32_EPREG_X(UEDAT0
) = (Word
& 0xFF);
662 __AVR32_EPREG_X(UEDAT0
) = (Word
>> 8);
663 #elif defined(__AVR__)
664 UPDATX
= (Word
& 0xFF);
665 UPDATX
= (Word
>> 8);
669 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
672 * \ingroup Group_PipePrimitiveRW
674 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer
676 static inline void Pipe_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
677 static inline void Pipe_Write_Word_BE(const uint16_t Word
)
679 #if defined(__AVR32__)
680 __AVR32_EPREG_X(UEDAT0
) = (Word
>> 8);
681 __AVR32_EPREG_X(UEDAT0
) = (Word
& 0xFF);
682 #elif defined(__AVR__)
683 UPDATX
= (Word
>> 8);
684 UPDATX
= (Word
& 0xFF);
688 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes.
690 * \ingroup Group_PipePrimitiveRW
692 static inline void Pipe_Discard_Word(void) ATTR_ALWAYS_INLINE
;
693 static inline void Pipe_Discard_Word(void)
697 #if defined(__AVR32__)
698 Dummy
= __AVR32_EPREG_X(UEDAT0
);
699 Dummy
= __AVR32_EPREG_X(UEDAT0
);
700 #elif defined(__AVR__)
706 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
709 * \ingroup Group_PipePrimitiveRW
711 * \return Next double word in the currently selected pipe's FIFO buffer
713 static inline uint32_t Pipe_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
714 static inline uint32_t Pipe_Read_DWord_LE(void)
722 #if defined(__AVR32__)
723 Data
.Bytes
[0] = __AVR32_EPREG_X(UEDAT0
);
724 Data
.Bytes
[1] = __AVR32_EPREG_X(UEDAT0
);
725 Data
.Bytes
[2] = __AVR32_EPREG_X(UEDAT0
);
726 Data
.Bytes
[3] = __AVR32_EPREG_X(UEDAT0
);
727 #elif defined(__AVR__)
728 Data
.Bytes
[0] = UPDATX
;
729 Data
.Bytes
[1] = UPDATX
;
730 Data
.Bytes
[2] = UPDATX
;
731 Data
.Bytes
[3] = UPDATX
;
737 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
740 * \ingroup Group_PipePrimitiveRW
742 * \return Next double word in the currently selected pipe's FIFO buffer
744 static inline uint32_t Pipe_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
745 static inline uint32_t Pipe_Read_DWord_BE(void)
753 #if defined(__AVR32__)
754 Data
.Bytes
[3] = __AVR32_EPREG_X(UEDAT0
);
755 Data
.Bytes
[2] = __AVR32_EPREG_X(UEDAT0
);
756 Data
.Bytes
[1] = __AVR32_EPREG_X(UEDAT0
);
757 Data
.Bytes
[0] = __AVR32_EPREG_X(UEDAT0
);
758 #elif defined(__AVR__)
759 Data
.Bytes
[3] = UPDATX
;
760 Data
.Bytes
[2] = UPDATX
;
761 Data
.Bytes
[1] = UPDATX
;
762 Data
.Bytes
[0] = UPDATX
;
768 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
771 * \ingroup Group_PipePrimitiveRW
773 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer
775 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
776 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
)
778 #if defined(__AVR32__)
779 __AVR32_EPREG_X(UEDAT0
) = (DWord
& 0xFF);
780 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 8);
781 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 16);
782 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 24);
783 #elif defined(__AVR__)
784 UPDATX
= (DWord
& 0xFF);
785 UPDATX
= (DWord
>> 8);
786 UPDATX
= (DWord
>> 16);
787 UPDATX
= (DWord
>> 24);
791 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
794 * \ingroup Group_PipePrimitiveRW
796 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer
798 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
799 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
)
801 #if defined(__AVR32__)
802 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 24);
803 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 16);
804 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 8);
805 __AVR32_EPREG_X(UEDAT0
) = (DWord
& 0xFF);
806 #elif defined(__AVR__)
807 UPDATX
= (DWord
>> 24);
808 UPDATX
= (DWord
>> 16);
809 UPDATX
= (DWord
>> 8);
810 UPDATX
= (DWord
& 0xFF);
814 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes.
816 * \ingroup Group_PipePrimitiveRW
818 static inline void Pipe_Discard_DWord(void) ATTR_ALWAYS_INLINE
;
819 static inline void Pipe_Discard_DWord(void)
823 #if defined(__AVR32__)
824 Dummy
= __AVR32_EPREG_X(UEDAT0
);
825 Dummy
= __AVR32_EPREG_X(UEDAT0
);
826 Dummy
= __AVR32_EPREG_X(UEDAT0
);
827 Dummy
= __AVR32_EPREG_X(UEDAT0
);
828 #elif defined(__AVR__)
836 /* External Variables: */
837 /** Global indicating the maximum packet size of the default control pipe located at address
838 * 0 in the device. This value is set to the value indicated in the attached device's device
839 * descriptor once the USB interface is initialized into host mode and a device is attached
842 * \note This variable should be treated as read-only in the user application, and never manually
845 extern uint8_t USB_ControlPipeSize
;
847 /* Function Prototypes: */
848 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
849 #define __CALLBACK_PARAM , StreamCallbackPtr_t Callback
851 #define __CALLBACK_PARAM
854 /** Configures the specified pipe number with the given pipe type, token, target endpoint number in the
855 * attached device, bank size and banking mode. Pipes should be allocated in ascending order by their
856 * address in the device (i.e. pipe 1 should be configured before pipe 2 and so on) to prevent fragmentation
857 * of the USB FIFO memory.
859 * The pipe type may be one of the EP_TYPE_* macros listed in LowLevel.h, the token may be one of the
860 * PIPE_TOKEN_* masks.
862 * The bank size must indicate the maximum packet size that the pipe can handle. Different pipe
863 * numbers can handle different maximum packet sizes - refer to the chosen USB AVR's datasheet to
864 * determine the maximum bank size for each pipe.
866 * The banking mode may be either \ref PIPE_BANK_SINGLE or \ref PIPE_BANK_DOUBLE.
868 * A newly configured pipe is frozen by default, and must be unfrozen before use via the \ref Pipe_Unfreeze()
869 * before being used. Pipes should be kept frozen unless waiting for data from a device while in IN mode, or
870 * sending data to the device in OUT mode. IN type pipes are also automatically configured to accept infinite
871 * numbers of IN requests without automatic freezing - this can be overridden by a call to
872 * \ref Pipe_SetFiniteINRequests().
874 * \note The default control pipe does not have to be manually configured, as it is automatically
875 * configured by the library internally.
877 * \note This routine will select the specified pipe, and the pipe will remain selected once the
878 * routine completes regardless of if the pipe configuration succeeds.
880 * \return Boolean true if the configuration is successful, false otherwise
882 bool Pipe_ConfigurePipe(const uint8_t Number
, const uint8_t Type
, const uint8_t Token
, const uint8_t EndpointNumber
,
883 const uint16_t Size
, const uint8_t Banks
);
885 /** Spin-loops until the currently selected non-control pipe is ready for the next packed of data to be read
886 * or written to it, aborting in the case of an error condition (such as a timeout or device disconnect).
888 * \ingroup Group_PipeRW
890 * \return A value from the Pipe_WaitUntilReady_ErrorCodes_t enum.
892 uint8_t Pipe_WaitUntilReady(void);
894 /** Determines if a pipe has been bound to the given device endpoint address. If a pipe which is bound to the given
895 * endpoint is found, it is automatically selected.
897 * \param[in] EndpointAddress Address and direction mask of the endpoint within the attached device to check
899 * \return Boolean true if a pipe bound to the given endpoint address of the specified direction is found, false
902 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress
);
904 /** Reads and discards the given number of bytes from the pipe, discarding fully read packets from the host
905 * as needed. The last packet is not automatically discarded once the remaining bytes has been read; the
906 * user is responsible for manually discarding the last packet from the device via the \ref Pipe_ClearIN() macro.
907 * Between each USB packet, the given stream callback function is executed repeatedly until the next packet is ready,
908 * allowing for early aborts of stream transfers.
910 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
911 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
912 * disabled and this function has the Callback parameter omitted.
914 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
915 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
917 * \ingroup Group_PipeStreamRW
919 * \param[in] Length Number of bytes to send via the currently selected pipe.
920 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
922 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
924 uint8_t Pipe_Discard_Stream(uint16_t Length __CALLBACK_PARAM
);
926 /** Writes the given number of bytes to the pipe from the given buffer in little endian,
927 * sending full packets to the device as needed. The last packet filled is not automatically sent;
928 * the user is responsible for manually sending the last written packet to the host via the
929 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
930 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
932 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
933 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
934 * disabled and this function has the Callback parameter omitted.
936 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
937 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
939 * \ingroup Group_PipeStreamRW
941 * \param[in] Buffer Pointer to the source data buffer to read from.
942 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
943 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
945 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
947 uint8_t Pipe_Write_Stream_LE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
949 /** EEPROM buffer source version of \ref Pipe_Write_Stream_LE().
951 * \ingroup Group_PipeStreamRW
953 * \param[in] Buffer Pointer to the source data buffer to read from.
954 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
955 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
957 * \note Not available on AVR32 UC3B targets.
959 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
961 uint8_t Pipe_Write_EStream_LE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
963 /** FLASH buffer source version of \ref Pipe_Write_Stream_LE().
965 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
967 * \ingroup Group_PipeStreamRW
969 * \param[in] Buffer Pointer to the source data buffer to read from.
970 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
971 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
973 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
975 uint8_t Pipe_Write_PStream_LE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
977 /** Writes the given number of bytes to the pipe from the given buffer in big endian,
978 * sending full packets to the device as needed. The last packet filled is not automatically sent;
979 * the user is responsible for manually sending the last written packet to the host via the
980 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
981 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
983 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
984 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
985 * disabled and this function has the Callback parameter omitted.
987 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
988 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
990 * \ingroup Group_PipeStreamRW
992 * \param[in] Buffer Pointer to the source data buffer to read from.
993 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
994 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
996 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
998 uint8_t Pipe_Write_Stream_BE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1000 /** EEPROM buffer source version of \ref Pipe_Write_Stream_BE().
1002 * \ingroup Group_PipeStreamRW
1004 * \param[in] Buffer Pointer to the source data buffer to read from.
1005 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
1006 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1008 * \note Not available on AVR32 UC3B targets.
1010 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1012 uint8_t Pipe_Write_EStream_BE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1014 /** FLASH buffer source version of \ref Pipe_Write_Stream_BE().
1016 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
1018 * \ingroup Group_PipeStreamRW
1020 * \param[in] Buffer Pointer to the source data buffer to read from.
1021 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
1022 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1024 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1026 uint8_t Pipe_Write_PStream_BE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1028 /** Reads the given number of bytes from the pipe into the given buffer in little endian,
1029 * sending full packets to the device as needed. The last packet filled is not automatically sent;
1030 * the user is responsible for manually sending the last written packet to the host via the
1031 * \ref Pipe_ClearIN() macro. Between each USB packet, the given stream callback function is
1032 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
1034 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
1035 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
1036 * disabled and this function has the Callback parameter omitted.
1038 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
1039 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
1041 * \ingroup Group_PipeStreamRW
1043 * \param[out] Buffer Pointer to the source data buffer to write to.
1044 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1045 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1047 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1049 uint8_t Pipe_Read_Stream_LE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1051 /** EEPROM buffer source version of \ref Pipe_Read_Stream_LE().
1053 * \ingroup Group_PipeStreamRW
1055 * \param[out] Buffer Pointer to the source data buffer to write to.
1056 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1057 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1059 * \note Not available on AVR32 UC3B targets.
1061 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1063 uint8_t Pipe_Read_EStream_LE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1065 /** Reads the given number of bytes from the pipe into the given buffer in big endian,
1066 * sending full packets to the device as needed. The last packet filled is not automatically sent;
1067 * the user is responsible for manually sending the last written packet to the host via the
1068 * \ref Pipe_ClearIN() macro. Between each USB packet, the given stream callback function is
1069 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
1071 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
1072 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
1073 * disabled and this function has the Callback parameter omitted.
1075 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
1076 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
1078 * \ingroup Group_PipeStreamRW
1080 * \param[out] Buffer Pointer to the source data buffer to write to.
1081 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1082 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1084 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1086 uint8_t Pipe_Read_Stream_BE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1088 /** EEPROM buffer source version of \ref Pipe_Read_Stream_BE().
1090 * \ingroup Group_PipeStreamRW
1092 * \param[out] Buffer Pointer to the source data buffer to write to.
1093 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1094 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1096 * \note Not available on AVR32 UC3B targets.
1098 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1100 uint8_t Pipe_Read_EStream_BE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1102 /* Private Interface - For use in library only: */
1103 #if !defined(__DOXYGEN__)
1105 #define PIPE_TOKEN_MASK (0x03 << PTOKEN0)
1107 #if !defined(ENDPOINT_CONTROLEP)
1108 #define ENDPOINT_CONTROLEP 0
1111 #define Pipe_AllocateMemory() MACROS{ UPCFG1X |= (1 << ALLOC); }MACROE
1112 #define Pipe_DeallocateMemory() MACROS{ UPCFG1X &= ~(1 << ALLOC); }MACROE
1114 /* Function Prototypes: */
1115 void Pipe_ClearPipes(void);
1117 /* Inline Functions: */
1118 static inline uintN_t
Pipe_BytesToEPSizeMask(uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
1119 static inline uintN_t
Pipe_BytesToEPSizeMask(uint16_t Bytes
)
1121 #if defined(__AVR32__)
1124 #elif defined(__AVR__)
1126 return (0 << EPSIZE0
);
1127 else if (Bytes
<= 16)
1128 return (1 << EPSIZE0
);
1129 else if (Bytes
<= 32)
1130 return (2 << EPSIZE0
);
1131 else if (Bytes
<= 64)
1132 return (3 << EPSIZE0
);
1133 else if (Bytes
<= 128)
1134 return (4 << EPSIZE0
);
1136 return (5 << EPSIZE0
);
1142 /* Disable C linkage for C++ Compilers: */
1143 #if defined(__cplusplus)