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
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 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
147 * will fire the common pipe interrupt vector.
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 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
161 * will fire the common pipe interrupt vector.
163 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector. */
164 #define PIPE_INT_OUT UPIENX, (1 << TXOUTE), UPINTX, (1 << TXOUTI)
166 /** Interrupt definition for the pipe SETUP bank ready interrupt (for CONTROL type pipes). Should be
167 * used with the USB_INT_* macros located in USBInterrupt.h.
169 * This interrupt will fire if enabled on an CONTROL type pipe when the pipe is ready for a new
172 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
173 * will fire the common pipe interrupt vector.
175 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
177 #define PIPE_INT_SETUP UPIENX, (1 << TXSTPE) , UPINTX, (1 << TXSTPI)
179 /** Interrupt definition for the pipe error interrupt. Should be used with the USB_INT_* macros
180 * located in USBInterrupt.h.
182 * This interrupt will fire if enabled on a particular pipe if an error occurs on that pipe, such
183 * as a CRC mismatch error.
185 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
186 * will fire the common pipe interrupt vector.
188 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
190 * \see Pipe_GetErrorFlags() for more information on the pipe errors.
192 #define PIPE_INT_ERROR UPIENX, (1 << PERRE), UPINTX, (1 << PERRI)
194 /** Interrupt definition for the pipe NAK received interrupt. Should be used with the USB_INT_* macros
195 * located in USBInterrupt.h.
197 * This interrupt will fire if enabled on a particular pipe if an attached device returns a NAK in
198 * response to a sent packet.
200 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
201 * will fire the common pipe interrupt vector.
203 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
205 * \see Pipe_IsNAKReceived() for more information on pipe NAKs.
207 #define PIPE_INT_NAK UPIENX, (1 << NAKEDE), UPINTX, (1 << NAKEDI)
209 /** Interrupt definition for the pipe STALL received interrupt. Should be used with the USB_INT_* macros
210 * located in USBInterrupt.h.
212 * This interrupt will fire if enabled on a particular pipe if an attached device returns a STALL on the
213 * currently selected pipe. This will also fire if the pipe is an isochronous pipe and a CRC error occurs.
215 * This interrupt must be enabled on *each* pipe which requires it (after the pipe is selected), and
216 * will fire the common pipe interrupt vector.
218 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
220 #define PIPE_INT_STALL UPIENX, (1 << RXSTALLE), UPINTX, (1 << RXSTALLI)
222 /** Indicates the number of bytes currently stored in the current pipe's selected bank. */
223 #define Pipe_BytesInPipe() UPBCX
225 /** Resets the desired pipe, including the pipe banks and flags. */
226 #define Pipe_ResetPipe(pipenum) MACROS{ UPRST = (1 << pipenum); UPRST = 0; }MACROE
228 /** Selects the given pipe number. Any pipe operations which do not require the pipe number to be
229 * indicated will operate on the currently selected pipe.
231 #define Pipe_SelectPipe(pipenum) MACROS{ UPNUM = pipenum; }MACROE
233 /** Returns the pipe address of the currently selected pipe. This is typically used to save the
234 * currently selected pipe number so that it can be restored after another pipe has been manipulated.
236 #define Pipe_GetCurrentPipe() (UPNUM & PIPE_PIPENUM_MASK)
238 /** Enables the currently selected pipe so that data can be sent and received through it to and from
239 * an attached device.
241 * \note Pipes must first be configured properly rather than just being enabled via the
242 * Pipe_ConfigurePipe() macro, which calls Pipe_EnablePipe() automatically.
244 #define Pipe_EnablePipe() MACROS{ UPCONX |= (1 << PEN); }MACROE
246 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
247 * from an attached device.
249 #define Pipe_DisablePipe() MACROS{ UPCONX &= ~(1 << PEN); }MACROE
251 /** Returns true if the currently selected pipe is enabled, false otherwise. */
252 #define Pipe_IsEnabled() ((UPCONX & (1 << PEN)) ? true : false)
254 /** Sets the token for the currently selected endpoint to one of the tokens specified by the PIPE_TOKEN_*
255 * masks. This should only be used on CONTROL type endpoints, to allow for bidirectional transfer of
256 * data during control requests.
258 #define Pipe_SetToken(token) MACROS{ UPCFG0X = ((UPCFG0X & ~PIPE_TOKEN_MASK) | token); }MACROE
260 /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */
261 #define Pipe_SetInfiniteINRequests() MACROS{ UPCONX |= (1 << INMODE); }MACROE
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 #define Pipe_SetFiniteINRequests(n) MACROS{ UPCONX &= ~(1 << INMODE); UPINRQX = n; }MACROE
268 /** Returns true if the currently selected pipe is configured, false otherwise. */
269 #define Pipe_IsConfigured() ((UPSTAX & (1 << CFGOK)) ? true : false)
271 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds. */
272 #define Pipe_SetInterruptPeriod(ms) MACROS{ UPCFG2X = ms; }MACROE
274 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
277 #define Pipe_GetPipeInterrupts() UPINT
279 /** Clears the interrupt flag for the specified pipe number. */
280 #define Pipe_ClearPipeInterrupt(n) MACROS{ UPINT &= ~(1 << n); }MACROE
282 /** Returns true if the specified pipe's interrupt period has elapsed, false otherwise. */
283 #define Pipe_HasPipeInterrupted(n) ((UPINT & (1 << n)) ? true : false)
285 /** Clears the pipe bank, and switches to the alternate bank if the currently selected pipe is
286 * dual-banked. When cleared, this either frees the bank up for the next packet from the host
287 * (if the endpoint is of the OUT direction) or sends the packet contents to the host (if the
288 * pipe is of the IN direction).
290 #define Pipe_ClearCurrentBank() MACROS{ UPINTX &= ~(1 << FIFOCON); }MACROE
292 /** Unfreezes the pipe, allowing it to communicate with an attached device. */
293 #define Pipe_Unfreeze() MACROS{ UPCONX &= ~(1 << PFREEZE); }MACROE
295 /** Freezes the pipe, preventing it from communicating with an attached device. */
296 #define Pipe_Freeze() MACROS{ UPCONX |= (1 << PFREEZE); }MACROE
298 /** Clears the master pipe error flag. */
299 #define Pipe_ClearError() MACROS{ UPINTX &= ~(1 << PERRI); }MACROE
301 /** Returns true if the master pipe error flag is set for the currently selected pipe, indicating that
302 * some sort of hardware error has occurred on the pipe.
304 * \see Pipe_GetErrorFlags() macro for information on retreiving the exact error flag.
306 #define Pipe_IsError() ((UPINTX & (1 << PERRI)) ? true : false)
308 /** Clears all the currently selected pipe's hardware error flags, but does not clear the master error
309 * flag for the pipe. */
310 #define Pipe_ClearErrorFlags() MACROS{ UPERRX = 0; }MACROE
312 /** Returns a mask of the hardware error flags which have occured on the currently selected pipe. This
313 * value can then be masked against the PIPE_ERRORFLAG_* masks to determine what error has occurred.
315 #define Pipe_GetErrorFlags() UPERRX
317 /** Returns true if the currently selected pipe may be read from (if data is waiting in the pipe
318 * bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT
319 * direction). This function will return false if an error has occured in the pipe, or if the pipe
320 * is an IN direction and no packet has been received, or if the pipe is an OUT direction and the
323 #define Pipe_ReadWriteAllowed() ((UPINTX & (1 << RWAL)) ? true : false)
325 /** Clears the flag indicating that a SETUP request has been sent to the attached device from the
326 * currently selected CONTROL type pipe.
328 #define Pipe_ClearSetupSent() MACROS{ UPINTX &= ~(1 << TXSTPI); }MACROE
330 /** Returns true if no SETUP request is currently being sent to the attached device, false otherwise. */
331 #define Pipe_IsSetupSent() ((UPINTX & (1 << TXSTPI)) ? true : false)
333 /** Returns true if the currently selected pipe has been stalled by the attached device, false otherwise. */
334 #define Pipe_IsStalled() ((UPINTX & (1 << RXSTALLI)) ? true : false)
336 /** Clears the stall condition on the currently selected pipe. */
337 #define Pipe_ClearStall() MACROS{ UPINTX &= ~(1 << RXSTALLI); }MACROE
339 /** Returns true if an IN request has been received on the currently selected CONTROL type pipe, false
342 #define Pipe_IsSetupINReceived() ((UPINTX & (1 << RXINI)) ? true : false)
344 /** Returns true if the currently selected CONTROL type pipe is ready to send an OUT request, false
347 #define Pipe_IsSetupOUTReady() ((UPINTX & (1 << TXOUTI)) ? true : false)
349 /** Acknowedges the reception of a setup IN request from the attached device on the currently selected
350 * CONTROL type endpoint, allowing for the transmission of a setup OUT packet, or the reception of
351 * another setup IN packet.
353 #define Pipe_ClearSetupIN() MACROS{ UPINTX &= ~(1 << RXINI); UPINTX &= ~(1 << FIFOCON); }MACROE
355 /** Sends the currently selected CONTROL type pipe's contents to the device as a setup OUT packet. */
356 #define Pipe_ClearSetupOUT() MACROS{ UPINTX &= ~(1 << TXOUTI); UPINTX &= ~(1 << FIFOCON); }MACROE
358 /** Returns true if the device sent a NAK (Negative Acknowedge) in response to the last sent packet on
359 * the currently selected pipe. This ocurrs when the host sends a packet to the device, but the device
360 * is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been
361 * received, it must be cleard using Pipe_ClearNAKReceived() before the previous (or any other) packet
364 #define Pipe_IsNAKReceived() ((UPINTX & (1 << NAKEDI)) ? true : false)
366 /** Clears the NAK condition on the currently selected pipe.
368 * \see Pipe_IsNAKReceived() for more details.
370 #define Pipe_ClearNAKReceived() MACROS{ UPINTX &= ~(1 << NAKEDI); }MACROE
373 /** Enum for the possible error return codes of the Pipe_WaitUntilReady function */
374 enum Pipe_WaitUntilReady_ErrorCodes_t
376 PIPE_READYWAIT_NoError
= 0, /**< Pipe ready for next packet, no error */
377 PIPE_READYWAIT_PipeStalled
= 1, /**< The device stalled the pipe while waiting. */
378 PIPE_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while waiting. */
379 PIPE_READYWAIT_Timeout
= 3, /**< The device failed to accept or send the next packet
380 * within the software timeout period set by the
381 * USB_STREAM_TIMEOUT_MS macro.
385 /** Enum for the possible error return codes of the Pipe_*_Stream_* functions. */
386 enum Pipe_Stream_RW_ErrorCodes_t
388 PIPE_RWSTREAM_ERROR_NoError
= 0, /**< Command completed successfully, no error. */
389 PIPE_RWSTREAM_ERROR_PipeStalled
= 1, /**< The device stalled the pipe during the transfer. */
390 PIPE_RWSTREAM_ERROR_DeviceDisconnected
= 2, /**< Device was disconnected from the host during
393 PIPE_RWSTREAM_ERROR_Timeout
= 3, /**< The device failed to accept or send the next packet
394 * within the software timeout period set by the
395 * USB_STREAM_TIMEOUT_MS macro.
397 PIPE_RWSTREAM_ERROR_CallbackAborted
= 4, /**< Indicates that the stream's callback function aborted
398 * the transfer early.
402 /* Inline Functions: */
403 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes. */
404 static inline uint8_t Pipe_Read_Byte(void) ATTR_WARN_UNUSED_RESULT
;
405 static inline uint8_t Pipe_Read_Byte(void)
410 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes. */
411 static inline void Pipe_Write_Byte(const uint8_t Byte
)
416 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes. */
417 static inline void Pipe_Discard_Byte(void)
424 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
427 static inline uint16_t Pipe_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT
;
428 static inline uint16_t Pipe_Read_Word_LE(void)
433 Data
|= (((uint16_t)UPDATX
) << 8);
438 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
441 static inline uint16_t Pipe_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT
;
442 static inline uint16_t Pipe_Read_Word_BE(void)
446 Data
= (((uint16_t)UPDATX
) << 8);
452 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
455 static inline void Pipe_Write_Word_LE(const uint16_t Word
)
457 UPDATX
= (Word
& 0xFF);
458 UPDATX
= (Word
>> 8);
461 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
464 static inline void Pipe_Write_Word_BE(const uint16_t Word
)
466 UPDATX
= (Word
>> 8);
467 UPDATX
= (Word
& 0xFF);
470 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes. */
471 static inline void Pipe_Ignore_Word(void)
479 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
482 static inline uint32_t Pipe_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT
;
483 static inline uint32_t Pipe_Read_DWord_LE(void)
491 Data
.Bytes
[0] = UPDATX
;
492 Data
.Bytes
[1] = UPDATX
;
493 Data
.Bytes
[2] = UPDATX
;
494 Data
.Bytes
[3] = UPDATX
;
499 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
502 static inline uint32_t Pipe_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT
;
503 static inline uint32_t Pipe_Read_DWord_BE(void)
511 Data
.Bytes
[3] = UPDATX
;
512 Data
.Bytes
[2] = UPDATX
;
513 Data
.Bytes
[1] = UPDATX
;
514 Data
.Bytes
[0] = UPDATX
;
519 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
522 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
)
524 Pipe_Write_Word_LE(DWord
);
525 Pipe_Write_Word_LE(DWord
>> 16);
528 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
531 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
)
533 Pipe_Write_Word_BE(DWord
>> 16);
534 Pipe_Write_Word_BE(DWord
);
537 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes. */
538 static inline void Pipe_Ignore_DWord(void)
548 /* External Variables: */
549 /** Global indicating the maximum packet size of the default control pipe located at address
550 * 0 in the device. This value is set to the value indicated in the attached device's device
551 * descriptor once the USB interface is initialized into host mode and a device is attached
554 * \note This variable should be treated as read-only in the user application, and never manually
557 extern uint8_t USB_ControlPipeSize
;
559 /* Function Prototypes: */
560 /** Configures the specified pipe number with the given pipe type, token, target endpoint number in the
561 * attached device, bank size and banking mode. Pipes should be allocated in ascending order by their
562 * address in the device (i.e. pipe 1 should be configured before pipe 2 and so on).
564 * The pipe type may be one of the EP_TYPE_* macros listed in LowLevel.h, the token may be one of the
565 * PIPE_TOKEN_* masks.
567 * The bank size must indicate the maximum packet size that the pipe can handle. Different pipe
568 * numbers can handle different maximum packet sizes - refer to the chosen USB AVR's datasheet to
569 * determine the maximum bank size for each pipe.
571 * The banking mode may be either PIPE_BANK_SINGLE or PIPE_BANK_DOUBLE.
573 * A newly configured pipe is frozen by default, and must be unfrozen before use via the Pipe_Unfreeze() macro.
575 * \note This routine will select the specified pipe, and the pipe will remain selected once the
576 * routine completes regardless of if the pipe configuration succeeds.
578 * \return Boolean true if the configuration is successful, false otherwise
580 bool Pipe_ConfigurePipe(const uint8_t Number
, const uint8_t Type
, const uint8_t Token
, const uint8_t EndpointNumber
,
581 const uint16_t Size
, const uint8_t Banks
);
583 /** Spinloops until the currently selected non-control pipe is ready for the next packed of data
584 * to be read or written to it.
586 * \note This routine should not be called on CONTROL type pipes.
588 * \return A value from the Pipe_WaitUntilReady_ErrorCodes_t enum.
590 uint8_t Pipe_WaitUntilReady(void);
592 /** Writes the given number of bytes to the pipe from the given buffer in little endian,
593 * sending full packets to the device as needed. The last packet filled is not automatically sent;
594 * the user is responsible for manually sending the last written packet to the host via the
595 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
596 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
598 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
599 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
600 * and this function has the Callback parameter ommitted.
602 * \param Buffer Pointer to the source data buffer to read from.
603 * \param Length Number of bytes to read for the currently selected pipe into the buffer.
604 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
606 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
608 uint8_t Pipe_Write_Stream_LE(const void* Buffer
, uint16_t Length
609 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
610 , uint8_t (* const Callback
)(void)
612 ) ATTR_NON_NULL_PTR_ARG(1);
614 /** Writes the given number of bytes to the pipe from the given buffer in big endian,
615 * sending full packets to the device as needed. The last packet filled is not automatically sent;
616 * the user is responsible for manually sending the last written packet to the host via the
617 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
618 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
620 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
621 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
622 * and this function has the Callback parameter ommitted.
624 * \param Buffer Pointer to the source data buffer to read from.
625 * \param Length Number of bytes to read for the currently selected pipe into the buffer.
626 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
628 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
630 uint8_t Pipe_Write_Stream_BE(const void* Buffer
, uint16_t Length
631 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
632 , uint8_t (* const Callback
)(void)
634 ) ATTR_NON_NULL_PTR_ARG(1);
636 /** Reads and discards the given number of bytes from the pipe, discarding fully read packets from the host
637 * as needed. The last packet is not automatically discarded once the remaining bytes has been read; the
638 * user is responsible for manually discarding the last packet from the host via the Pipe_ClearCurrentBank() macro.
639 * Between each USB packet, the given stream callback function is executed repeatedly until the next packet is ready,
640 * allowing for early aborts of stream transfers.
642 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
643 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
644 * and this function has the Callback parameter ommitted.
646 * \param Length Number of bytes to send via the currently selected pipe.
647 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
649 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
651 uint8_t Pipe_Discard_Stream(uint16_t Length
652 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
653 , uint8_t (* const Callback
)(void)
657 /** Reads the given number of bytes from the pipe into the given buffer in little endian,
658 * sending full packets to the device as needed. The last packet filled is not automatically sent;
659 * the user is responsible for manually sending the last written packet to the host via the
660 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
661 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
663 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
664 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
665 * and this function has the Callback parameter ommitted.
667 * \param Buffer Pointer to the source data buffer to write to.
668 * \param Length Number of bytes to read for the currently selected pipe to read from.
669 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
671 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
673 uint8_t Pipe_Read_Stream_LE(void* Buffer
, uint16_t Length
674 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
675 , uint8_t (* const Callback
)(void)
677 ) ATTR_NON_NULL_PTR_ARG(1);
679 /** Reads the given number of bytes from the pipe into the given buffer in big endian,
680 * sending full packets to the device as needed. The last packet filled is not automatically sent;
681 * the user is responsible for manually sending the last written packet to the host via the
682 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
683 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
685 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
686 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
687 * and this function has the Callback parameter ommitted.
689 * \param Buffer Pointer to the source data buffer to write to.
690 * \param Length Number of bytes to read for the currently selected pipe to read from.
691 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
693 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
695 uint8_t Pipe_Read_Stream_BE(void* Buffer
, uint16_t Length
696 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
697 , uint8_t (* const Callback
)(void)
699 ) ATTR_NON_NULL_PTR_ARG(1);
701 /* Function Aliases: */
702 /** Alias for Pipe_Discard_Byte().
704 #define Pipe_Ignore_Byte() Pipe_Discard_Byte()
706 /** Alias for Pipe_Discard_Word().
708 #define Pipe_Ignore_Word() Pipe_Discard_Word()
710 /** Alias for Pipe_Discard_DWord().
712 #define Pipe_Ignore_DWord() Pipe_Discard_DWord()
714 /** Alias for Pipe_Read_Word_LE(). By default USB transfers use little endian format, thus
715 * the command with no endianness specifier indicates little endian mode.
717 #define Pipe_Read_Word() Pipe_Read_Word_LE()
719 /** Alias for Pipe_Write_Word_LE(). By default USB transfers use little endian format, thus
720 * the command with no endianness specifier indicates little endian mode.
722 #define Pipe_Write_Word(Word) Pipe_Write_Word_LE(Word)
724 /** Alias for Pipe_Read_DWord_LE(). By default USB transfers use little endian format, thus
725 * the command with no endianness specifier indicates little endian mode.
727 #define Pipe_Read_DWord() Pipe_Read_DWord_LE()
729 /** Alias for Pipe_Write_DWord_LE(). By default USB transfers use little endian format, thus
730 * the command with no endianness specifier indicates little endian mode.
732 #define Pipe_Write_DWord(DWord) Pipe_Write_DWord_LE(DWord)
734 /** Alias for Pipe_Read_Stream_LE(). By default USB transfers use little endian format, thus
735 * the command with no endianness specifier indicates little endian mode.
737 #if !defined(NO_STREAM_CALLBACKS)
738 #define Pipe_Read_Stream(Buffer, Length, Callback) Pipe_Read_Stream_LE(Buffer, Length, Callback)
740 #define Pipe_Read_Stream(Buffer, Length) Pipe_Read_Stream_LE(Buffer, Length)
743 /** Alias for Pipe_Write_Stream_LE(). By default USB transfers use little endian format, thus
744 * the command with no endianness specifier indicates little endian mode.
746 #if !defined(NO_STREAM_CALLBACKS)
747 #define Pipe_Write_Stream(Buffer, Length, Callback) Pipe_Read_Stream_LE(Buffer, Length, Callback)
749 #define Pipe_Write_Stream(Buffer, Length) Pipe_Read_Stream_LE(Buffer, Length)
752 /* Private Interface - For use in library only: */
753 #if !defined(__DOXYGEN__)
755 #define PIPE_TOKEN_MASK (0x03 << PTOKEN0)
757 #define Pipe_AllocateMemory() MACROS{ UPCFG1X |= (1 << ALLOC); }MACROE
758 #define Pipe_DeallocateMemory() MACROS{ UPCFG1X &= ~(1 << ALLOC); }MACROE
760 /* Function Prototypes: */
761 void Pipe_ClearPipes(void);
763 /* Inline Functions: */
764 static inline uint8_t Pipe_BytesToEPSizeMask(uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYSINLINE
;
765 static inline uint8_t Pipe_BytesToEPSizeMask(uint16_t Bytes
)
768 return (0 << EPSIZE0
);
769 else if (Bytes
<= 16)
770 return (1 << EPSIZE0
);
771 else if (Bytes
<= 32)
772 return (2 << EPSIZE0
);
773 else if (Bytes
<= 64)
774 return (3 << EPSIZE0
);
775 else if (Bytes
<= (8 << 4))
776 return (4 << EPSIZE0
);
778 return (5 << EPSIZE0
);
783 /* Disable C linkage for C++ Compilers: */
784 #if defined(__cplusplus)