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 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
175 * the device. Different USB AVR models support different amounts of pipes, this value reflects
176 * the maximum number of pipes for the currently selected AVR model.
178 #define PIPE_TOTAL_PIPES 7
180 /** Size in bytes of the largest pipe bank size possible in the device. Not all banks on each AVR
181 * model supports the largest bank size possible on the device; different pipe numbers support
182 * different maximum bank sizes. This value reflects the largest possible bank of any pipe on the
183 * currently selected USB AVR model.
185 #define PIPE_MAX_SIZE 256
188 /** Enum for the possible error return codes of the \ref Pipe_WaitUntilReady() function.
190 * \ingroup Group_PipeRW_UC3B
192 enum Pipe_WaitUntilReady_ErrorCodes_t
194 PIPE_READYWAIT_NoError
= 0, /**< Pipe ready for next packet, no error. */
195 PIPE_READYWAIT_PipeStalled
= 1, /**< The device stalled the pipe while waiting. */
196 PIPE_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while waiting. */
197 PIPE_READYWAIT_Timeout
= 3, /**< The device failed to accept or send the next packet
198 * within the software timeout period set by the
199 * \ref USB_STREAM_TIMEOUT_MS macro.
203 /* Inline Functions: */
204 /** Indicates the number of bytes currently stored in the current pipes's selected bank.
206 * \note The return width of this function may differ, depending on the maximum pipe bank size
207 * of the selected AVR model.
209 * \ingroup Group_PipeRW_UC3B
211 * \return Total number of bytes in the currently selected pipe's FIFO buffer.
213 static inline uint16_t Pipe_BytesInPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
214 static inline uint16_t Pipe_BytesInPipe(void)
216 return ((avr32_usbb_upsta0_t
*)AVR32_USBB_UPSTA0
)[USB_SelectedPipe
].pbyct
;
219 /** Returns the pipe address of the currently selected pipe. This is typically used to save the
220 * currently selected pipe number so that it can be restored after another pipe has been manipulated.
222 * \return Index of the currently selected pipe.
224 static inline uint8_t Pipe_GetCurrentPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
225 static inline uint8_t Pipe_GetCurrentPipe(void)
227 return USB_SelectedPipe
;
230 /** Selects the given pipe number. Any pipe operations which do not require the pipe number to be
231 * indicated will operate on the currently selected pipe.
233 * \param[in] PipeNumber Index of the pipe to select.
235 static inline void Pipe_SelectPipe(const uint8_t PipeNumber
) ATTR_ALWAYS_INLINE
;
236 static inline void Pipe_SelectPipe(const uint8_t PipeNumber
)
238 USB_SelectedPipe
= PipeNumber
;
241 /** Resets the desired pipe, including the pipe banks and flags.
243 * \param[in] PipeNumber Index of the pipe to reset.
245 static inline void Pipe_ResetPipe(const uint8_t PipeNumber
) ATTR_ALWAYS_INLINE
;
246 static inline void Pipe_ResetPipe(const uint8_t PipeNumber
)
248 AVR32_USBB
.uprst
|= (AVR32_USBB_PRST0_MASK
<< PipeNumber
);
249 AVR32_USBB
.uprst
&= ~(AVR32_USBB_PRST0_MASK
<< PipeNumber
);
252 /** Enables the currently selected pipe so that data can be sent and received through it to and from
253 * an attached device.
255 * \pre The currently selected pipe must first be configured properly via \ref Pipe_ConfigurePipe().
257 static inline void Pipe_EnablePipe(void) ATTR_ALWAYS_INLINE
;
258 static inline void Pipe_EnablePipe(void)
260 AVR32_USBB
.uprst
|= (AVR32_USBB_PEN0_MASK
<< USB_SelectedPipe
);
263 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
264 * from an attached device.
266 static inline void Pipe_DisablePipe(void) ATTR_ALWAYS_INLINE
;
267 static inline void Pipe_DisablePipe(void)
269 AVR32_USBB
.uprst
&= ~(AVR32_USBB_PEN0_MASK
<< USB_SelectedPipe
);
272 /** Determines if the currently selected pipe is enabled, but not necessarily configured.
274 * \return Boolean \c true if the currently selected pipe is enabled, \c false otherwise.
276 static inline bool Pipe_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
277 static inline bool Pipe_IsEnabled(void)
279 return ((AVR32_USBB
.uprst
& (AVR32_USBB_PEN0_MASK
<< USB_SelectedPipe
)) ?
true : false);
282 /** Gets the current pipe token, indicating the pipe's data direction and type.
284 * \return The current pipe token, as a \c PIPE_TOKEN_* mask.
286 static inline uint8_t Pipe_GetPipeToken(void) ATTR_ALWAYS_INLINE
;
287 static inline uint8_t Pipe_GetPipeToken(void)
289 return ((avr32_usbb_upcfg0_t
*)AVR32_USBB_UPCFG0
)[USB_SelectedPipe
].ptoken
;
292 /** Sets the token for the currently selected pipe to one of the tokens specified by the \c PIPE_TOKEN_*
293 * masks. This can be used on CONTROL type pipes, to allow for bidirectional transfer of data during
294 * control requests, or on regular pipes to allow for half-duplex bidirectional data transfer to devices
295 * which have two endpoints of opposite direction sharing the same endpoint address within the device.
297 * \param[in] Token New pipe token to set the selected pipe to, as a \c PIPE_TOKEN_* mask.
299 static inline void Pipe_SetPipeToken(const uint8_t Token
) ATTR_ALWAYS_INLINE
;
300 static inline void Pipe_SetPipeToken(const uint8_t Token
)
302 ((avr32_usbb_upcfg0_t
*)AVR32_USBB_UPCFG0
)[USB_SelectedPipe
].ptoken
= Token
;
305 /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */
306 static inline void Pipe_SetInfiniteINRequests(void) ATTR_ALWAYS_INLINE
;
307 static inline void Pipe_SetInfiniteINRequests(void)
309 ((avr32_usbb_upinrq0_t
*)AVR32_USBB_UPINRQ0
)[USB_SelectedPipe
].inmode
= true;
312 /** Configures the currently selected pipe to only allow the specified number of IN requests to be
313 * accepted by the pipe before it is automatically frozen.
315 * \param[in] TotalINRequests Total number of IN requests that the pipe may receive before freezing.
317 static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests
) ATTR_ALWAYS_INLINE
;
318 static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests
)
320 ((avr32_usbb_upinrq0_t
*)AVR32_USBB_UPINRQ0
)[USB_SelectedPipe
].inmode
= false;
321 ((avr32_usbb_upinrq0_t
*)AVR32_USBB_UPINRQ0
)[USB_SelectedPipe
].inrq
= TotalINRequests
;
324 /** Determines if the currently selected pipe is configured.
326 * \return Boolean \c true if the selected pipe is configured, \c false otherwise.
328 static inline bool Pipe_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
329 static inline bool Pipe_IsConfigured(void)
331 return ((avr32_usbb_upsta0_t
*)AVR32_USBB_UPSTA0
)[USB_SelectedPipe
].cfgok
;
334 /** Retrieves the endpoint number of the endpoint within the attached device that the currently selected
337 * \return Endpoint number the currently selected pipe is bound to.
339 static inline uint8_t Pipe_BoundEndpointNumber(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
340 static inline uint8_t Pipe_BoundEndpointNumber(void)
342 return ((avr32_usbb_upcfg0_t
*)AVR32_USBB_UPCFG0
)[USB_SelectedPipe
].pepnum
;
345 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds.
347 * \param[in] Milliseconds Number of milliseconds between each pipe poll.
349 static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds
) ATTR_ALWAYS_INLINE
;
350 static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds
)
352 ((avr32_usbb_upcfg0_t
*)AVR32_USBB_UPCFG0
)[USB_SelectedPipe
].intfrq
= Milliseconds
;
355 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
358 * \return Mask whose bits indicate which pipes have interrupted.
360 static inline uint8_t Pipe_GetPipeInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
361 static inline uint8_t Pipe_GetPipeInterrupts(void)
363 return ((AVR32_USBB
.uhint
& (AVR32_USBB_P6INT_MASK
| AVR32_USBB_P5INT_MASK
|
364 AVR32_USBB_P4INT_MASK
| AVR32_USBB_P3INT_MASK
|
365 AVR32_USBB_P2INT_MASK
| AVR32_USBB_P1INT_MASK
|
366 AVR32_USBB_P0INT_MASK
)) >> AVR32_USBB_P0INT_OFFSET
);
369 /** Determines if the specified pipe number has interrupted (valid only for INTERRUPT type
372 * \param[in] PipeNumber Index of the pipe whose interrupt flag should be tested.
374 * \return Boolean \c true if the specified pipe has interrupted, \c false otherwise.
376 static inline bool Pipe_HasPipeInterrupted(const uint8_t PipeNumber
) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
377 static inline bool Pipe_HasPipeInterrupted(const uint8_t PipeNumber
)
379 return ((AVR32_USBB
.uhint
& (AVR32_USBB_P0INTES_MASK
<< USB_SelectedPipe
)) ?
true : false);
382 /** Unfreezes the selected pipe, allowing it to communicate with an attached device. */
383 static inline void Pipe_Unfreeze(void) ATTR_ALWAYS_INLINE
;
384 static inline void Pipe_Unfreeze(void)
386 ((avr32_usbb_upcon0clr_t
*)AVR32_USBB_UPCON0CLR
)[USB_SelectedPipe
].pfreezec
= true;
389 /** Freezes the selected pipe, preventing it from communicating with an attached device. */
390 static inline void Pipe_Freeze(void) ATTR_ALWAYS_INLINE
;
391 static inline void Pipe_Freeze(void)
393 ((avr32_usbb_upcon0set_t
*)AVR32_USBB_UPCON0SET
)[USB_SelectedPipe
].pfreezes
= true;
396 /** Determines if the currently selected pipe is frozen, and not able to accept data.
398 * \return Boolean \c true if the currently selected pipe is frozen, \c false otherwise.
400 static inline bool Pipe_IsFrozen(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
401 static inline bool Pipe_IsFrozen(void)
403 return ((((avr32_usbb_upcon0_t
*)AVR32_USBB_UPCON0
)[USB_SelectedPipe
].pfreeze
) ?
true : false);
406 /** Clears the error flags for the currently selected pipe. */
407 static inline void Pipe_ClearError(void) ATTR_ALWAYS_INLINE
;
408 static inline void Pipe_ClearError(void)
410 ((uint32_t*)AVR32_USBB_UPERR0
)[USB_SelectedPipe
] = 0;
411 ((avr32_usbb_upsta0clr_t
*)AVR32_USBB_UPSTA0CLR
)[USB_SelectedPipe
].overfic
= true;
414 /** Determines if the master pipe error flag is set for the currently selected pipe, indicating that
415 * some sort of hardware error has occurred on the pipe.
417 * \see \ref Pipe_GetErrorFlags() macro for information on retrieving the exact error flag.
419 * \return Boolean \c true if an error has occurred on the selected pipe, \c false otherwise.
421 static inline bool Pipe_IsError(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
422 static inline bool Pipe_IsError(void)
424 return ((((uint32_t*)AVR32_USBB_UPSTA0
)[USB_SelectedPipe
] &
425 (AVR32_USBB_PERRI_MASK
| AVR32_USBB_OVERFI_MASK
)) ?
true : false);
428 /** Gets a mask of the hardware error flags which have occurred on the currently selected pipe. This
429 * value can then be masked against the \c PIPE_ERRORFLAG_* masks to determine what error has occurred.
431 * \return Mask comprising of \c PIPE_ERRORFLAG_* bits indicating what error has occurred on the selected pipe.
433 static inline uint8_t Pipe_GetErrorFlags(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
434 static inline uint8_t Pipe_GetErrorFlags(void)
437 return ((((uint32_t*)AVR32_USBB_UPERR0
)[USB_SelectedPipe
] &
438 (PIPE_ERRORFLAG_CRC16
| PIPE_ERRORFLAG_TIMEOUT
|
439 PIPE_ERRORFLAG_PID
| PIPE_ERRORFLAG_DATAPID
|
440 PIPE_ERRORFLAG_DATATGL
)) |
441 ((((uint32_t*)AVR32_USBB_UPSTA0
)[USB_SelectedPipe
] << 8) &
442 PIPE_ERRORFLAG_OVERFLOW
));
445 /** Retrieves the number of busy banks in the currently selected pipe, which have been queued for
446 * transmission via the \ref Pipe_ClearOUT() command, or are awaiting acknowledgement via the
447 * \ref Pipe_ClearIN() command.
449 * \ingroup Group_PipePacketManagement_UC3B
451 * \return Total number of busy banks in the selected pipe.
453 static inline uint8_t Pipe_GetBusyBanks(void)
455 return ((avr32_usbb_upsta0_t
*)AVR32_USBB_UPSTA0
)[USB_SelectedPipe
].nbusybk
;
458 /** Determines if the currently selected pipe may be read from (if data is waiting in the pipe
459 * bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT
460 * direction). This function will return false if an error has occurred in the pipe, or if the pipe
461 * is an IN direction and no packet (or an empty packet) has been received, or if the pipe is an OUT
462 * direction and the pipe bank is full.
464 * \note This function is not valid on CONTROL type pipes.
466 * \ingroup Group_PipePacketManagement_UC3B
468 * \return Boolean \c true if the currently selected pipe may be read from or written to, depending
471 static inline bool Pipe_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
472 static inline bool Pipe_IsReadWriteAllowed(void)
474 return ((avr32_usbb_upsta0_t
*)AVR32_USBB_UPSTA0
)[USB_SelectedPipe
].rwall
;
477 /** Determines if a packet has been received on the currently selected IN pipe from the attached device.
479 * \ingroup Group_PipePacketManagement_UC3B
481 * \return Boolean \c true if the current pipe has received an IN packet, \c false otherwise.
483 static inline bool Pipe_IsINReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
484 static inline bool Pipe_IsINReceived(void)
486 return ((avr32_usbb_upsta0_t
*)AVR32_USBB_UPSTA0
)[USB_SelectedPipe
].rxini
;
489 /** Determines if the currently selected OUT pipe is ready to send an OUT packet to the attached device.
491 * \ingroup Group_PipePacketManagement_UC3B
493 * \return Boolean \c true if the current pipe is ready for an OUT packet, \c false otherwise.
495 static inline bool Pipe_IsOUTReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
496 static inline bool Pipe_IsOUTReady(void)
498 return ((avr32_usbb_upsta0_t
*)AVR32_USBB_UPSTA0
)[USB_SelectedPipe
].txouti
;
501 /** Determines if no SETUP request is currently being sent to the attached device on the selected
504 * \ingroup Group_PipePacketManagement_UC3B
506 * \return Boolean \c true if the current pipe is ready for a SETUP packet, \c false otherwise.
508 static inline bool Pipe_IsSETUPSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
509 static inline bool Pipe_IsSETUPSent(void)
511 return ((avr32_usbb_upsta0_t
*)AVR32_USBB_UPSTA0
)[USB_SelectedPipe
].txstpi
;
514 /** Sends the currently selected CONTROL type pipe's contents to the device as a SETUP packet.
516 * \ingroup Group_PipePacketManagement_UC3B
518 static inline void Pipe_ClearSETUP(void) ATTR_ALWAYS_INLINE
;
519 static inline void Pipe_ClearSETUP(void)
521 ((avr32_usbb_upsta0clr_t
*)AVR32_USBB_UPSTA0CLR
)[USB_SelectedPipe
].txstpic
= true;
524 /** Acknowledges the reception of a setup IN request from the attached device on the currently selected
525 * pipe, freeing the bank ready for the next packet.
527 * \ingroup Group_PipePacketManagement_UC3B
529 static inline void Pipe_ClearIN(void) ATTR_ALWAYS_INLINE
;
530 static inline void Pipe_ClearIN(void)
532 ((avr32_usbb_upsta0clr_t
*)AVR32_USBB_UPSTA0CLR
)[USB_SelectedPipe
].rxinic
= true;
533 ((avr32_usbb_upcon0clr_t
*)AVR32_USBB_UPCON0CLR
)[USB_SelectedPipe
].fifoconc
= true;
536 /** Sends the currently selected pipe's contents to the device as an OUT packet on the selected pipe, freeing
537 * the bank ready for the next packet.
539 * \ingroup Group_PipePacketManagement_UC3B
541 static inline void Pipe_ClearOUT(void) ATTR_ALWAYS_INLINE
;
542 static inline void Pipe_ClearOUT(void)
544 ((avr32_usbb_upsta0clr_t
*)AVR32_USBB_UPSTA0CLR
)[USB_SelectedPipe
].txoutic
= true;
545 ((avr32_usbb_upcon0clr_t
*)AVR32_USBB_UPCON0CLR
)[USB_SelectedPipe
].fifoconc
= true;
548 /** Determines if the device sent a NAK (Negative Acknowledge) in response to the last sent packet on
549 * the currently selected pipe. This occurs when the host sends a packet to the device, but the device
550 * is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been
551 * received, it must be cleared using \ref Pipe_ClearNAKReceived() before the previous (or any other) packet
554 * \ingroup Group_PipePacketManagement_UC3B
556 * \return Boolean \c true if an NAK has been received on the current pipe, \c false otherwise.
558 static inline bool Pipe_IsNAKReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
559 static inline bool Pipe_IsNAKReceived(void)
561 return ((avr32_usbb_upsta0_t
*)AVR32_USBB_UPSTA0
)[USB_SelectedPipe
].nakedi
;
564 /** Clears the NAK condition on the currently selected pipe.
566 * \ingroup Group_PipePacketManagement_UC3B
568 * \see \ref Pipe_IsNAKReceived() for more details.
570 static inline void Pipe_ClearNAKReceived(void) ATTR_ALWAYS_INLINE
;
571 static inline void Pipe_ClearNAKReceived(void)
573 ((avr32_usbb_upsta0clr_t
*)AVR32_USBB_UPSTA0CLR
)[USB_SelectedPipe
].nakedic
= true;
576 /** Determines if the currently selected pipe has had the STALL condition set by the attached device.
578 * \ingroup Group_PipePacketManagement_UC3B
580 * \return Boolean \c true if the current pipe has been stalled by the attached device, \c false otherwise.
582 static inline bool Pipe_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
583 static inline bool Pipe_IsStalled(void)
585 return ((avr32_usbb_upsta0_t
*)AVR32_USBB_UPSTA0
)[USB_SelectedPipe
].rxstalldi
;
588 /** Clears the STALL condition detection flag on the currently selected pipe, but does not clear the
589 * STALL condition itself (this must be done via a ClearFeature control request to the device).
591 * \ingroup Group_PipePacketManagement_UC3B
593 static inline void Pipe_ClearStall(void) ATTR_ALWAYS_INLINE
;
594 static inline void Pipe_ClearStall(void)
596 ((avr32_usbb_upsta0clr_t
*)AVR32_USBB_UPSTA0CLR
)[USB_SelectedPipe
].rxstalldic
= true;
599 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes.
601 * \ingroup Group_PipePrimitiveRW_UC3B
603 * \return Next byte in the currently selected pipe's FIFO buffer.
605 static inline uint8_t Pipe_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
606 static inline uint8_t Pipe_Read_Byte(void)
608 return *((uint8_t*)AVR32_USBB_EP_DATA
);
611 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes.
613 * \ingroup Group_PipePrimitiveRW_UC3B
615 * \param[in] Byte Next byte to write into the the currently selected pipe's FIFO buffer.
617 static inline void Pipe_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
618 static inline void Pipe_Write_Byte(const uint8_t Byte
)
620 *((uint8_t*)AVR32_USBB_EP_DATA
) = Byte
;
623 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes.
625 * \ingroup Group_PipePrimitiveRW_UC3B
627 static inline void Pipe_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
628 static inline void Pipe_Discard_Byte(void)
632 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
635 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
638 * \ingroup Group_PipePrimitiveRW_UC3B
640 * \return Next word in the currently selected pipe's FIFO buffer.
642 static inline uint16_t Pipe_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
643 static inline uint16_t Pipe_Read_Word_LE(void)
651 Data
.Bytes
[0] = *((uint8_t*)AVR32_USBB_EP_DATA
);
652 Data
.Bytes
[1] = *((uint8_t*)AVR32_USBB_EP_DATA
);
657 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
660 * \ingroup Group_PipePrimitiveRW_UC3B
662 * \return Next word in the currently selected pipe's FIFO buffer.
664 static inline uint16_t Pipe_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
665 static inline uint16_t Pipe_Read_Word_BE(void)
673 Data
.Bytes
[1] = *((uint8_t*)AVR32_USBB_EP_DATA
);
674 Data
.Bytes
[0] = *((uint8_t*)AVR32_USBB_EP_DATA
);
679 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
682 * \ingroup Group_PipePrimitiveRW_UC3B
684 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer.
686 static inline void Pipe_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
687 static inline void Pipe_Write_Word_LE(const uint16_t Word
)
689 *((uint8_t*)AVR32_USBB_EP_DATA
) = (Word
& 0xFF);
690 *((uint8_t*)AVR32_USBB_EP_DATA
) = (Word
>> 8);
693 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
696 * \ingroup Group_PipePrimitiveRW_UC3B
698 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer.
700 static inline void Pipe_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
701 static inline void Pipe_Write_Word_BE(const uint16_t Word
)
703 *((uint8_t*)AVR32_USBB_EP_DATA
) = (Word
>> 8);
704 *((uint8_t*)AVR32_USBB_EP_DATA
) = (Word
& 0xFF);
707 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes.
709 * \ingroup Group_PipePrimitiveRW_UC3B
711 static inline void Pipe_Discard_Word(void) ATTR_ALWAYS_INLINE
;
712 static inline void Pipe_Discard_Word(void)
716 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
717 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
720 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
723 * \ingroup Group_PipePrimitiveRW_UC3B
725 * \return Next double word in the currently selected pipe's FIFO buffer.
727 static inline uint32_t Pipe_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
728 static inline uint32_t Pipe_Read_DWord_LE(void)
736 Data
.Bytes
[0] = *((uint8_t*)AVR32_USBB_EP_DATA
);
737 Data
.Bytes
[1] = *((uint8_t*)AVR32_USBB_EP_DATA
);
738 Data
.Bytes
[2] = *((uint8_t*)AVR32_USBB_EP_DATA
);
739 Data
.Bytes
[3] = *((uint8_t*)AVR32_USBB_EP_DATA
);
744 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
747 * \ingroup Group_PipePrimitiveRW_UC3B
749 * \return Next double word in the currently selected pipe's FIFO buffer.
751 static inline uint32_t Pipe_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
752 static inline uint32_t Pipe_Read_DWord_BE(void)
760 Data
.Bytes
[3] = *((uint8_t*)AVR32_USBB_EP_DATA
);
761 Data
.Bytes
[2] = *((uint8_t*)AVR32_USBB_EP_DATA
);
762 Data
.Bytes
[1] = *((uint8_t*)AVR32_USBB_EP_DATA
);
763 Data
.Bytes
[0] = *((uint8_t*)AVR32_USBB_EP_DATA
);
768 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
771 * \ingroup Group_PipePrimitiveRW_UC3B
773 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer.
775 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
776 static inline void Pipe_Write_DWord_LE(const uint32_t DWord
)
778 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
& 0xFF);
779 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 8);
780 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 16);
781 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 24);
784 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
787 * \ingroup Group_PipePrimitiveRW_UC3B
789 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer.
791 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
792 static inline void Pipe_Write_DWord_BE(const uint32_t DWord
)
794 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 24);
795 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 16);
796 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
>> 8);
797 *((uint8_t*)AVR32_USBB_EP_DATA
) = (DWord
& 0xFF);
800 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes.
802 * \ingroup Group_PipePrimitiveRW_UC3B
804 static inline void Pipe_Discard_DWord(void) ATTR_ALWAYS_INLINE
;
805 static inline void Pipe_Discard_DWord(void)
809 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
810 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
811 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
812 Dummy
= *((uint8_t*)AVR32_USBB_EP_DATA
);
815 /* External Variables: */
816 /** Global indicating the maximum packet size of the default control pipe located at address
817 * 0 in the device. This value is set to the value indicated in the attached device's device
818 * descriptor once the USB interface is initialized into host mode and a device is attached
821 * \note This variable should be treated as read-only in the user application, and never manually
824 extern uint8_t USB_ControlPipeSize
;
826 /* Function Prototypes: */
827 /** Configures the specified pipe number with the given pipe type, token, target endpoint number in the
828 * attached device, bank size and banking mode.
830 * A newly configured pipe is frozen by default, and must be unfrozen before use via the \ref Pipe_Unfreeze()
831 * before being used. Pipes should be kept frozen unless waiting for data from a device while in IN mode, or
832 * sending data to the device in OUT mode. IN type pipes are also automatically configured to accept infinite
833 * numbers of IN requests without automatic freezing - this can be overridden by a call to
834 * \ref Pipe_SetFiniteINRequests().
836 * \param[in] Number Pipe number to configure. This must be more than 0 and less than \ref PIPE_TOTAL_PIPES.
838 * \param[in] Type Type of pipe to configure, an \c EP_TYPE_* mask. Not all pipe types are available on Low
839 * Speed USB devices - refer to the USB 2.0 specification.
841 * \param[in] Token Pipe data token, either \ref PIPE_TOKEN_SETUP, \ref PIPE_TOKEN_OUT or \ref PIPE_TOKEN_IN.
842 * All pipes (except Control type) are unidirectional - data may only be read from or
843 * written to the pipe bank based on its direction, not both.
845 * \param[in] EndpointNumber Endpoint index within the attached device that the pipe should interface to.
847 * \param[in] Size Size of the pipe's bank, where packets are stored before they are transmitted to
848 * the USB device, or after they have been received from the USB device (depending on
849 * the pipe's data direction). The bank size must indicate the maximum packet size that
850 * the pipe can handle.
852 * \param[in] Banks Number of banks to use for the pipe being configured, a \c PIPE_BANK_* mask. More banks
853 * uses more USB DPRAM, but offers better performance. Isochronous type pipes <b>must</b>
854 * have at least two banks.
856 * \note When the \c ORDERED_EP_CONFIG compile time option is used, Pipes <b>must</b> be configured in ascending order,
857 * or bank corruption will occur.
860 * \note Certain models of USB AVR's pipes may have different maximum packet sizes based on the pipe's
861 * index - refer to the chosen USB AVR's datasheet to determine the maximum bank size for each pipe.
864 * \note The default control pipe should not be manually configured by the user application, as it is
865 * automatically configured by the library internally.
868 * \note This routine will automatically select the specified pipe upon success. Upon failure, the pipe which
869 * failed to reconfigure correctly will be selected.
871 * \return Boolean \c true if the configuration succeeded, \c false otherwise.
873 bool Pipe_ConfigurePipe(const uint8_t Number
,
876 const uint8_t EndpointNumber
,
878 const uint8_t Banks
);
880 /** Spin-loops until the currently selected non-control pipe is ready for the next packed of data to be read
881 * or written to it, aborting in the case of an error condition (such as a timeout or device disconnect).
883 * \ingroup Group_PipeRW_UC3B
885 * \return A value from the \ref Pipe_WaitUntilReady_ErrorCodes_t enum.
887 uint8_t Pipe_WaitUntilReady(void);
889 /** Determines if a pipe has been bound to the given device endpoint address. If a pipe which is bound to the given
890 * endpoint is found, it is automatically selected.
892 * \param[in] EndpointAddress Address and direction mask of the endpoint within the attached device to check.
894 * \return Boolean \c true if a pipe bound to the given endpoint address of the specified direction is found,
895 * \c false otherwise.
897 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress
);
899 /* Private Interface - For use in library only: */
900 #if !defined(__DOXYGEN__)
902 #if !defined(ENDPOINT_CONTROLEP)
903 #define ENDPOINT_CONTROLEP 0
906 /* Inline Functions: */
907 static inline uint8_t Pipe_BytesToEPSizeMask(const uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
908 static inline uint8_t Pipe_BytesToEPSizeMask(const uint16_t Bytes
)
911 uint16_t CheckBytes
= 8;
913 while ((CheckBytes
< Bytes
) && (CheckBytes
< PIPE_MAX_SIZE
))
919 return (MaskVal
<< AVR32_USBB_PSIZE_OFFSET
);
922 /* Function Prototypes: */
923 void Pipe_ClearPipes(void);
926 /* Disable C linkage for C++ Compilers: */
927 #if defined(__cplusplus)