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
32 * \brief USB host pipe management definitions.
34 * This file contains structures, function prototypes and macros related to the management of the device's
35 * data pipes when the library is initialized in USB host mode.
37 * \note This file should not be included directly. It is automatically included as needed by the USB driver
38 * dispatch header located in LUFA/Drivers/USB/USB.h.
41 /** \ingroup Group_USB
42 * @defgroup Group_PipeManagement Pipe Management
44 * This module contains functions, macros and enums related to pipe management when in USB Host mode. This
45 * module contains the pipe management macros, as well as pipe interrupt and data send/receive functions
46 * for various data types.
51 /** @defgroup Group_PipeRW Pipe Data Reading and Writing
53 * Functions, macros, variables, enums and types related to data reading and writing from and to pipes.
56 /** \ingroup Group_PipeRW
57 * @defgroup Group_PipePrimitiveRW Read/Write of Primitive Data Types
59 * Functions, macros, variables, enums and types related to data reading and writing of primitive data types
63 /** \ingroup Group_PipeRW
64 * @defgroup Group_PipeStreamRW Read/Write of Multi-Byte Streams
66 * Functions, macros, variables, enums and types related to data reading and writing of data streams from
70 /** @defgroup Group_PipePacketManagement Pipe Packet Management
72 * Functions, macros, variables, enums and types related to packet management of pipes.
75 /** @defgroup Group_PipeControlReq Pipe Control Request Management
77 * Module for host mode request processing. This module allows for the transmission of standard, class and
78 * vendor control requests to the default control endpoint of an attached device while in host mode.
80 * \see Chapter 9 of the USB 2.0 specification.
88 #include <avr/pgmspace.h>
89 #include <avr/eeprom.h>
92 #include "../../../Common/Common.h"
93 #include "../HighLevel/USBTask.h"
95 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
96 #include "../HighLevel/StreamCallbacks.h"
99 /* Enable C linkage for C++ Compilers: */
100 #if defined(__cplusplus)
104 /* Preprocessor Checks: */
105 #if !defined(__INCLUDE_FROM_USB_DRIVER)
106 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
109 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
110 #define __CALLBACK_PARAM , StreamCallbackPtr_t Callback
112 #define __CALLBACK_PARAM
115 /* Public Interface - May be used in end-application: */
117 /** Mask for \ref Pipe_GetErrorFlags(), indicating that an overflow error occurred in the pipe on the received data. */
118 #define PIPE_ERRORFLAG_OVERFLOW (1 << 6)
120 /** Mask for \ref Pipe_GetErrorFlags(), indicating that an underflow error occurred in the pipe on the received data. */
121 #define PIPE_ERRORFLAG_UNDERFLOW (1 << 5)
123 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a CRC error occurred in the pipe on the received data. */
124 #define PIPE_ERRORFLAG_CRC16 (1 << 4)
126 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware timeout error occurred in the pipe. */
127 #define PIPE_ERRORFLAG_TIMEOUT (1 << 3)
129 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware PID error occurred in the pipe. */
130 #define PIPE_ERRORFLAG_PID (1 << 2)
132 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data PID error occurred in the pipe. */
133 #define PIPE_ERRORFLAG_DATAPID (1 << 1)
135 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data toggle error occurred in the pipe. */
136 #define PIPE_ERRORFLAG_DATATGL (1 << 0)
138 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a SETUP token (for CONTROL type pipes),
139 * which will trigger a control request on the attached device when data is written to the pipe.
141 #define PIPE_TOKEN_SETUP (0 << PTOKEN0)
143 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
144 * indicating that the pipe data will flow from device to host.
146 #define PIPE_TOKEN_IN (1 << PTOKEN0)
148 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a OUT token (for non-CONTROL type pipes),
149 * indicating that the pipe data will flow from host to device.
151 #define PIPE_TOKEN_OUT (2 << PTOKEN0)
153 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
154 * should have one single bank, which requires less USB FIFO memory but results in slower transfers as
155 * only one USB device (the AVR or the attached device) can access the pipe's bank at the one time.
157 #define PIPE_BANK_SINGLE (0 << EPBK0)
159 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
160 * should have two banks, which requires more USB FIFO memory but results in faster transfers as one
161 * USB device (the AVR or the attached device) can access one bank while the other accesses the second
164 #define PIPE_BANK_DOUBLE (1 << EPBK0)
166 /** Pipe address for the default control pipe, which always resides in address 0. This is
167 * defined for convenience to give more readable code when used with the pipe macros.
169 #define PIPE_CONTROLPIPE 0
171 /** Default size of the default control pipe's bank, until altered by the Endpoint0Size value
172 * in the device descriptor of the attached device.
174 #define PIPE_CONTROLPIPE_DEFAULT_SIZE 64
176 /** Pipe number mask, for masking against pipe addresses to retrieve the pipe's numerical address
179 #define PIPE_PIPENUM_MASK 0x07
181 /** Total number of pipes (including the default control pipe at address 0) which may be used in
182 * the device. Different USB AVR models support different amounts of pipes, this value reflects
183 * the maximum number of pipes for the currently selected AVR model.
185 #define PIPE_TOTAL_PIPES 7
187 /** Size in bytes of the largest pipe bank size possible in the device. Not all banks on each AVR
188 * model supports the largest bank size possible on the device; different pipe numbers support
189 * different maximum bank sizes. This value reflects the largest possible bank of any pipe on the
190 * currently selected USB AVR model.
192 #define PIPE_MAX_SIZE 256
194 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
195 * numerical address in the attached device.
197 #define PIPE_EPNUM_MASK 0x0F
199 /** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's
200 * direction for comparing with the ENDPOINT_DESCRIPTOR_DIR_* masks.
202 #define PIPE_EPDIR_MASK 0x80
205 /** Enum for the possible error return codes of the Pipe_WaitUntilReady function.
207 * \ingroup Group_PipeRW
209 enum Pipe_WaitUntilReady_ErrorCodes_t
211 PIPE_READYWAIT_NoError
= 0, /**< Pipe ready for next packet, no error. */
212 PIPE_READYWAIT_PipeStalled
= 1, /**< The device stalled the pipe while waiting. */
213 PIPE_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while waiting. */
214 PIPE_READYWAIT_Timeout
= 3, /**< The device failed to accept or send the next packet
215 * within the software timeout period set by the
216 * \ref USB_STREAM_TIMEOUT_MS macro.
220 /** Enum for the possible error return codes of the Pipe_*_Stream_* functions.
222 * \ingroup Group_PipeRW
224 enum Pipe_Stream_RW_ErrorCodes_t
226 PIPE_RWSTREAM_NoError
= 0, /**< Command completed successfully, no error. */
227 PIPE_RWSTREAM_PipeStalled
= 1, /**< The device stalled the pipe during the transfer. */
228 PIPE_RWSTREAM_DeviceDisconnected
= 2, /**< Device was disconnected from the host during
231 PIPE_RWSTREAM_Timeout
= 3, /**< The device failed to accept or send the next packet
232 * within the software timeout period set by the
233 * \ref USB_STREAM_TIMEOUT_MS macro.
235 PIPE_RWSTREAM_CallbackAborted
= 4, /**< Indicates that the stream's callback function aborted
236 * the transfer early.
240 /* Inline Functions: */
241 /** Indicates the number of bytes currently stored in the current pipes's selected bank.
243 * \note The return width of this function may differ, depending on the maximum pipe bank size
244 * of the selected AVR model.
246 * \ingroup Group_PipeRW
248 * \return Total number of bytes in the currently selected Pipe's FIFO buffer.
250 static inline uint16_t Pipe_BytesInPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
251 static inline uint16_t Pipe_BytesInPipe(void)
256 /** Returns the pipe address of the currently selected pipe. This is typically used to save the
257 * currently selected pipe number so that it can be restored after another pipe has been manipulated.
259 * \return Index of the currently selected pipe.
261 static inline uint8_t Pipe_GetCurrentPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
262 static inline uint8_t Pipe_GetCurrentPipe(void)
264 return (UPNUM
& PIPE_PIPENUM_MASK
);
267 /** Selects the given pipe number. Any pipe operations which do not require the pipe number to be
268 * indicated will operate on the currently selected pipe.
270 * \param[in] PipeNumber Index of the pipe to select.
272 static inline void Pipe_SelectPipe(const uint8_t PipeNumber
) ATTR_ALWAYS_INLINE
;
273 static inline void Pipe_SelectPipe(const uint8_t PipeNumber
)
278 /** Resets the desired pipe, including the pipe banks and flags.
280 * \param[in] PipeNumber Index of the pipe to reset.
282 static inline void Pipe_ResetPipe(const uint8_t PipeNumber
) ATTR_ALWAYS_INLINE
;
283 static inline void Pipe_ResetPipe(const uint8_t PipeNumber
)
285 UPRST
= (1 << PipeNumber
);
289 /** Enables the currently selected pipe so that data can be sent and received through it to and from
290 * an attached device.
292 * \pre The currently selected pipe must first be configured properly via \ref Pipe_ConfigurePipe().
294 static inline void Pipe_EnablePipe(void) ATTR_ALWAYS_INLINE
;
295 static inline void Pipe_EnablePipe(void)
297 UPCONX
|= (1 << PEN
);
300 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
301 * from an attached device.
303 static inline void Pipe_DisablePipe(void) ATTR_ALWAYS_INLINE
;
304 static inline void Pipe_DisablePipe(void)
306 UPCONX
&= ~(1 << PEN
);
309 /** Determines if the currently selected pipe is enabled, but not necessarily configured.
311 * \return Boolean True if the currently selected pipe is enabled, false otherwise.
313 static inline bool Pipe_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
314 static inline bool Pipe_IsEnabled(void)
316 return ((UPCONX
& (1 << PEN
)) ?
true : false);
319 /** Gets the current pipe token, indicating the pipe's data direction and type.
321 * \return The current pipe token, as a PIPE_TOKEN_* mask.
323 static inline uint8_t Pipe_GetPipeToken(void) ATTR_ALWAYS_INLINE
;
324 static inline uint8_t Pipe_GetPipeToken(void)
326 return (UPCFG0X
& (0x03 << PTOKEN0
));
329 /** Sets the token for the currently selected pipe to one of the tokens specified by the PIPE_TOKEN_*
330 * masks. This can be used on CONTROL type pipes, to allow for bidirectional transfer of data during
331 * control requests, or on regular pipes to allow for half-duplex bidirectional data transfer to devices
332 * which have two endpoints of opposite direction sharing the same endpoint address within the device.
334 * \param[in] Token New pipe token to set the selected pipe to, as a PIPE_TOKEN_* mask.
336 static inline void Pipe_SetPipeToken(const uint8_t Token
) ATTR_ALWAYS_INLINE
;
337 static inline void Pipe_SetPipeToken(const uint8_t Token
)
339 UPCFG0X
= ((UPCFG0X
& ~(0x03 << PTOKEN0
)) | Token
);
342 /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */
343 static inline void Pipe_SetInfiniteINRequests(void) ATTR_ALWAYS_INLINE
;
344 static inline void Pipe_SetInfiniteINRequests(void)
346 UPCONX
|= (1 << INMODE
);
349 /** Configures the currently selected pipe to only allow the specified number of IN requests to be
350 * accepted by the pipe before it is automatically frozen.
352 * \param[in] TotalINRequests Total number of IN requests that the pipe may receive before freezing.
354 static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests
) ATTR_ALWAYS_INLINE
;
355 static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests
)
357 UPCONX
&= ~(1 << INMODE
);
358 UPINRQX
= TotalINRequests
;
361 /** Determines if the currently selected pipe is configured.
363 * \return Boolean true if the selected pipe is configured, false otherwise.
365 static inline bool Pipe_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
366 static inline bool Pipe_IsConfigured(void)
368 return ((UPSTAX
& (1 << CFGOK
)) ?
true : false);
371 /** Retrieves the endpoint number of the endpoint within the attached device that the currently selected
374 * \return Endpoint number the currently selected pipe is bound to.
376 static inline uint8_t Pipe_BoundEndpointNumber(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
377 static inline uint8_t Pipe_BoundEndpointNumber(void)
379 return ((UPCFG0X
>> PEPNUM0
) & PIPE_EPNUM_MASK
);
382 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds.
384 * \param[in] Milliseconds Number of milliseconds between each pipe poll.
386 static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds
) ATTR_ALWAYS_INLINE
;
387 static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds
)
389 UPCFG2X
= Milliseconds
;
392 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
395 * \return Mask whose bits indicate which pipes have interrupted.
397 static inline uint8_t Pipe_GetPipeInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
398 static inline uint8_t Pipe_GetPipeInterrupts(void)
403 /** Determines if the specified pipe number has interrupted (valid only for INTERRUPT type
406 * \param[in] PipeNumber Index of the pipe whose interrupt flag should be tested.
408 * \return Boolean true if the specified pipe has interrupted, false otherwise.
410 static inline bool Pipe_HasPipeInterrupted(const uint8_t PipeNumber
) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
411 static inline bool Pipe_HasPipeInterrupted(const uint8_t PipeNumber
)
413 return ((UPINT
& (1 << PipeNumber
)) ?
true : false);
416 /** Unfreezes the selected pipe, allowing it to communicate with an attached device. */
417 static inline void Pipe_Unfreeze(void) ATTR_ALWAYS_INLINE
;
418 static inline void Pipe_Unfreeze(void)
420 UPCONX
&= ~(1 << PFREEZE
);
423 /** Freezes the selected pipe, preventing it from communicating with an attached device. */
424 static inline void Pipe_Freeze(void) ATTR_ALWAYS_INLINE
;
425 static inline void Pipe_Freeze(void)
427 UPCONX
|= (1 << PFREEZE
);
430 /** Determines if the currently selected pipe is frozen, and not able to accept data.
432 * \return Boolean true if the currently selected pipe is frozen, false otherwise.
434 static inline bool Pipe_IsFrozen(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
435 static inline bool Pipe_IsFrozen(void)
437 return ((UPCONX
& (1 << PFREEZE
)) ?
true : false);
440 /** Clears the master pipe error flag. */
441 static inline void Pipe_ClearError(void) ATTR_ALWAYS_INLINE
;
442 static inline void Pipe_ClearError(void)
444 UPINTX
&= ~(1 << PERRI
);
447 /** Determines if the master pipe error flag is set for the currently selected pipe, indicating that
448 * some sort of hardware error has occurred on the pipe.
450 * \see \ref Pipe_GetErrorFlags() macro for information on retrieving the exact error flag.
452 * \return Boolean true if an error has occurred on the selected pipe, false otherwise.
454 static inline bool Pipe_IsError(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
455 static inline bool Pipe_IsError(void)
457 return ((UPINTX
& (1 << PERRI
)) ?
true : false);
460 /** Clears all the currently selected pipe's hardware error flags, but does not clear the master error
463 static inline void Pipe_ClearErrorFlags(void) ATTR_ALWAYS_INLINE
;
464 static inline void Pipe_ClearErrorFlags(void)
469 /** Gets a mask of the hardware error flags which have occurred on the currently selected pipe. This
470 * value can then be masked against the PIPE_ERRORFLAG_* masks to determine what error has occurred.
472 * \return Mask comprising of PIPE_ERRORFLAG_* bits indicating what error has occurred on the selected pipe.
474 static inline uint8_t Pipe_GetErrorFlags(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
475 static inline uint8_t Pipe_GetErrorFlags(void)
477 return ((UPERRX
& (PIPE_ERRORFLAG_CRC16
| PIPE_ERRORFLAG_TIMEOUT
|
478 PIPE_ERRORFLAG_PID
| PIPE_ERRORFLAG_DATAPID
|
479 PIPE_ERRORFLAG_DATATGL
)) |
480 (UPSTAX
& (PIPE_ERRORFLAG_OVERFLOW
| PIPE_ERRORFLAG_UNDERFLOW
)));
483 /** Determines if the currently selected pipe may be read from (if data is waiting in the pipe
484 * bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT
485 * direction). This function will return false if an error has occurred in the pipe, or if the pipe
486 * is an IN direction and no packet (or an empty packet) has been received, or if the pipe is an OUT
487 * direction and the pipe bank is full.
489 * \note This function is not valid on CONTROL type pipes.
491 * \ingroup Group_PipePacketManagement
493 * \return Boolean true if the currently selected pipe may be read from or written to, depending on its direction.
495 static inline bool Pipe_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
496 static inline bool Pipe_IsReadWriteAllowed(void)
498 return ((UPINTX
& (1 << RWAL
)) ?
true : false);
501 /** Determines if an IN request has been received on the currently selected pipe.
503 * \ingroup Group_PipePacketManagement
505 * \return Boolean true if the current pipe has received an IN packet, false otherwise.
507 static inline bool Pipe_IsINReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
508 static inline bool Pipe_IsINReceived(void)
510 return ((UPINTX
& (1 << RXINI
)) ?
true : false);
513 /** Determines if the currently selected pipe is ready to send an OUT request.
515 * \ingroup Group_PipePacketManagement
517 * \return Boolean true if the current pipe is ready for an OUT packet, false otherwise.
519 static inline bool Pipe_IsOUTReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
520 static inline bool Pipe_IsOUTReady(void)
522 return ((UPINTX
& (1 << TXOUTI
)) ?
true : false);
525 /** Determines if no SETUP request is currently being sent to the attached device on the selected
528 * \ingroup Group_PipePacketManagement
530 * \return Boolean true if the current pipe is ready for a SETUP packet, false otherwise.
532 static inline bool Pipe_IsSETUPSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
533 static inline bool Pipe_IsSETUPSent(void)
535 return ((UPINTX
& (1 << TXSTPI
)) ?
true : false);
538 /** Sends the currently selected CONTROL type pipe's contents to the device as a SETUP packet.
540 * \ingroup Group_PipePacketManagement
542 static inline void Pipe_ClearSETUP(void) ATTR_ALWAYS_INLINE
;
543 static inline void Pipe_ClearSETUP(void)
545 UPINTX
&= ~((1 << TXSTPI
) | (1 << FIFOCON
));
548 /** Acknowledges the reception of a setup IN request from the attached device on the currently selected
549 * pipe, freeing the bank ready for the next packet.
551 * \ingroup Group_PipePacketManagement
553 static inline void Pipe_ClearIN(void) ATTR_ALWAYS_INLINE
;
554 static inline void Pipe_ClearIN(void)
556 UPINTX
&= ~((1 << RXINI
) | (1 << FIFOCON
));
559 /** Sends the currently selected pipe's contents to the device as an OUT packet on the selected pipe, freeing
560 * the bank ready for the next packet.
562 * \ingroup Group_PipePacketManagement
564 static inline void Pipe_ClearOUT(void) ATTR_ALWAYS_INLINE
;
565 static inline void Pipe_ClearOUT(void)
567 UPINTX
&= ~((1 << TXOUTI
) | (1 << FIFOCON
));
570 /** Determines if the device sent a NAK (Negative Acknowledge) in response to the last sent packet on
571 * the currently selected pipe. This occurs when the host sends a packet to the device, but the device
572 * is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been
573 * received, it must be cleared using \ref Pipe_ClearNAKReceived() before the previous (or any other) packet
576 * \ingroup Group_PipePacketManagement
578 * \return Boolean true if an NAK has been received on the current pipe, false otherwise.
580 static inline bool Pipe_IsNAKReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
581 static inline bool Pipe_IsNAKReceived(void)
583 return ((UPINTX
& (1 << NAKEDI
)) ?
true : false);
586 /** Clears the NAK condition on the currently selected pipe.
588 * \ingroup Group_PipePacketManagement
590 * \see \ref Pipe_IsNAKReceived() for more details.
592 static inline void Pipe_ClearNAKReceived(void) ATTR_ALWAYS_INLINE
;
593 static inline void Pipe_ClearNAKReceived(void)
595 UPINTX
&= ~(1 << NAKEDI
);
598 /** Determines if the currently selected pipe has had the STALL condition set by the attached device.
600 * \ingroup Group_PipePacketManagement
602 * \return Boolean true if the current pipe has been stalled by the attached device, false otherwise.
604 static inline bool Pipe_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
605 static inline bool Pipe_IsStalled(void)
607 return ((UPINTX
& (1 << RXSTALLI
)) ?
true : false);
610 /** Clears the STALL condition detection flag on the currently selected pipe, but does not clear the
611 * STALL condition itself (this must be done via a ClearFeature control request to the device).
613 * \ingroup Group_PipePacketManagement
615 static inline void Pipe_ClearStall(void) ATTR_ALWAYS_INLINE
;
616 static inline void Pipe_ClearStall(void)
618 UPINTX
&= ~(1 << RXSTALLI
);
621 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes.
623 * \ingroup Group_PipePrimitiveRW
625 * \return Next byte in the currently selected pipe's FIFO buffer.
627 static inline uint8_t Pipe_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
628 static inline uint8_t Pipe_Read_Byte(void)
633 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes.
635 * \ingroup Group_PipePrimitiveRW
637 * \param[in] Byte Next byte to write into the the currently selected pipe's FIFO buffer.
639 static inline void Pipe_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
640 static inline void Pipe_Write_Byte(const uint8_t Byte
)
645 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes.
647 * \ingroup Group_PipePrimitiveRW
649 static inline void Pipe_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
650 static inline void Pipe_Discard_Byte(void)
657 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
660 * \ingroup Group_PipePrimitiveRW
662 * \return Next word in the currently selected pipe's FIFO buffer.
664 static inline uint16_t Pipe_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
665 static inline uint16_t Pipe_Read_Word_LE(void)
673 Data
.Bytes
[0] = UPDATX
;
674 Data
.Bytes
[1] = UPDATX
;
679 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
682 * \ingroup Group_PipePrimitiveRW
684 * \return Next word in the currently selected pipe's FIFO buffer.
686 static inline uint16_t Pipe_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
687 static inline uint16_t Pipe_Read_Word_BE(void)
695 Data
.Bytes
[1] = UPDATX
;
696 Data
.Bytes
[0] = UPDATX
;
701 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
704 * \ingroup Group_PipePrimitiveRW
706 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer.
708 static inline void Pipe_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
709 static inline void Pipe_Write_Word_LE(const uint16_t Word
)
711 UPDATX
= (Word
& 0xFF);
712 UPDATX
= (Word
>> 8);
715 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
718 * \ingroup Group_PipePrimitiveRW
720 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer.
722 static inline void Pipe_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
723 static inline void Pipe_Write_Word_BE(const uint16_t Word
)
725 UPDATX
= (Word
>> 8);
726 UPDATX
= (Word
& 0xFF);
729 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes.
731 * \ingroup Group_PipePrimitiveRW
733 static inline void Pipe_Discard_Word(void) ATTR_ALWAYS_INLINE
;
734 static inline void Pipe_Discard_Word(void)
742 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
745 * \ingroup Group_PipePrimitiveRW
747 * \return Next double word in the currently selected pipe's FIFO buffer.
749 static inline uint32_t Pipe_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
750 static inline uint32_t Pipe_Read_DWord_LE(void)
758 Data
.Bytes
[0] = UPDATX
;
759 Data
.Bytes
[1] = UPDATX
;
760 Data
.Bytes
[2] = UPDATX
;
761 Data
.Bytes
[3] = UPDATX
;
766 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
769 * \ingroup Group_PipePrimitiveRW
771 * \return Next double word in the currently selected pipe's FIFO buffer.
773 static inline uint32_t Pipe_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
774 static inline uint32_t Pipe_Read_DWord_BE(void)
782 Data
.Bytes
[3] = UPDATX
;
783 Data
.Bytes
[2] = UPDATX
;
784 Data
.Bytes
[1] = UPDATX
;
785 Data
.Bytes
[0] = UPDATX
;
790 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
793 * \ingroup Group_PipePrimitiveRW
795 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer.
797 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
798 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
)
800 UPDATX
= (DWord
& 0xFF);
801 UPDATX
= (DWord
>> 8);
802 UPDATX
= (DWord
>> 16);
803 UPDATX
= (DWord
>> 24);
806 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
809 * \ingroup Group_PipePrimitiveRW
811 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer.
813 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
814 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
)
816 UPDATX
= (DWord
>> 24);
817 UPDATX
= (DWord
>> 16);
818 UPDATX
= (DWord
>> 8);
819 UPDATX
= (DWord
& 0xFF);
822 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes.
824 * \ingroup Group_PipePrimitiveRW
826 static inline void Pipe_Discard_DWord(void) ATTR_ALWAYS_INLINE
;
827 static inline void Pipe_Discard_DWord(void)
837 /* External Variables: */
838 /** Global indicating the maximum packet size of the default control pipe located at address
839 * 0 in the device. This value is set to the value indicated in the attached device's device
840 * descriptor once the USB interface is initialized into host mode and a device is attached
843 * \note This variable should be treated as read-only in the user application, and never manually
846 extern uint8_t USB_ControlPipeSize
;
848 /* Function Prototypes: */
849 /** Configures the specified pipe number with the given pipe type, token, target endpoint number in the
850 * attached device, bank size and banking mode. Pipes should be allocated in ascending order by their
851 * address in the device (i.e. pipe 1 should be configured before pipe 2 and so on) to prevent fragmentation
852 * of the USB FIFO memory.
854 * The pipe type may be one of the EP_TYPE_* macros listed in LowLevel.h, the token may be one of the
855 * PIPE_TOKEN_* masks.
857 * The bank size must indicate the maximum packet size that the pipe can handle. Different pipe
858 * numbers can handle different maximum packet sizes - refer to the chosen USB AVR's datasheet to
859 * determine the maximum bank size for each pipe.
861 * The banking mode may be either \ref PIPE_BANK_SINGLE or \ref PIPE_BANK_DOUBLE.
863 * A newly configured pipe is frozen by default, and must be unfrozen before use via the \ref Pipe_Unfreeze()
864 * before being used. Pipes should be kept frozen unless waiting for data from a device while in IN mode, or
865 * sending data to the device in OUT mode. IN type pipes are also automatically configured to accept infinite
866 * numbers of IN requests without automatic freezing - this can be overridden by a call to
867 * \ref Pipe_SetFiniteINRequests().
869 * \note The default control pipe should not be manually configured by the user application, as it
870 * is automatically configured by the library internally.
873 * \note This routine will select the specified pipe, and the pipe will remain selected once the
874 * routine completes regardless of if the pipe configuration succeeds.
876 * \return Boolean true if the configuration is successful, false otherwise.
878 bool Pipe_ConfigurePipe(const uint8_t Number
,
881 const uint8_t EndpointNumber
,
883 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
927 /** Writes the given number of bytes to the pipe from the given buffer in little endian,
928 * sending full packets to the device as needed. The last packet filled is not automatically sent;
929 * the user is responsible for manually sending the last written packet to the host via the
930 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
931 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
933 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
934 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
935 * disabled and this function has the Callback parameter omitted.
937 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
938 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
940 * \ingroup Group_PipeStreamRW
942 * \param[in] Buffer Pointer to the source data buffer to read from.
943 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
944 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
946 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
948 uint8_t Pipe_Write_Stream_LE(const void* Buffer
,
950 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
952 /** EEPROM buffer source version of \ref Pipe_Write_Stream_LE().
954 * \ingroup Group_PipeStreamRW
956 * \param[in] Buffer Pointer to the source data buffer to read from.
957 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
958 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
960 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
962 uint8_t Pipe_Write_EStream_LE(const void* Buffer
,
964 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
966 /** FLASH buffer source version of \ref Pipe_Write_Stream_LE().
968 * \pre The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
970 * \ingroup Group_PipeStreamRW
972 * \param[in] Buffer Pointer to the source data buffer to read from.
973 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
974 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
976 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
978 uint8_t Pipe_Write_PStream_LE(const void* Buffer
,
980 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
982 /** Writes the given number of bytes to the pipe from the given buffer in big endian,
983 * sending full packets to the device as needed. The last packet filled is not automatically sent;
984 * the user is responsible for manually sending the last written packet to the host via the
985 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
986 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
988 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
989 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
990 * disabled and this function has the Callback parameter omitted.
992 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
993 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
995 * \ingroup Group_PipeStreamRW
997 * \param[in] Buffer Pointer to the source data buffer to read from.
998 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
999 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
1001 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1003 uint8_t Pipe_Write_Stream_BE(const void* Buffer
,
1005 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1007 /** EEPROM buffer source version of \ref Pipe_Write_Stream_BE().
1009 * \ingroup Group_PipeStreamRW
1011 * \param[in] Buffer Pointer to the source data buffer to read from.
1012 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
1013 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
1015 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1017 uint8_t Pipe_Write_EStream_BE(const void* Buffer
,
1019 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1021 /** FLASH buffer source version of \ref Pipe_Write_Stream_BE().
1023 * \pre The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
1025 * \ingroup Group_PipeStreamRW
1027 * \param[in] Buffer Pointer to the source data buffer to read from.
1028 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
1029 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
1031 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1033 uint8_t Pipe_Write_PStream_BE(const void* Buffer
,
1035 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1037 /** Reads the given number of bytes from the pipe into the given buffer in little 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_LE(void* Buffer
,
1060 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1062 /** EEPROM buffer source version of \ref Pipe_Read_Stream_LE().
1064 * \ingroup Group_PipeStreamRW
1066 * \param[out] Buffer Pointer to the source data buffer to write to.
1067 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1068 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
1070 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1072 uint8_t Pipe_Read_EStream_LE(void* Buffer
,
1074 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1076 /** Reads the given number of bytes from the pipe into the given buffer in big endian,
1077 * sending full packets to the device as needed. The last packet filled is not automatically sent;
1078 * the user is responsible for manually sending the last written packet to the host via the
1079 * \ref Pipe_ClearIN() macro. Between each USB packet, the given stream callback function is
1080 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
1082 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
1083 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
1084 * disabled and this function has the Callback parameter omitted.
1086 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
1087 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
1089 * \ingroup Group_PipeStreamRW
1091 * \param[out] Buffer Pointer to the source data buffer to write to.
1092 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1093 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
1095 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1097 uint8_t Pipe_Read_Stream_BE(void* Buffer
,
1099 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1101 /** EEPROM buffer source version of \ref Pipe_Read_Stream_BE().
1103 * \ingroup Group_PipeStreamRW
1105 * \param[out] Buffer Pointer to the source data buffer to write to.
1106 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1107 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
1109 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1111 uint8_t Pipe_Read_EStream_BE(void* Buffer
,
1113 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1115 /* Private Interface - For use in library only: */
1116 #if !defined(__DOXYGEN__)
1118 #if !defined(ENDPOINT_CONTROLEP)
1119 #define ENDPOINT_CONTROLEP 0
1122 /* Inline Functions: */
1123 static inline uint8_t Pipe_BytesToEPSizeMask(const uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
1124 static inline uint8_t Pipe_BytesToEPSizeMask(const uint16_t Bytes
)
1126 uint8_t MaskVal
= 0;
1127 uint16_t CheckBytes
= 8;
1129 while ((CheckBytes
< Bytes
) && (CheckBytes
< PIPE_MAX_SIZE
))
1135 return (MaskVal
<< EPSIZE0
);
1138 /* Function Prototypes: */
1139 void Pipe_ClearPipes(void);
1142 /* Disable C linkage for C++ Compilers: */
1143 #if defined(__cplusplus)