]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/AVR8/Pipe_AVR8.h
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / AVR8 / Pipe_AVR8.h
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 /** \file\r
32  *  \brief USB Pipe definitions for the AVR8 microcontrollers.\r
33  *  \copydetails Group_PipeManagement_AVR8\r
34  *\r
35  *  \note This file should not be included directly. It is automatically included as needed by the USB driver\r
36  *        dispatch header located in LUFA/Drivers/USB/USB.h.\r
37  */\r
38 \r
39 /** \ingroup Group_PipeRW\r
40  *  \defgroup Group_PipeRW_AVR8 Pipe Data Reading and Writing (AVR8)\r
41  *  \brief Pipe data read/write definitions for the Atmel AVR8 architecture.\r
42  *\r
43  *  Functions, macros, variables, enums and types related to data reading and writing from and to pipes.\r
44  */\r
45 \r
46 /** \ingroup Group_PipePrimitiveRW\r
47  *  \defgroup Group_PipePrimitiveRW_AVR8 Read/Write of Primitive Data Types (AVR8)\r
48  *  \brief Pipe primitive data read/write definitions for the Atmel AVR8 architecture.\r
49  *\r
50  *  Functions, macros, variables, enums and types related to data reading and writing of primitive data types\r
51  *  from and to pipes.\r
52  */\r
53 \r
54 /** \ingroup Group_PipePacketManagement\r
55  *  \defgroup Group_PipePacketManagement_AVR8 Pipe Packet Management (AVR8)\r
56  *  \brief Pipe packet management definitions for the Atmel AVR8 architecture.\r
57  *\r
58  *  Functions, macros, variables, enums and types related to packet management of pipes.\r
59  */\r
60 \r
61 /** \ingroup Group_PipeControlReq\r
62  *  \defgroup Group_PipeControlReq_AVR8 Pipe Control Request Management (AVR8)\r
63  *  \brief Pipe control request management definitions for the Atmel AVR8 architecture.\r
64  *\r
65  *  Module for host mode request processing. This module allows for the transmission of standard, class and\r
66  *  vendor control requests to the default control endpoint of an attached device while in host mode.\r
67  *\r
68  *  \see Chapter 9 of the USB 2.0 specification.\r
69  */\r
70 \r
71 /** \ingroup Group_PipeManagement\r
72  *  \defgroup Group_PipeManagement_AVR8 Pipe Management (AVR8)\r
73  *  \brief Pipe management definitions for the Atmel AVR8 architecture.\r
74  *\r
75  *  This module contains functions, macros and enums related to pipe management when in USB Host mode. This\r
76  *  module contains the pipe management macros, as well as pipe interrupt and data send/receive functions\r
77  *  for various data types.\r
78  *\r
79  *  @{\r
80  */\r
81 \r
82 #ifndef __PIPE_AVR8_H__\r
83 #define __PIPE_AVR8_H__\r
84 \r
85         /* Includes: */\r
86                 #include "../../../../Common/Common.h"\r
87                 #include "../USBTask.h"\r
88 \r
89         /* Enable C linkage for C++ Compilers: */\r
90                 #if defined(__cplusplus)\r
91                         extern "C" {\r
92                 #endif\r
93 \r
94         /* Preprocessor Checks: */\r
95                 #if !defined(__INCLUDE_FROM_USB_DRIVER)\r
96                         #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.\r
97                 #endif\r
98 \r
99         /* Public Interface - May be used in end-application: */\r
100                 /* Macros: */\r
101                         /** \name Pipe Error Flag Masks */\r
102                         //@{\r
103                         /** Mask for \ref Pipe_GetErrorFlags(), indicating that an overflow error occurred in the pipe on the received data. */\r
104                         #define PIPE_ERRORFLAG_OVERFLOW         (1 << 6)\r
105 \r
106                         /** Mask for \ref Pipe_GetErrorFlags(), indicating that an underflow error occurred in the pipe on the received data. */\r
107                         #define PIPE_ERRORFLAG_UNDERFLOW        (1 << 5)\r
108 \r
109                         /** Mask for \ref Pipe_GetErrorFlags(), indicating that a CRC error occurred in the pipe on the received data. */\r
110                         #define PIPE_ERRORFLAG_CRC16            (1 << 4)\r
111 \r
112                         /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware timeout error occurred in the pipe. */\r
113                         #define PIPE_ERRORFLAG_TIMEOUT          (1 << 3)\r
114 \r
115                         /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware PID error occurred in the pipe. */\r
116                         #define PIPE_ERRORFLAG_PID              (1 << 2)\r
117 \r
118                         /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data PID error occurred in the pipe. */\r
119                         #define PIPE_ERRORFLAG_DATAPID          (1 << 1)\r
120 \r
121                         /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data toggle error occurred in the pipe. */\r
122                         #define PIPE_ERRORFLAG_DATATGL          (1 << 0)\r
123                         //@}\r
124 \r
125                         /** \name Pipe Token Masks */\r
126                         //@{\r
127                         /** Token mask for \ref Pipe_SetPipeToken() and \ref Pipe_GetPipeToken(). This sets the pipe as a SETUP token (for CONTROL type pipes),\r
128                          *  which will trigger a control request on the attached device when data is written to the pipe.\r
129                          */\r
130                         #define PIPE_TOKEN_SETUP                (0 << PTOKEN0)\r
131 \r
132                         /** Token mask for \ref Pipe_SetPipeToken() and \ref Pipe_GetPipeToken(). This sets the pipe as a IN token (for non-CONTROL type pipes),\r
133                          *  indicating that the pipe data will flow from device to host.\r
134                          */\r
135                         #define PIPE_TOKEN_IN                   (1 << PTOKEN0)\r
136 \r
137                         /** Token mask for \ref Pipe_SetPipeToken() and \ref Pipe_GetPipeToken(). This sets the pipe as a OUT token (for non-CONTROL type pipes),\r
138                          *  indicating that the pipe data will flow from host to device.\r
139                          */\r
140                         #define PIPE_TOKEN_OUT                  (2 << PTOKEN0)\r
141                         //@}\r
142 \r
143                         /** Default size of the default control pipe's bank, until altered by the Endpoint0Size value\r
144                          *  in the device descriptor of the attached device.\r
145                          */\r
146                         #define PIPE_CONTROLPIPE_DEFAULT_SIZE   64\r
147 \r
148                         /** Total number of pipes (including the default control pipe at address 0) which may be used in\r
149                          *  the device. Different USB AVR models support different amounts of pipes, this value reflects\r
150                          *  the maximum number of pipes for the currently selected AVR model.\r
151                          */\r
152                         #define PIPE_TOTAL_PIPES                7\r
153 \r
154                         /** Size in bytes of the largest pipe bank size possible in the device. Not all banks on each AVR\r
155                          *  model supports the largest bank size possible on the device; different pipe numbers support\r
156                          *  different maximum bank sizes. This value reflects the largest possible bank of any pipe on the\r
157                          *  currently selected USB AVR model.\r
158                          */\r
159                         #define PIPE_MAX_SIZE                   256\r
160 \r
161                 /* Enums: */\r
162                         /** Enum for the possible error return codes of the \ref Pipe_WaitUntilReady() function.\r
163                          *\r
164                          *  \ingroup Group_PipeRW_AVR8\r
165                          */\r
166                         enum Pipe_WaitUntilReady_ErrorCodes_t\r
167                         {\r
168                                 PIPE_READYWAIT_NoError                 = 0, /**< Pipe ready for next packet, no error. */\r
169                                 PIPE_READYWAIT_PipeStalled             = 1,     /**< The device stalled the pipe while waiting. */\r
170                                 PIPE_READYWAIT_DeviceDisconnected      = 2,     /**< Device was disconnected from the host while waiting. */\r
171                                 PIPE_READYWAIT_Timeout                 = 3, /**< The device failed to accept or send the next packet\r
172                                                                              *   within the software timeout period set by the\r
173                                                                              *   \ref USB_STREAM_TIMEOUT_MS macro.\r
174                                                                              */\r
175                         };\r
176 \r
177                 /* Inline Functions: */\r
178                         /** Indicates the number of bytes currently stored in the current pipes's selected bank.\r
179                          *\r
180                          *  \ingroup Group_PipeRW_AVR8\r
181                          *\r
182                          *  \return Total number of bytes in the currently selected pipe's FIFO buffer.\r
183                          */\r
184                         static inline uint16_t Pipe_BytesInPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
185                         static inline uint16_t Pipe_BytesInPipe(void)\r
186                         {\r
187                                 return UPBCX;\r
188                         }\r
189 \r
190                         /** Determines the currently selected pipe's direction.\r
191                          *\r
192                          *  \return The currently selected pipe's direction, as a \c PIPE_DIR_* mask.\r
193                          */\r
194                         static inline uint8_t Pipe_GetPipeDirection(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
195                         static inline uint8_t Pipe_GetPipeDirection(void)\r
196                         {\r
197                                 return (UPCFG0X & (1 << EPDIR)) ? PIPE_DIR_IN : PIPE_DIR_OUT;\r
198                         }\r
199                         \r
200                         /** Returns the pipe address of the currently selected pipe. This is typically used to save the\r
201                          *  currently selected pipe address so that it can be restored after another pipe has been manipulated.\r
202                          *\r
203                          *  \return Index of the currently selected pipe.\r
204                          */\r
205                         static inline uint8_t Pipe_GetCurrentPipe(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
206                         static inline uint8_t Pipe_GetCurrentPipe(void)\r
207                         {\r
208                                 return ((UPNUM & PIPE_PIPENUM_MASK) | Pipe_GetPipeDirection());\r
209                         }\r
210 \r
211                         /** Selects the given pipe address. Any pipe operations which do not require the pipe address to be\r
212                          *  indicated will operate on the currently selected pipe.\r
213                          *\r
214                          *  \param[in] Address  Address of the pipe to select.\r
215                          */\r
216                         static inline void Pipe_SelectPipe(const uint8_t Address) ATTR_ALWAYS_INLINE;\r
217                         static inline void Pipe_SelectPipe(const uint8_t Address)\r
218                         {\r
219                                 UPNUM = (Address & PIPE_PIPENUM_MASK);\r
220                         }\r
221 \r
222                         /** Resets the desired pipe, including the pipe banks and flags.\r
223                          *\r
224                          *  \param[in] Address  Address of the pipe to reset.\r
225                          */\r
226                         static inline void Pipe_ResetPipe(const uint8_t Address) ATTR_ALWAYS_INLINE;\r
227                         static inline void Pipe_ResetPipe(const uint8_t Address)\r
228                         {\r
229                                 UPRST = (1 << (Address & PIPE_PIPENUM_MASK));\r
230                                 UPRST = 0;\r
231                         }\r
232 \r
233                         /** Enables the currently selected pipe so that data can be sent and received through it to and from\r
234                          *  an attached device.\r
235                          *\r
236                          *  \pre The currently selected pipe must first be configured properly via \ref Pipe_ConfigurePipe().\r
237                          */\r
238                         static inline void Pipe_EnablePipe(void) ATTR_ALWAYS_INLINE;\r
239                         static inline void Pipe_EnablePipe(void)\r
240                         {\r
241                                 UPCONX |= (1 << PEN);\r
242                         }\r
243 \r
244                         /** Disables the currently selected pipe so that data cannot be sent and received through it to and\r
245                          *  from an attached device.\r
246                          */\r
247                         static inline void Pipe_DisablePipe(void) ATTR_ALWAYS_INLINE;\r
248                         static inline void Pipe_DisablePipe(void)\r
249                         {\r
250                                 UPCONX &= ~(1 << PEN);\r
251                         }\r
252 \r
253                         /** Determines if the currently selected pipe is enabled, but not necessarily configured.\r
254                          *\r
255                          * \return Boolean \c true if the currently selected pipe is enabled, \c false otherwise.\r
256                          */\r
257                         static inline bool Pipe_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
258                         static inline bool Pipe_IsEnabled(void)\r
259                         {\r
260                                 return ((UPCONX & (1 << PEN)) ? true : false);\r
261                         }\r
262 \r
263                         /** Gets the current pipe token, indicating the pipe's data direction and type.\r
264                          *\r
265                          *  \return The current pipe token, as a \c PIPE_TOKEN_* mask.\r
266                          */\r
267                         static inline uint8_t Pipe_GetPipeToken(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
268                         static inline uint8_t Pipe_GetPipeToken(void)\r
269                         {\r
270                                 return (UPCFG0X & (0x03 << PTOKEN0));\r
271                         }\r
272 \r
273                         /** Sets the token for the currently selected pipe to one of the tokens specified by the \c PIPE_TOKEN_*\r
274                          *  masks. This can be used on CONTROL type pipes, to allow for bidirectional transfer of data during\r
275                          *  control requests, or on regular pipes to allow for half-duplex bidirectional data transfer to devices\r
276                          *  which have two endpoints of opposite direction sharing the same endpoint address within the device.\r
277                          *\r
278                          *  \param[in] Token  New pipe token to set the selected pipe to, as a \c PIPE_TOKEN_* mask.\r
279                          */\r
280                         static inline void Pipe_SetPipeToken(const uint8_t Token) ATTR_ALWAYS_INLINE;\r
281                         static inline void Pipe_SetPipeToken(const uint8_t Token)\r
282                         {\r
283                                 UPCFG0X = ((UPCFG0X & ~(0x03 << PTOKEN0)) | Token);\r
284                         }\r
285 \r
286                         /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */\r
287                         static inline void Pipe_SetInfiniteINRequests(void) ATTR_ALWAYS_INLINE;\r
288                         static inline void Pipe_SetInfiniteINRequests(void)\r
289                         {\r
290                                 UPCONX |= (1 << INMODE);\r
291                         }\r
292 \r
293                         /** Configures the currently selected pipe to only allow the specified number of IN requests to be\r
294                          *  accepted by the pipe before it is automatically frozen.\r
295                          *\r
296                          *  \param[in] TotalINRequests  Total number of IN requests that the pipe may receive before freezing.\r
297                          */\r
298                         static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests) ATTR_ALWAYS_INLINE;\r
299                         static inline void Pipe_SetFiniteINRequests(const uint8_t TotalINRequests)\r
300                         {\r
301                                 UPCONX &= ~(1 << INMODE);\r
302                                 UPINRQX = TotalINRequests;\r
303                         }\r
304 \r
305                         /** Determines if the currently selected pipe is configured.\r
306                          *\r
307                          *  \return Boolean \c true if the selected pipe is configured, \c false otherwise.\r
308                          */\r
309                         static inline bool Pipe_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
310                         static inline bool Pipe_IsConfigured(void)\r
311                         {\r
312                                 return ((UPSTAX & (1 << CFGOK)) ? true : false);\r
313                         }\r
314 \r
315                         /** Retrieves the endpoint address of the endpoint within the attached device that the currently selected\r
316                          *  pipe is bound to.\r
317                          *\r
318                          *  \return Endpoint address the currently selected pipe is bound to.\r
319                          */\r
320                         static inline uint8_t Pipe_GetBoundEndpointAddress(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
321                         static inline uint8_t Pipe_GetBoundEndpointAddress(void)\r
322                         {\r
323                                 uint8_t UPCFG0X_Temp = UPCFG0X;\r
324                                 \r
325                                 return (((UPCFG0X_Temp >> PEPNUM0) & PIPE_EPNUM_MASK) | ((UPCFG0X_Temp & PEPNUM1) ? ENDPOINT_DIR_OUT : ENDPOINT_DIR_IN));\r
326                         }\r
327 \r
328                         /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds.\r
329                          *\r
330                          *  \param[in] Milliseconds  Number of milliseconds between each pipe poll.\r
331                          */\r
332                         static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds) ATTR_ALWAYS_INLINE;\r
333                         static inline void Pipe_SetInterruptPeriod(const uint8_t Milliseconds)\r
334                         {\r
335                                 UPCFG2X = Milliseconds;\r
336                         }\r
337 \r
338                         /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should\r
339                          *  be serviced.\r
340                          *\r
341                          *  \return Mask whose bits indicate which pipes have interrupted.\r
342                          */\r
343                         static inline uint8_t Pipe_GetPipeInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
344                         static inline uint8_t Pipe_GetPipeInterrupts(void)\r
345                         {\r
346                                 return UPINT;\r
347                         }\r
348 \r
349                         /** Determines if the specified pipe address has interrupted (valid only for INTERRUPT type\r
350                          *  pipes).\r
351                          *\r
352                          *  \param[in] Address  Address of the pipe whose interrupt flag should be tested.\r
353                          *\r
354                          *  \return Boolean \c true if the specified pipe has interrupted, \c false otherwise.\r
355                          */\r
356                         static inline bool Pipe_HasPipeInterrupted(const uint8_t Address) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
357                         static inline bool Pipe_HasPipeInterrupted(const uint8_t Address)\r
358                         {\r
359                                 return ((UPINT & (1 << (Address & PIPE_PIPENUM_MASK))) ? true : false);\r
360                         }\r
361 \r
362                         /** Unfreezes the selected pipe, allowing it to communicate with an attached device. */\r
363                         static inline void Pipe_Unfreeze(void) ATTR_ALWAYS_INLINE;\r
364                         static inline void Pipe_Unfreeze(void)\r
365                         {\r
366                                 UPCONX &= ~(1 << PFREEZE);\r
367                         }\r
368 \r
369                         /** Freezes the selected pipe, preventing it from communicating with an attached device. */\r
370                         static inline void Pipe_Freeze(void) ATTR_ALWAYS_INLINE;\r
371                         static inline void Pipe_Freeze(void)\r
372                         {\r
373                                 UPCONX |= (1 << PFREEZE);\r
374                         }\r
375 \r
376                         /** Determines if the currently selected pipe is frozen, and not able to accept data.\r
377                          *\r
378                          *  \return Boolean \c true if the currently selected pipe is frozen, \c false otherwise.\r
379                          */\r
380                         static inline bool Pipe_IsFrozen(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
381                         static inline bool Pipe_IsFrozen(void)\r
382                         {\r
383                                 return ((UPCONX & (1 << PFREEZE)) ? true : false);\r
384                         }\r
385 \r
386                         /** Clears the error flags for the currently selected pipe. */\r
387                         static inline void Pipe_ClearError(void) ATTR_ALWAYS_INLINE;\r
388                         static inline void Pipe_ClearError(void)\r
389                         {\r
390                                 UPERRX = 0;\r
391                                 UPINTX &= ~(1 << PERRI);\r
392                         }\r
393 \r
394                         /** Determines if the master pipe error flag is set for the currently selected pipe, indicating that\r
395                          *  some sort of hardware error has occurred on the pipe.\r
396                          *\r
397                          *  \see \ref Pipe_GetErrorFlags() macro for information on retrieving the exact error flag.\r
398                          *\r
399                          *  \return Boolean \c true if an error has occurred on the selected pipe, \c false otherwise.\r
400                          */\r
401                         static inline bool Pipe_IsError(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
402                         static inline bool Pipe_IsError(void)\r
403                         {\r
404                                 return ((UPINTX & (1 << PERRI)) ? true : false);\r
405                         }\r
406 \r
407                         /** Gets a mask of the hardware error flags which have occurred on the currently selected pipe. This\r
408                          *  value can then be masked against the \c PIPE_ERRORFLAG_* masks to determine what error has occurred.\r
409                          *\r
410                          *  \return  Mask comprising of \c PIPE_ERRORFLAG_* bits indicating what error has occurred on the selected pipe.\r
411                          */\r
412                         static inline uint8_t Pipe_GetErrorFlags(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
413                         static inline uint8_t Pipe_GetErrorFlags(void)\r
414                         {\r
415                                 return ((UPERRX & (PIPE_ERRORFLAG_CRC16 | PIPE_ERRORFLAG_TIMEOUT |\r
416                                                    PIPE_ERRORFLAG_PID   | PIPE_ERRORFLAG_DATAPID |\r
417                                                    PIPE_ERRORFLAG_DATATGL)) |\r
418                                         (UPSTAX & (PIPE_ERRORFLAG_OVERFLOW | PIPE_ERRORFLAG_UNDERFLOW)));\r
419                         }\r
420 \r
421                         /** Retrieves the number of busy banks in the currently selected pipe, which have been queued for\r
422                          *  transmission via the \ref Pipe_ClearOUT() command, or are awaiting acknowledgement via the\r
423                          *  \ref Pipe_ClearIN() command.\r
424                          *\r
425                          *  \ingroup Group_PipePacketManagement_AVR8\r
426                          *\r
427                          *  \return Total number of busy banks in the selected pipe.\r
428                          */\r
429                         static inline uint8_t Pipe_GetBusyBanks(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
430                         static inline uint8_t Pipe_GetBusyBanks(void)\r
431                         {\r
432                                 return (UPSTAX & (0x03 << NBUSYBK0));\r
433                         }\r
434 \r
435                         /** Determines if the currently selected pipe may be read from (if data is waiting in the pipe\r
436                          *  bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT\r
437                          *  direction). This function will return false if an error has occurred in the pipe, or if the pipe\r
438                          *  is an IN direction and no packet (or an empty packet) has been received, or if the pipe is an OUT\r
439                          *  direction and the pipe bank is full.\r
440                          *\r
441                          *  \note This function is not valid on CONTROL type pipes.\r
442                          *\r
443                          *  \ingroup Group_PipePacketManagement_AVR8\r
444                          *\r
445                          *  \return Boolean \c true if the currently selected pipe may be read from or written to, depending\r
446                          *          on its direction.\r
447                          */\r
448                         static inline bool Pipe_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
449                         static inline bool Pipe_IsReadWriteAllowed(void)\r
450                         {\r
451                                 return ((UPINTX & (1 << RWAL)) ? true : false);\r
452                         }\r
453 \r
454                         /** Determines if a packet has been received on the currently selected IN pipe from the attached device.\r
455                          *\r
456                          *  \ingroup Group_PipePacketManagement_AVR8\r
457                          *\r
458                          *  \return Boolean \c true if the current pipe has received an IN packet, \c false otherwise.\r
459                          */\r
460                         static inline bool Pipe_IsINReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
461                         static inline bool Pipe_IsINReceived(void)\r
462                         {\r
463                                 return ((UPINTX & (1 << RXINI)) ? true : false);\r
464                         }\r
465 \r
466                         /** Determines if the currently selected OUT pipe is ready to send an OUT packet to the attached device.\r
467                          *\r
468                          *  \ingroup Group_PipePacketManagement_AVR8\r
469                          *\r
470                          *  \return Boolean \c true if the current pipe is ready for an OUT packet, \c false otherwise.\r
471                          */\r
472                         static inline bool Pipe_IsOUTReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
473                         static inline bool Pipe_IsOUTReady(void)\r
474                         {\r
475                                 return ((UPINTX & (1 << TXOUTI)) ? true : false);\r
476                         }\r
477 \r
478                         /** Determines if no SETUP request is currently being sent to the attached device on the selected\r
479                          *  CONTROL type pipe.\r
480                          *\r
481                          *  \ingroup Group_PipePacketManagement_AVR8\r
482                          *\r
483                          *  \return Boolean \c true if the current pipe is ready for a SETUP packet, \c false otherwise.\r
484                          */\r
485                         static inline bool Pipe_IsSETUPSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
486                         static inline bool Pipe_IsSETUPSent(void)\r
487                         {\r
488                                 return ((UPINTX & (1 << TXSTPI)) ? true : false);\r
489                         }\r
490 \r
491                         /** Sends the currently selected CONTROL type pipe's contents to the device as a SETUP packet.\r
492                          *\r
493                          *  \ingroup Group_PipePacketManagement_AVR8\r
494                          */\r
495                         static inline void Pipe_ClearSETUP(void) ATTR_ALWAYS_INLINE;\r
496                         static inline void Pipe_ClearSETUP(void)\r
497                         {\r
498                                 UPINTX &= ~((1 << TXSTPI) | (1 << FIFOCON));\r
499                         }\r
500 \r
501                         /** Acknowledges the reception of a setup IN request from the attached device on the currently selected\r
502                          *  pipe, freeing the bank ready for the next packet.\r
503                          *\r
504                          *  \ingroup Group_PipePacketManagement_AVR8\r
505                          */\r
506                         static inline void Pipe_ClearIN(void) ATTR_ALWAYS_INLINE;\r
507                         static inline void Pipe_ClearIN(void)\r
508                         {\r
509                                 UPINTX &= ~((1 << RXINI) | (1 << FIFOCON));\r
510                         }\r
511 \r
512                         /** Sends the currently selected pipe's contents to the device as an OUT packet on the selected pipe, freeing\r
513                          *  the bank ready for the next packet.\r
514                          *\r
515                          *  \ingroup Group_PipePacketManagement_AVR8\r
516                          */\r
517                         static inline void Pipe_ClearOUT(void) ATTR_ALWAYS_INLINE;\r
518                         static inline void Pipe_ClearOUT(void)\r
519                         {\r
520                                 UPINTX &= ~((1 << TXOUTI) | (1 << FIFOCON));\r
521                         }\r
522 \r
523                         /** Determines if the device sent a NAK (Negative Acknowledge) in response to the last sent packet on\r
524                          *  the currently selected pipe. This occurs when the host sends a packet to the device, but the device\r
525                          *  is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been\r
526                          *  received, it must be cleared using \ref Pipe_ClearNAKReceived() before the previous (or any other) packet\r
527                          *  can be re-sent.\r
528                          *\r
529                          *  \ingroup Group_PipePacketManagement_AVR8\r
530                          *\r
531                          *  \return Boolean \c true if an NAK has been received on the current pipe, \c false otherwise.\r
532                          */\r
533                         static inline bool Pipe_IsNAKReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
534                         static inline bool Pipe_IsNAKReceived(void)\r
535                         {\r
536                                 return ((UPINTX & (1 << NAKEDI)) ? true : false);\r
537                         }\r
538 \r
539                         /** Clears the NAK condition on the currently selected pipe.\r
540                          *\r
541                          *  \ingroup Group_PipePacketManagement_AVR8\r
542                          *\r
543                          *  \see \ref Pipe_IsNAKReceived() for more details.\r
544                          */\r
545                         static inline void Pipe_ClearNAKReceived(void) ATTR_ALWAYS_INLINE;\r
546                         static inline void Pipe_ClearNAKReceived(void)\r
547                         {\r
548                                 UPINTX &= ~(1 << NAKEDI);\r
549                         }\r
550 \r
551                         /** Determines if the currently selected pipe has had the STALL condition set by the attached device.\r
552                          *\r
553                          *  \ingroup Group_PipePacketManagement_AVR8\r
554                          *\r
555                          *  \return Boolean \c true if the current pipe has been stalled by the attached device, \c false otherwise.\r
556                          */\r
557                         static inline bool Pipe_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
558                         static inline bool Pipe_IsStalled(void)\r
559                         {\r
560                                 return ((UPINTX & (1 << RXSTALLI)) ? true : false);\r
561                         }\r
562 \r
563                         /** Clears the STALL condition detection flag on the currently selected pipe, but does not clear the\r
564                          *  STALL condition itself (this must be done via a ClearFeature control request to the device).\r
565                          *\r
566                          *  \ingroup Group_PipePacketManagement_AVR8\r
567                          */\r
568                         static inline void Pipe_ClearStall(void) ATTR_ALWAYS_INLINE;\r
569                         static inline void Pipe_ClearStall(void)\r
570                         {\r
571                                 UPINTX &= ~(1 << RXSTALLI);\r
572                         }\r
573 \r
574                         /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes.\r
575                          *\r
576                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
577                          *\r
578                          *  \return Next byte in the currently selected pipe's FIFO buffer.\r
579                          */\r
580                         static inline uint8_t Pipe_Read_8(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
581                         static inline uint8_t Pipe_Read_8(void)\r
582                         {\r
583                                 return UPDATX;\r
584                         }\r
585 \r
586                         /** Writes one byte to the currently selected pipe's bank, for IN direction pipes.\r
587                          *\r
588                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
589                          *\r
590                          *  \param[in] Data  Data to write into the the currently selected pipe's FIFO buffer.\r
591                          */\r
592                         static inline void Pipe_Write_8(const uint8_t Data) ATTR_ALWAYS_INLINE;\r
593                         static inline void Pipe_Write_8(const uint8_t Data)\r
594                         {\r
595                                 UPDATX = Data;\r
596                         }\r
597 \r
598                         /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes.\r
599                          *\r
600                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
601                          */\r
602                         static inline void Pipe_Discard_8(void) ATTR_ALWAYS_INLINE;\r
603                         static inline void Pipe_Discard_8(void)\r
604                         {\r
605                                 uint8_t Dummy;\r
606 \r
607                                 Dummy = UPDATX;\r
608 \r
609                                 (void)Dummy;\r
610                         }\r
611 \r
612                         /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT\r
613                          *  direction pipes.\r
614                          *\r
615                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
616                          *\r
617                          *  \return Next two bytes in the currently selected pipe's FIFO buffer.\r
618                          */\r
619                         static inline uint16_t Pipe_Read_16_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
620                         static inline uint16_t Pipe_Read_16_LE(void)\r
621                         {\r
622                                 union\r
623                                 {\r
624                                         uint16_t Value;\r
625                                         uint8_t  Bytes[2];\r
626                                 } Data;\r
627 \r
628                                 Data.Bytes[0] = UPDATX;\r
629                                 Data.Bytes[1] = UPDATX;\r
630 \r
631                                 return Data.Value;\r
632                         }\r
633 \r
634                         /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT\r
635                          *  direction pipes.\r
636                          *\r
637                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
638                          *\r
639                          *  \return Next two bytes in the currently selected pipe's FIFO buffer.\r
640                          */\r
641                         static inline uint16_t Pipe_Read_16_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
642                         static inline uint16_t Pipe_Read_16_BE(void)\r
643                         {\r
644                                 union\r
645                                 {\r
646                                         uint16_t Value;\r
647                                         uint8_t  Bytes[2];\r
648                                 } Data;\r
649 \r
650                                 Data.Bytes[1] = UPDATX;\r
651                                 Data.Bytes[0] = UPDATX;\r
652 \r
653                                 return Data.Value;\r
654                         }\r
655 \r
656                         /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN\r
657                          *  direction pipes.\r
658                          *\r
659                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
660                          *\r
661                          *  \param[in] Data  Data to write to the currently selected pipe's FIFO buffer.\r
662                          */\r
663                         static inline void Pipe_Write_16_LE(const uint16_t Data) ATTR_ALWAYS_INLINE;\r
664                         static inline void Pipe_Write_16_LE(const uint16_t Data)\r
665                         {\r
666                                 UPDATX = (Data & 0xFF);\r
667                                 UPDATX = (Data >> 8);\r
668                         }\r
669 \r
670                         /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN\r
671                          *  direction pipes.\r
672                          *\r
673                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
674                          *\r
675                          *  \param[in] Data  Data to write to the currently selected pipe's FIFO buffer.\r
676                          */\r
677                         static inline void Pipe_Write_16_BE(const uint16_t Data) ATTR_ALWAYS_INLINE;\r
678                         static inline void Pipe_Write_16_BE(const uint16_t Data)\r
679                         {\r
680                                 UPDATX = (Data >> 8);\r
681                                 UPDATX = (Data & 0xFF);\r
682                         }\r
683 \r
684                         /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes.\r
685                          *\r
686                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
687                          */\r
688                         static inline void Pipe_Discard_16(void) ATTR_ALWAYS_INLINE;\r
689                         static inline void Pipe_Discard_16(void)\r
690                         {\r
691                                 uint8_t Dummy;\r
692 \r
693                                 Dummy = UPDATX;\r
694                                 Dummy = UPDATX;\r
695 \r
696                                 (void)Dummy;\r
697                         }\r
698 \r
699                         /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT\r
700                          *  direction pipes.\r
701                          *\r
702                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
703                          *\r
704                          *  \return Next four bytes in the currently selected pipe's FIFO buffer.\r
705                          */\r
706                         static inline uint32_t Pipe_Read_32_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
707                         static inline uint32_t Pipe_Read_32_LE(void)\r
708                         {\r
709                                 union\r
710                                 {\r
711                                         uint32_t Value;\r
712                                         uint8_t  Bytes[4];\r
713                                 } Data;\r
714 \r
715                                 Data.Bytes[0] = UPDATX;\r
716                                 Data.Bytes[1] = UPDATX;\r
717                                 Data.Bytes[2] = UPDATX;\r
718                                 Data.Bytes[3] = UPDATX;\r
719 \r
720                                 return Data.Value;\r
721                         }\r
722 \r
723                         /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT\r
724                          *  direction pipes.\r
725                          *\r
726                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
727                          *\r
728                          *  \return Next four bytes in the currently selected pipe's FIFO buffer.\r
729                          */\r
730                         static inline uint32_t Pipe_Read_32_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
731                         static inline uint32_t Pipe_Read_32_BE(void)\r
732                         {\r
733                                 union\r
734                                 {\r
735                                         uint32_t DWord;\r
736                                         uint8_t  Bytes[4];\r
737                                 } Data;\r
738 \r
739                                 Data.Bytes[3] = UPDATX;\r
740                                 Data.Bytes[2] = UPDATX;\r
741                                 Data.Bytes[1] = UPDATX;\r
742                                 Data.Bytes[0] = UPDATX;\r
743 \r
744                                 return Data.DWord;\r
745                         }\r
746 \r
747                         /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN\r
748                          *  direction pipes.\r
749                          *\r
750                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
751                          *\r
752                          *  \param[in] Data  Data to write to the currently selected pipe's FIFO buffer.\r
753                          */\r
754                         static inline void Pipe_Write_32_LE(const uint32_t Data) ATTR_ALWAYS_INLINE;\r
755                         static inline void Pipe_Write_32_LE(const uint32_t Data)\r
756                         {\r
757                                 UPDATX = (Data &  0xFF);\r
758                                 UPDATX = (Data >> 8);\r
759                                 UPDATX = (Data >> 16);\r
760                                 UPDATX = (Data >> 24);\r
761                         }\r
762 \r
763                         /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN\r
764                          *  direction pipes.\r
765                          *\r
766                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
767                          *\r
768                          *  \param[in] Data  Data to write to the currently selected pipe's FIFO buffer.\r
769                          */\r
770                         static inline void Pipe_Write_32_BE(const uint32_t Data) ATTR_ALWAYS_INLINE;\r
771                         static inline void Pipe_Write_32_BE(const uint32_t Data)\r
772                         {\r
773                                 UPDATX = (Data >> 24);\r
774                                 UPDATX = (Data >> 16);\r
775                                 UPDATX = (Data >> 8);\r
776                                 UPDATX = (Data &  0xFF);\r
777                         }\r
778 \r
779                         /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes.\r
780                          *\r
781                          *  \ingroup Group_PipePrimitiveRW_AVR8\r
782                          */\r
783                         static inline void Pipe_Discard_32(void) ATTR_ALWAYS_INLINE;\r
784                         static inline void Pipe_Discard_32(void)\r
785                         {\r
786                                 uint8_t Dummy;\r
787 \r
788                                 Dummy = UPDATX;\r
789                                 Dummy = UPDATX;\r
790                                 Dummy = UPDATX;\r
791                                 Dummy = UPDATX;\r
792 \r
793                                 (void)Dummy;\r
794                         }\r
795 \r
796                 /* External Variables: */\r
797                         /** Global indicating the maximum packet size of the default control pipe located at address\r
798                          *  0 in the device. This value is set to the value indicated in the attached device's device\r
799                      *  descriptor once the USB interface is initialized into host mode and a device is attached\r
800                          *  to the USB bus.\r
801                          *\r
802                          *  \attention This variable should be treated as read-only in the user application, and never manually\r
803                          *             changed in value.\r
804                          */\r
805                         extern uint8_t USB_Host_ControlPipeSize;\r
806 \r
807                 /* Function Prototypes: */\r
808                         /** Configures a table of pipe descriptions, in sequence. This function can be used to configure multiple\r
809                          *  pipes at the same time.\r
810                          *\r
811                          *  \note Pipe with a zero address will be ignored, thus this function cannot be used to configure the\r
812                          *        control pipe.\r
813                          *\r
814                          *  \param[in] Table    Pointer to a table of pipe descriptions.\r
815                          *  \param[in] Entries  Number of entries in the pipe table to configure.\r
816                          *\r
817                          *  \return Boolean \c true if all pipes configured successfully, \c false otherwise.\r
818                          */                     \r
819                         bool Pipe_ConfigurePipeTable(const USB_Pipe_Table_t* const Table,\r
820                                                      const uint8_t Entries);\r
821                                                                                  \r
822                         /** Configures the specified pipe address with the given pipe type, endpoint address within the attached device, bank size\r
823                          *  and number of hardware banks.\r
824                          *\r
825                          *  A newly configured pipe is frozen by default, and must be unfrozen before use via the \ref Pipe_Unfreeze()\r
826                          *  before being used. Pipes should be kept frozen unless waiting for data from a device while in IN mode, or\r
827                          *  sending data to the device in OUT mode. IN type pipes are also automatically configured to accept infinite\r
828                          *  numbers of IN requests without automatic freezing - this can be overridden by a call to\r
829                          *  \ref Pipe_SetFiniteINRequests().\r
830                          *\r
831                          *  \param[in] Address          Pipe address to configure.\r
832                          *\r
833                          *  \param[in] Type             Type of pipe to configure, an \c EP_TYPE_* mask. Not all pipe types are available on Low\r
834                          *                              Speed USB devices - refer to the USB 2.0 specification.\r
835                          *\r
836                          *  \param[in] EndpointAddress  Endpoint address within the attached device that the pipe should interface to.\r
837                          *\r
838                          *  \param[in] Size             Size of the pipe's bank, where packets are stored before they are transmitted to\r
839                          *                              the USB device, or after they have been received from the USB device (depending on\r
840                          *                              the pipe's data direction). The bank size must indicate the maximum packet size that\r
841                          *                              the pipe can handle.\r
842                          *\r
843                          *  \param[in] Banks            Number of banks to use for the pipe being configured.\r
844                          *\r
845                          *  \attention When the \c ORDERED_EP_CONFIG compile time option is used, Pipes <b>must</b> be configured in ascending order,\r
846                          *             or bank corruption will occur.\r
847                          *\r
848                          *  \note Certain microcontroller model's pipes may have different maximum packet sizes based on the pipe's\r
849                          *        index - refer to the chosen microcontroller's datasheet to determine the maximum bank size for each pipe.\r
850                          *        \n\n\r
851                          *\r
852                          *  \note The default control pipe should not be manually configured by the user application, as it is\r
853                          *        automatically configured by the library internally.\r
854                          *        \n\n\r
855                          *\r
856                          *  \note This routine will automatically select the specified pipe upon success. Upon failure, the pipe which\r
857                          *        failed to reconfigure correctly will be selected.\r
858                          *\r
859                          *  \return Boolean \c true if the configuration succeeded, \c false otherwise.\r
860                          */\r
861                         bool Pipe_ConfigurePipe(const uint8_t Address,\r
862                                                 const uint8_t Type,\r
863                                                 const uint8_t EndpointAddress,\r
864                                                 const uint16_t Size,\r
865                                                 const uint8_t Banks);\r
866 \r
867                         /** Spin-loops until the currently selected non-control pipe is ready for the next packet of data to be read\r
868                          *  or written to it, aborting in the case of an error condition (such as a timeout or device disconnect).\r
869                          *\r
870                          *  \ingroup Group_PipeRW_AVR8\r
871                          *\r
872                          *  \return A value from the \ref Pipe_WaitUntilReady_ErrorCodes_t enum.\r
873                          */\r
874                         uint8_t Pipe_WaitUntilReady(void);\r
875 \r
876                         /** Determines if a pipe has been bound to the given device endpoint address. If a pipe which is bound to the given\r
877                          *  endpoint is found, it is automatically selected.\r
878                          *\r
879                          *  \param[in] EndpointAddress Address and direction mask of the endpoint within the attached device to check.\r
880                          *\r
881                          *  \return Boolean \c true if a pipe bound to the given endpoint address of the specified direction is found,\r
882                          *          \c false otherwise.\r
883                          */\r
884                         bool Pipe_IsEndpointBound(const uint8_t EndpointAddress) ATTR_WARN_UNUSED_RESULT;\r
885 \r
886         /* Private Interface - For use in library only: */\r
887         #if !defined(__DOXYGEN__)\r
888                 /* Macros: */\r
889                         #if !defined(ENDPOINT_CONTROLEP)\r
890                                 #define ENDPOINT_CONTROLEP          0\r
891                         #endif\r
892 \r
893                 /* Inline Functions: */\r
894                         static inline uint8_t Pipe_BytesToEPSizeMask(const uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE;\r
895                         static inline uint8_t Pipe_BytesToEPSizeMask(const uint16_t Bytes)\r
896                         {\r
897                                 uint8_t  MaskVal    = 0;\r
898                                 uint16_t CheckBytes = 8;\r
899 \r
900                                 while ((CheckBytes < Bytes) && (CheckBytes < PIPE_MAX_SIZE))\r
901                                 {\r
902                                         MaskVal++;\r
903                                         CheckBytes <<= 1;\r
904                                 }\r
905 \r
906                                 return (MaskVal << EPSIZE0);\r
907                         }\r
908 \r
909                 /* Function Prototypes: */\r
910                         void Pipe_ClearPipes(void);\r
911         #endif\r
912 \r
913         /* Disable C linkage for C++ Compilers: */\r
914                 #if defined(__cplusplus)\r
915                         }\r
916                 #endif\r
917 \r
918 #endif\r
919 \r
920 /** @} */\r
921 \r