]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/AVR8/Endpoint_AVR8.h
Squashed 'tmk_core/' content from commit 05caacc
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / AVR8 / Endpoint_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 Endpoint definitions for the AVR8 microcontrollers.\r
33  *  \copydetails Group_EndpointManagement_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_EndpointRW\r
40  *  \defgroup Group_EndpointRW_AVR8 Endpoint Data Reading and Writing (AVR8)\r
41  *  \brief Endpoint 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 endpoints.\r
44  */\r
45 \r
46 /** \ingroup Group_EndpointPrimitiveRW\r
47  *  \defgroup Group_EndpointPrimitiveRW_AVR8 Read/Write of Primitive Data Types (AVR8)\r
48  *  \brief Endpoint primitive 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 endpoints.\r
52  */\r
53 \r
54 /** \ingroup Group_EndpointPacketManagement\r
55  *  \defgroup Group_EndpointPacketManagement_AVR8 Endpoint Packet Management (AVR8)\r
56  *  \brief Endpoint packet management definitions for the Atmel AVR8 architecture.\r
57  *\r
58  *  Functions, macros, variables, enums and types related to packet management of endpoints.\r
59  */\r
60 \r
61 /** \ingroup Group_EndpointManagement\r
62  *  \defgroup Group_EndpointManagement_AVR8 Endpoint Management (AVR8)\r
63  *  \brief Endpoint management definitions for the Atmel AVR8 architecture.\r
64  *\r
65  *  Functions, macros and enums related to endpoint management when in USB Device mode. This\r
66  *  module contains the endpoint management macros, as well as endpoint interrupt and data\r
67  *  send/receive functions for various data types.\r
68  *\r
69  *  @{\r
70  */\r
71 \r
72 #ifndef __ENDPOINT_AVR8_H__\r
73 #define __ENDPOINT_AVR8_H__\r
74 \r
75         /* Includes: */\r
76                 #include "../../../../Common/Common.h"\r
77                 #include "../USBTask.h"\r
78                 #include "../USBInterrupt.h"\r
79 \r
80         /* Enable C linkage for C++ Compilers: */\r
81                 #if defined(__cplusplus)\r
82                         extern "C" {\r
83                 #endif\r
84 \r
85         /* Preprocessor Checks: */\r
86                 #if !defined(__INCLUDE_FROM_USB_DRIVER)\r
87                         #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.\r
88                 #endif\r
89 \r
90         /* Private Interface - For use in library only: */\r
91         #if !defined(__DOXYGEN__)\r
92                 /* Inline Functions: */\r
93                         static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST\r
94                                                                                                ATTR_ALWAYS_INLINE;\r
95                         static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes)\r
96                         {\r
97                                 uint8_t  MaskVal    = 0;\r
98                                 uint16_t CheckBytes = 8;\r
99 \r
100                                 while (CheckBytes < Bytes)\r
101                                 {\r
102                                         MaskVal++;\r
103                                         CheckBytes <<= 1;\r
104                                 }\r
105 \r
106                                 return (MaskVal << EPSIZE0);\r
107                         }\r
108 \r
109                 /* Function Prototypes: */\r
110                         void Endpoint_ClearEndpoints(void);\r
111                         bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,\r
112                                                             const uint8_t UECFG0XData,\r
113                                                             const uint8_t UECFG1XData);\r
114 \r
115         #endif\r
116 \r
117         /* Public Interface - May be used in end-application: */\r
118                 /* Macros: */\r
119                         #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))\r
120                                 /** Default size of the default control endpoint's bank, until altered by the control endpoint bank size\r
121                                  *  value in the device descriptor. Not available if the \c FIXED_CONTROL_ENDPOINT_SIZE token is defined.\r
122                                  */\r
123                                 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE     8\r
124                         #endif\r
125 \r
126                         #if !defined(CONTROL_ONLY_DEVICE) || defined(__DOXYGEN__)\r
127                                 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)\r
128                                         #define ENDPOINT_TOTAL_ENDPOINTS        7\r
129                                 #else\r
130                                         /** Total number of endpoints (including the default control endpoint at address 0) which may\r
131                                          *  be used in the device. Different USB AVR models support different amounts of endpoints,\r
132                                          *  this value reflects the maximum number of endpoints for the currently selected AVR model.\r
133                                          */\r
134                                         #define ENDPOINT_TOTAL_ENDPOINTS        5\r
135                                 #endif\r
136                         #else\r
137                                 #define ENDPOINT_TOTAL_ENDPOINTS            1\r
138                         #endif\r
139 \r
140                 /* Enums: */\r
141                         /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function.\r
142                          *\r
143                          *  \ingroup Group_EndpointRW_AVR8\r
144                          */\r
145                         enum Endpoint_WaitUntilReady_ErrorCodes_t\r
146                         {\r
147                                 ENDPOINT_READYWAIT_NoError                 = 0, /**< Endpoint is ready for next packet, no error. */\r
148                                 ENDPOINT_READYWAIT_EndpointStalled         = 1, /**< The endpoint was stalled during the stream\r
149                                                                                  *   transfer by the host or device.\r
150                                                                                  */\r
151                                 ENDPOINT_READYWAIT_DeviceDisconnected      = 2, /**< Device was disconnected from the host while\r
152                                                                                  *   waiting for the endpoint to become ready.\r
153                                                                                  */\r
154                                 ENDPOINT_READYWAIT_BusSuspended            = 3, /**< The USB bus has been suspended by the host and\r
155                                                                                  *   no USB endpoint traffic can occur until the bus\r
156                                                                                  *   has resumed.\r
157                                                                                  */\r
158                                 ENDPOINT_READYWAIT_Timeout                 = 4, /**< The host failed to accept or send the next packet\r
159                                                                                  *   within the software timeout period set by the\r
160                                                                                  *   \ref USB_STREAM_TIMEOUT_MS macro.\r
161                                                                                  */\r
162                         };\r
163 \r
164                 /* Inline Functions: */\r
165                         /** Configures the specified endpoint address with the given endpoint type, bank size and number of hardware\r
166                          *  banks. Once configured, the endpoint may be read from or written to, depending on its direction.\r
167                          *\r
168                          *  \param[in] Address    Endpoint address to configure.\r
169                          *\r
170                          *  \param[in] Type       Type of endpoint to configure, a \c EP_TYPE_* mask. Not all endpoint types\r
171                          *                        are available on Low Speed USB devices - refer to the USB 2.0 specification.\r
172                          *\r
173                          *  \param[in] Size       Size of the endpoint's bank, where packets are stored before they are transmitted\r
174                          *                        to the USB host, or after they have been received from the USB host (depending on\r
175                          *                        the endpoint's data direction). The bank size must indicate the maximum packet size\r
176                          *                        that the endpoint can handle.\r
177                          *\r
178                          *  \param[in] Banks      Number of banks to use for the endpoint being configured.\r
179                          *\r
180                          *  \attention When the \c ORDERED_EP_CONFIG compile time option is used, Endpoints <b>must</b> be configured in\r
181                          *             ascending order, or bank corruption will occur.\r
182                          *\r
183                          *  \note Different endpoints may have different maximum packet sizes based on the endpoint's index - please\r
184                          *        refer to the chosen microcontroller model's datasheet to determine the maximum bank size for each endpoint.\r
185                          *        \n\n\r
186                          *\r
187                          *  \note The default control endpoint should not be manually configured by the user application, as\r
188                          *        it is automatically configured by the library internally.\r
189                          *        \n\n\r
190                          *\r
191                          *  \note This routine will automatically select the specified endpoint upon success. Upon failure, the endpoint\r
192                          *        which failed to reconfigure correctly will be selected.\r
193                          *\r
194                          *  \return Boolean \c true if the configuration succeeded, \c false otherwise.\r
195                          */\r
196                         static inline bool Endpoint_ConfigureEndpoint(const uint8_t Address,\r
197                                                                       const uint8_t Type,\r
198                                                                       const uint16_t Size,\r
199                                                                       const uint8_t Banks) ATTR_ALWAYS_INLINE;\r
200                         static inline bool Endpoint_ConfigureEndpoint(const uint8_t Address,\r
201                                                                       const uint8_t Type,\r
202                                                                       const uint16_t Size,\r
203                                                                       const uint8_t Banks)\r
204                         {\r
205                                 uint8_t Number = (Address & ENDPOINT_EPNUM_MASK);\r
206                         \r
207                                 if (Number >= ENDPOINT_TOTAL_ENDPOINTS)\r
208                                   return false;\r
209                         \r
210                                 return Endpoint_ConfigureEndpoint_Prv(Number,\r
211                                                                       ((Type << EPTYPE0) | ((Address & ENDPOINT_DIR_IN) ? (1 << EPDIR) : 0)),\r
212                                                                       ((1 << ALLOC) | ((Banks > 1) ? (1 << EPBK0) : 0) | Endpoint_BytesToEPSizeMask(Size)));\r
213                         }\r
214 \r
215                         /** Indicates the number of bytes currently stored in the current endpoint's selected bank.\r
216                          *\r
217                          *  \ingroup Group_EndpointRW_AVR8\r
218                          *\r
219                          *  \return Total number of bytes in the currently selected Endpoint's FIFO buffer.\r
220                          */\r
221                         static inline uint16_t Endpoint_BytesInEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
222                         static inline uint16_t Endpoint_BytesInEndpoint(void)\r
223                         {\r
224                                 #if (defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)) && !defined(__AVR_ATmega32U6__)\r
225                                         return UEBCX;\r
226                                 #elif defined(USB_SERIES_4_AVR) || defined(__AVR_ATmega32U6__)\r
227                                         return (((uint16_t)UEBCHX << 8) | UEBCLX);\r
228                                 #elif defined(USB_SERIES_2_AVR)\r
229                                         return UEBCLX;\r
230                                 #endif\r
231                         }\r
232 \r
233                         /** Determines the currently selected endpoint's direction.\r
234                          *\r
235                          *  \return The currently selected endpoint's direction, as a \c ENDPOINT_DIR_* mask.\r
236                          */\r
237                         static inline uint8_t Endpoint_GetEndpointDirection(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
238                         static inline uint8_t Endpoint_GetEndpointDirection(void)\r
239                         {\r
240                                 return (UECFG0X & (1 << EPDIR)) ? ENDPOINT_DIR_IN : ENDPOINT_DIR_OUT;\r
241                         }\r
242 \r
243                         /** Get the endpoint address of the currently selected endpoint. This is typically used to save\r
244                          *  the currently selected endpoint so that it can be restored after another endpoint has been\r
245                          *  manipulated.\r
246                          *\r
247                          *  \return Index of the currently selected endpoint.\r
248                          */\r
249                         static inline uint8_t Endpoint_GetCurrentEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
250                         static inline uint8_t Endpoint_GetCurrentEndpoint(void)\r
251                         {\r
252                                 #if !defined(CONTROL_ONLY_DEVICE)\r
253                                         return ((UENUM & ENDPOINT_EPNUM_MASK) | Endpoint_GetEndpointDirection());\r
254                                 #else\r
255                                         return ENDPOINT_CONTROLEP;\r
256                                 #endif\r
257                         }\r
258 \r
259                         /** Selects the given endpoint address.\r
260                          *\r
261                          *  Any endpoint operations which do not require the endpoint address to be indicated will operate on\r
262                          *  the currently selected endpoint.\r
263                          *\r
264                          *  \param[in] Address Endpoint address to select.\r
265                          */\r
266                         static inline void Endpoint_SelectEndpoint(const uint8_t Address) ATTR_ALWAYS_INLINE;\r
267                         static inline void Endpoint_SelectEndpoint(const uint8_t Address)\r
268                         {\r
269                                 #if !defined(CONTROL_ONLY_DEVICE)\r
270                                         UENUM = (Address & ENDPOINT_EPNUM_MASK);\r
271                                 #endif\r
272                         }\r
273 \r
274                         /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's\r
275                          *  data In and Out pointers to the bank's contents.\r
276                          *\r
277                          *  \param[in] Address  Endpoint address whose FIFO buffers are to be reset.\r
278                          */\r
279                         static inline void Endpoint_ResetEndpoint(const uint8_t Address) ATTR_ALWAYS_INLINE;\r
280                         static inline void Endpoint_ResetEndpoint(const uint8_t Address)\r
281                         {\r
282                                 UERST = (1 << (Address & ENDPOINT_EPNUM_MASK));\r
283                                 UERST = 0;\r
284                         }\r
285 \r
286                         /** Enables the currently selected endpoint so that data can be sent and received through it to\r
287                          *  and from a host.\r
288                          *\r
289                          *  \note Endpoints must first be configured properly via \ref Endpoint_ConfigureEndpoint().\r
290                          */\r
291                         static inline void Endpoint_EnableEndpoint(void) ATTR_ALWAYS_INLINE;\r
292                         static inline void Endpoint_EnableEndpoint(void)\r
293                         {\r
294                                 UECONX |= (1 << EPEN);\r
295                         }\r
296 \r
297                         /** Disables the currently selected endpoint so that data cannot be sent and received through it\r
298                          *  to and from a host.\r
299                          */\r
300                         static inline void Endpoint_DisableEndpoint(void) ATTR_ALWAYS_INLINE;\r
301                         static inline void Endpoint_DisableEndpoint(void)\r
302                         {\r
303                                 UECONX &= ~(1 << EPEN);\r
304                         }\r
305 \r
306                         /** Determines if the currently selected endpoint is enabled, but not necessarily configured.\r
307                          *\r
308                          * \return Boolean \c true if the currently selected endpoint is enabled, \c false otherwise.\r
309                          */\r
310                         static inline bool Endpoint_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
311                         static inline bool Endpoint_IsEnabled(void)\r
312                         {\r
313                                 return ((UECONX & (1 << EPEN)) ? true : false);\r
314                         }\r
315 \r
316                         /** Retrieves the number of busy banks in the currently selected endpoint, which have been queued for\r
317                          *  transmission via the \ref Endpoint_ClearIN() command, or are awaiting acknowledgement via the\r
318                          *  \ref Endpoint_ClearOUT() command.\r
319                          *\r
320                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
321                          *\r
322                          *  \return Total number of busy banks in the selected endpoint.\r
323                          */\r
324                         static inline uint8_t Endpoint_GetBusyBanks(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;\r
325                         static inline uint8_t Endpoint_GetBusyBanks(void)\r
326                         {\r
327                                 return (UESTA0X & (0x03 << NBUSYBK0));\r
328                         }\r
329 \r
330                         /** Aborts all pending IN transactions on the currently selected endpoint, once the bank\r
331                          *  has been queued for transmission to the host via \ref Endpoint_ClearIN(). This function\r
332                          *  will terminate all queued transactions, resetting the endpoint banks ready for a new\r
333                          *  packet.\r
334                          *\r
335                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
336                          */\r
337                         static inline void Endpoint_AbortPendingIN(void)\r
338                         {\r
339                                 while (Endpoint_GetBusyBanks() != 0)\r
340                                 {\r
341                                         UEINTX |= (1 << RXOUTI);\r
342                                         while (UEINTX & (1 << RXOUTI));\r
343                                 }\r
344                         }\r
345 \r
346                         /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint\r
347                          *  bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN\r
348                          *  direction). This function will return false if an error has occurred in the endpoint, if the endpoint\r
349                          *  is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN\r
350                          *  direction and the endpoint bank is full.\r
351                          *\r
352                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
353                          *\r
354                          *  \return Boolean \c true if the currently selected endpoint may be read from or written to, depending\r
355                          *          on its direction.\r
356                          */\r
357                         static inline bool Endpoint_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
358                         static inline bool Endpoint_IsReadWriteAllowed(void)\r
359                         {\r
360                                 return ((UEINTX & (1 << RWAL)) ? true : false);\r
361                         }\r
362 \r
363                         /** Determines if the currently selected endpoint is configured.\r
364                          *\r
365                          *  \return Boolean \c true if the currently selected endpoint has been configured, \c false otherwise.\r
366                          */\r
367                         static inline bool Endpoint_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
368                         static inline bool Endpoint_IsConfigured(void)\r
369                         {\r
370                                 return ((UESTA0X & (1 << CFGOK)) ? true : false);\r
371                         }\r
372 \r
373                         /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their\r
374                          *  interrupt duration has elapsed. Which endpoints have interrupted can be determined by\r
375                          *  masking the return value against <tt>(1 << <i>{Endpoint Number}</i>)</tt>.\r
376                          *\r
377                          *  \return Mask whose bits indicate which endpoints have interrupted.\r
378                          */\r
379                         static inline uint8_t Endpoint_GetEndpointInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
380                         static inline uint8_t Endpoint_GetEndpointInterrupts(void)\r
381                         {\r
382                                 return UEINT;\r
383                         }\r
384 \r
385                         /** Determines if the specified endpoint number has interrupted (valid only for INTERRUPT type\r
386                          *  endpoints).\r
387                          *\r
388                          *  \param[in] Address  Address of the endpoint whose interrupt flag should be tested.\r
389                          *\r
390                          *  \return Boolean \c true if the specified endpoint has interrupted, \c false otherwise.\r
391                          */\r
392                         static inline bool Endpoint_HasEndpointInterrupted(const uint8_t Address) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
393                         static inline bool Endpoint_HasEndpointInterrupted(const uint8_t Address)\r
394                         {\r
395                                 return ((Endpoint_GetEndpointInterrupts() & (1 << (Address & ENDPOINT_EPNUM_MASK))) ? true : false);\r
396                         }\r
397 \r
398                         /** Determines if the selected IN endpoint is ready for a new packet to be sent to the host.\r
399                          *\r
400                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
401                          *\r
402                          *  \return Boolean \c true if the current endpoint is ready for an IN packet, \c false otherwise.\r
403                          */\r
404                         static inline bool Endpoint_IsINReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
405                         static inline bool Endpoint_IsINReady(void)\r
406                         {\r
407                                 return ((UEINTX & (1 << TXINI)) ? true : false);\r
408                         }\r
409 \r
410                         /** Determines if the selected OUT endpoint has received new packet from the host.\r
411                          *\r
412                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
413                          *\r
414                          *  \return Boolean \c true if current endpoint is has received an OUT packet, \c false otherwise.\r
415                          */\r
416                         static inline bool Endpoint_IsOUTReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
417                         static inline bool Endpoint_IsOUTReceived(void)\r
418                         {\r
419                                 return ((UEINTX & (1 << RXOUTI)) ? true : false);\r
420                         }\r
421 \r
422                         /** Determines if the current CONTROL type endpoint has received a SETUP packet.\r
423                          *\r
424                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
425                          *\r
426                          *  \return Boolean \c true if the selected endpoint has received a SETUP packet, \c false otherwise.\r
427                          */\r
428                         static inline bool Endpoint_IsSETUPReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
429                         static inline bool Endpoint_IsSETUPReceived(void)\r
430                         {\r
431                                 return ((UEINTX & (1 << RXSTPI)) ? true : false);\r
432                         }\r
433 \r
434                         /** Clears a received SETUP packet on the currently selected CONTROL type endpoint, freeing up the\r
435                          *  endpoint for the next packet.\r
436                          *\r
437                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
438                          *\r
439                          *  \note This is not applicable for non CONTROL type endpoints.\r
440                          */\r
441                         static inline void Endpoint_ClearSETUP(void) ATTR_ALWAYS_INLINE;\r
442                         static inline void Endpoint_ClearSETUP(void)\r
443                         {\r
444                                 UEINTX &= ~(1 << RXSTPI);\r
445                         }\r
446 \r
447                         /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the\r
448                          *  next packet and switching to the alternative endpoint bank if double banked.\r
449                          *\r
450                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
451                          */\r
452                         static inline void Endpoint_ClearIN(void) ATTR_ALWAYS_INLINE;\r
453                         static inline void Endpoint_ClearIN(void)\r
454                         {\r
455                                 #if !defined(CONTROL_ONLY_DEVICE)\r
456                                         UEINTX &= ~((1 << TXINI) | (1 << FIFOCON));\r
457                                 #else\r
458                                         UEINTX &= ~(1 << TXINI);\r
459                                 #endif\r
460                         }\r
461 \r
462                         /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint\r
463                          *  for the next packet and switching to the alternative endpoint bank if double banked.\r
464                          *\r
465                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
466                          */\r
467                         static inline void Endpoint_ClearOUT(void) ATTR_ALWAYS_INLINE;\r
468                         static inline void Endpoint_ClearOUT(void)\r
469                         {\r
470                                 #if !defined(CONTROL_ONLY_DEVICE)\r
471                                         UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON));\r
472                                 #else\r
473                                         UEINTX &= ~(1 << RXOUTI);\r
474                                 #endif\r
475                         }\r
476 \r
477                         /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the\r
478                          *  indicated endpoint and that the current transfer sequence should be aborted. This provides a\r
479                          *  way for devices to indicate invalid commands to the host so that the current transfer can be\r
480                          *  aborted and the host can begin its own recovery sequence.\r
481                          *\r
482                          *  The currently selected endpoint remains stalled until either the \ref Endpoint_ClearStall() macro\r
483                          *  is called, or the host issues a CLEAR FEATURE request to the device for the currently selected\r
484                          *  endpoint.\r
485                          *\r
486                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
487                          */\r
488                         static inline void Endpoint_StallTransaction(void) ATTR_ALWAYS_INLINE;\r
489                         static inline void Endpoint_StallTransaction(void)\r
490                         {\r
491                                 UECONX |= (1 << STALLRQ);\r
492                         }\r
493 \r
494                         /** Clears the STALL condition on the currently selected endpoint.\r
495                          *\r
496                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
497                          */\r
498                         static inline void Endpoint_ClearStall(void) ATTR_ALWAYS_INLINE;\r
499                         static inline void Endpoint_ClearStall(void)\r
500                         {\r
501                                 UECONX |= (1 << STALLRQC);\r
502                         }\r
503 \r
504                         /** Determines if the currently selected endpoint is stalled, false otherwise.\r
505                          *\r
506                          *  \ingroup Group_EndpointPacketManagement_AVR8\r
507                          *\r
508                          *  \return Boolean \c true if the currently selected endpoint is stalled, \c false otherwise.\r
509                          */\r
510                         static inline bool Endpoint_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
511                         static inline bool Endpoint_IsStalled(void)\r
512                         {\r
513                                 return ((UECONX & (1 << STALLRQ)) ? true : false);\r
514                         }\r
515 \r
516                         /** Resets the data toggle of the currently selected endpoint. */\r
517                         static inline void Endpoint_ResetDataToggle(void) ATTR_ALWAYS_INLINE;\r
518                         static inline void Endpoint_ResetDataToggle(void)\r
519                         {\r
520                                 UECONX |= (1 << RSTDT);\r
521                         }\r
522 \r
523                         /** Sets the direction of the currently selected endpoint.\r
524                          *\r
525                          *  \param[in] DirectionMask  New endpoint direction, as a \c ENDPOINT_DIR_* mask.\r
526                          */\r
527                         static inline void Endpoint_SetEndpointDirection(const uint8_t DirectionMask) ATTR_ALWAYS_INLINE;\r
528                         static inline void Endpoint_SetEndpointDirection(const uint8_t DirectionMask)\r
529                         {\r
530                                 UECFG0X = ((UECFG0X & ~(1 << EPDIR)) | (DirectionMask ? (1 << EPDIR) : 0));\r
531                         }\r
532 \r
533                         /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints.\r
534                          *\r
535                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
536                          *\r
537                          *  \return Next byte in the currently selected endpoint's FIFO buffer.\r
538                          */\r
539                         static inline uint8_t Endpoint_Read_8(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
540                         static inline uint8_t Endpoint_Read_8(void)\r
541                         {\r
542                                 return UEDATX;\r
543                         }\r
544 \r
545                         /** Writes one byte to the currently selected endpoint's bank, for IN direction endpoints.\r
546                          *\r
547                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
548                          *\r
549                          *  \param[in] Data  Data to write into the the currently selected endpoint's FIFO buffer.\r
550                          */\r
551                         static inline void Endpoint_Write_8(const uint8_t Data) ATTR_ALWAYS_INLINE;\r
552                         static inline void Endpoint_Write_8(const uint8_t Data)\r
553                         {\r
554                                 UEDATX = Data;\r
555                         }\r
556 \r
557                         /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints.\r
558                          *\r
559                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
560                          */\r
561                         static inline void Endpoint_Discard_8(void) ATTR_ALWAYS_INLINE;\r
562                         static inline void Endpoint_Discard_8(void)\r
563                         {\r
564                                 uint8_t Dummy;\r
565 \r
566                                 Dummy = UEDATX;\r
567 \r
568                                 (void)Dummy;\r
569                         }\r
570 \r
571                         /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT\r
572                          *  direction endpoints.\r
573                          *\r
574                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
575                          *\r
576                          *  \return Next two bytes in the currently selected endpoint's FIFO buffer.\r
577                          */\r
578                         static inline uint16_t Endpoint_Read_16_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
579                         static inline uint16_t Endpoint_Read_16_LE(void)\r
580                         {\r
581                                 union\r
582                                 {\r
583                                         uint16_t Value;\r
584                                         uint8_t  Bytes[2];\r
585                                 } Data;\r
586 \r
587                                 Data.Bytes[0] = UEDATX;\r
588                                 Data.Bytes[1] = UEDATX;\r
589 \r
590                                 return Data.Value;\r
591                         }\r
592 \r
593                         /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT\r
594                          *  direction endpoints.\r
595                          *\r
596                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
597                          *\r
598                          *  \return Next two bytes in the currently selected endpoint's FIFO buffer.\r
599                          */\r
600                         static inline uint16_t Endpoint_Read_16_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
601                         static inline uint16_t Endpoint_Read_16_BE(void)\r
602                         {\r
603                                 union\r
604                                 {\r
605                                         uint16_t Value;\r
606                                         uint8_t  Bytes[2];\r
607                                 } Data;\r
608 \r
609                                 Data.Bytes[1] = UEDATX;\r
610                                 Data.Bytes[0] = UEDATX;\r
611 \r
612                                 return Data.Value;\r
613                         }\r
614 \r
615                         /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN\r
616                          *  direction endpoints.\r
617                          *\r
618                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
619                          *\r
620                          *  \param[in] Data  Data to write to the currently selected endpoint's FIFO buffer.\r
621                          */\r
622                         static inline void Endpoint_Write_16_LE(const uint16_t Data) ATTR_ALWAYS_INLINE;\r
623                         static inline void Endpoint_Write_16_LE(const uint16_t Data)\r
624                         {\r
625                                 UEDATX = (Data & 0xFF);\r
626                                 UEDATX = (Data >> 8);\r
627                         }\r
628 \r
629                         /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN\r
630                          *  direction endpoints.\r
631                          *\r
632                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
633                          *\r
634                          *  \param[in] Data  Data to write to the currently selected endpoint's FIFO buffer.\r
635                          */\r
636                         static inline void Endpoint_Write_16_BE(const uint16_t Data) ATTR_ALWAYS_INLINE;\r
637                         static inline void Endpoint_Write_16_BE(const uint16_t Data)\r
638                         {\r
639                                 UEDATX = (Data >> 8);\r
640                                 UEDATX = (Data & 0xFF);\r
641                         }\r
642 \r
643                         /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints.\r
644                          *\r
645                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
646                          */\r
647                         static inline void Endpoint_Discard_16(void) ATTR_ALWAYS_INLINE;\r
648                         static inline void Endpoint_Discard_16(void)\r
649                         {\r
650                                 uint8_t Dummy;\r
651 \r
652                                 Dummy = UEDATX;\r
653                                 Dummy = UEDATX;\r
654 \r
655                                 (void)Dummy;\r
656                         }\r
657 \r
658                         /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT\r
659                          *  direction endpoints.\r
660                          *\r
661                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
662                          *\r
663                          *  \return Next four bytes in the currently selected endpoint's FIFO buffer.\r
664                          */\r
665                         static inline uint32_t Endpoint_Read_32_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
666                         static inline uint32_t Endpoint_Read_32_LE(void)\r
667                         {\r
668                                 union\r
669                                 {\r
670                                         uint32_t Value;\r
671                                         uint8_t  Bytes[4];\r
672                                 } Data;\r
673 \r
674                                 Data.Bytes[0] = UEDATX;\r
675                                 Data.Bytes[1] = UEDATX;\r
676                                 Data.Bytes[2] = UEDATX;\r
677                                 Data.Bytes[3] = UEDATX;\r
678 \r
679                                 return Data.Value;\r
680                         }\r
681 \r
682                         /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT\r
683                          *  direction endpoints.\r
684                          *\r
685                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
686                          *\r
687                          *  \return Next four bytes in the currently selected endpoint's FIFO buffer.\r
688                          */\r
689                         static inline uint32_t Endpoint_Read_32_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
690                         static inline uint32_t Endpoint_Read_32_BE(void)\r
691                         {\r
692                                 union\r
693                                 {\r
694                                         uint32_t Value;\r
695                                         uint8_t  Bytes[4];\r
696                                 } Data;\r
697 \r
698                                 Data.Bytes[3] = UEDATX;\r
699                                 Data.Bytes[2] = UEDATX;\r
700                                 Data.Bytes[1] = UEDATX;\r
701                                 Data.Bytes[0] = UEDATX;\r
702 \r
703                                 return Data.Value;\r
704                         }\r
705 \r
706                         /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN\r
707                          *  direction endpoints.\r
708                          *\r
709                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
710                          *\r
711                          *  \param[in] Data  Data to write to the currently selected endpoint's FIFO buffer.\r
712                          */\r
713                         static inline void Endpoint_Write_32_LE(const uint32_t Data) ATTR_ALWAYS_INLINE;\r
714                         static inline void Endpoint_Write_32_LE(const uint32_t Data)\r
715                         {\r
716                                 UEDATX = (Data &  0xFF);\r
717                                 UEDATX = (Data >> 8);\r
718                                 UEDATX = (Data >> 16);\r
719                                 UEDATX = (Data >> 24);\r
720                         }\r
721 \r
722                         /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN\r
723                          *  direction endpoints.\r
724                          *\r
725                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
726                          *\r
727                          *  \param[in] Data  Data to write to the currently selected endpoint's FIFO buffer.\r
728                          */\r
729                         static inline void Endpoint_Write_32_BE(const uint32_t Data) ATTR_ALWAYS_INLINE;\r
730                         static inline void Endpoint_Write_32_BE(const uint32_t Data)\r
731                         {\r
732                                 UEDATX = (Data >> 24);\r
733                                 UEDATX = (Data >> 16);\r
734                                 UEDATX = (Data >> 8);\r
735                                 UEDATX = (Data &  0xFF);\r
736                         }\r
737 \r
738                         /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.\r
739                          *\r
740                          *  \ingroup Group_EndpointPrimitiveRW_AVR8\r
741                          */\r
742                         static inline void Endpoint_Discard_32(void) ATTR_ALWAYS_INLINE;\r
743                         static inline void Endpoint_Discard_32(void)\r
744                         {\r
745                                 uint8_t Dummy;\r
746 \r
747                                 Dummy = UEDATX;\r
748                                 Dummy = UEDATX;\r
749                                 Dummy = UEDATX;\r
750                                 Dummy = UEDATX;\r
751 \r
752                                 (void)Dummy;\r
753                         }\r
754 \r
755                 /* External Variables: */\r
756                         /** Global indicating the maximum packet size of the default control endpoint located at address\r
757                          *  0 in the device. This value is set to the value indicated in the device descriptor in the user\r
758                          *  project once the USB interface is initialized into device mode.\r
759                          *\r
760                          *  If space is an issue, it is possible to fix this to a static value by defining the control\r
761                          *  endpoint size in the \c FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile\r
762                          *  via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically\r
763                          *  read from the descriptors at runtime and instead fixed to the given value. When used, it is\r
764                          *  important that the descriptor control endpoint size value matches the size given as the\r
765                          *  \c FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the \c FIXED_CONTROL_ENDPOINT_SIZE token\r
766                          *  be used in the device descriptors to ensure this.\r
767                          *\r
768                          *  \attention This variable should be treated as read-only in the user application, and never manually\r
769                          *             changed in value.\r
770                          */\r
771                         #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))\r
772                                 extern uint8_t USB_Device_ControlEndpointSize;\r
773                         #else\r
774                                 #define USB_Device_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE\r
775                         #endif\r
776 \r
777                 /* Function Prototypes: */\r
778                         /** Configures a table of endpoint descriptions, in sequence. This function can be used to configure multiple\r
779                          *  endpoints at the same time.\r
780                          *\r
781                          *  \note Endpoints with a zero address will be ignored, thus this function cannot be used to configure the\r
782                          *        control endpoint.\r
783                          *\r
784                          *  \param[in] Table    Pointer to a table of endpoint descriptions.\r
785                          *  \param[in] Entries  Number of entries in the endpoint table to configure.\r
786                          *\r
787                          *  \return Boolean \c true if all endpoints configured successfully, \c false otherwise.\r
788                          */\r
789                         bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,\r
790                                                              const uint8_t Entries);\r
791 \r
792                         /** Completes the status stage of a control transfer on a CONTROL type endpoint automatically,\r
793                          *  with respect to the data direction. This is a convenience function which can be used to\r
794                          *  simplify user control request handling.\r
795                          *\r
796                          *  \note This routine should not be called on non CONTROL type endpoints.\r
797                          */\r
798                         void Endpoint_ClearStatusStage(void);\r
799 \r
800                         /** Spin-loops until the currently selected non-control endpoint is ready for the next packet of data\r
801                          *  to be read or written to it.\r
802                          *\r
803                          *  \note This routine should not be called on CONTROL type endpoints.\r
804                          *\r
805                          *  \ingroup Group_EndpointRW_AVR8\r
806                          *\r
807                          *  \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.\r
808                          */\r
809                         uint8_t Endpoint_WaitUntilReady(void);\r
810 \r
811         /* Disable C linkage for C++ Compilers: */\r
812                 #if defined(__cplusplus)\r
813                         }\r
814                 #endif\r
815 \r
816 #endif\r
817 \r
818 /** @} */\r
819 \r