3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
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
33 * Functions, macros and enums related to pipe management when in USB Host mode. This
34 * module contains the pipe management macros, as well as pipe interrupt and data
35 * send/recieve functions for various datatypes.
45 #include "../../../Common/Common.h"
46 #include "../HighLevel/USBTask.h"
48 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
49 #include "StreamCallbacks.h"
51 /* Enable C linkage for C++ Compilers: */
52 #if defined(__cplusplus)
56 /* Public Interface - May be used in end-application: */
58 /** Mask for Pipe_GetErrorFlags(), indicating that a CRC error occurred in the pipe on the received data. */
59 #define PIPE_ERRORFLAG_CRC16 (1 << 4)
61 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware timeout error occurred in the pipe. */
62 #define PIPE_ERRORFLAG_TIMEOUT (1 << 3)
64 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware PID error occurred in the pipe. */
65 #define PIPE_ERRORFLAG_PID (1 << 2)
67 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware data PID error occurred in the pipe. */
68 #define PIPE_ERRORFLAG_DATAPID (1 << 1)
70 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware data toggle error occurred in the pipe. */
71 #define PIPE_ERRORFLAG_DATATGL (1 << 0)
73 /** Token mask for Pipe_ConfigurePipe(). This sets the pipe as a SETUP token (for CONTROL type pipes),
74 * which will trigger a control request on the attached device when data is written to the pipe.
76 #define PIPE_TOKEN_SETUP (0b00 << PTOKEN0)
78 /** Token mask for Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
79 * indicating that the pipe data will flow from device to host.
81 #define PIPE_TOKEN_IN (0b01 << PTOKEN0)
83 /** Token mask for Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
84 * indicating that the pipe data will flow from host to device.
86 #define PIPE_TOKEN_OUT (0b10 << PTOKEN0)
88 /** Mask for the bank mode selection for the Pipe_ConfigurePipe() macro. This indicates that the pipe
89 * should have one single bank, which requires less USB FIFO memory but results in slower transfers as
90 * only one USB device (the AVR or the attached device) can access the pipe's bank at the one time.
92 #define PIPE_BANK_SINGLE (0 << EPBK0)
94 /** Mask for the bank mode selection for the Pipe_ConfigurePipe() macro. This indicates that the pipe
95 * should have two banks, which requires more USB FIFO memory but results in faster transfers as one
96 * USB device (the AVR or the attached device) can access one bank while the other accesses the second
99 #define PIPE_BANK_DOUBLE (1 << EPBK0)
101 /** Pipe address for the default control pipe, which always resides in address 0. This is
102 * defined for convenience to give more readable code when used with the pipe macros.
104 #define PIPE_CONTROLPIPE 0
106 /** Default size of the default control pipe's bank, until altered by the Endpoint0Size value
107 * in the device descriptor of the attached device.
109 #define PIPE_CONTROLPIPE_DEFAULT_SIZE 8
111 /** Pipe number mask, for masking against pipe addresses to retrieve the pipe's numerical address
114 #define PIPE_PIPENUM_MASK 0x07
116 /** Total number of pipes (including the default control pipe at address 0) which may be used in
117 * the device. Different USB AVR models support different amounts of pipes, this value reflects
118 * the maximum number of pipes for the currently selected AVR model.
120 #define PIPE_TOTAL_PIPES 7
122 /** Size in bytes of the largest pipe bank size possible in the device. Not all banks on each AVR
123 * model supports the largest bank size possible on the device; different pipe numbers support
124 * different maximum bank sizes. This value reflects the largest possible bank of any pipe on the
125 * currently selected USB AVR model.
127 #define PIPE_MAX_SIZE 256
129 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
130 * numerical address in the attached device.
132 #define PIPE_EPNUM_MASK 0x07
134 /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
135 * bank size in the attached device.
137 #define PIPE_EPSIZE_MASK 0x7FF
139 /** Interrupt definition for the pipe IN interrupt (for INTERRUPT type pipes). Should be used with
140 * the USB_INT_* macros located in USBInterrupt.h.
142 * This interrupt will fire if enabled on an INTERRUPT type pipe if the pipe interrupt period has
143 * elapsed and the pipe is ready for the next packet from the attached device to be read out from its
144 * FIFO buffer (if received).
146 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
147 * is selected), and will fire the common pipe interrupt vector.
149 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
151 #define PIPE_INT_IN UPIENX, (1 << RXINE) , UPINTX, (1 << RXINI)
153 /** Interrupt definition for the pipe OUT interrupt (for INTERRUPT type pipes). Should be used with
154 * the USB_INT_* macros located in USBInterrupt.h.
156 * This interrupt will fire if enabled on an INTERRUPT type endpoint if a the pipe interrupt period
157 * has elapsed and the pipe is ready for a packet to be written to the pipe's FIFO buffer and sent
158 * to the attached device (if required).
160 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
161 * is selected), and will fire the common pipe interrupt vector.
163 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
165 #define PIPE_INT_OUT UPIENX, (1 << TXOUTE), UPINTX, (1 << TXOUTI)
167 /** Interrupt definition for the pipe SETUP bank ready interrupt (for CONTROL type pipes). Should be
168 * used with the USB_INT_* macros located in USBInterrupt.h.
170 * This interrupt will fire if enabled on an CONTROL type pipe when the pipe is ready for a new
173 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
174 * is selected), and will fire the common pipe interrupt vector.
176 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
178 #define PIPE_INT_SETUP UPIENX, (1 << TXSTPE) , UPINTX, (1 << TXSTPI)
180 /** Interrupt definition for the pipe error interrupt. Should be used with the USB_INT_* macros
181 * located in USBInterrupt.h.
183 * This interrupt will fire if enabled on a particular pipe if an error occurs on that pipe, such
184 * as a CRC mismatch error.
186 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
187 * is selected), and will fire the common pipe interrupt vector.
189 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
191 * \see Pipe_GetErrorFlags() for more information on the pipe errors.
193 #define PIPE_INT_ERROR UPIENX, (1 << PERRE), UPINTX, (1 << PERRI)
195 /** Interrupt definition for the pipe NAK received interrupt. Should be used with the USB_INT_* macros
196 * located in USBInterrupt.h.
198 * This interrupt will fire if enabled on a particular pipe if an attached device returns a NAK in
199 * response to a sent packet.
201 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
202 * is selected), and will fire the common pipe interrupt vector.
204 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
206 * \see Pipe_IsNAKReceived() for more information on pipe NAKs.
208 #define PIPE_INT_NAK UPIENX, (1 << NAKEDE), UPINTX, (1 << NAKEDI)
210 /** Interrupt definition for the pipe STALL received interrupt. Should be used with the USB_INT_* macros
211 * located in USBInterrupt.h.
213 * This interrupt will fire if enabled on a particular pipe if an attached device returns a STALL on the
214 * currently selected pipe. This will also fire if the pipe is an isochronous pipe and a CRC error occurs.
216 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
217 * is selected), and will fire the common pipe interrupt vector.
219 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
221 #define PIPE_INT_STALL UPIENX, (1 << RXSTALLE), UPINTX, (1 << RXSTALLI)
223 /** Indicates the number of bytes currently stored in the current pipe's selected bank. */
224 #define Pipe_BytesInPipe() UPBCX
226 /** Resets the desired pipe, including the pipe banks and flags. */
227 #define Pipe_ResetPipe(pipenum) MACROS{ UPRST = (1 << pipenum); UPRST = 0; }MACROE
229 /** Selects the given pipe number. Any pipe operations which do not require the pipe number to be
230 * indicated will operate on the currently selected pipe.
232 #define Pipe_SelectPipe(pipenum) MACROS{ UPNUM = pipenum; }MACROE
234 /** Returns the pipe address of the currently selected pipe. This is typically used to save the
235 * currently selected pipe number so that it can be restored after another pipe has been manipulated.
237 #define Pipe_GetCurrentPipe() (UPNUM & PIPE_PIPENUM_MASK)
239 /** Enables the currently selected pipe so that data can be sent and received through it to and from
240 * an attached device.
242 * \note Pipes must first be configured properly rather than just being enabled via the
243 * Pipe_ConfigurePipe() macro, which calls Pipe_EnablePipe() automatically.
245 #define Pipe_EnablePipe() MACROS{ UPCONX |= (1 << PEN); }MACROE
247 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
248 * from an attached device.
250 #define Pipe_DisablePipe() MACROS{ UPCONX &= ~(1 << PEN); }MACROE
252 /** Returns true if the currently selected pipe is enabled, false otherwise. */
253 #define Pipe_IsEnabled() ((UPCONX & (1 << PEN)) ? true : false)
255 /** Sets the token for the currently selected endpoint to one of the tokens specified by the PIPE_TOKEN_*
256 * masks. This should only be used on CONTROL type endpoints, to allow for bidirectional transfer of
257 * data during control requests.
259 #define Pipe_SetToken(token) MACROS{ UPCFG0X = ((UPCFG0X & ~PIPE_TOKEN_MASK) | token); }MACROE
261 /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */
262 #define Pipe_SetInfiniteINRequests() MACROS{ UPCONX |= (1 << INMODE); }MACROE
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 #define Pipe_SetFiniteINRequests(n) MACROS{ UPCONX &= ~(1 << INMODE); UPINRQX = n; }MACROE
269 /** Returns true if the currently selected pipe is configured, false otherwise. */
270 #define Pipe_IsConfigured() ((UPSTAX & (1 << CFGOK)) ? true : false)
272 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds. */
273 #define Pipe_SetInterruptPeriod(ms) MACROS{ UPCFG2X = ms; }MACROE
275 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
278 #define Pipe_GetPipeInterrupts() UPINT
280 /** Clears the interrupt flag for the specified pipe number. */
281 #define Pipe_ClearPipeInterrupt(n) MACROS{ UPINT &= ~(1 << n); }MACROE
283 /** Returns true if the specified pipe's interrupt period has elapsed, false otherwise. */
284 #define Pipe_HasPipeInterrupted(n) ((UPINT & (1 << n)) ? true : false)
286 /** Clears the pipe bank, and switches to the alternate bank if the currently selected pipe is
287 * dual-banked. When cleared, this either frees the bank up for the next packet from the host
288 * (if the endpoint is of the OUT direction) or sends the packet contents to the host (if the
289 * pipe is of the IN direction).
291 #define Pipe_ClearCurrentBank() MACROS{ UPINTX &= ~(1 << FIFOCON); }MACROE
293 /** Unfreezes the pipe, allowing it to communicate with an attached device. */
294 #define Pipe_Unfreeze() MACROS{ UPCONX &= ~(1 << PFREEZE); }MACROE
296 /** Freezes the pipe, preventing it from communicating with an attached device. */
297 #define Pipe_Freeze() MACROS{ UPCONX |= (1 << PFREEZE); }MACROE
299 /** Clears the master pipe error flag. */
300 #define Pipe_ClearError() MACROS{ UPINTX &= ~(1 << PERRI); }MACROE
302 /** Returns true if the master pipe error flag is set for the currently selected pipe, indicating that
303 * some sort of hardware error has occurred on the pipe.
305 * \see Pipe_GetErrorFlags() macro for information on retreiving the exact error flag.
307 #define Pipe_IsError() ((UPINTX & (1 << PERRI)) ? true : false)
309 /** Clears all the currently selected pipe's hardware error flags, but does not clear the master error
310 * flag for the pipe. */
311 #define Pipe_ClearErrorFlags() MACROS{ UPERRX = 0; }MACROE
313 /** Returns a mask of the hardware error flags which have occured on the currently selected pipe. This
314 * value can then be masked against the PIPE_ERRORFLAG_* masks to determine what error has occurred.
316 #define Pipe_GetErrorFlags() UPERRX
318 /** Returns true if the currently selected pipe may be read from (if data is waiting in the pipe
319 * bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT
320 * direction). This function will return false if an error has occured in the pipe, or if the pipe
321 * is an IN direction and no packet has been received, or if the pipe is an OUT direction and the
324 #define Pipe_ReadWriteAllowed() ((UPINTX & (1 << RWAL)) ? true : false)
326 /** Clears the flag indicating that a SETUP request has been sent to the attached device from the
327 * currently selected CONTROL type pipe.
329 #define Pipe_ClearSetupSent() MACROS{ UPINTX &= ~(1 << TXSTPI); }MACROE
331 /** Returns true if no SETUP request is currently being sent to the attached device, false otherwise. */
332 #define Pipe_IsSetupSent() ((UPINTX & (1 << TXSTPI)) ? true : false)
334 /** Returns true if the currently selected pipe has been stalled by the attached device, false otherwise. */
335 #define Pipe_IsStalled() ((UPINTX & (1 << RXSTALLI)) ? true : false)
337 /** Clears the stall condition on the currently selected pipe. */
338 #define Pipe_ClearStall() MACROS{ UPINTX &= ~(1 << RXSTALLI); }MACROE
340 /** Returns true if an IN request has been received on the currently selected CONTROL type pipe, false
343 #define Pipe_IsSetupINReceived() ((UPINTX & (1 << RXINI)) ? true : false)
345 /** Returns true if the currently selected CONTROL type pipe is ready to send an OUT request, false
348 #define Pipe_IsSetupOUTReady() ((UPINTX & (1 << TXOUTI)) ? true : false)
350 /** Acknowledges the reception of a setup IN request from the attached device on the currently selected
351 * CONTROL type endpoint, allowing for the transmission of a setup OUT packet, or the reception of
352 * another setup IN packet.
354 #define Pipe_ClearSetupIN() MACROS{ UPINTX &= ~(1 << RXINI); UPINTX &= ~(1 << FIFOCON); }MACROE
356 /** Sends the currently selected CONTROL type pipe's contents to the device as a setup OUT packet. */
357 #define Pipe_ClearSetupOUT() MACROS{ UPINTX &= ~(1 << TXOUTI); UPINTX &= ~(1 << FIFOCON); }MACROE
359 /** Returns true if the device sent a NAK (Negative Acknowledge) in response to the last sent packet on
360 * the currently selected pipe. This ocurrs when the host sends a packet to the device, but the device
361 * is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been
362 * received, it must be cleard using Pipe_ClearNAKReceived() before the previous (or any other) packet
365 #define Pipe_IsNAKReceived() ((UPINTX & (1 << NAKEDI)) ? true : false)
367 /** Clears the NAK condition on the currently selected pipe.
369 * \see Pipe_IsNAKReceived() for more details.
371 #define Pipe_ClearNAKReceived() MACROS{ UPINTX &= ~(1 << NAKEDI); }MACROE
374 /** Enum for the possible error return codes of the Pipe_WaitUntilReady function */
375 enum Pipe_WaitUntilReady_ErrorCodes_t
377 PIPE_READYWAIT_NoError
= 0, /**< Pipe ready for next packet, no error */
378 PIPE_READYWAIT_PipeStalled
= 1, /**< The device stalled the pipe while waiting. */
379 PIPE_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while waiting. */
380 PIPE_READYWAIT_Timeout
= 3, /**< The device failed to accept or send the next packet
381 * within the software timeout period set by the
382 * USB_STREAM_TIMEOUT_MS macro.
386 /** Enum for the possible error return codes of the Pipe_*_Stream_* functions. */
387 enum Pipe_Stream_RW_ErrorCodes_t
389 PIPE_RWSTREAM_ERROR_NoError
= 0, /**< Command completed successfully, no error. */
390 PIPE_RWSTREAM_ERROR_PipeStalled
= 1, /**< The device stalled the pipe during the transfer. */
391 PIPE_RWSTREAM_ERROR_DeviceDisconnected
= 2, /**< Device was disconnected from the host during
394 PIPE_RWSTREAM_ERROR_Timeout
= 3, /**< The device failed to accept or send the next packet
395 * within the software timeout period set by the
396 * USB_STREAM_TIMEOUT_MS macro.
398 PIPE_RWSTREAM_ERROR_CallbackAborted
= 4, /**< Indicates that the stream's callback function aborted
399 * the transfer early.
403 /* Inline Functions: */
404 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes. */
405 static inline uint8_t Pipe_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
406 static inline uint8_t Pipe_Read_Byte(void)
411 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes. */
412 static inline void Pipe_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
413 static inline void Pipe_Write_Byte(const uint8_t Byte
)
418 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes. */
419 static inline void Pipe_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
420 static inline void Pipe_Discard_Byte(void)
427 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
430 static inline uint16_t Pipe_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
431 static inline uint16_t Pipe_Read_Word_LE(void)
436 Data
|= (((uint16_t)UPDATX
) << 8);
441 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
444 static inline uint16_t Pipe_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
445 static inline uint16_t Pipe_Read_Word_BE(void)
449 Data
= (((uint16_t)UPDATX
) << 8);
455 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
458 static inline void Pipe_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
459 static inline void Pipe_Write_Word_LE(const uint16_t Word
)
461 UPDATX
= (Word
& 0xFF);
462 UPDATX
= (Word
>> 8);
465 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
468 static inline void Pipe_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
469 static inline void Pipe_Write_Word_BE(const uint16_t Word
)
471 UPDATX
= (Word
>> 8);
472 UPDATX
= (Word
& 0xFF);
475 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes. */
476 static inline void Pipe_Ignore_Word(void) ATTR_ALWAYS_INLINE
;
477 static inline void Pipe_Ignore_Word(void)
485 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
488 static inline uint32_t Pipe_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
489 static inline uint32_t Pipe_Read_DWord_LE(void)
497 Data
.Bytes
[0] = UPDATX
;
498 Data
.Bytes
[1] = UPDATX
;
499 Data
.Bytes
[2] = UPDATX
;
500 Data
.Bytes
[3] = UPDATX
;
505 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
508 static inline uint32_t Pipe_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
509 static inline uint32_t Pipe_Read_DWord_BE(void)
517 Data
.Bytes
[3] = UPDATX
;
518 Data
.Bytes
[2] = UPDATX
;
519 Data
.Bytes
[1] = UPDATX
;
520 Data
.Bytes
[0] = UPDATX
;
525 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
528 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
529 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
)
531 Pipe_Write_Word_LE(DWord
);
532 Pipe_Write_Word_LE(DWord
>> 16);
535 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
538 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
539 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
)
541 Pipe_Write_Word_BE(DWord
>> 16);
542 Pipe_Write_Word_BE(DWord
);
545 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes. */
546 static inline void Pipe_Ignore_DWord(void) ATTR_ALWAYS_INLINE
;
547 static inline void Pipe_Ignore_DWord(void)
557 /* External Variables: */
558 /** Global indicating the maximum packet size of the default control pipe located at address
559 * 0 in the device. This value is set to the value indicated in the attached device's device
560 * descriptor once the USB interface is initialized into host mode and a device is attached
563 * \note This variable should be treated as read-only in the user application, and never manually
566 extern uint8_t USB_ControlPipeSize
;
568 /* Function Prototypes: */
569 /** Configures the specified pipe number with the given pipe type, token, target endpoint number in the
570 * attached device, bank size and banking mode. Pipes should be allocated in ascending order by their
571 * address in the device (i.e. pipe 1 should be configured before pipe 2 and so on).
573 * The pipe type may be one of the EP_TYPE_* macros listed in LowLevel.h, the token may be one of the
574 * PIPE_TOKEN_* masks.
576 * The bank size must indicate the maximum packet size that the pipe can handle. Different pipe
577 * numbers can handle different maximum packet sizes - refer to the chosen USB AVR's datasheet to
578 * determine the maximum bank size for each pipe.
580 * The banking mode may be either PIPE_BANK_SINGLE or PIPE_BANK_DOUBLE.
582 * A newly configured pipe is frozen by default, and must be unfrozen before use via the Pipe_Unfreeze() macro.
584 * \note This routine will select the specified pipe, and the pipe will remain selected once the
585 * routine completes regardless of if the pipe configuration succeeds.
587 * \return Boolean true if the configuration is successful, false otherwise
589 bool Pipe_ConfigurePipe(const uint8_t Number
, const uint8_t Type
, const uint8_t Token
, const uint8_t EndpointNumber
,
590 const uint16_t Size
, const uint8_t Banks
);
592 /** Spinloops until the currently selected non-control pipe is ready for the next packed of data
593 * to be read or written to it.
595 * \note This routine should not be called on CONTROL type pipes.
597 * \return A value from the Pipe_WaitUntilReady_ErrorCodes_t enum.
599 uint8_t Pipe_WaitUntilReady(void);
601 /** Writes the given number of bytes to the pipe from the given buffer in little endian,
602 * sending full packets to the device as needed. The last packet filled is not automatically sent;
603 * the user is responsible for manually sending the last written packet to the host via the
604 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
605 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
607 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
608 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
609 * and this function has the Callback parameter ommitted.
611 * \param Buffer Pointer to the source data buffer to read from.
612 * \param Length Number of bytes to read for the currently selected pipe into the buffer.
613 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
615 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
617 uint8_t Pipe_Write_Stream_LE(const void* Buffer
, uint16_t Length
618 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
619 , uint8_t (* const Callback
)(void)
621 ) ATTR_NON_NULL_PTR_ARG(1);
623 /** Writes the given number of bytes to the pipe from the given buffer in big endian,
624 * sending full packets to the device as needed. The last packet filled is not automatically sent;
625 * the user is responsible for manually sending the last written packet to the host via the
626 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
627 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
629 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
630 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
631 * and this function has the Callback parameter ommitted.
633 * \param Buffer Pointer to the source data buffer to read from.
634 * \param Length Number of bytes to read for the currently selected pipe into the buffer.
635 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
637 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
639 uint8_t Pipe_Write_Stream_BE(const void* Buffer
, uint16_t Length
640 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
641 , uint8_t (* const Callback
)(void)
643 ) ATTR_NON_NULL_PTR_ARG(1);
645 /** Reads and discards the given number of bytes from the pipe, discarding fully read packets from the host
646 * as needed. The last packet is not automatically discarded once the remaining bytes has been read; the
647 * user is responsible for manually discarding the last packet from the host via the Pipe_ClearCurrentBank() macro.
648 * Between each USB packet, the given stream callback function is executed repeatedly until the next packet is ready,
649 * allowing for early aborts of stream transfers.
651 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
652 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
653 * and this function has the Callback parameter ommitted.
655 * \param Length Number of bytes to send via the currently selected pipe.
656 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
658 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
660 uint8_t Pipe_Discard_Stream(uint16_t Length
661 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
662 , uint8_t (* const Callback
)(void)
666 /** Reads the given number of bytes from the pipe into the given buffer in little endian,
667 * sending full packets to the device as needed. The last packet filled is not automatically sent;
668 * the user is responsible for manually sending the last written packet to the host via the
669 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
670 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
672 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
673 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
674 * and this function has the Callback parameter ommitted.
676 * \param Buffer Pointer to the source data buffer to write to.
677 * \param Length Number of bytes to read for the currently selected pipe to read from.
678 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
680 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
682 uint8_t Pipe_Read_Stream_LE(void* Buffer
, uint16_t Length
683 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
684 , uint8_t (* const Callback
)(void)
686 ) ATTR_NON_NULL_PTR_ARG(1);
688 /** Reads the given number of bytes from the pipe into the given buffer in big endian,
689 * sending full packets to the device as needed. The last packet filled is not automatically sent;
690 * the user is responsible for manually sending the last written packet to the host via the
691 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
692 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
694 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
695 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
696 * and this function has the Callback parameter ommitted.
698 * \param Buffer Pointer to the source data buffer to write to.
699 * \param Length Number of bytes to read for the currently selected pipe to read from.
700 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
702 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
704 uint8_t Pipe_Read_Stream_BE(void* Buffer
, uint16_t Length
705 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
706 , uint8_t (* const Callback
)(void)
708 ) ATTR_NON_NULL_PTR_ARG(1);
710 /* Function Aliases: */
711 /** Alias for Pipe_Discard_Byte().
713 #define Pipe_Ignore_Byte() Pipe_Discard_Byte()
715 /** Alias for Pipe_Discard_Word().
717 #define Pipe_Ignore_Word() Pipe_Discard_Word()
719 /** Alias for Pipe_Discard_DWord().
721 #define Pipe_Ignore_DWord() Pipe_Discard_DWord()
723 /** Alias for Pipe_Read_Word_LE(). By default USB transfers use little endian format, thus
724 * the command with no endianness specifier indicates little endian mode.
726 #define Pipe_Read_Word() Pipe_Read_Word_LE()
728 /** Alias for Pipe_Write_Word_LE(). By default USB transfers use little endian format, thus
729 * the command with no endianness specifier indicates little endian mode.
731 #define Pipe_Write_Word(Word) Pipe_Write_Word_LE(Word)
733 /** Alias for Pipe_Read_DWord_LE(). By default USB transfers use little endian format, thus
734 * the command with no endianness specifier indicates little endian mode.
736 #define Pipe_Read_DWord() Pipe_Read_DWord_LE()
738 /** Alias for Pipe_Write_DWord_LE(). By default USB transfers use little endian format, thus
739 * the command with no endianness specifier indicates little endian mode.
741 #define Pipe_Write_DWord(DWord) Pipe_Write_DWord_LE(DWord)
743 /** Alias for Pipe_Read_Stream_LE(). By default USB transfers use little endian format, thus
744 * the command with no endianness specifier indicates little endian mode.
746 #if !defined(NO_STREAM_CALLBACKS)
747 #define Pipe_Read_Stream(Buffer, Length, Callback) Pipe_Read_Stream_LE(Buffer, Length, Callback)
749 #define Pipe_Read_Stream(Buffer, Length) Pipe_Read_Stream_LE(Buffer, Length)
752 /** Alias for Pipe_Write_Stream_LE(). By default USB transfers use little endian format, thus
753 * the command with no endianness specifier indicates little endian mode.
755 #if !defined(NO_STREAM_CALLBACKS)
756 #define Pipe_Write_Stream(Buffer, Length, Callback) Pipe_Read_Stream_LE(Buffer, Length, Callback)
758 #define Pipe_Write_Stream(Buffer, Length) Pipe_Read_Stream_LE(Buffer, Length)
761 /* Private Interface - For use in library only: */
762 #if !defined(__DOXYGEN__)
764 #define PIPE_TOKEN_MASK (0x03 << PTOKEN0)
766 #define Pipe_AllocateMemory() MACROS{ UPCFG1X |= (1 << ALLOC); }MACROE
767 #define Pipe_DeallocateMemory() MACROS{ UPCFG1X &= ~(1 << ALLOC); }MACROE
769 /* Function Prototypes: */
770 void Pipe_ClearPipes(void);
772 /* Inline Functions: */
773 static inline uint8_t Pipe_BytesToEPSizeMask(uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
774 static inline uint8_t Pipe_BytesToEPSizeMask(uint16_t Bytes
)
777 return (0 << EPSIZE0
);
778 else if (Bytes
<= 16)
779 return (1 << EPSIZE0
);
780 else if (Bytes
<= 32)
781 return (2 << EPSIZE0
);
782 else if (Bytes
<= 64)
783 return (3 << EPSIZE0
);
784 else if (Bytes
<= 128)
785 return (4 << EPSIZE0
);
787 return (5 << EPSIZE0
);
792 /* Disable C linkage for C++ Compilers: */
793 #if defined(__cplusplus)