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"
89 #include "../HighLevel/USBTask.h"
91 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
92 #include "../HighLevel/StreamCallbacks.h"
95 /* Enable C linkage for C++ Compilers: */
96 #if defined(__cplusplus)
100 /* Preprocessor Checks: */
101 #if !defined(__INCLUDE_FROM_USB_DRIVER)
102 #error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
105 /* Public Interface - May be used in end-application: */
107 /** Mask for \ref Pipe_GetErrorFlags(), indicating that an overflow error occurred in the pipe on the received data. */
108 #define PIPE_ERRORFLAG_OVERFLOW (1 << 6)
110 /** Mask for \ref Pipe_GetErrorFlags(), indicating that an underflow error occurred in the pipe on the received data. */
111 #define PIPE_ERRORFLAG_UNDERFLOW (1 << 5)
113 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a CRC error occurred in the pipe on the received data. */
114 #define PIPE_ERRORFLAG_CRC16 (1 << 4)
116 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware timeout error occurred in the pipe. */
117 #define PIPE_ERRORFLAG_TIMEOUT (1 << 3)
119 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware PID error occurred in the pipe. */
120 #define PIPE_ERRORFLAG_PID (1 << 2)
122 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data PID error occurred in the pipe. */
123 #define PIPE_ERRORFLAG_DATAPID (1 << 1)
125 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data toggle error occurred in the pipe. */
126 #define PIPE_ERRORFLAG_DATATGL (1 << 0)
128 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a SETUP token (for CONTROL type pipes),
129 * which will trigger a control request on the attached device when data is written to the pipe.
131 #define PIPE_TOKEN_SETUP (0 << PTOKEN0)
133 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
134 * indicating that the pipe data will flow from device to host.
136 #define PIPE_TOKEN_IN (1 << PTOKEN0)
138 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a OUT token (for non-CONTROL type pipes),
139 * indicating that the pipe data will flow from host to device.
141 #define PIPE_TOKEN_OUT (2 << PTOKEN0)
143 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
144 * should have one single bank, which requires less USB FIFO memory but results in slower transfers as
145 * only one USB device (the AVR or the attached device) can access the pipe's bank at the one time.
147 #define PIPE_BANK_SINGLE (0 << EPBK0)
149 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
150 * should have two banks, which requires more USB FIFO memory but results in faster transfers as one
151 * USB device (the AVR or the attached device) can access one bank while the other accesses the second
154 #define PIPE_BANK_DOUBLE (1 << EPBK0)
156 /** Pipe address for the default control pipe, which always resides in address 0. This is
157 * defined for convenience to give more readable code when used with the pipe macros.
159 #define PIPE_CONTROLPIPE 0
161 /** Default size of the default control pipe's bank, until altered by the Endpoint0Size value
162 * in the device descriptor of the attached device.
164 #define PIPE_CONTROLPIPE_DEFAULT_SIZE 64
166 /** Pipe number mask, for masking against pipe addresses to retrieve the pipe's numerical address
169 #define PIPE_PIPENUM_MASK 0x07
171 /** Total number of pipes (including the default control pipe at address 0) which may be used in
172 * the device. Different USB AVR models support different amounts of pipes, this value reflects
173 * the maximum number of pipes for the currently selected AVR model.
175 #define PIPE_TOTAL_PIPES 7
177 /** Size in bytes of the largest pipe bank size possible in the device. Not all banks on each AVR
178 * model supports the largest bank size possible on the device; different pipe numbers support
179 * different maximum bank sizes. This value reflects the largest possible bank of any pipe on the
180 * currently selected USB AVR model.
182 #define PIPE_MAX_SIZE 256
184 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
185 * numerical address in the attached device.
187 #define PIPE_EPNUM_MASK 0x0F
189 /** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's
190 * direction for comparing with the ENDPOINT_DESCRIPTOR_DIR_* masks.
192 #define PIPE_EPDIR_MASK 0x80
194 /* Pseudo-Function Macros: */
195 #if defined(__DOXYGEN__)
196 /** Indicates the number of bytes currently stored in the current pipes's selected bank.
198 * \note The return width of this function may differ, depending on the maximum pipe bank size
199 * of the selected AVR model.
201 * \ingroup Group_PipeRW
203 * \return Total number of bytes in the currently selected Pipe's FIFO buffer
205 static inline uint16_t Pipe_BytesInPipe(void);
207 /** Returns the pipe address of the currently selected pipe. This is typically used to save the
208 * currently selected pipe number so that it can be restored after another pipe has been manipulated.
210 * \return Index of the currently selected pipe
212 static inline uint8_t Pipe_GetCurrentPipe(void);
214 /** Selects the given pipe number. Any pipe operations which do not require the pipe number to be
215 * indicated will operate on the currently selected pipe.
217 * \param[in] PipeNumber Index of the pipe to select
219 static inline void Pipe_SelectPipe(uint8_t PipeNumber
);
221 /** Resets the desired pipe, including the pipe banks and flags.
223 * \param[in] PipeNumber Index of the pipe to reset
225 static inline void Pipe_ResetPipe(uint8_t PipeNumber
);
227 /** Enables the currently selected pipe so that data can be sent and received through it to and from
228 * an attached device.
230 * \note Pipes must first be configured properly via \ref Pipe_ConfigurePipe().
232 static inline void Pipe_EnablePipe(void);
234 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
235 * from an attached device.
237 static inline void Pipe_DisablePipe(void);
239 /** Determines if the currently selected pipe is enabled, but not necessarily configured.
241 * \return Boolean True if the currently selected pipe is enabled, false otherwise
243 static inline bool Pipe_IsEnabled(void);
245 /** Gets the current pipe token, indicating the pipe's data direction and type.
247 * \return The current pipe token, as a PIPE_TOKEN_* mask
249 static inline uint8_t Pipe_GetPipeToken(void);
251 /** Sets the token for the currently selected pipe to one of the tokens specified by the PIPE_TOKEN_*
252 * masks. This can be used on CONTROL type pipes, to allow for bidirectional transfer of data during
253 * control requests, or on regular pipes to allow for half-duplex bidirectional data transfer to devices
254 * which have two endpoints of opposite direction sharing the same endpoint address within the device.
256 * \param[in] Token New pipe token to set the selected pipe to, as a PIPE_TOKEN_* mask
258 static inline void Pipe_SetPipeToken(uint8_t Token
);
260 /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */
261 static inline void Pipe_SetInfiniteINRequests(void);
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.
266 * \param[in] TotalINRequests Total number of IN requests that the pipe may receive before freezing
268 static inline void Pipe_SetFiniteINRequests(uint8_t TotalINRequests
);
270 /** Determines if the currently selected pipe is configured.
272 * \return Boolean true if the selected pipe is configured, false otherwise
274 static inline bool Pipe_IsConfigured(void);
276 /** Retrieves the endpoint number of the endpoint within the attached device that the currently selected
279 * \return Endpoint number the currently selected pipe is bound to
281 static inline uint8_t Pipe_BoundEndpointNumber(void);
283 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds.
285 * \param[in] Milliseconds Number of milliseconds between each pipe poll
287 static inline void Pipe_SetInterruptPeriod(uint8_t Milliseconds
);
289 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
292 * \return Mask whose bits indicate which pipes have interrupted
294 static inline uint8_t Pipe_GetPipeInterrupts(void);
296 /** Determines if the specified pipe number has interrupted (valid only for INTERRUPT type
299 * \param[in] PipeNumber Index of the pipe whose interrupt flag should be tested
301 * \return Boolean true if the specified pipe has interrupted, false otherwise
303 static inline bool Pipe_HasPipeInterrupted(uint8_t PipeNumber
);
305 /** Unfreezes the selected pipe, allowing it to communicate with an attached device. */
306 static inline void Pipe_Unfreeze(void);
308 /** Freezes the selected pipe, preventing it from communicating with an attached device. */
309 static inline void Pipe_Freeze(void);
311 /** Determines if the currently selected pipe is frozen, and not able to accept data.
313 * \return Boolean true if the currently selected pipe is frozen, false otherwise
315 static inline bool Pipe_IsFrozen(void);
317 /** Clears the master pipe error flag. */
318 static inline void Pipe_ClearError(void);
320 /** Determines if the master pipe error flag is set for the currently selected pipe, indicating that
321 * some sort of hardware error has occurred on the pipe.
323 * \see \ref Pipe_GetErrorFlags() macro for information on retrieving the exact error flag.
325 * \return Boolean true if an error has occurred on the selected pipe, false otherwise
327 static inline bool Pipe_IsError(void);
329 /** Clears all the currently selected pipe's hardware error flags, but does not clear the master error
332 static inline void Pipe_ClearErrorFlags(void);
334 /** Gets a mask of the hardware error flags which have occurred on the currently selected pipe. This
335 * value can then be masked against the PIPE_ERRORFLAG_* masks to determine what error has occurred.
337 * \return Mask comprising of PIPE_ERRORFLAG_* bits indicating what error has occurred on the selected pipe
339 static inline uint8_t Pipe_GetErrorFlags(void);
341 /** Determines if the currently selected pipe may be read from (if data is waiting in the pipe
342 * bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT
343 * direction). This function will return false if an error has occurred in the pipe, or if the pipe
344 * is an IN direction and no packet (or an empty packet) has been received, or if the pipe is an OUT
345 * direction and the pipe bank is full.
347 * \note This function is not valid on CONTROL type pipes.
349 * \ingroup Group_PipePacketManagement
351 * \return Boolean true if the currently selected pipe may be read from or written to, depending on its direction
353 static inline bool Pipe_IsReadWriteAllowed(void);
355 /** Determines if an IN request has been received on the currently selected pipe.
357 * \ingroup Group_PipePacketManagement
359 * \return Boolean true if the current pipe has received an IN packet, false otherwise.
361 static inline bool Pipe_IsINReceived(void);
363 /** Determines if the currently selected pipe is ready to send an OUT request.
365 * \ingroup Group_PipePacketManagement
367 * \return Boolean true if the current pipe is ready for an OUT packet, false otherwise.
369 static inline bool Pipe_IsOUTReady(void);
371 /** Determines if no SETUP request is currently being sent to the attached device on the selected
374 * \ingroup Group_PipePacketManagement
376 * \return Boolean true if the current pipe is ready for a SETUP packet, false otherwise.
378 static inline bool Pipe_IsSETUPSent(void);
380 /** Sends the currently selected CONTROL type pipe's contents to the device as a SETUP packet.
382 * \ingroup Group_PipePacketManagement
384 static inline void Pipe_ClearSETUP(void);
386 /** Acknowledges the reception of a setup IN request from the attached device on the currently selected
387 * pipe, freeing the bank ready for the next packet.
389 * \ingroup Group_PipePacketManagement
391 static inline void Pipe_ClearIN(void);
393 /** Sends the currently selected pipe's contents to the device as an OUT packet on the selected pipe, freeing
394 * the bank ready for the next packet.
396 * \ingroup Group_PipePacketManagement
398 static inline void Pipe_ClearOUT(void);
400 /** Determines if the device sent a NAK (Negative Acknowledge) in response to the last sent packet on
401 * the currently selected pipe. This occurs when the host sends a packet to the device, but the device
402 * is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been
403 * received, it must be cleared using \ref Pipe_ClearNAKReceived() before the previous (or any other) packet
406 * \ingroup Group_PipePacketManagement
408 * \return Boolean true if an NAK has been received on the current pipe, false otherwise
410 static inline bool Pipe_IsNAKReceived(void);
412 /** Clears the NAK condition on the currently selected pipe.
414 * \ingroup Group_PipePacketManagement
416 * \see \ref Pipe_IsNAKReceived() for more details.
418 static inline void Pipe_ClearNAKReceived(void);
420 /** Determines if the currently selected pipe has had the STALL condition set by the attached device.
422 * \ingroup Group_PipePacketManagement
424 * \return Boolean true if the current pipe has been stalled by the attached device, false otherwise
426 static inline bool Pipe_IsStalled(void);
428 /** Clears the STALL condition detection flag on the currently selected pipe, but does not clear the
429 * STALL condition itself (this must be done via a ClearFeature control request to the device).
431 * \ingroup Group_PipePacketManagement
433 static inline void Pipe_ClearStall(void);
435 #define Pipe_BytesInPipe() UPBCX
437 #define Pipe_GetCurrentPipe() (UPNUM & PIPE_PIPENUM_MASK)
439 #define Pipe_SelectPipe(pipenum) MACROS{ UPNUM = (pipenum); }MACROE
441 #define Pipe_ResetPipe(pipenum) MACROS{ UPRST = (1 << (pipenum)); UPRST = 0; }MACROE
443 #define Pipe_EnablePipe() MACROS{ UPCONX |= (1 << PEN); }MACROE
445 #define Pipe_DisablePipe() MACROS{ UPCONX &= ~(1 << PEN); }MACROE
447 #define Pipe_IsEnabled() ((UPCONX & (1 << PEN)) ? true : false)
449 #define Pipe_GetPipeToken() (UPCFG0X & PIPE_TOKEN_MASK)
451 #define Pipe_SetPipeToken(token) MACROS{ UPCFG0X = ((UPCFG0X & ~PIPE_TOKEN_MASK) | (token)); }MACROE
453 #define Pipe_SetInfiniteINRequests() MACROS{ UPCONX |= (1 << INMODE); }MACROE
455 #define Pipe_SetFiniteINRequests(n) MACROS{ UPCONX &= ~(1 << INMODE); UPINRQX = (n); }MACROE
457 #define Pipe_IsConfigured() ((UPSTAX & (1 << CFGOK)) ? true : false)
459 #define Pipe_BoundEndpointNumber() ((UPCFG0X >> PEPNUM0) & PIPE_EPNUM_MASK)
461 #define Pipe_SetInterruptPeriod(ms) MACROS{ UPCFG2X = (ms); }MACROE
463 #define Pipe_GetPipeInterrupts() UPINT
465 #define Pipe_HasPipeInterrupted(n) ((UPINT & (1 << (n))) ? true : false)
467 #define Pipe_Unfreeze() MACROS{ UPCONX &= ~(1 << PFREEZE); }MACROE
469 #define Pipe_Freeze() MACROS{ UPCONX |= (1 << PFREEZE); }MACROE
471 #define Pipe_IsFrozen() ((UPCONX & (1 << PFREEZE)) ? true : false)
473 #define Pipe_ClearError() MACROS{ UPINTX &= ~(1 << PERRI); }MACROE
475 #define Pipe_IsError() ((UPINTX & (1 << PERRI)) ? true : false)
477 #define Pipe_ClearErrorFlags() MACROS{ UPERRX = 0; }MACROE
479 #define Pipe_GetErrorFlags() ((UPERRX & (PIPE_ERRORFLAG_CRC16 | PIPE_ERRORFLAG_TIMEOUT | \
480 PIPE_ERRORFLAG_PID | PIPE_ERRORFLAG_DATAPID | \
481 PIPE_ERRORFLAG_DATATGL)) | \
482 (UPSTAX & PIPE_ERRORFLAG_OVERFLOW | PIPE_ERRORFLAG_UNDERFLOW))
484 #define Pipe_IsReadWriteAllowed() ((UPINTX & (1 << RWAL)) ? true : false)
486 #define Pipe_IsINReceived() ((UPINTX & (1 << RXINI)) ? true : false)
488 #define Pipe_IsOUTReady() ((UPINTX & (1 << TXOUTI)) ? true : false)
490 #define Pipe_IsSETUPSent() ((UPINTX & (1 << TXSTPI)) ? true : false)
492 #define Pipe_ClearIN() MACROS{ uint8_t Temp = UPINTX; UPINTX = (Temp & ~(1 << RXINI)); \
493 UPINTX = (Temp & ~(1 << FIFOCON)); }MACROE
495 #define Pipe_ClearOUT() MACROS{ uint8_t Temp = UPINTX; UPINTX = (Temp & ~(1 << TXOUTI)); \
496 UPINTX = (Temp & ~(1 << FIFOCON)); }MACROE
498 #define Pipe_ClearSETUP() MACROS{ uint8_t Temp = UPINTX; UPINTX = (Temp & ~(1 << TXSTPI)); \
499 UPINTX = (Temp & ~(1 << FIFOCON)); }MACROE
501 #define Pipe_IsNAKReceived() ((UPINTX & (1 << NAKEDI)) ? true : false)
503 #define Pipe_ClearNAKReceived() MACROS{ UPINTX &= ~(1 << NAKEDI); }MACROE
505 #define Pipe_IsStalled() ((UPINTX & (1 << RXSTALLI)) ? true : false)
507 #define Pipe_ClearStall() MACROS{ UPINTX &= ~(1 << RXSTALLI); }MACROE
511 /** Enum for the possible error return codes of the Pipe_WaitUntilReady function
513 * \ingroup Group_PipeRW
515 enum Pipe_WaitUntilReady_ErrorCodes_t
517 PIPE_READYWAIT_NoError
= 0, /**< Pipe ready for next packet, no error */
518 PIPE_READYWAIT_PipeStalled
= 1, /**< The device stalled the pipe while waiting. */
519 PIPE_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while waiting. */
520 PIPE_READYWAIT_Timeout
= 3, /**< The device failed to accept or send the next packet
521 * within the software timeout period set by the
522 * \ref USB_STREAM_TIMEOUT_MS macro.
526 /** Enum for the possible error return codes of the Pipe_*_Stream_* functions.
528 * \ingroup Group_PipeRW
530 enum Pipe_Stream_RW_ErrorCodes_t
532 PIPE_RWSTREAM_NoError
= 0, /**< Command completed successfully, no error. */
533 PIPE_RWSTREAM_PipeStalled
= 1, /**< The device stalled the pipe during the transfer. */
534 PIPE_RWSTREAM_DeviceDisconnected
= 2, /**< Device was disconnected from the host during
537 PIPE_RWSTREAM_Timeout
= 3, /**< The device failed to accept or send the next packet
538 * within the software timeout period set by the
539 * \ref USB_STREAM_TIMEOUT_MS macro.
541 PIPE_RWSTREAM_CallbackAborted
= 4, /**< Indicates that the stream's callback function aborted
542 * the transfer early.
546 /* Inline Functions: */
547 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes.
549 * \ingroup Group_PipePrimitiveRW
551 * \return Next byte in the currently selected pipe's FIFO buffer
553 static inline uint8_t Pipe_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
554 static inline uint8_t Pipe_Read_Byte(void)
556 #if defined(__AVR32__)
558 #elif defined(__AVR__)
563 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes.
565 * \ingroup Group_PipePrimitiveRW
567 * \param[in] Byte Next byte to write into the the currently selected pipe's FIFO buffer
569 static inline void Pipe_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
570 static inline void Pipe_Write_Byte(const uint8_t Byte
)
572 #if defined(__AVR32__)
574 #elif defined(__AVR__)
579 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes.
581 * \ingroup Group_PipePrimitiveRW
583 static inline void Pipe_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
584 static inline void Pipe_Discard_Byte(void)
588 #if defined(__AVR32__)
590 #elif defined(__AVR__)
595 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
598 * \ingroup Group_PipePrimitiveRW
600 * \return Next word in the currently selected pipe's FIFO buffer
602 static inline uint16_t Pipe_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
603 static inline uint16_t Pipe_Read_Word_LE(void)
611 #if defined(__AVR32__)
613 #elif defined(__AVR__)
614 Data
.Bytes
[0] = UPDATX
;
615 Data
.Bytes
[1] = UPDATX
;
621 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
624 * \ingroup Group_PipePrimitiveRW
626 * \return Next word in the currently selected pipe's FIFO buffer
628 static inline uint16_t Pipe_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
629 static inline uint16_t Pipe_Read_Word_BE(void)
637 #if defined(__AVR32__)
639 #elif defined(__AVR__)
640 Data
.Bytes
[1] = UPDATX
;
641 Data
.Bytes
[0] = UPDATX
;
647 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
650 * \ingroup Group_PipePrimitiveRW
652 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer
654 static inline void Pipe_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
655 static inline void Pipe_Write_Word_LE(const uint16_t Word
)
657 #if defined(__AVR32__)
659 #elif defined(__AVR__)
660 UPDATX
= (Word
& 0xFF);
661 UPDATX
= (Word
>> 8);
665 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
668 * \ingroup Group_PipePrimitiveRW
670 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer
672 static inline void Pipe_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
673 static inline void Pipe_Write_Word_BE(const uint16_t Word
)
675 #if defined(__AVR32__)
677 #elif defined(__AVR__)
678 UPDATX
= (Word
>> 8);
679 UPDATX
= (Word
& 0xFF);
683 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes.
685 * \ingroup Group_PipePrimitiveRW
687 static inline void Pipe_Discard_Word(void) ATTR_ALWAYS_INLINE
;
688 static inline void Pipe_Discard_Word(void)
692 #if defined(__AVR32__)
694 #elif defined(__AVR__)
700 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
703 * \ingroup Group_PipePrimitiveRW
705 * \return Next double word in the currently selected pipe's FIFO buffer
707 static inline uint32_t Pipe_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
708 static inline uint32_t Pipe_Read_DWord_LE(void)
716 #if defined(__AVR32__)
718 #elif defined(__AVR__)
719 Data
.Bytes
[0] = UPDATX
;
720 Data
.Bytes
[1] = UPDATX
;
721 Data
.Bytes
[2] = UPDATX
;
722 Data
.Bytes
[3] = UPDATX
;
728 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
731 * \ingroup Group_PipePrimitiveRW
733 * \return Next double word in the currently selected pipe's FIFO buffer
735 static inline uint32_t Pipe_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
736 static inline uint32_t Pipe_Read_DWord_BE(void)
744 #if defined(__AVR32__)
746 #elif defined(__AVR__)
747 Data
.Bytes
[3] = UPDATX
;
748 Data
.Bytes
[2] = UPDATX
;
749 Data
.Bytes
[1] = UPDATX
;
750 Data
.Bytes
[0] = UPDATX
;
755 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
758 * \ingroup Group_PipePrimitiveRW
760 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer
762 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
763 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
)
765 #if defined(__AVR32__)
767 #elif defined(__AVR__)
768 UPDATX
= (DWord
& 0xFF);
769 UPDATX
= (DWord
>> 8);
770 UPDATX
= (DWord
>> 16);
771 UPDATX
= (DWord
>> 24);
775 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
778 * \ingroup Group_PipePrimitiveRW
780 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer
782 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
783 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
)
785 #if defined(__AVR32__)
787 #elif defined(__AVR__)
788 UPDATX
= (DWord
>> 24);
789 UPDATX
= (DWord
>> 16);
790 UPDATX
= (DWord
>> 8);
791 UPDATX
= (DWord
& 0xFF);
795 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes.
797 * \ingroup Group_PipePrimitiveRW
799 static inline void Pipe_Discard_DWord(void) ATTR_ALWAYS_INLINE
;
800 static inline void Pipe_Discard_DWord(void)
804 #if defined(__AVR32__)
806 #elif defined(__AVR__)
814 /* External Variables: */
815 /** Global indicating the maximum packet size of the default control pipe located at address
816 * 0 in the device. This value is set to the value indicated in the attached device's device
817 * descriptor once the USB interface is initialized into host mode and a device is attached
820 * \note This variable should be treated as read-only in the user application, and never manually
823 extern uint8_t USB_ControlPipeSize
;
825 /* Function Prototypes: */
826 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
827 #define __CALLBACK_PARAM , StreamCallbackPtr_t Callback
829 #define __CALLBACK_PARAM
832 /** Configures the specified pipe number with the given pipe type, token, target endpoint number in the
833 * attached device, bank size and banking mode. Pipes should be allocated in ascending order by their
834 * address in the device (i.e. pipe 1 should be configured before pipe 2 and so on) to prevent fragmentation
835 * of the USB FIFO memory.
837 * The pipe type may be one of the EP_TYPE_* macros listed in LowLevel.h, the token may be one of the
838 * PIPE_TOKEN_* masks.
840 * The bank size must indicate the maximum packet size that the pipe can handle. Different pipe
841 * numbers can handle different maximum packet sizes - refer to the chosen USB AVR's datasheet to
842 * determine the maximum bank size for each pipe.
844 * The banking mode may be either \ref PIPE_BANK_SINGLE or \ref PIPE_BANK_DOUBLE.
846 * A newly configured pipe is frozen by default, and must be unfrozen before use via the \ref Pipe_Unfreeze()
847 * before being used. Pipes should be kept frozen unless waiting for data from a device while in IN mode, or
848 * sending data to the device in OUT mode. IN type pipes are also automatically configured to accept infinite
849 * numbers of IN requests without automatic freezing - this can be overridden by a call to
850 * \ref Pipe_SetFiniteINRequests().
852 * \note The default control pipe does not have to be manually configured, as it is automatically
853 * configured by the library internally.
855 * \note This routine will select the specified pipe, and the pipe will remain selected once the
856 * routine completes regardless of if the pipe configuration succeeds.
858 * \return Boolean true if the configuration is successful, false otherwise
860 bool Pipe_ConfigurePipe(const uint8_t Number
, const uint8_t Type
, const uint8_t Token
, const uint8_t EndpointNumber
,
861 const uint16_t Size
, const uint8_t Banks
);
863 /** Spin-loops until the currently selected non-control pipe is ready for the next packed of data to be read
864 * or written to it, aborting in the case of an error condition (such as a timeout or device disconnect).
866 * \ingroup Group_PipeRW
868 * \return A value from the Pipe_WaitUntilReady_ErrorCodes_t enum.
870 uint8_t Pipe_WaitUntilReady(void);
872 /** Determines if a pipe has been bound to the given device endpoint address. If a pipe which is bound to the given
873 * endpoint is found, it is automatically selected.
875 * \param[in] EndpointAddress Address and direction mask of the endpoint within the attached device to check
877 * \return Boolean true if a pipe bound to the given endpoint address of the specified direction is found, false
880 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress
);
882 /** Reads and discards the given number of bytes from the pipe, discarding fully read packets from the host
883 * as needed. The last packet is not automatically discarded once the remaining bytes has been read; the
884 * user is responsible for manually discarding the last packet from the device via the \ref Pipe_ClearIN() macro.
885 * Between each USB packet, the given stream callback function is executed repeatedly until the next packet is ready,
886 * allowing for early aborts of stream transfers.
888 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
889 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
890 * disabled and this function has the Callback parameter omitted.
892 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
893 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
895 * \ingroup Group_PipeStreamRW
897 * \param[in] Length Number of bytes to send via the currently selected pipe.
898 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
900 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
902 uint8_t Pipe_Discard_Stream(uint16_t Length __CALLBACK_PARAM
);
904 /** Writes the given number of bytes to the pipe from the given buffer in little endian,
905 * sending full packets to the device as needed. The last packet filled is not automatically sent;
906 * the user is responsible for manually sending the last written packet to the host via the
907 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
908 * executed repeatedly until the next packet is ready, 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] Buffer Pointer to the source data buffer to read from.
920 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
921 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
923 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
925 uint8_t Pipe_Write_Stream_LE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
927 /** EEPROM buffer source version of \ref Pipe_Write_Stream_LE().
929 * \ingroup Group_PipeStreamRW
931 * \param[in] Buffer Pointer to the source data buffer to read from.
932 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
933 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
935 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
937 uint8_t Pipe_Write_EStream_LE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
939 /** FLASH buffer source version of \ref Pipe_Write_Stream_LE().
941 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
943 * \ingroup Group_PipeStreamRW
945 * \param[in] Buffer Pointer to the source data buffer to read from.
946 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
947 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
949 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
951 uint8_t Pipe_Write_PStream_LE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
953 /** Writes the given number of bytes to the pipe from the given buffer in big endian,
954 * sending full packets to the device as needed. The last packet filled is not automatically sent;
955 * the user is responsible for manually sending the last written packet to the host via the
956 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
957 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
959 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
960 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
961 * disabled and this function has the Callback parameter omitted.
963 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
964 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
966 * \ingroup Group_PipeStreamRW
968 * \param[in] Buffer Pointer to the source data buffer to read from.
969 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
970 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
972 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
974 uint8_t Pipe_Write_Stream_BE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
976 /** EEPROM buffer source version of \ref Pipe_Write_Stream_BE().
978 * \ingroup Group_PipeStreamRW
980 * \param[in] Buffer Pointer to the source data buffer to read from.
981 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
982 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
984 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
986 uint8_t Pipe_Write_EStream_BE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
988 /** FLASH buffer source version of \ref Pipe_Write_Stream_BE().
990 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
992 * \ingroup Group_PipeStreamRW
994 * \param[in] Buffer Pointer to the source data buffer to read from.
995 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
996 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
998 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1000 uint8_t Pipe_Write_PStream_BE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1002 /** Reads the given number of bytes from the pipe into the given buffer in little endian,
1003 * sending full packets to the device as needed. The last packet filled is not automatically sent;
1004 * the user is responsible for manually sending the last written packet to the host via the
1005 * \ref Pipe_ClearIN() macro. Between each USB packet, the given stream callback function is
1006 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
1008 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
1009 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
1010 * disabled and this function has the Callback parameter omitted.
1012 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
1013 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
1015 * \ingroup Group_PipeStreamRW
1017 * \param[out] Buffer Pointer to the source data buffer to write to.
1018 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1019 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1021 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1023 uint8_t Pipe_Read_Stream_LE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1025 /** EEPROM buffer source version of \ref Pipe_Read_Stream_LE().
1027 * \ingroup Group_PipeStreamRW
1029 * \param[out] Buffer Pointer to the source data buffer to write to.
1030 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1031 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1033 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1035 uint8_t Pipe_Read_EStream_LE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1037 /** Reads the given number of bytes from the pipe into the given buffer in big endian,
1038 * sending full packets to the device as needed. The last packet filled is not automatically sent;
1039 * the user is responsible for manually sending the last written packet to the host via the
1040 * \ref Pipe_ClearIN() macro. Between each USB packet, the given stream callback function is
1041 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
1043 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
1044 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
1045 * disabled and this function has the Callback parameter omitted.
1047 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
1048 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
1050 * \ingroup Group_PipeStreamRW
1052 * \param[out] Buffer Pointer to the source data buffer to write to.
1053 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1054 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1056 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1058 uint8_t Pipe_Read_Stream_BE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1060 /** EEPROM buffer source version of \ref Pipe_Read_Stream_BE().
1062 * \ingroup Group_PipeStreamRW
1064 * \param[out] Buffer Pointer to the source data buffer to write to.
1065 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1066 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1068 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1070 uint8_t Pipe_Read_EStream_BE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1072 /* Private Interface - For use in library only: */
1073 #if !defined(__DOXYGEN__)
1075 #define PIPE_TOKEN_MASK (0x03 << PTOKEN0)
1077 #if !defined(ENDPOINT_CONTROLEP)
1078 #define ENDPOINT_CONTROLEP 0
1081 #define Pipe_AllocateMemory() MACROS{ UPCFG1X |= (1 << ALLOC); }MACROE
1082 #define Pipe_DeallocateMemory() MACROS{ UPCFG1X &= ~(1 << ALLOC); }MACROE
1084 /* Function Prototypes: */
1085 void Pipe_ClearPipes(void);
1087 /* Inline Functions: */
1088 static inline uint8_t Pipe_BytesToEPSizeMask(uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
1089 static inline uint8_t Pipe_BytesToEPSizeMask(uint16_t Bytes
)
1092 return (0 << EPSIZE0
);
1093 else if (Bytes
<= 16)
1094 return (1 << EPSIZE0
);
1095 else if (Bytes
<= 32)
1096 return (2 << EPSIZE0
);
1097 else if (Bytes
<= 64)
1098 return (3 << EPSIZE0
);
1099 else if (Bytes
<= 128)
1100 return (4 << EPSIZE0
);
1102 return (5 << EPSIZE0
);
1107 /* Disable C linkage for C++ Compilers: */
1108 #if defined(__cplusplus)