3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 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 Pipe definitions for the AVR32 UC3B microcontrollers.
33 * \copydetails Group_PipeManagement_UC3B
35 * \note This file should not be included directly. It is automatically included as needed by the USB driver
36 * dispatch header located in LUFA/Drivers/USB/USB.h.
39 /** \ingroup Group_PipeRW
40 * \defgroup Group_PipeRW_UC3B Pipe Data Reading and Writing (UC3B)
41 * \brief Pipe data read/write definitions for the Atmel AVR32 UC3B architecture.
43 * Functions, macros, variables, enums and types related to data reading and writing from and to pipes.
46 /** \ingroup Group_PipePrimitiveRW
47 * \defgroup Group_PipePrimitiveRW_UC3B Read/Write of Primitive Data Types (UC3B)
48 * \brief Pipe primative data read/write definitions for the Atmel AVR32 UC3B architecture.
50 * Functions, macros, variables, enums and types related to data reading and writing of primitive data types
54 /** \ingroup Group_PipePacketManagement
55 * \defgroup Group_PipePacketManagement_UC3B Pipe Packet Management (UC3B)
56 * \brief Pipe packet management definitions for the Atmel AVR32 UC3B architecture.
58 * Functions, macros, variables, enums and types related to packet management of pipes.
61 /** \ingroup Group_PipeControlReq
62 * \defgroup Group_PipeControlReq_UC3B Pipe Control Request Management (UC3B)
63 * \brief Pipe control request management definitions for the Atmel AVR32 UC3B architecture.
65 * Module for host mode request processing. This module allows for the transmission of standard, class and
66 * vendor control requests to the default control endpoint of an attached device while in host mode.
68 * \see Chapter 9 of the USB 2.0 specification.
71 /** \ingroup Group_PipeManagement
72 * \defgroup Group_PipeManagement_UC3B Pipe Management (UC3B)
73 * \brief Pipe management definitions for the Atmel AVR32 UC3B architecture.
75 * This module contains functions, macros and enums related to pipe management when in USB Host mode. This
76 * module contains the pipe management macros, as well as pipe interrupt and data send/receive functions
77 * for various data types.
82 #ifndef __PIPE_UC3B_H__
83 #define __PIPE_UC3B_H__
86 #include "../../../../Common/Common.h"
87 #include "../USBTask.h"
89 /* Enable C linkage for C++ Compilers: */
90 #if defined(__cplusplus)
94 /* Preprocessor Checks: */
95 #if !defined(__INCLUDE_FROM_USB_DRIVER)
96 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
99 /* Private Interface - For use in library only: */
100 #if !defined(__DOXYGEN__)
101 /* External Variables: */
102 extern volatile uint8_t USB_SelectedPipe
;
105 /* Public Interface - May be used in end-application: */
107 /** \name Pipe Error Flag Masks */
109 /** Mask for \ref Pipe_GetErrorFlags(), indicating that an overflow error occurred in the pipe on the received data. */
110 #define PIPE_ERRORFLAG_OVERFLOW (AVR32_USBB_UPSTA0_OVERFI_MASK << 8)
112 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a CRC error occurred in the pipe on the received data. */
113 #define PIPE_ERRORFLAG_CRC16 AVR32_USBB_UPERR0_CRC16_MASK
115 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware timeout error occurred in the pipe. */
116 #define PIPE_ERRORFLAG_TIMEOUT AVR32_USBB_UPERR0_TIMEOUT_MASK
118 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware PID error occurred in the pipe. */
119 #define PIPE_ERRORFLAG_PID AVR32_USBB_UPERR0_PID_MASK
121 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data PID error occurred in the pipe. */
122 #define PIPE_ERRORFLAG_DATAPID AVR32_USBB_UPERR0_DATAPID_MASK
124 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data toggle error occurred in the pipe. */
125 #define PIPE_ERRORFLAG_DATATGL AVR32_USBB_UPERR0_DATATGL_MASK
128 /** \name Pipe Token Masks */
130 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a SETUP token (for CONTROL type pipes),
131 * which will trigger a control request on the attached device when data is written to the pipe.
133 #define PIPE_TOKEN_SETUP AVR32_USBB_UPCFG0_PTOKEN_SETUP
135 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
136 * indicating that the pipe data will flow from device to host.
138 #define PIPE_TOKEN_IN AVR32_USBB_UPCFG0_PTOKEN_IN
140 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a OUT token (for non-CONTROL type pipes),
141 * indicating that the pipe data will flow from host to device.
143 #define PIPE_TOKEN_OUT AVR32_USBB_UPCFG0_PTOKEN_OUT
146 /** \name Pipe Bank Mode Masks */
148 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
149 * should have one single bank, which requires less USB FIFO memory but results in slower transfers as
150 * only one USB device (the AVR or the attached device) can access the pipe's bank at the one time.
152 #define PIPE_BANK_SINGLE AVR32_USBB_UPCFG0_PBK_SINGLE
154 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
155 * should have two banks, which requires more USB FIFO memory but results in faster transfers as one
156 * USB device (the AVR or the attached device) can access one bank while the other accesses the second
159 #define PIPE_BANK_DOUBLE AVR32_USBB_UPCFG0_PBK_DOUBLE
161 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
162 * should have three banks, which requires more USB FIFO memory but results in faster transfers as one
163 * USB device (the AVR or the attached device) can access one bank while the other accesses the remaining
166 #define PIPE_BANK_TRIPLE AVR32_USBB_UPCFG0_PBK_TRIPLE
169 /** Default size of the default control pipe's bank, until altered by the Endpoint0Size value
170 * in the device descriptor of the attached device.
172 #define PIPE_CONTROLPIPE_DEFAULT_SIZE 64
174 /** Total number of pipes (including the default control pipe at address 0) which may be used in
177 #define PIPE_TOTAL_PIPES 7
179 /** Size in bytes of the largest pipe bank size possible in the device. Not all banks on each AVR
180 * model supports the largest bank size possible on the device; different pipe numbers support
181 * different maximum bank sizes. This value reflects the largest possible bank of any pipe on the
182 * currently selected UC3B AVR model.
184 #define PIPE_MAX_SIZE 256
187 /** Enum for the possible error return codes of the \ref Pipe_WaitUntilReady() function.
189 * \ingroup Group_PipeRW_UC3B
191 enum Pipe_WaitUntilReady_ErrorCodes_t
193 PIPE_READYWAIT_NoError
= 0, /**< Pipe ready for next packet, no error. */
194 PIPE_READYWAIT_PipeStalled
= 1, /**< The device stalled the pipe while waiting. */
195 PIPE_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while waiting. */
196 PIPE_READYWAIT_Timeout
= 3, /**< The device failed to accept or send the next packet
197 * within the software timeout period set by the
198 * \ref USB_STREAM_TIMEOUT_MS macro.
202 /* Inline Functions: */
203 /** Indicates the number of bytes currently stored in the current pipes's selected bank.
205 * \note The return width of this function may differ, depending on the maximum pipe bank size
206 * of the selected AVR model.
208 * \ingroup Group_PipeRW_UC3B
210 * \return Total number of bytes in the currently selected pipe's FIFO buffer.
212 static inline uint16_t Pipe_BytesInPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
213 static inline uint16_t Pipe_BytesInPipe(void)
215 return (&AVR32_USBB
.UPSTA0
)[USB_SelectedPipe
].pbyct
;
218 /** Returns the pipe address of the currently selected pipe. This is typically used to save the
219 * currently selected pipe number so that it can be restored after another pipe has been manipulated.
221 * \return Index of the currently selected pipe.
223 static inline uint8_t Pipe_GetCurrentPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
224 static inline uint8_t Pipe_GetCurrentPipe(void)
226 return USB_SelectedPipe
;
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 * \param[in] PipeNumber Index of the pipe to select.
234 static inline void Pipe_SelectPipe(const uint8_t PipeNumber
) ATTR_ALWAYS_INLINE
;
235 static inline void Pipe_SelectPipe(const uint8_t PipeNumber
)
237 USB_SelectedPipe
= PipeNumber
;
240 /** Resets the desired pipe, including the pipe banks and flags.
242 * \param[in] PipeNumber Index of the pipe to reset.
244 static inline void Pipe_ResetPipe(const uint8_t PipeNumber
) ATTR_ALWAYS_INLINE
;
245 static inline void Pipe_ResetPipe(const uint8_t PipeNumber
)
247 AVR32_USBB
.uprst
|= (AVR32_USBB_PRST0_MASK
<< PipeNumber
);
248 AVR32_USBB
.uprst
&= ~(AVR32_USBB_PRST0_MASK
<< PipeNumber
);
251 /** Enables the currently selected pipe so that data can be sent and received through it to and from
252 * an attached device.
254 * \pre The currently selected pipe must first be configured properly via \ref Pipe_ConfigurePipe().
256 static inline void Pipe_EnablePipe(void) ATTR_ALWAYS_INLINE
;
257 static inline void Pipe_EnablePipe(void)
259 AVR32_USBB
.uprst
|= (AVR32_USBB_PEN0_MASK
<< USB_SelectedPipe
);
262 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
263 * from an attached device.
265 static inline void Pipe_DisablePipe(void) ATTR_ALWAYS_INLINE
;
266 static inline void Pipe_DisablePipe(void)
268 AVR32_USBB
.uprst
&= ~(AVR32_USBB_PEN0_MASK
<< USB_SelectedPipe
);
271 /** Determines if the currently selected pipe is enabled, but not necessarily configured.
273 * \return Boolean \c true if the currently selected pipe is enabled, \c false otherwise.
275 static inline bool Pipe_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
276 static inline bool Pipe_IsEnabled(void)
278 return ((AVR32_USBB
.uprst
& (AVR32_USBB_PEN0_MASK
<< USB_SelectedPipe
)) ?
true : false);
281 /** Gets the current pipe token, indicating the pipe's data direction and type.
283 * \return The current pipe token, as a \c PIPE_TOKEN_* mask.
285 static inline uint8_t Pipe_GetPipeToken(void) ATTR_ALWAYS_INLINE
;
286 static inline uint8_t Pipe_GetPipeToken(void)
288 return (&AVR32_USBB
.UPCFG0
)[USB_SelectedPipe
].ptoken
;
291 /** Sets the token for the currently selected pipe to one of the tokens specified by the \c PIPE_TOKEN_*
292 * masks. This can be used on CONTROL type pipes, to allow for bidirectional transfer of data during
293 * control requests, or on regular pipes to allow for half-duplex bidirectional data transfer to devices
294 * which have two endpoints of opposite direction sharing the same endpoint address within the device.
296 * \param[in] Token New pipe token to set the selected pipe to, as a \c PIPE_TOKEN_* mask.
298 static inline void Pipe_SetPipeToken(const uint8_t Token
) ATTR_ALWAYS_INLINE
;
299 static inline void Pipe_SetPipeToken(const uint8_t Token
)
301 (&AVR32_USBB
.UPCFG0
)[USB_SelectedPipe
].ptoken
= Token
;
304 /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */
305 static inline void Pipe_SetInfiniteINRequests(void) ATTR_ALWAYS_INLINE
;
306 static inline void Pipe_SetInfiniteINRequests(void)
308 (&AVR32_USBB
.UPINRQ0
)[USB_SelectedPipe
].inmode
= true;
311 /** Configures the currently selected pipe to only allow the specified number of IN requests to be
312 * accepted by the pipe before it is automatically frozen.
314 * \param[in] TotalINRequests Total number of IN requests that the pipe may receive before freezing.
316 static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests
) ATTR_ALWAYS_INLINE
;
317 static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests
)
319 (&AVR32_USBB
.UPINRQ0
)[USB_SelectedPipe
].inmode
= false;
320 (&AVR32_USBB
.UPINRQ0
)[USB_SelectedPipe
].inrq
= TotalINRequests
;
323 /** Determines if the currently selected pipe is configured.
325 * \return Boolean \c true if the selected pipe is configured, \c false otherwise.
327 static inline bool Pipe_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
328 static inline bool Pipe_IsConfigured(void)
330 return (&AVR32_USBB
.UPSTA0
)[USB_SelectedPipe
].cfgok
;
333 /** Retrieves the endpoint number of the endpoint within the attached device that the currently selected
336 * \return Endpoint number the currently selected pipe is bound to.
338 static inline uint8_t Pipe_BoundEndpointNumber(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
339 static inline uint8_t Pipe_BoundEndpointNumber(void)
341 return (&AVR32_USBB
.UPCFG0
)[USB_SelectedPipe
].pepnum
;
344 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds.
346 * \param[in] Milliseconds Number of milliseconds between each pipe poll.
348 static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds
) ATTR_ALWAYS_INLINE
;
349 static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds
)
351 (&AVR32_USBB
.UPCFG0
)[USB_SelectedPipe
].intfrq
= Milliseconds
;
354 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
357 * \return Mask whose bits indicate which pipes have interrupted.
359 static inline uint8_t Pipe_GetPipeInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
360 static inline uint8_t Pipe_GetPipeInterrupts(void)
362 return ((AVR32_USBB
.uhint
& (AVR32_USBB_P6INT_MASK
| AVR32_USBB_P5INT_MASK
|
363 AVR32_USBB_P4INT_MASK
| AVR32_USBB_P3INT_MASK
|
364 AVR32_USBB_P2INT_MASK
| AVR32_USBB_P1INT_MASK
|
365 AVR32_USBB_P0INT_MASK
)) >> AVR32_USBB_P0INT_OFFSET
);
368 /** Determines if the specified pipe number has interrupted (valid only for INTERRUPT type
371 * \param[in] PipeNumber Index of the pipe whose interrupt flag should be tested.
373 * \return Boolean \c true if the specified pipe has interrupted, \c false otherwise.
375 static inline bool Pipe_HasPipeInterrupted(const uint8_t PipeNumber
) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
376 static inline bool Pipe_HasPipeInterrupted(const uint8_t PipeNumber
)
378 return ((AVR32_USBB
.uhint
& (AVR32_USBB_P0INTES_MASK
<< USB_SelectedPipe
)) ?
true : false);
381 /** Unfreezes the selected pipe, allowing it to communicate with an attached device. */
382 static inline void Pipe_Unfreeze(void) ATTR_ALWAYS_INLINE
;
383 static inline void Pipe_Unfreeze(void)
385 (&AVR32_USBB
.UPCON0CLR
)[USB_SelectedPipe
].pfreezec
= true;
388 /** Freezes the selected pipe, preventing it from communicating with an attached device. */
389 static inline void Pipe_Freeze(void) ATTR_ALWAYS_INLINE
;
390 static inline void Pipe_Freeze(void)
392 (&AVR32_USBB
.UPCON0SET
)[USB_SelectedPipe
].pfreezes
= true;
395 /** Determines if the currently selected pipe is frozen, and not able to accept data.
397 * \return Boolean \c true if the currently selected pipe is frozen, \c false otherwise.
399 static inline bool Pipe_IsFrozen(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
400 static inline bool Pipe_IsFrozen(void)
402 return (((&AVR32_USBB
.UPCON0
)[USB_SelectedPipe
].pfreeze
) ?
true : false);
405 /** Clears the error flags for the currently selected pipe. */
406 static inline void Pipe_ClearError(void) ATTR_ALWAYS_INLINE
;
407 static inline void Pipe_ClearError(void)
409 (&AVR32_USBB
.uperr0
)[USB_SelectedPipe
] = 0;
410 (&AVR32_USBB
.UPSTA0CLR
)[USB_SelectedPipe
].overfic
= true;
413 /** Determines if the master pipe error flag is set for the currently selected pipe, indicating that
414 * some sort of hardware error has occurred on the pipe.
416 * \see \ref Pipe_GetErrorFlags() macro for information on retrieving the exact error flag.
418 * \return Boolean \c true if an error has occurred on the selected pipe, \c false otherwise.
420 static inline bool Pipe_IsError(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
421 static inline bool Pipe_IsError(void)
423 return (((&AVR32_USBB
.upsta0
)[USB_SelectedPipe
] &
424 (AVR32_USBB_PERRI_MASK
| AVR32_USBB_OVERFI_MASK
)) ?
true : false);
427 /** Gets a mask of the hardware error flags which have occurred on the currently selected pipe. This
428 * value can then be masked against the \c PIPE_ERRORFLAG_* masks to determine what error has occurred.
430 * \return Mask comprising of \c PIPE_ERRORFLAG_* bits indicating what error has occurred on the selected pipe.
432 static inline uint8_t Pipe_GetErrorFlags(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
433 static inline uint8_t Pipe_GetErrorFlags(void)
436 return (((&AVR32_USBB
.uperr0
)[USB_SelectedPipe
] &
437 (PIPE_ERRORFLAG_CRC16
| PIPE_ERRORFLAG_TIMEOUT
|
438 PIPE_ERRORFLAG_PID
| PIPE_ERRORFLAG_DATAPID
|
439 PIPE_ERRORFLAG_DATATGL
)) |
440 (((&AVR32_USBB
.upsta0
)[USB_SelectedPipe
] << 8) &
441 PIPE_ERRORFLAG_OVERFLOW
));
444 /** Retrieves the number of busy banks in the currently selected pipe, which have been queued for
445 * transmission via the \ref Pipe_ClearOUT() command, or are awaiting acknowledgement via the
446 * \ref Pipe_ClearIN() command.
448 * \ingroup Group_PipePacketManagement_UC3B
450 * \return Total number of busy banks in the selected pipe.
452 static inline uint8_t Pipe_GetBusyBanks(void)
454 return (&AVR32_USBB
.UPSTA0
)[USB_SelectedPipe
].nbusybk
;
457 /** Determines if the currently selected pipe may be read from (if data is waiting in the pipe
458 * bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT
459 * direction). This function will return false if an error has occurred in the pipe, or if the pipe
460 * is an IN direction and no packet (or an empty packet) has been received, or if the pipe is an OUT
461 * direction and the pipe bank is full.
463 * \note This function is not valid on CONTROL type pipes.
465 * \ingroup Group_PipePacketManagement_UC3B
467 * \return Boolean \c true if the currently selected pipe may be read from or written to, depending
470 static inline bool Pipe_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
471 static inline bool Pipe_IsReadWriteAllowed(void)
473 return (&AVR32_USBB
.UPSTA0
)[USB_SelectedPipe
].rwall
;
476 /** Determines if a packet has been received on the currently selected IN pipe from the attached device.
478 * \ingroup Group_PipePacketManagement_UC3B
480 * \return Boolean \c true if the current pipe has received an IN packet, \c false otherwise.
482 static inline bool Pipe_IsINReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
483 static inline bool Pipe_IsINReceived(void)
485 return (&AVR32_USBB
.UPSTA0
)[USB_SelectedPipe
].rxini
;
488 /** Determines if the currently selected OUT pipe is ready to send an OUT packet to the attached device.
490 * \ingroup Group_PipePacketManagement_UC3B
492 * \return Boolean \c true if the current pipe is ready for an OUT packet, \c false otherwise.
494 static inline bool Pipe_IsOUTReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
495 static inline bool Pipe_IsOUTReady(void)
497 return (&AVR32_USBB
.UPSTA0
)[USB_SelectedPipe
].txouti
;
500 /** Determines if no SETUP request is currently being sent to the attached device on the selected
503 * \ingroup Group_PipePacketManagement_UC3B
505 * \return Boolean \c true if the current pipe is ready for a SETUP packet, \c false otherwise.
507 static inline bool Pipe_IsSETUPSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
508 static inline bool Pipe_IsSETUPSent(void)
510 return (&AVR32_USBB
.UPSTA0
)[USB_SelectedPipe
].txstpi
;
513 /** Sends the currently selected CONTROL type pipe's contents to the device as a SETUP packet.
515 * \ingroup Group_PipePacketManagement_UC3B
517 static inline void Pipe_ClearSETUP(void) ATTR_ALWAYS_INLINE
;
518 static inline void Pipe_ClearSETUP(void)
520 (&AVR32_USBB
.UPSTA0CLR
)[USB_SelectedPipe
].txstpic
= true;
523 /** Acknowledges the reception of a setup IN request from the attached device on the currently selected
524 * pipe, freeing the bank ready for the next packet.
526 * \ingroup Group_PipePacketManagement_UC3B
528 static inline void Pipe_ClearIN(void) ATTR_ALWAYS_INLINE
;
529 static inline void Pipe_ClearIN(void)
531 (&AVR32_USBB
.UPSTA0CLR
)[USB_SelectedPipe
].rxinic
= true;
532 (&AVR32_USBB
.UPCON0CLR
)[USB_SelectedPipe
].fifoconc
= true;
535 /** Sends the currently selected pipe's contents to the device as an OUT packet on the selected pipe, freeing
536 * the bank ready for the next packet.
538 * \ingroup Group_PipePacketManagement_UC3B
540 static inline void Pipe_ClearOUT(void) ATTR_ALWAYS_INLINE
;
541 static inline void Pipe_ClearOUT(void)
543 (&AVR32_USBB
.UPSTA0CLR
)[USB_SelectedPipe
].txoutic
= true;
544 (&AVR32_USBB
.UPCON0CLR
)[USB_SelectedPipe
].fifoconc
= true;
547 /** Determines if the device sent a NAK (Negative Acknowledge) in response to the last sent packet on
548 * the currently selected pipe. This occurs when the host sends a packet to the device, but the device
549 * is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been
550 * received, it must be cleared using \ref Pipe_ClearNAKReceived() before the previous (or any other) packet
553 * \ingroup Group_PipePacketManagement_UC3B
555 * \return Boolean \c true if an NAK has been received on the current pipe, \c false otherwise.
557 static inline bool Pipe_IsNAKReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
558 static inline bool Pipe_IsNAKReceived(void)
560 return (&AVR32_USBB
.UPSTA0
)[USB_SelectedPipe
].nakedi
;
563 /** Clears the NAK condition on the currently selected pipe.
565 * \ingroup Group_PipePacketManagement_UC3B
567 * \see \ref Pipe_IsNAKReceived() for more details.
569 static inline void Pipe_ClearNAKReceived(void) ATTR_ALWAYS_INLINE
;
570 static inline void Pipe_ClearNAKReceived(void)
572 (&AVR32_USBB
.UPSTA0CLR
)[USB_SelectedPipe
].nakedic
= true;
575 /** Determines if the currently selected pipe has had the STALL condition set by the attached device.
577 * \ingroup Group_PipePacketManagement_UC3B
579 * \return Boolean \c true if the current pipe has been stalled by the attached device, \c false otherwise.
581 static inline bool Pipe_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
582 static inline bool Pipe_IsStalled(void)
584 return (&AVR32_USBB
.UPSTA0
)[USB_SelectedPipe
].rxstalldi
;
587 /** Clears the STALL condition detection flag on the currently selected pipe, but does not clear the
588 * STALL condition itself (this must be done via a ClearFeature control request to the device).
590 * \ingroup Group_PipePacketManagement_UC3B
592 static inline void Pipe_ClearStall(void) ATTR_ALWAYS_INLINE
;
593 static inline void Pipe_ClearStall(void)
595 (&AVR32_USBB
.UPSTA0CLR
)[USB_SelectedPipe
].rxstalldic
= true;
598 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes.
600 * \ingroup Group_PipePrimitiveRW_UC3B
602 * \return Next byte in the currently selected pipe's FIFO buffer.
604 static inline uint8_t Pipe_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
605 static inline uint8_t Pipe_Read_Byte(void)
607 return *((uint8_t*)AVR32_USBB_EP_DATA
);
610 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes.
612 * \ingroup Group_PipePrimitiveRW_UC3B
614 * \param[in] Byte Next byte to write into the the currently selected pipe's FIFO buffer.
616 static inline void Pipe_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
617 static inline void Pipe_Write_Byte(const uint8_t Byte
)
619 *((uint8_t*)AVR32_USBB_EP_DATA
) = Byte
;
622 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes.
624 * \ingroup Group_PipePrimitiveRW_UC3B
626 static inline void Pipe_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
627 static inline void Pipe_Discard_Byte(void)
631 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
634 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
637 * \ingroup Group_PipePrimitiveRW_UC3B
639 * \return Next word in the currently selected pipe's FIFO buffer.
641 static inline uint16_t Pipe_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
642 static inline uint16_t Pipe_Read_Word_LE(void)
650 Data
.Bytes
[0] = *((uint8_t*)AVR32_USBB_EP_DATA
);
651 Data
.Bytes
[1] = *((uint8_t*)AVR32_USBB_EP_DATA
);
656 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
659 * \ingroup Group_PipePrimitiveRW_UC3B
661 * \return Next word in the currently selected pipe's FIFO buffer.
663 static inline uint16_t Pipe_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
664 static inline uint16_t Pipe_Read_Word_BE(void)
672 Data
.Bytes
[1] = *((uint8_t*)AVR32_USBB_EP_DATA
);
673 Data
.Bytes
[0] = *((uint8_t*)AVR32_USBB_EP_DATA
);
678 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
681 * \ingroup Group_PipePrimitiveRW_UC3B
683 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer.
685 static inline void Pipe_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
686 static inline void Pipe_Write_Word_LE(const uint16_t Word
)
688 *((uint8_t*)AVR32_USBB_EP_DATA
) = (Word
& 0xFF);
689 *((uint8_t*)AVR32_USBB_EP_DATA
) = (Word
>> 8);
692 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
695 * \ingroup Group_PipePrimitiveRW_UC3B
697 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer.
699 static inline void Pipe_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
700 static inline void Pipe_Write_Word_BE(const uint16_t Word
)
702 *((uint8_t*)AVR32_USBB_EP_DATA
) = (Word
>> 8);
703 *((uint8_t*)AVR32_USBB_EP_DATA
) = (Word
& 0xFF);
706 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes.
708 * \ingroup Group_PipePrimitiveRW_UC3B
710 static inline void Pipe_Discard_Word(void) ATTR_ALWAYS_INLINE
;
711 static inline void Pipe_Discard_Word(void)
715 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
716 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
719 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
722 * \ingroup Group_PipePrimitiveRW_UC3B
724 * \return Next double word in the currently selected pipe's FIFO buffer.
726 static inline uint32_t Pipe_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
727 static inline uint32_t Pipe_Read_DWord_LE(void)
735 Data
.Bytes
[0] = *((uint8_t*)AVR32_USBB_EP_DATA
);
736 Data
.Bytes
[1] = *((uint8_t*)AVR32_USBB_EP_DATA
);
737 Data
.Bytes
[2] = *((uint8_t*)AVR32_USBB_EP_DATA
);
738 Data
.Bytes
[3] = *((uint8_t*)AVR32_USBB_EP_DATA
);
743 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
746 * \ingroup Group_PipePrimitiveRW_UC3B
748 * \return Next double word in the currently selected pipe's FIFO buffer.
750 static inline uint32_t Pipe_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
751 static inline uint32_t Pipe_Read_DWord_BE(void)
759 Data
.Bytes
[3] = *((uint8_t*)AVR32_USBB_EP_DATA
);
760 Data
.Bytes
[2] = *((uint8_t*)AVR32_USBB_EP_DATA
);
761 Data
.Bytes
[1] = *((uint8_t*)AVR32_USBB_EP_DATA
);
762 Data
.Bytes
[0] = *((uint8_t*)AVR32_USBB_EP_DATA
);
767 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
770 * \ingroup Group_PipePrimitiveRW_UC3B
772 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer.
774 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
775 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
)
777 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
& 0xFF);
778 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 8);
779 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 16);
780 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 24);
783 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
786 * \ingroup Group_PipePrimitiveRW_UC3B
788 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer.
790 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
791 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
)
793 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 24);
794 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 16);
795 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 8);
796 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
& 0xFF);
799 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes.
801 * \ingroup Group_PipePrimitiveRW_UC3B
803 static inline void Pipe_Discard_DWord(void) ATTR_ALWAYS_INLINE
;
804 static inline void Pipe_Discard_DWord(void)
808 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
809 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
810 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
811 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
814 /* External Variables: */
815 /** Global indicating the maximum packet size of the default control pipe located at address
816 * 0 in the device. This value is set to the value indicated in the attached device's device
817 * descriptor once the USB interface is initialized into host mode and a device is attached
820 * \note This variable should be treated as read-only in the user application, and never manually
823 extern uint8_t USB_ControlPipeSize
;
825 /* Function Prototypes: */
826 /** Configures the specified pipe number with the given pipe type, token, target endpoint number in the
827 * attached device, bank size and banking mode.
829 * A newly configured pipe is frozen by default, and must be unfrozen before use via the \ref Pipe_Unfreeze()
830 * before being used. Pipes should be kept frozen unless waiting for data from a device while in IN mode, or
831 * sending data to the device in OUT mode. IN type pipes are also automatically configured to accept infinite
832 * numbers of IN requests without automatic freezing - this can be overridden by a call to
833 * \ref Pipe_SetFiniteINRequests().
835 * \param[in] Number Pipe number to configure. This must be more than 0 and less than \ref PIPE_TOTAL_PIPES.
837 * \param[in] Type Type of pipe to configure, an \c EP_TYPE_* mask. Not all pipe types are available on Low
838 * Speed USB devices - refer to the USB 2.0 specification.
840 * \param[in] Token Pipe data token, either \ref PIPE_TOKEN_SETUP, \ref PIPE_TOKEN_OUT or \ref PIPE_TOKEN_IN.
841 * All pipes (except Control type) are unidirectional - data may only be read from or
842 * written to the pipe bank based on its direction, not both.
844 * \param[in] EndpointNumber Endpoint index within the attached device that the pipe should interface to.
846 * \param[in] Size Size of the pipe's bank, where packets are stored before they are transmitted to
847 * the USB device, or after they have been received from the USB device (depending on
848 * the pipe's data direction). The bank size must indicate the maximum packet size that
849 * the pipe can handle.
851 * \param[in] Banks Number of banks to use for the pipe being configured, a \c PIPE_BANK_* mask. More banks
852 * uses more USB DPRAM, but offers better performance. Isochronous type pipes <b>must</b>
853 * have at least two banks.
855 * \note When the \c ORDERED_EP_CONFIG compile time option is used, Pipes <b>must</b> be configured in ascending order,
856 * or bank corruption will occur.
859 * \note Certain microcontroller model's pipes may have different maximum packet sizes based on the pipe's
860 * index - refer to the chosen microcontroller's datasheet to determine the maximum bank size for each pipe.
863 * \note The default control pipe should not be manually configured by the user application, as it is
864 * automatically configured by the library internally.
867 * \note This routine will automatically select the specified pipe upon success. Upon failure, the pipe which
868 * failed to reconfigure correctly will be selected.
870 * \return Boolean \c true if the configuration succeeded, \c false otherwise.
872 bool Pipe_ConfigurePipe(const uint8_t Number
,
875 const uint8_t EndpointNumber
,
877 const uint8_t Banks
);
879 /** Spin-loops until the currently selected non-control pipe is ready for the next packed of data to be read
880 * or written to it, aborting in the case of an error condition (such as a timeout or device disconnect).
882 * \ingroup Group_PipeRW_UC3B
884 * \return A value from the \ref Pipe_WaitUntilReady_ErrorCodes_t enum.
886 uint8_t Pipe_WaitUntilReady(void);
888 /** Determines if a pipe has been bound to the given device endpoint address. If a pipe which is bound to the given
889 * endpoint is found, it is automatically selected.
891 * \param[in] EndpointAddress Address and direction mask of the endpoint within the attached device to check.
893 * \return Boolean \c true if a pipe bound to the given endpoint address of the specified direction is found,
894 * \c false otherwise.
896 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress
);
898 /* Private Interface - For use in library only: */
899 #if !defined(__DOXYGEN__)
901 #if !defined(ENDPOINT_CONTROLEP)
902 #define ENDPOINT_CONTROLEP 0
905 /* Inline Functions: */
906 static inline uint8_t Pipe_BytesToEPSizeMask(const uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
907 static inline uint8_t Pipe_BytesToEPSizeMask(const uint16_t Bytes
)
910 uint16_t CheckBytes
= 8;
912 while ((CheckBytes
< Bytes
) && (CheckBytes
< PIPE_MAX_SIZE
))
918 return (MaskVal
<< AVR32_USBB_PSIZE_OFFSET
);
921 /* Function Prototypes: */
922 void Pipe_ClearPipes(void);
925 /* Disable C linkage for C++ Compilers: */
926 #if defined(__cplusplus)