1d7a002e4f3f101bdae58de446993f858eb89132
[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 /* Private Interface - For use in library only: */
100 #if !defined(__DOXYGEN__)
101 /* External Variables: */
102 extern uint8_t USB_SelectedPipe;
103 #endif
104
105 /* Public Interface - May be used in end-application: */
106 /* Macros: */
107 /** \name Pipe Error Flag Masks */
108 //@{
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)
111
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
114
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
117
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
120
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
123
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
126 //@}
127
128 /** \name Pipe Token Masks */
129 //@{
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.
132 */
133 #define PIPE_TOKEN_SETUP AVR32_USBB_UPCFG0_PTOKEN_SETUP
134
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.
137 */
138 #define PIPE_TOKEN_IN AVR32_USBB_UPCFG0_PTOKEN_IN
139
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.
142 */
143 #define PIPE_TOKEN_OUT AVR32_USBB_UPCFG0_PTOKEN_OUT
144 //@}
145
146 /** \name Pipe Bank Mode Masks */
147 //@{
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.
151 */
152 #define PIPE_BANK_SINGLE AVR32_USBB_UPCFG0_PBK_SINGLE
153
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
157 * bank.
158 */
159 #define PIPE_BANK_DOUBLE AVR32_USBB_UPCFG0_PBK_DOUBLE
160
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
164 * banks.
165 */
166 #define PIPE_BANK_TRIPLE AVR32_USBB_UPCFG0_PBK_TRIPLE
167 //@}
168
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.
171 */
172 #define PIPE_CONTROLPIPE_DEFAULT_SIZE 64
173
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.
177 */
178 #define PIPE_TOTAL_PIPES 7
179
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.
184 */
185 #define PIPE_MAX_SIZE 256
186
187 /* Enums: */
188 /** Enum for the possible error return codes of the \ref Pipe_WaitUntilReady() function.
189 *
190 * \ingroup Group_PipeRW_UC3B
191 */
192 enum Pipe_WaitUntilReady_ErrorCodes_t
193 {
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.
200 */
201 };
202
203 /* Inline Functions: */
204 /** Indicates the number of bytes currently stored in the current pipes's selected bank.
205 *
206 * \note The return width of this function may differ, depending on the maximum pipe bank size
207 * of the selected AVR model.
208 *
209 * \ingroup Group_PipeRW_UC3B
210 *
211 * \return Total number of bytes in the currently selected pipe's FIFO buffer.
212 */
213 static inline uint16_t Pipe_BytesInPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
214 static inline uint16_t Pipe_BytesInPipe(void)
215 {
216 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].pbyct;
217 }
218
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.
221 *
222 * \return Index of the currently selected pipe.
223 */
224 static inline uint8_t Pipe_GetCurrentPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
225 static inline uint8_t Pipe_GetCurrentPipe(void)
226 {
227 return USB_SelectedPipe;
228 }
229
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.
232 *
233 * \param[in] PipeNumber Index of the pipe to select.
234 */
235 static inline void Pipe_SelectPipe(const uint8_t PipeNumber) ATTR_ALWAYS_INLINE;
236 static inline void Pipe_SelectPipe(const uint8_t PipeNumber)
237 {
238 USB_SelectedPipe = PipeNumber;
239 }
240
241 /** Resets the desired pipe, including the pipe banks and flags.
242 *
243 * \param[in] PipeNumber Index of the pipe to reset.
244 */
245 static inline void Pipe_ResetPipe(const uint8_t PipeNumber) ATTR_ALWAYS_INLINE;
246 static inline void Pipe_ResetPipe(const uint8_t PipeNumber)
247 {
248 AVR32_USBB.uprst |= (AVR32_USBB_PRST0_MASK << PipeNumber);
249 AVR32_USBB.uprst &= ~(AVR32_USBB_PRST0_MASK << PipeNumber);
250 }
251
252 /** Enables the currently selected pipe so that data can be sent and received through it to and from
253 * an attached device.
254 *
255 * \pre The currently selected pipe must first be configured properly via \ref Pipe_ConfigurePipe().
256 */
257 static inline void Pipe_EnablePipe(void) ATTR_ALWAYS_INLINE;
258 static inline void Pipe_EnablePipe(void)
259 {
260 AVR32_USBB.uprst |= (AVR32_USBB_PEN0_MASK << USB_SelectedPipe);
261 }
262
263 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
264 * from an attached device.
265 */
266 static inline void Pipe_DisablePipe(void) ATTR_ALWAYS_INLINE;
267 static inline void Pipe_DisablePipe(void)
268 {
269 AVR32_USBB.uprst &= ~(AVR32_USBB_PEN0_MASK << USB_SelectedPipe);
270 }
271
272 /** Determines if the currently selected pipe is enabled, but not necessarily configured.
273 *
274 * \return Boolean \c true if the currently selected pipe is enabled, \c false otherwise.
275 */
276 static inline bool Pipe_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
277 static inline bool Pipe_IsEnabled(void)
278 {
279 return ((AVR32_USBB.uprst & (AVR32_USBB_PEN0_MASK << USB_SelectedPipe)) ? true : false);
280 }
281
282 /** Gets the current pipe token, indicating the pipe's data direction and type.
283 *
284 * \return The current pipe token, as a \c PIPE_TOKEN_* mask.
285 */
286 static inline uint8_t Pipe_GetPipeToken(void) ATTR_ALWAYS_INLINE;
287 static inline uint8_t Pipe_GetPipeToken(void)
288 {
289 return ((avr32_usbb_upcfg0_t*)AVR32_USBB_UPCFG0)[USB_SelectedPipe].ptoken;
290 }
291
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.
296 *
297 * \param[in] Token New pipe token to set the selected pipe to, as a \c PIPE_TOKEN_* mask.
298 */
299 static inline void Pipe_SetPipeToken(const uint8_t Token) ATTR_ALWAYS_INLINE;
300 static inline void Pipe_SetPipeToken(const uint8_t Token)
301 {
302 ((avr32_usbb_upcfg0_t*)AVR32_USBB_UPCFG0)[USB_SelectedPipe].ptoken = Token;
303 }
304
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)
308 {
309 ((avr32_usbb_upinrq0_t*)AVR32_USBB_UPINRQ0)[USB_SelectedPipe].inmode = true;
310 }
311
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.
314 *
315 * \param[in] TotalINRequests Total number of IN requests that the pipe may receive before freezing.
316 */
317 static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests) ATTR_ALWAYS_INLINE;
318 static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests)
319 {
320 ((avr32_usbb_upinrq0_t*)AVR32_USBB_UPINRQ0)[USB_SelectedPipe].inmode = false;
321 ((avr32_usbb_upinrq0_t*)AVR32_USBB_UPINRQ0)[USB_SelectedPipe].inrq = TotalINRequests;
322 }
323
324 /** Determines if the currently selected pipe is configured.
325 *
326 * \return Boolean \c true if the selected pipe is configured, \c false otherwise.
327 */
328 static inline bool Pipe_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
329 static inline bool Pipe_IsConfigured(void)
330 {
331 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].cfgok;
332 }
333
334 /** Retrieves the endpoint number of the endpoint within the attached device that the currently selected
335 * pipe is bound to.
336 *
337 * \return Endpoint number the currently selected pipe is bound to.
338 */
339 static inline uint8_t Pipe_BoundEndpointNumber(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
340 static inline uint8_t Pipe_BoundEndpointNumber(void)
341 {
342 return ((avr32_usbb_upcfg0_t*)AVR32_USBB_UPCFG0)[USB_SelectedPipe].pepnum;
343 }
344
345 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds.
346 *
347 * \param[in] Milliseconds Number of milliseconds between each pipe poll.
348 */
349 static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds) ATTR_ALWAYS_INLINE;
350 static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds)
351 {
352 ((avr32_usbb_upcfg0_t*)AVR32_USBB_UPCFG0)[USB_SelectedPipe].intfrq = Milliseconds;
353 }
354
355 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
356 * be serviced.
357 *
358 * \return Mask whose bits indicate which pipes have interrupted.
359 */
360 static inline uint8_t Pipe_GetPipeInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
361 static inline uint8_t Pipe_GetPipeInterrupts(void)
362 {
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);
367 }
368
369 /** Determines if the specified pipe number has interrupted (valid only for INTERRUPT type
370 * pipes).
371 *
372 * \param[in] PipeNumber Index of the pipe whose interrupt flag should be tested.
373 *
374 * \return Boolean \c true if the specified pipe has interrupted, \c false otherwise.
375 */
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)
378 {
379 return ((AVR32_USBB.uhint & (AVR32_USBB_P0INTES_MASK << USB_SelectedPipe)) ? true : false);
380 }
381
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)
385 {
386 ((avr32_usbb_upcon0clr_t*)AVR32_USBB_UPCON0CLR)[USB_SelectedPipe].pfreezec = true;
387 }
388
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)
392 {
393 ((avr32_usbb_upcon0set_t*)AVR32_USBB_UPCON0SET)[USB_SelectedPipe].pfreezes = true;
394 }
395
396 /** Determines if the currently selected pipe is frozen, and not able to accept data.
397 *
398 * \return Boolean \c true if the currently selected pipe is frozen, \c false otherwise.
399 */
400 static inline bool Pipe_IsFrozen(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
401 static inline bool Pipe_IsFrozen(void)
402 {
403 return ((((avr32_usbb_upcon0_t*)AVR32_USBB_UPCON0)[USB_SelectedPipe].pfreeze) ? true : false);
404 }
405
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)
409 {
410 ((uint32_t*)AVR32_USBB_UPERR0)[USB_SelectedPipe] = 0;
411 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].overfic = true;
412 }
413
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.
416 *
417 * \see \ref Pipe_GetErrorFlags() macro for information on retrieving the exact error flag.
418 *
419 * \return Boolean \c true if an error has occurred on the selected pipe, \c false otherwise.
420 */
421 static inline bool Pipe_IsError(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
422 static inline bool Pipe_IsError(void)
423 {
424 return ((((uint32_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe] &
425 (AVR32_USBB_PERRI_MASK | AVR32_USBB_OVERFI_MASK)) ? true : false);
426 }
427
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.
430 *
431 * \return Mask comprising of \c PIPE_ERRORFLAG_* bits indicating what error has occurred on the selected pipe.
432 */
433 static inline uint8_t Pipe_GetErrorFlags(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
434 static inline uint8_t Pipe_GetErrorFlags(void)
435 {
436
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));
443 }
444
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.
448 *
449 * \ingroup Group_PipePacketManagement_UC3B
450 *
451 * \return Total number of busy banks in the selected pipe.
452 */
453 static inline uint8_t Pipe_GetBusyBanks(void)
454 {
455 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].nbusybk;
456 }
457
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.
463 *
464 * \note This function is not valid on CONTROL type pipes.
465 *
466 * \ingroup Group_PipePacketManagement_UC3B
467 *
468 * \return Boolean \c true if the currently selected pipe may be read from or written to, depending
469 * on its direction.
470 */
471 static inline bool Pipe_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
472 static inline bool Pipe_IsReadWriteAllowed(void)
473 {
474 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].rwall;
475 }
476
477 /** Determines if a packet has been received on the currently selected IN pipe from the attached device.
478 *
479 * \ingroup Group_PipePacketManagement_UC3B
480 *
481 * \return Boolean \c true if the current pipe has received an IN packet, \c false otherwise.
482 */
483 static inline bool Pipe_IsINReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
484 static inline bool Pipe_IsINReceived(void)
485 {
486 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].rxini;
487 }
488
489 /** Determines if the currently selected OUT pipe is ready to send an OUT packet to the attached device.
490 *
491 * \ingroup Group_PipePacketManagement_UC3B
492 *
493 * \return Boolean \c true if the current pipe is ready for an OUT packet, \c false otherwise.
494 */
495 static inline bool Pipe_IsOUTReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
496 static inline bool Pipe_IsOUTReady(void)
497 {
498 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].txouti;
499 }
500
501 /** Determines if no SETUP request is currently being sent to the attached device on the selected
502 * CONTROL type pipe.
503 *
504 * \ingroup Group_PipePacketManagement_UC3B
505 *
506 * \return Boolean \c true if the current pipe is ready for a SETUP packet, \c false otherwise.
507 */
508 static inline bool Pipe_IsSETUPSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
509 static inline bool Pipe_IsSETUPSent(void)
510 {
511 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].txstpi;
512 }
513
514 /** Sends the currently selected CONTROL type pipe's contents to the device as a SETUP packet.
515 *
516 * \ingroup Group_PipePacketManagement_UC3B
517 */
518 static inline void Pipe_ClearSETUP(void) ATTR_ALWAYS_INLINE;
519 static inline void Pipe_ClearSETUP(void)
520 {
521 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].txstpic = true;
522 }
523
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.
526 *
527 * \ingroup Group_PipePacketManagement_UC3B
528 */
529 static inline void Pipe_ClearIN(void) ATTR_ALWAYS_INLINE;
530 static inline void Pipe_ClearIN(void)
531 {
532 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].rxinic = true;
533 ((avr32_usbb_upcon0clr_t*)AVR32_USBB_UPCON0CLR)[USB_SelectedPipe].fifoconc = true;
534 }
535
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.
538 *
539 * \ingroup Group_PipePacketManagement_UC3B
540 */
541 static inline void Pipe_ClearOUT(void) ATTR_ALWAYS_INLINE;
542 static inline void Pipe_ClearOUT(void)
543 {
544 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].txoutic = true;
545 ((avr32_usbb_upcon0clr_t*)AVR32_USBB_UPCON0CLR)[USB_SelectedPipe].fifoconc = true;
546 }
547
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
552 * can be re-sent.
553 *
554 * \ingroup Group_PipePacketManagement_UC3B
555 *
556 * \return Boolean \c true if an NAK has been received on the current pipe, \c false otherwise.
557 */
558 static inline bool Pipe_IsNAKReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
559 static inline bool Pipe_IsNAKReceived(void)
560 {
561 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].nakedi;
562 }
563
564 /** Clears the NAK condition on the currently selected pipe.
565 *
566 * \ingroup Group_PipePacketManagement_UC3B
567 *
568 * \see \ref Pipe_IsNAKReceived() for more details.
569 */
570 static inline void Pipe_ClearNAKReceived(void) ATTR_ALWAYS_INLINE;
571 static inline void Pipe_ClearNAKReceived(void)
572 {
573 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].nakedic = true;
574 }
575
576 /** Determines if the currently selected pipe has had the STALL condition set by the attached device.
577 *
578 * \ingroup Group_PipePacketManagement_UC3B
579 *
580 * \return Boolean \c true if the current pipe has been stalled by the attached device, \c false otherwise.
581 */
582 static inline bool Pipe_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
583 static inline bool Pipe_IsStalled(void)
584 {
585 return ((avr32_usbb_upsta0_t*)AVR32_USBB_UPSTA0)[USB_SelectedPipe].rxstalldi;
586 }
587
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).
590 *
591 * \ingroup Group_PipePacketManagement_UC3B
592 */
593 static inline void Pipe_ClearStall(void) ATTR_ALWAYS_INLINE;
594 static inline void Pipe_ClearStall(void)
595 {
596 ((avr32_usbb_upsta0clr_t*)AVR32_USBB_UPSTA0CLR)[USB_SelectedPipe].rxstalldic = true;
597 }
598
599 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes.
600 *
601 * \ingroup Group_PipePrimitiveRW_UC3B
602 *
603 * \return Next byte in the currently selected pipe's FIFO buffer.
604 */
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)
607 {
608 return *((uint8_t*)AVR32_USBB_EP_DATA);
609 }
610
611 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes.
612 *
613 * \ingroup Group_PipePrimitiveRW_UC3B
614 *
615 * \param[in] Byte Next byte to write into the the currently selected pipe's FIFO buffer.
616 */
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)
619 {
620 *((uint8_t*)AVR32_USBB_EP_DATA) = Byte;
621 }
622
623 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes.
624 *
625 * \ingroup Group_PipePrimitiveRW_UC3B
626 */
627 static inline void Pipe_Discard_Byte(void) ATTR_ALWAYS_INLINE;
628 static inline void Pipe_Discard_Byte(void)
629 {
630 uint8_t Dummy;
631
632 Dummy = *((uint8_t*)AVR32_USBB_EP_DATA);
633 }
634
635 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
636 * direction pipes.
637 *
638 * \ingroup Group_PipePrimitiveRW_UC3B
639 *
640 * \return Next word in the currently selected pipe's FIFO buffer.
641 */
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)
644 {
645 union
646 {
647 uint16_t Word;
648 uint8_t Bytes[2];
649 } Data;
650
651 Data.Bytes[0] = *((uint8_t*)AVR32_USBB_EP_DATA);
652 Data.Bytes[1] = *((uint8_t*)AVR32_USBB_EP_DATA);
653
654 return Data.Word;
655 }
656
657 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
658 * direction pipes.
659 *
660 * \ingroup Group_PipePrimitiveRW_UC3B
661 *
662 * \return Next word in the currently selected pipe's FIFO buffer.
663 */
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)
666 {
667 union
668 {
669 uint16_t Word;
670 uint8_t Bytes[2];
671 } Data;
672
673 Data.Bytes[1] = *((uint8_t*)AVR32_USBB_EP_DATA);
674 Data.Bytes[0] = *((uint8_t*)AVR32_USBB_EP_DATA);
675
676 return Data.Word;
677 }
678
679 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
680 * direction pipes.
681 *
682 * \ingroup Group_PipePrimitiveRW_UC3B
683 *
684 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer.
685 */
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)
688 {
689 *((uint8_t*)AVR32_USBB_EP_DATA) = (Word & 0xFF);
690 *((uint8_t*)AVR32_USBB_EP_DATA) = (Word >> 8);
691 }
692
693 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
694 * direction pipes.
695 *
696 * \ingroup Group_PipePrimitiveRW_UC3B
697 *
698 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer.
699 */
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)
702 {
703 *((uint8_t*)AVR32_USBB_EP_DATA) = (Word >> 8);
704 *((uint8_t*)AVR32_USBB_EP_DATA) = (Word & 0xFF);
705 }
706
707 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes.
708 *
709 * \ingroup Group_PipePrimitiveRW_UC3B
710 */
711 static inline void Pipe_Discard_Word(void) ATTR_ALWAYS_INLINE;
712 static inline void Pipe_Discard_Word(void)
713 {
714 uint8_t Dummy;
715
716 Dummy = *((uint8_t*)AVR32_USBB_EP_DATA);
717 Dummy = *((uint8_t*)AVR32_USBB_EP_DATA);
718 }
719
720 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
721 * direction pipes.
722 *
723 * \ingroup Group_PipePrimitiveRW_UC3B
724 *
725 * \return Next double word in the currently selected pipe's FIFO buffer.
726 */
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)
729 {
730 union
731 {
732 uint32_t DWord;
733 uint8_t Bytes[4];
734 } Data;
735
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);
740
741 return Data.DWord;
742 }
743
744 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
745 * direction pipes.
746 *
747 * \ingroup Group_PipePrimitiveRW_UC3B
748 *
749 * \return Next double word in the currently selected pipe's FIFO buffer.
750 */
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)
753 {
754 union
755 {
756 uint32_t DWord;
757 uint8_t Bytes[4];
758 } Data;
759
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);
764
765 return Data.DWord;
766 }
767
768 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
769 * direction pipes.
770 *
771 * \ingroup Group_PipePrimitiveRW_UC3B
772 *
773 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer.
774 */
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)
777 {
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);
782 }
783
784 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
785 * direction pipes.
786 *
787 * \ingroup Group_PipePrimitiveRW_UC3B
788 *
789 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer.
790 */
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)
793 {
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);
798 }
799
800 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes.
801 *
802 * \ingroup Group_PipePrimitiveRW_UC3B
803 */
804 static inline void Pipe_Discard_DWord(void) ATTR_ALWAYS_INLINE;
805 static inline void Pipe_Discard_DWord(void)
806 {
807 uint8_t Dummy;
808
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);
813 }
814
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
819 * to the USB bus.
820 *
821 * \note This variable should be treated as read-only in the user application, and never manually
822 * changed in value.
823 */
824 extern uint8_t USB_ControlPipeSize;
825
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.
829 *
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().
835 *
836 * \param[in] Number Pipe number to configure. This must be more than 0 and less than \ref PIPE_TOTAL_PIPES.
837 *
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.
840 *
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.
844 *
845 * \param[in] EndpointNumber Endpoint index within the attached device that the pipe should interface to.
846 *
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.
851 *
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.
855 *
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.
858 * \n\n
859 *
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.
862 * \n\n
863 *
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.
866 * \n\n
867 *
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.
870 *
871 * \return Boolean \c true if the configuration succeeded, \c false otherwise.
872 */
873 bool Pipe_ConfigurePipe(const uint8_t Number,
874 const uint8_t Type,
875 const uint8_t Token,
876 const uint8_t EndpointNumber,
877 const uint16_t Size,
878 const uint8_t Banks);
879
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).
882 *
883 * \ingroup Group_PipeRW_UC3B
884 *
885 * \return A value from the \ref Pipe_WaitUntilReady_ErrorCodes_t enum.
886 */
887 uint8_t Pipe_WaitUntilReady(void);
888
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.
891 *
892 * \param[in] EndpointAddress Address and direction mask of the endpoint within the attached device to check.
893 *
894 * \return Boolean \c true if a pipe bound to the given endpoint address of the specified direction is found,
895 * \c false otherwise.
896 */
897 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress);
898
899 /* Private Interface - For use in library only: */
900 #if !defined(__DOXYGEN__)
901 /* Macros: */
902 #if !defined(ENDPOINT_CONTROLEP)
903 #define ENDPOINT_CONTROLEP 0
904 #endif
905
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)
909 {
910 uint8_t MaskVal = 0;
911 uint16_t CheckBytes = 8;
912
913 while ((CheckBytes < Bytes) && (CheckBytes < PIPE_MAX_SIZE))
914 {
915 MaskVal++;
916 CheckBytes <<= 1;
917 }
918
919 return (MaskVal << AVR32_USBB_PSIZE_OFFSET);
920 }
921
922 /* Function Prototypes: */
923 void Pipe_ClearPipes(void);
924 #endif
925
926 /* Disable C linkage for C++ Compilers: */
927 #if defined(__cplusplus)
928 }
929 #endif
930
931 #endif
932
933 /** @} */
934