5663a76f2945e97c81710ae32c8fa69271f5fe79
[pub/USBasp.git] / LUFA / Drivers / USB / Core / UC3B / Pipe_UC3B.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 * \brief USB Pipe definitions for the AVR32 UC3B microcontrollers.
33 * \copydetails Group_PipeManagement_UC3B
34 *
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.
37 */
38
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.
42 *
43 * Functions, macros, variables, enums and types related to data reading and writing from and to pipes.
44 */
45
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.
49 *
50 * Functions, macros, variables, enums and types related to data reading and writing of primitive data types
51 * from and to pipes.
52 */
53
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.
57 *
58 * Functions, macros, variables, enums and types related to packet management of pipes.
59 */
60
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.
64 *
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.
67 *
68 * \see Chapter 9 of the USB 2.0 specification.
69 */
70
71 /** \ingroup Group_PipeManagement
72 * \defgroup Group_PipeManagement_UC3B Pipe Management (UC3B)
73 * \brief Pipe management definitions for the Atmel AVR32 UC3B architecture.
74 *
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.
78 *
79 * @{
80 */
81
82 #ifndef __PIPE_UC3B_H__
83 #define __PIPE_UC3B_H__
84
85 /* Includes: */
86 #include "../../../../Common/Common.h"
87 #include "../USBTask.h"
88
89 /* Enable C linkage for C++ Compilers: */
90 #if defined(__cplusplus)
91 extern "C" {
92 #endif
93
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.
97 #endif
98
99 /* Public Interface - May be used in end-application: */
100 /* Macros: */
101 /** \name Pipe Error Flag Masks */
102 //@{
103 /** Mask for \ref Pipe_GetErrorFlags(), indicating that an overflow error occurred in the pipe on the received data. */
104 #define PIPE_ERRORFLAG_OVERFLOW ((1 << AVR32_USBB_UPSTA0_OVERFI_OFFSET) << 8)
105
106 /** Mask for \ref Pipe_GetErrorFlags(), indicating that an underflow error occurred in the pipe on the received data. */
107 #define PIPE_ERRORFLAG_UNDERFLOW ((1 << AVR32_USBB_UPSTA0_UNDERFI_OFFSET) << 8)
108
109 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a CRC error occurred in the pipe on the received data. */
110 #define PIPE_ERRORFLAG_CRC16 (1 << AVR32_USBB_UPERR0_CRC16_OFFSET)
111
112 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware timeout error occurred in the pipe. */
113 #define PIPE_ERRORFLAG_TIMEOUT (1 << AVR32_USBB_UPERR0_TIMEOUT_OFFSET)
114
115 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware PID error occurred in the pipe. */
116 #define PIPE_ERRORFLAG_PID (1 << AVR32_USBB_UPERR0_PID_OFFSET)
117
118 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data PID error occurred in the pipe. */
119 #define PIPE_ERRORFLAG_DATAPID (1 << AVR32_USBB_UPERR0_DATAPID_OFFSET)
120
121 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data toggle error occurred in the pipe. */
122 #define PIPE_ERRORFLAG_DATATGL (1 << AVR32_USBB_UPERR0_DATATGL_OFFSET)
123 //@}
124
125 /** \name Pipe Token Masks */
126 //@{
127 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a SETUP token (for CONTROL type pipes),
128 * which will trigger a control request on the attached device when data is written to the pipe.
129 */
130 #define PIPE_TOKEN_SETUP AVR32_USBB_UPCFG0_PTOKEN_SETUP
131
132 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
133 * indicating that the pipe data will flow from device to host.
134 */
135 #define PIPE_TOKEN_IN AVR32_USBB_UPCFG0_PTOKEN_IN
136
137 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a OUT token (for non-CONTROL type pipes),
138 * indicating that the pipe data will flow from host to device.
139 */
140 #define PIPE_TOKEN_OUT AVR32_USBB_UPCFG0_PTOKEN_OUT
141 //@}
142
143 /** \name Pipe Bank Mode Masks */
144 //@{
145 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
146 * should have one single bank, which requires less USB FIFO memory but results in slower transfers as
147 * only one USB device (the AVR or the attached device) can access the pipe's bank at the one time.
148 */
149 #define PIPE_BANK_SINGLE AVR32_USBB_UPCFG0_PBK_SINGLE
150
151 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
152 * should have two banks, which requires more USB FIFO memory but results in faster transfers as one
153 * USB device (the AVR or the attached device) can access one bank while the other accesses the second
154 * bank.
155 */
156 #define PIPE_BANK_DOUBLE AVR32_USBB_UPCFG0_PBK_DOUBLE
157
158 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
159 * should have three banks, which requires more USB FIFO memory but results in faster transfers as one
160 * USB device (the AVR or the attached device) can access one bank while the other accesses the remaining
161 * banks.
162 */
163 #define PIPE_BANK_TRIPLE AVR32_USBB_UPCFG0_PBK_TRIPLE
164 //@}
165
166 /** Pipe address for the default control pipe, which always resides in address 0. This is
167 * defined for convenience to give more readable code when used with the pipe macros.
168 */
169 #define PIPE_CONTROLPIPE 0
170
171 /** Default size of the default control pipe's bank, until altered by the Endpoint0Size value
172 * in the device descriptor of the attached device.
173 */
174 #define PIPE_CONTROLPIPE_DEFAULT_SIZE 64
175
176 /** Pipe number mask, for masking against pipe addresses to retrieve the pipe's numerical address
177 * in the device.
178 */
179 #define PIPE_PIPENUM_MASK 0x07
180
181 /** Total number of pipes (including the default control pipe at address 0) which may be used in
182 * the device. Different USB AVR models support different amounts of pipes, this value reflects
183 * the maximum number of pipes for the currently selected AVR model.
184 */
185 #define PIPE_TOTAL_PIPES 7
186
187 /** Size in bytes of the largest pipe bank size possible in the device. Not all banks on each AVR
188 * model supports the largest bank size possible on the device; different pipe numbers support
189 * different maximum bank sizes. This value reflects the largest possible bank of any pipe on the
190 * currently selected USB AVR model.
191 */
192 #define PIPE_MAX_SIZE 256
193
194 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
195 * numerical address in the attached device.
196 */
197 #define PIPE_EPNUM_MASK 0x0F
198
199 /** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's
200 * direction for comparing with the \c ENDPOINT_DESCRIPTOR_DIR_* masks.
201 */
202 #define PIPE_EPDIR_MASK 0x80
203
204 /* Enums: */
205 /** Enum for the possible error return codes of the \ref Pipe_WaitUntilReady() function.
206 *
207 * \ingroup Group_PipeRW_UC3B
208 */
209 enum Pipe_WaitUntilReady_ErrorCodes_t
210 {
211 PIPE_READYWAIT_NoError = 0, /**< Pipe ready for next packet, no error. */
212 PIPE_READYWAIT_PipeStalled = 1, /**< The device stalled the pipe while waiting. */
213 PIPE_READYWAIT_DeviceDisconnected = 2, /**< Device was disconnected from the host while waiting. */
214 PIPE_READYWAIT_Timeout = 3, /**< The device failed to accept or send the next packet
215 * within the software timeout period set by the
216 * \ref USB_STREAM_TIMEOUT_MS macro.
217 */
218 };
219
220 /* Inline Functions: */
221 /** Indicates the number of bytes currently stored in the current pipes's selected bank.
222 *
223 * \note The return width of this function may differ, depending on the maximum pipe bank size
224 * of the selected AVR model.
225 *
226 * \ingroup Group_PipeRW_UC3B
227 *
228 * \return Total number of bytes in the currently selected pipe's FIFO buffer.
229 */
230 static inline uint16_t Pipe_BytesInPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
231 static inline uint16_t Pipe_BytesInPipe(void)
232 {
233 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].pbyct;
234 }
235
236 /** Returns the pipe address of the currently selected pipe. This is typically used to save the
237 * currently selected pipe number so that it can be restored after another pipe has been manipulated.
238 *
239 * \return Index of the currently selected pipe.
240 */
241 static inline uint8_t Pipe_GetCurrentPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
242 static inline uint8_t Pipe_GetCurrentPipe(void)
243 {
244 return USB_SelectedPipe;
245 }
246
247 /** Selects the given pipe number. Any pipe operations which do not require the pipe number to be
248 * indicated will operate on the currently selected pipe.
249 *
250 * \param[in] PipeNumber Index of the pipe to select.
251 */
252 static inline void Pipe_SelectPipe(const uint8_t PipeNumber) ATTR_ALWAYS_INLINE;
253 static inline void Pipe_SelectPipe(const uint8_t PipeNumber)
254 {
255 USB_SelectedPipe = PipeNumber;
256 }
257
258 /** Resets the desired pipe, including the pipe banks and flags.
259 *
260 * \param[in] PipeNumber Index of the pipe to reset.
261 */
262 static inline void Pipe_ResetPipe(const uint8_t PipeNumber) ATTR_ALWAYS_INLINE;
263 static inline void Pipe_ResetPipe(const uint8_t PipeNumber)
264 {
265 AVR32_USBB.uprst |= (AVR32_USBB_PRST0_MASK << PipeNumber);
266 AVR32_USBB.uprst &= ~(AVR32_USBB_PRST0_MASK << PipeNumber);
267 }
268
269 /** Enables the currently selected pipe so that data can be sent and received through it to and from
270 * an attached device.
271 *
272 * \pre The currently selected pipe must first be configured properly via \ref Pipe_ConfigurePipe().
273 */
274 static inline void Pipe_EnablePipe(void) ATTR_ALWAYS_INLINE;
275 static inline void Pipe_EnablePipe(void)
276 {
277 AVR32_USBB.uprst |= (AVR32_USBB_PEN0_MASK << PipeNumber);
278 }
279
280 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
281 * from an attached device.
282 */
283 static inline void Pipe_DisablePipe(void) ATTR_ALWAYS_INLINE;
284 static inline void Pipe_DisablePipe(void)
285 {
286 AVR32_USBB.uprst &= ~(AVR32_USBB_PEN0_MASK << PipeNumber);
287 }
288
289 /** Determines if the currently selected pipe is enabled, but not necessarily configured.
290 *
291 * \return Boolean \c true if the currently selected pipe is enabled, \c false otherwise.
292 */
293 static inline bool Pipe_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
294 static inline bool Pipe_IsEnabled(void)
295 {
296 return ((AVR32_USBB.uprst & (AVR32_USBB_PEN0_MASK << PipeNumber)) ? true : false);
297 }
298
299 /** Gets the current pipe token, indicating the pipe's data direction and type.
300 *
301 * \return The current pipe token, as a \c PIPE_TOKEN_* mask.
302 */
303 static inline uint8_t Pipe_GetPipeToken(void) ATTR_ALWAYS_INLINE;
304 static inline uint8_t Pipe_GetPipeToken(void)
305 {
306 return ((avr32_usbb_upcfg0_t*)AVR32_USBB_UPCFG0)[USB_SelectedPipe].ptoken;
307 }
308
309 /** Sets the token for the currently selected pipe to one of the tokens specified by the \c PIPE_TOKEN_*
310 * masks. This can be used on CONTROL type pipes, to allow for bidirectional transfer of data during
311 * control requests, or on regular pipes to allow for half-duplex bidirectional data transfer to devices
312 * which have two endpoints of opposite direction sharing the same endpoint address within the device.
313 *
314 * \param[in] Token New pipe token to set the selected pipe to, as a \c PIPE_TOKEN_* mask.
315 */
316 static inline void Pipe_SetPipeToken(const uint8_t Token) ATTR_ALWAYS_INLINE;
317 static inline void Pipe_SetPipeToken(const uint8_t Token)
318 {
319 ((avr32_usbb_upcfg0_t*)AVR32_USBB_UPCFG0)[USB_SelectedPipe].ptoken = Token;
320 }
321
322 /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */
323 static inline void Pipe_SetInfiniteINRequests(void) ATTR_ALWAYS_INLINE;
324 static inline void Pipe_SetInfiniteINRequests(void)
325 {
326 ((avr32_usbb_upinrq0_t*)AVR32_USBB_UPINRQ0)[USB_SelectedPipe].inmode = true;
327 }
328
329 /** Configures the currently selected pipe to only allow the specified number of IN requests to be
330 * accepted by the pipe before it is automatically frozen.
331 *
332 * \param[in] TotalINRequests Total number of IN requests that the pipe may receive before freezing.
333 */
334 static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests) ATTR_ALWAYS_INLINE;
335 static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests)
336 {
337 ((avr32_usbb_upinrq0_t*)AVR32_USBB_UPINRQ0)[USB_SelectedPipe].inmode = false;
338 ((avr32_usbb_upinrq0_t*)AVR32_USBB_UPINRQ0)[USB_SelectedPipe].inrq = TotalINRequests;
339 }
340
341 /** Determines if the currently selected pipe is configured.
342 *
343 * \return Boolean \c true if the selected pipe is configured, \c false otherwise.
344 */
345 static inline bool Pipe_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
346 static inline bool Pipe_IsConfigured(void)
347 {
348 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].cfgok;
349 }
350
351 /** Retrieves the endpoint number of the endpoint within the attached device that the currently selected
352 * pipe is bound to.
353 *
354 * \return Endpoint number the currently selected pipe is bound to.
355 */
356 static inline uint8_t Pipe_BoundEndpointNumber(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
357 static inline uint8_t Pipe_BoundEndpointNumber(void)
358 {
359 return ((avr32_usbb_upcfg0_t*)AVR32_USBB_UPCFG0)[USB_SelectedPipe].pepnum;
360 }
361
362 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds.
363 *
364 * \param[in] Milliseconds Number of milliseconds between each pipe poll.
365 */
366 static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds) ATTR_ALWAYS_INLINE;
367 static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds)
368 {
369 ((avr32_usbb_upcfg0_t*)AVR32_USBB_UPCFG0)[USB_SelectedPipe].intfrq = Milliseconds;
370 }
371
372 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
373 * be serviced.
374 *
375 * \return Mask whose bits indicate which pipes have interrupted.
376 */
377 static inline uint8_t Pipe_GetPipeInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
378 static inline uint8_t Pipe_GetPipeInterrupts(void)
379 {
380 return ((AVR32_USBB.uhint & (AVR32_USBB_P6INT_MASK | AVR32_USBB_P5INT_MASK |
381 AVR32_USBB_P4INT_MASK | AVR32_USBB_P3INT_MASK |
382 AVR32_USBB_P2INT_MASK | AVR32_USBB_P1INT_MASK |
383 AVR32_USBB_P0INT_MASK)) >> AVR32_USBB_P0INT_OFFSET);
384 }
385
386 /** Determines if the specified pipe number has interrupted (valid only for INTERRUPT type
387 * pipes).
388 *
389 * \param[in] PipeNumber Index of the pipe whose interrupt flag should be tested.
390 *
391 * \return Boolean \c true if the specified pipe has interrupted, \c false otherwise.
392 */
393 static inline bool Pipe_HasPipeInterrupted(const uint8_t PipeNumber) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
394 static inline bool Pipe_HasPipeInterrupted(const uint8_t PipeNumber)
395 {
396 return ((AVR32_USBB.uhint & (AVR32_USBB_P0INTES_MASK << USB_SelectedPipe)) ? true : false);
397 }
398
399 /** Unfreezes the selected pipe, allowing it to communicate with an attached device. */
400 static inline void Pipe_Unfreeze(void) ATTR_ALWAYS_INLINE;
401 static inline void Pipe_Unfreeze(void)
402 {
403 ((avr32_usbb_upcon0clr_t*)AVR32_USBB_UPCON0CLR)[USB_SelectedPipe].pfreezec = true;
404 }
405
406 /** Freezes the selected pipe, preventing it from communicating with an attached device. */
407 static inline void Pipe_Freeze(void) ATTR_ALWAYS_INLINE;
408 static inline void Pipe_Freeze(void)
409 {
410 ((avr32_usbb_upcon0set_t*)AVR32_USBB_UPCON0SET)[USB_SelectedPipe].pfreezes = true;
411 }
412
413 /** Determines if the currently selected pipe is frozen, and not able to accept data.
414 *
415 * \return Boolean \c true if the currently selected pipe is frozen, \c false otherwise.
416 */
417 static inline bool Pipe_IsFrozen(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
418 static inline bool Pipe_IsFrozen(void)
419 {
420 return ((((avr32_usbb_upcon0_t*)AVR32_USBB_UPCON0)[USB_SelectedPipe].pfreeze) ? true : false);
421 }
422
423 /** Clears the error flags for the currently selected pipe. */
424 static inline void Pipe_ClearError(void) ATTR_ALWAYS_INLINE;
425 static inline void Pipe_ClearError(void)
426 {
427 ((uint32_t*)AVR32_USBB_UPERR0)[USB_SelectedPipe] = 0;
428 }
429
430 /** Determines if the master pipe error flag is set for the currently selected pipe, indicating that
431 * some sort of hardware error has occurred on the pipe.
432 *
433 * \see \ref Pipe_GetErrorFlags() macro for information on retrieving the exact error flag.
434 *
435 * \return Boolean \c true if an error has occurred on the selected pipe, \c false otherwise.
436 */
437 static inline bool Pipe_IsError(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
438 static inline bool Pipe_IsError(void)
439 {
440 return ((((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].perri) ? true : false);
441 }
442
443 /** Gets a mask of the hardware error flags which have occurred on the currently selected pipe. This
444 * value can then be masked against the \c PIPE_ERRORFLAG_* masks to determine what error has occurred.
445 *
446 * \return Mask comprising of \c PIPE_ERRORFLAG_* bits indicating what error has occurred on the selected pipe.
447 */
448 static inline uint8_t Pipe_GetErrorFlags(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
449 static inline uint8_t Pipe_GetErrorFlags(void)
450 {
451 return ((UPERRX & (PIPE_ERRORFLAG_CRC16 | PIPE_ERRORFLAG_TIMEOUT |
452 PIPE_ERRORFLAG_PID | PIPE_ERRORFLAG_DATAPID |
453 PIPE_ERRORFLAG_DATATGL)) |
454 (UPSTAX & (PIPE_ERRORFLAG_OVERFLOW | PIPE_ERRORFLAG_UNDERFLOW)));
455 }
456
457 /** Retrieves the number of busy banks in the currently selected pipe, which have been queued for
458 * transmission via the \ref Pipe_ClearOUT() command, or are awaiting acknowledgement via the
459 * \ref Pipe_ClearIN() command.
460 *
461 * \ingroup Group_PipePacketManagement_UC3B
462 *
463 * \return Total number of busy banks in the selected pipe.
464 */
465 static inline uint8_t Pipe_GetBusyBanks(void)
466 {
467 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].nbusybk;
468 }
469
470 /** Determines if the currently selected pipe may be read from (if data is waiting in the pipe
471 * bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT
472 * direction). This function will return false if an error has occurred in the pipe, or if the pipe
473 * is an IN direction and no packet (or an empty packet) has been received, or if the pipe is an OUT
474 * direction and the pipe bank is full.
475 *
476 * \note This function is not valid on CONTROL type pipes.
477 *
478 * \ingroup Group_PipePacketManagement_UC3B
479 *
480 * \return Boolean \c true if the currently selected pipe may be read from or written to, depending
481 * on its direction.
482 */
483 static inline bool Pipe_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
484 static inline bool Pipe_IsReadWriteAllowed(void)
485 {
486 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].rwall;
487 }
488
489 /** Determines if a packet has been received on the currently selected IN pipe from the attached device.
490 *
491 * \ingroup Group_PipePacketManagement_UC3B
492 *
493 * \return Boolean \c true if the current pipe has received an IN packet, \c false otherwise.
494 */
495 static inline bool Pipe_IsINReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
496 static inline bool Pipe_IsINReceived(void)
497 {
498 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].rxini;
499 }
500
501 /** Determines if the currently selected OUT pipe is ready to send an OUT packet to the attached device.
502 *
503 * \ingroup Group_PipePacketManagement_UC3B
504 *
505 * \return Boolean \c true if the current pipe is ready for an OUT packet, \c false otherwise.
506 */
507 static inline bool Pipe_IsOUTReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
508 static inline bool Pipe_IsOUTReady(void)
509 {
510 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].rxouti;
511 }
512
513 /** Determines if no SETUP request is currently being sent to the attached device on the selected
514 * CONTROL type pipe.
515 *
516 * \ingroup Group_PipePacketManagement_UC3B
517 *
518 * \return Boolean \c true if the current pipe is ready for a SETUP packet, \c false otherwise.
519 */
520 static inline bool Pipe_IsSETUPSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
521 static inline bool Pipe_IsSETUPSent(void)
522 {
523 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].txstpi;
524 }
525
526 /** Sends the currently selected CONTROL type pipe's contents to the device as a SETUP packet.
527 *
528 * \ingroup Group_PipePacketManagement_UC3B
529 */
530 static inline void Pipe_ClearSETUP(void) ATTR_ALWAYS_INLINE;
531 static inline void Pipe_ClearSETUP(void)
532 {
533 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].txstpic = true;
534 }
535
536 /** Acknowledges the reception of a setup IN request from the attached device on the currently selected
537 * pipe, freeing the bank ready for the next packet.
538 *
539 * \ingroup Group_PipePacketManagement_UC3B
540 */
541 static inline void Pipe_ClearIN(void) ATTR_ALWAYS_INLINE;
542 static inline void Pipe_ClearIN(void)
543 {
544 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].rxinic = true;
545 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].fifoconc = true;
546 }
547
548 /** Sends the currently selected pipe's contents to the device as an OUT packet on the selected pipe, freeing
549 * the bank ready for the next packet.
550 *
551 * \ingroup Group_PipePacketManagement_UC3B
552 */
553 static inline void Pipe_ClearOUT(void) ATTR_ALWAYS_INLINE;
554 static inline void Pipe_ClearOUT(void)
555 {
556 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].txoutic = true;
557 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].fifoconc = true;
558 }
559
560 /** Determines if the device sent a NAK (Negative Acknowledge) in response to the last sent packet on
561 * the currently selected pipe. This occurs when the host sends a packet to the device, but the device
562 * is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been
563 * received, it must be cleared using \ref Pipe_ClearNAKReceived() before the previous (or any other) packet
564 * can be re-sent.
565 *
566 * \ingroup Group_PipePacketManagement_UC3B
567 *
568 * \return Boolean \c true if an NAK has been received on the current pipe, \c false otherwise.
569 */
570 static inline bool Pipe_IsNAKReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
571 static inline bool Pipe_IsNAKReceived(void)
572 {
573 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].nakedi;
574 }
575
576 /** Clears the NAK condition on the currently selected pipe.
577 *
578 * \ingroup Group_PipePacketManagement_UC3B
579 *
580 * \see \ref Pipe_IsNAKReceived() for more details.
581 */
582 static inline void Pipe_ClearNAKReceived(void) ATTR_ALWAYS_INLINE;
583 static inline void Pipe_ClearNAKReceived(void)
584 {
585 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].nakedic = true;
586 }
587
588 /** Determines if the currently selected pipe has had the STALL condition set by the attached device.
589 *
590 * \ingroup Group_PipePacketManagement_UC3B
591 *
592 * \return Boolean \c true if the current pipe has been stalled by the attached device, \c false otherwise.
593 */
594 static inline bool Pipe_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
595 static inline bool Pipe_IsStalled(void)
596 {
597 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].rxstalledi;
598 }
599
600 /** Clears the STALL condition detection flag on the currently selected pipe, but does not clear the
601 * STALL condition itself (this must be done via a ClearFeature control request to the device).
602 *
603 * \ingroup Group_PipePacketManagement_UC3B
604 */
605 static inline void Pipe_ClearStall(void) ATTR_ALWAYS_INLINE;
606 static inline void Pipe_ClearStall(void)
607 {
608 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].rxstalledic = true;
609 }
610
611 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes.
612 *
613 * \ingroup Group_PipePrimitiveRW_UC3B
614 *
615 * \return Next byte in the currently selected pipe's FIFO buffer.
616 */
617 static inline uint8_t Pipe_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
618 static inline uint8_t Pipe_Read_Byte(void)
619 {
620 return *((uint8_t*)AVR32_USBB_EP_DATA);
621 }
622
623 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes.
624 *
625 * \ingroup Group_PipePrimitiveRW_UC3B
626 *
627 * \param[in] Byte Next byte to write into the the currently selected pipe's FIFO buffer.
628 */
629 static inline void Pipe_Write_Byte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
630 static inline void Pipe_Write_Byte(const uint8_t Byte)
631 {
632 *((uint8_t*)AVR32_USBB_EP_DATA) = Byte;
633 }
634
635 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes.
636 *
637 * \ingroup Group_PipePrimitiveRW_UC3B
638 */
639 static inline void Pipe_Discard_Byte(void) ATTR_ALWAYS_INLINE;
640 static inline void Pipe_Discard_Byte(void)
641 {
642 uint8_t Dummy;
643
644 Dummy = *((uint8_t*)AVR32_USBB_EP_DATA);
645 }
646
647 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
648 * direction pipes.
649 *
650 * \ingroup Group_PipePrimitiveRW_UC3B
651 *
652 * \return Next word in the currently selected pipe's FIFO buffer.
653 */
654 static inline uint16_t Pipe_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
655 static inline uint16_t Pipe_Read_Word_LE(void)
656 {
657 union
658 {
659 uint16_t Word;
660 uint8_t Bytes[2];
661 } Data;
662
663 Data.Bytes[0] = *((uint8_t*)AVR32_USBB_EP_DATA);
664 Data.Bytes[1] = *((uint8_t*)AVR32_USBB_EP_DATA);
665
666 return Data.Word;
667 }
668
669 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
670 * direction pipes.
671 *
672 * \ingroup Group_PipePrimitiveRW_UC3B
673 *
674 * \return Next word in the currently selected pipe's FIFO buffer.
675 */
676 static inline uint16_t Pipe_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
677 static inline uint16_t Pipe_Read_Word_BE(void)
678 {
679 union
680 {
681 uint16_t Word;
682 uint8_t Bytes[2];
683 } Data;
684
685 Data.Bytes[1] = *((uint8_t*)AVR32_USBB_EP_DATA);
686 Data.Bytes[0] = *((uint8_t*)AVR32_USBB_EP_DATA);
687
688 return Data.Word;
689 }
690
691 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
692 * direction pipes.
693 *
694 * \ingroup Group_PipePrimitiveRW_UC3B
695 *
696 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer.
697 */
698 static inline void Pipe_Write_Word_LE(const uint16_t Word) ATTR_ALWAYS_INLINE;
699 static inline void Pipe_Write_Word_LE(const uint16_t Word)
700 {
701 *((uint8_t*)AVR32_USBB_EP_DATA) = (Word & 0xFF);
702 *((uint8_t*)AVR32_USBB_EP_DATA) = (Word >> 8);
703 }
704
705 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
706 * direction pipes.
707 *
708 * \ingroup Group_PipePrimitiveRW_UC3B
709 *
710 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer.
711 */
712 static inline void Pipe_Write_Word_BE(const uint16_t Word) ATTR_ALWAYS_INLINE;
713 static inline void Pipe_Write_Word_BE(const uint16_t Word)
714 {
715 *((uint8_t*)AVR32_USBB_EP_DATA) = (Word >> 8);
716 *((uint8_t*)AVR32_USBB_EP_DATA) = (Word & 0xFF);
717 }
718
719 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes.
720 *
721 * \ingroup Group_PipePrimitiveRW_UC3B
722 */
723 static inline void Pipe_Discard_Word(void) ATTR_ALWAYS_INLINE;
724 static inline void Pipe_Discard_Word(void)
725 {
726 uint8_t Dummy;
727
728 Dummy = *((uint8_t*)AVR32_USBB_EP_DATA);
729 Dummy = *((uint8_t*)AVR32_USBB_EP_DATA);
730 }
731
732 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
733 * direction pipes.
734 *
735 * \ingroup Group_PipePrimitiveRW_UC3B
736 *
737 * \return Next double word in the currently selected pipe's FIFO buffer.
738 */
739 static inline uint32_t Pipe_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
740 static inline uint32_t Pipe_Read_DWord_LE(void)
741 {
742 union
743 {
744 uint32_t DWord;
745 uint8_t Bytes[4];
746 } Data;
747
748 Data.Bytes[0] = *((uint8_t*)AVR32_USBB_EP_DATA);
749 Data.Bytes[1] = *((uint8_t*)AVR32_USBB_EP_DATA);
750 Data.Bytes[2] = *((uint8_t*)AVR32_USBB_EP_DATA);
751 Data.Bytes[3] = *((uint8_t*)AVR32_USBB_EP_DATA);
752
753 return Data.DWord;
754 }
755
756 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
757 * direction pipes.
758 *
759 * \ingroup Group_PipePrimitiveRW_UC3B
760 *
761 * \return Next double word in the currently selected pipe's FIFO buffer.
762 */
763 static inline uint32_t Pipe_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
764 static inline uint32_t Pipe_Read_DWord_BE(void)
765 {
766 union
767 {
768 uint32_t DWord;
769 uint8_t Bytes[4];
770 } Data;
771
772 Data.Bytes[3] = *((uint8_t*)AVR32_USBB_EP_DATA);
773 Data.Bytes[2] = *((uint8_t*)AVR32_USBB_EP_DATA);
774 Data.Bytes[1] = *((uint8_t*)AVR32_USBB_EP_DATA);
775 Data.Bytes[0] = *((uint8_t*)AVR32_USBB_EP_DATA);
776
777 return Data.DWord;
778 }
779
780 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
781 * direction pipes.
782 *
783 * \ingroup Group_PipePrimitiveRW_UC3B
784 *
785 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer.
786 */
787 static inline void Pipe_Write_DWord_LE(const uint32_t DWord) ATTR_ALWAYS_INLINE;
788 static inline void Pipe_Write_DWord_LE(const uint32_t DWord)
789 {
790 *((uint8_t*)AVR32_USBB_EP_DATA) = (DWord & 0xFF);
791 *((uint8_t*)AVR32_USBB_EP_DATA) = (DWord >> 8);
792 *((uint8_t*)AVR32_USBB_EP_DATA) = (DWord >> 16);
793 *((uint8_t*)AVR32_USBB_EP_DATA) = (DWord >> 24);
794 }
795
796 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
797 * direction pipes.
798 *
799 * \ingroup Group_PipePrimitiveRW_UC3B
800 *
801 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer.
802 */
803 static inline void Pipe_Write_DWord_BE(const uint32_t DWord) ATTR_ALWAYS_INLINE;
804 static inline void Pipe_Write_DWord_BE(const uint32_t DWord)
805 {
806 *((uint8_t*)AVR32_USBB_EP_DATA) = (DWord >> 24);
807 *((uint8_t*)AVR32_USBB_EP_DATA) = (DWord >> 16);
808 *((uint8_t*)AVR32_USBB_EP_DATA) = (DWord >> 8);
809 *((uint8_t*)AVR32_USBB_EP_DATA) = (DWord & 0xFF);
810 }
811
812 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes.
813 *
814 * \ingroup Group_PipePrimitiveRW_UC3B
815 */
816 static inline void Pipe_Discard_DWord(void) ATTR_ALWAYS_INLINE;
817 static inline void Pipe_Discard_DWord(void)
818 {
819 uint8_t Dummy;
820
821 Dummy = *((uint8_t*)AVR32_USBB_EP_DATA);
822 Dummy = *((uint8_t*)AVR32_USBB_EP_DATA);
823 Dummy = *((uint8_t*)AVR32_USBB_EP_DATA);
824 Dummy = *((uint8_t*)AVR32_USBB_EP_DATA);
825 }
826
827 /* External Variables: */
828 /** Global indicating the maximum packet size of the default control pipe located at address
829 * 0 in the device. This value is set to the value indicated in the attached device's device
830 * descriptor once the USB interface is initialized into host mode and a device is attached
831 * to the USB bus.
832 *
833 * \note This variable should be treated as read-only in the user application, and never manually
834 * changed in value.
835 */
836 extern uint8_t USB_ControlPipeSize;
837
838 /* Function Prototypes: */
839 /** Configures the specified pipe number with the given pipe type, token, target endpoint number in the
840 * attached device, bank size and banking mode.
841 *
842 * A newly configured pipe is frozen by default, and must be unfrozen before use via the \ref Pipe_Unfreeze()
843 * before being used. Pipes should be kept frozen unless waiting for data from a device while in IN mode, or
844 * sending data to the device in OUT mode. IN type pipes are also automatically configured to accept infinite
845 * numbers of IN requests without automatic freezing - this can be overridden by a call to
846 * \ref Pipe_SetFiniteINRequests().
847 *
848 * \param[in] Number Pipe number to configure. This must be more than 0 and less than \ref PIPE_TOTAL_PIPES.
849 *
850 * \param[in] Type Type of pipe to configure, an \c EP_TYPE_* mask. Not all pipe types are available on Low
851 * Speed USB devices - refer to the USB 2.0 specification.
852 *
853 * \param[in] Token Pipe data token, either \ref PIPE_TOKEN_SETUP, \ref PIPE_TOKEN_OUT or \ref PIPE_TOKEN_IN.
854 * All pipes (except Control type) are unidirectional - data may only be read from or
855 * written to the pipe bank based on its direction, not both.
856 *
857 * \param[in] EndpointNumber Endpoint index within the attached device that the pipe should interface to.
858 *
859 * \param[in] Size Size of the pipe's bank, where packets are stored before they are transmitted to
860 * the USB device, or after they have been received from the USB device (depending on
861 * the pipe's data direction). The bank size must indicate the maximum packet size that
862 * the pipe can handle.
863 *
864 * \param[in] Banks Number of banks to use for the pipe being configured, a \c PIPE_BANK_* mask. More banks
865 * uses more USB DPRAM, but offers better performance. Isochronous type pipes <b>must</b>
866 * have at least two banks.
867 *
868 * \note When the \c ORDERED_EP_CONFIG compile time option is used, Pipes <b>must</b> be configured in ascending order,
869 * or bank corruption will occur.
870 * \n\n
871 *
872 * \note Certain models of USB AVR's pipes may have different maximum packet sizes based on the pipe's
873 * index - refer to the chosen USB AVR's datasheet to determine the maximum bank size for each pipe.
874 * \n\n
875 *
876 * \note The default control pipe should not be manually configured by the user application, as it is
877 * automatically configured by the library internally.
878 * \n\n
879 *
880 * \note This routine will automatically select the specified pipe upon success. Upon failure, the pipe which
881 * failed to reconfigure correctly will be selected.
882 *
883 * \return Boolean \c true if the configuration succeeded, \c false otherwise.
884 */
885 bool Pipe_ConfigurePipe(const uint8_t Number,
886 const uint8_t Type,
887 const uint8_t Token,
888 const uint8_t EndpointNumber,
889 const uint16_t Size,
890 const uint8_t Banks);
891
892 /** Spin-loops until the currently selected non-control pipe is ready for the next packed of data to be read
893 * or written to it, aborting in the case of an error condition (such as a timeout or device disconnect).
894 *
895 * \ingroup Group_PipeRW_UC3B
896 *
897 * \return A value from the \ref Pipe_WaitUntilReady_ErrorCodes_t enum.
898 */
899 uint8_t Pipe_WaitUntilReady(void);
900
901 /** Determines if a pipe has been bound to the given device endpoint address. If a pipe which is bound to the given
902 * endpoint is found, it is automatically selected.
903 *
904 * \param[in] EndpointAddress Address and direction mask of the endpoint within the attached device to check.
905 *
906 * \return Boolean \c true if a pipe bound to the given endpoint address of the specified direction is found,
907 * \c false otherwise.
908 */
909 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress);
910
911 /* Private Interface - For use in library only: */
912 #if !defined(__DOXYGEN__)
913 /* Macros: */
914 #if !defined(ENDPOINT_CONTROLEP)
915 #define ENDPOINT_CONTROLEP 0
916 #endif
917
918 /* Inline Functions: */
919 static inline uint8_t Pipe_BytesToEPSizeMask(const uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE;
920 static inline uint8_t Pipe_BytesToEPSizeMask(const uint16_t Bytes)
921 {
922 uint8_t MaskVal = 0;
923 uint16_t CheckBytes = 8;
924
925 while ((CheckBytes < Bytes) && (CheckBytes < PIPE_MAX_SIZE))
926 {
927 MaskVal++;
928 CheckBytes <<= 1;
929 }
930
931 return (MaskVal << AVR32_USBB_PSIZE_OFFSET);
932 }
933
934 /* Function Prototypes: */
935 void Pipe_ClearPipes(void);
936
937 /* External Variables: */
938 extern uint8_t USB_SelectedPipe;
939 #endif
940
941 /* Disable C linkage for C++ Compilers: */
942 #if defined(__cplusplus)
943 }
944 #endif
945
946 #endif
947
948 /** @} */
949