]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/UC3/Endpoint_UC3.h
Squashed 'tmk_core/' content from commit 05caacc
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / UC3 / Endpoint_UC3.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 AVR32 UC3 microcontrollers.\r
33  *  \copydetails Group_EndpointManagement_UC3\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_UC3 Endpoint Data Reading and Writing (UC3)\r
41  *  \brief Endpoint data read/write definitions for the Atmel AVR32 UC3 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_UC3 Read/Write of Primitive Data Types (UC3)\r
48  *  \brief Endpoint primitive read/write definitions for the Atmel AVR32 UC3 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_UC3 Endpoint Packet Management (UC3)\r
56  *  \brief Endpoint packet management definitions for the Atmel AVR32 UC3 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_UC3 Endpoint Management (UC3)\r
63  *  \brief Endpoint management definitions for the Atmel AVR32 UC3 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_UC3_H__\r
73 #define __ENDPOINT_UC3_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                 /* Macros: */\r
93                         #define ENDPOINT_HSB_ADDRESS_SPACE_SIZE            (64 * 1024UL)\r
94 \r
95                 /* Inline Functions: */\r
96                         static inline uint32_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST\r
97                                                                                                 ATTR_ALWAYS_INLINE;\r
98                         static inline uint32_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes)\r
99                         {\r
100                                 uint8_t  MaskVal    = 0;\r
101                                 uint16_t CheckBytes = 8;\r
102 \r
103                                 while (CheckBytes < Bytes)\r
104                                 {\r
105                                         MaskVal++;\r
106                                         CheckBytes <<= 1;\r
107                                 }\r
108 \r
109                                 return (MaskVal << AVR32_USBB_EPSIZE_OFFSET);\r
110                         }\r
111 \r
112                 /* Function Prototypes: */\r
113                         void Endpoint_ClearEndpoints(void);\r
114                         bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,\r
115                                                             const uint32_t UECFGXData);\r
116 \r
117                 /* External Variables: */\r
118                         extern volatile uint32_t USB_Endpoint_SelectedEndpoint;\r
119                         extern volatile uint8_t* USB_Endpoint_FIFOPos[];\r
120         #endif\r
121 \r
122         /* Public Interface - May be used in end-application: */\r
123                 /* Macros: */\r
124                         #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))\r
125                                 /** Default size of the default control endpoint's bank, until altered by the control endpoint bank size\r
126                                  *  value in the device descriptor. Not available if the \c FIXED_CONTROL_ENDPOINT_SIZE token is defined.\r
127                                  */\r
128                                 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE     8\r
129                         #endif\r
130 \r
131                         #if !defined(CONTROL_ONLY_DEVICE) || defined(__DOXYGEN__)\r
132                                 #if defined(USB_SERIES_UC3A3_AVR32) || defined(USB_SERIES_UC3A4_AVR32)\r
133                                         #define ENDPOINT_TOTAL_ENDPOINTS        8\r
134                                 #else\r
135                                         /** Total number of endpoints (including the default control endpoint at address 0) which may\r
136                                          *  be used in the device. Different AVR models support different amounts of endpoints,\r
137                                          *  this value reflects the maximum number of endpoints for the currently selected AVR model.\r
138                                          */\r
139                                         #define ENDPOINT_TOTAL_ENDPOINTS        7\r
140                                 #endif\r
141                         #else\r
142                                 #define ENDPOINT_TOTAL_ENDPOINTS            1\r
143                         #endif\r
144 \r
145                 /* Enums: */\r
146                         /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function.\r
147                          *\r
148                          *  \ingroup Group_EndpointRW_UC3\r
149                          */\r
150                         enum Endpoint_WaitUntilReady_ErrorCodes_t\r
151                         {\r
152                                 ENDPOINT_READYWAIT_NoError                 = 0, /**< Endpoint is ready for next packet, no error. */\r
153                                 ENDPOINT_READYWAIT_EndpointStalled         = 1, /**< The endpoint was stalled during the stream\r
154                                                                                  *   transfer by the host or device.\r
155                                                                                  */\r
156                                 ENDPOINT_READYWAIT_DeviceDisconnected      = 2, /**< Device was disconnected from the host while\r
157                                                                                  *   waiting for the endpoint to become ready.\r
158                                                                                  */\r
159                                 ENDPOINT_READYWAIT_BusSuspended            = 3, /**< The USB bus has been suspended by the host and\r
160                                                                                  *   no USB endpoint traffic can occur until the bus\r
161                                                                                  *   has resumed.\r
162                                                                                  */\r
163                                 ENDPOINT_READYWAIT_Timeout                 = 4, /**< The host failed to accept or send the next packet\r
164                                                                                  *   within the software timeout period set by the\r
165                                                                                  *   \ref USB_STREAM_TIMEOUT_MS macro.\r
166                                                                                  */\r
167                         };\r
168 \r
169                 /* Inline Functions: */\r
170                         /** Configures the specified endpoint address with the given endpoint type, direction, bank size\r
171                          *  and banking mode. Once configured, the endpoint may be read from or written to, depending\r
172                          *  on its direction.\r
173                          *\r
174                          *  \param[in] Address    Endpoint address to configure.\r
175                          *\r
176                          *  \param[in] Type       Type of endpoint to configure, a \c EP_TYPE_* mask. Not all endpoint types\r
177                          *                        are available on Low Speed USB devices - refer to the USB 2.0 specification.\r
178                          *\r
179                          *  \param[in] Size       Size of the endpoint's bank, where packets are stored before they are transmitted\r
180                          *                        to the USB host, or after they have been received from the USB host (depending on\r
181                          *                        the endpoint's data direction). The bank size must indicate the maximum packet size\r
182                          *                        that the endpoint can handle.\r
183                          *\r
184                          *  \param[in] Banks      Number of hardware banks to use for the endpoint being configured.\r
185                          *\r
186                          *  \attention When the \c ORDERED_EP_CONFIG compile time option is used, Endpoints <b>must</b> be configured in\r
187                          *             ascending order, or bank corruption will occur.\r
188                          *\r
189                          *  \note Different endpoints may have different maximum packet sizes based on the endpoint's index - refer to\r
190                          *        the chosen microcontroller model's datasheet to determine the maximum bank size for each endpoint.\r
191                          *        \n\n\r
192                          *\r
193                          *  \note The default control endpoint should not be manually configured by the user application, as\r
194                          *        it is automatically configured by the library internally.\r
195                          *        \n\n\r
196                          *\r
197                          *  \note This routine will automatically select the specified endpoint upon success. Upon failure, the endpoint\r
198                          *        which failed to reconfigure correctly will be selected.\r
199                          *\r
200                          *  \return Boolean \c true if the configuration succeeded, \c false otherwise.\r
201                          */\r
202                         static inline bool Endpoint_ConfigureEndpoint(const uint8_t Address,\r
203                                                                       const uint8_t Type,\r
204                                                                       const uint16_t Size,\r
205                                                                       const uint8_t Banks) ATTR_ALWAYS_INLINE;\r
206                         static inline bool Endpoint_ConfigureEndpoint(const uint8_t Address,\r
207                                                                       const uint8_t Type,\r
208                                                                       const uint16_t Size,\r
209                                                                       const uint8_t Banks)\r
210                         {\r
211                                 uint8_t Number = (Address & ENDPOINT_EPNUM_MASK);\r
212                         \r
213                                 if (Number >= ENDPOINT_TOTAL_ENDPOINTS)\r
214                                   return false;\r
215 \r
216                                 return Endpoint_ConfigureEndpoint_Prv(Number,\r
217                                                                       (AVR32_USBB_ALLOC_MASK |\r
218                                                                        ((uint32_t)Type << AVR32_USBB_EPTYPE_OFFSET) |\r
219                                                                        ((Address & ENDPOINT_DIR_IN) ? AVR32_USBB_UECFG0_EPDIR_MASK : 0) |\r
220                                                                        ((Banks > 1) ? AVR32_USBB_UECFG0_EPBK_SINGLE : AVR32_USBB_UECFG0_EPBK_DOUBLE) |\r
221                                                                        Endpoint_BytesToEPSizeMask(Size)));\r
222                         }\r
223 \r
224                         /** Indicates the number of bytes currently stored in the current endpoint's selected bank.\r
225                          *\r
226                          *  \ingroup Group_EndpointRW_UC3\r
227                          *\r
228                          *  \return Total number of bytes in the currently selected Endpoint's FIFO buffer.\r
229                          */\r
230                         static inline uint16_t Endpoint_BytesInEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
231                         static inline uint16_t Endpoint_BytesInEndpoint(void)\r
232                         {\r
233                                 return (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].byct;\r
234                         }\r
235 \r
236                         /** Determines the currently selected endpoint's direction.\r
237                          *\r
238                          *  \return The currently selected endpoint's direction, as a \c ENDPOINT_DIR_* mask.\r
239                          */\r
240                         static inline uint32_t Endpoint_GetEndpointDirection(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
241                         static inline uint32_t Endpoint_GetEndpointDirection(void)\r
242                         {\r
243                                 return ((&AVR32_USBB.UECFG0)[USB_Endpoint_SelectedEndpoint].epdir ? ENDPOINT_DIR_IN : ENDPOINT_DIR_OUT);\r
244                         }\r
245 \r
246                         /** Get the endpoint address of the currently selected endpoint. This is typically used to save\r
247                          *  the currently selected endpoint so that it can be restored after another endpoint has been\r
248                          *  manipulated.\r
249                          *\r
250                          *  \return Index of the currently selected endpoint.\r
251                          */\r
252                         static inline uint8_t Endpoint_GetCurrentEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
253                         static inline uint8_t Endpoint_GetCurrentEndpoint(void)\r
254                         {\r
255                                 return (USB_Endpoint_SelectedEndpoint | Endpoint_GetEndpointDirection());\r
256                         }\r
257 \r
258                         /** Selects the given endpoint address.\r
259                          *\r
260                          *  Any endpoint operations which do not require the endpoint address to be indicated will operate on\r
261                          *  the currently selected endpoint.\r
262                          *\r
263                          *  \param[in] Address  Endpoint address to select.\r
264                          */\r
265                         static inline void Endpoint_SelectEndpoint(const uint8_t Address) ATTR_ALWAYS_INLINE;\r
266                         static inline void Endpoint_SelectEndpoint(const uint8_t Address)\r
267                         {\r
268                                 USB_Endpoint_SelectedEndpoint = (Address & ENDPOINT_EPNUM_MASK);\r
269                         }\r
270 \r
271                         /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's\r
272                          *  data In and Out pointers to the bank's contents.\r
273                          *\r
274                          *  \param[in] Address  Endpoint number whose FIFO buffers are to be reset.\r
275                          */\r
276                         static inline void Endpoint_ResetEndpoint(const uint8_t Address) ATTR_ALWAYS_INLINE;\r
277                         static inline void Endpoint_ResetEndpoint(const uint8_t Address)\r
278                         {\r
279                                 uint32_t EndpointNumber = (Address & ENDPOINT_EPNUM_MASK);\r
280                         \r
281                                 AVR32_USBB.uerst |=  (AVR32_USBB_EPRST0_MASK << EndpointNumber);\r
282                                 AVR32_USBB.uerst &= ~(AVR32_USBB_EPRST0_MASK << EndpointNumber);\r
283                                 USB_Endpoint_FIFOPos[EndpointNumber] = &AVR32_USBB_SLAVE[EndpointNumber * ENDPOINT_HSB_ADDRESS_SPACE_SIZE];\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                                 AVR32_USBB.uerst |=  (AVR32_USBB_EPEN0_MASK << USB_Endpoint_SelectedEndpoint);\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                                 AVR32_USBB.uerst &= ~(AVR32_USBB_EPEN0_MASK << USB_Endpoint_SelectedEndpoint);\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 ((AVR32_USBB.uerst & (AVR32_USBB_EPEN0_MASK << USB_Endpoint_SelectedEndpoint)) ? 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_UC3\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 (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].nbusybk;\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_UC3\r
336                          */\r
337                         static inline void Endpoint_AbortPendingIN(void)\r
338                         {\r
339                                 while (Endpoint_GetBusyBanks() != 0)\r
340                                 {\r
341                                         (&AVR32_USBB.UECON0SET)[USB_Endpoint_SelectedEndpoint].killbks = true;\r
342                                         while ((&AVR32_USBB.UECON0)[USB_Endpoint_SelectedEndpoint].killbk);\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_UC3\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 (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].rwall;\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 (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].cfgok;\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 uint32_t Endpoint_GetEndpointInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
380                         static inline uint32_t Endpoint_GetEndpointInterrupts(void)\r
381                         {\r
382                                 return ((AVR32_USBB.udint & (AVR32_USBB_EP6INT_MASK | AVR32_USBB_EP5INT_MASK |\r
383                                                              AVR32_USBB_EP4INT_MASK | AVR32_USBB_EP3INT_MASK |\r
384                                                              AVR32_USBB_EP2INT_MASK | AVR32_USBB_EP1INT_MASK |\r
385                                                              AVR32_USBB_EP0INT_MASK)) >> AVR32_USBB_EP0INT_OFFSET);\r
386                         }\r
387 \r
388                         /** Determines if the specified endpoint number has interrupted (valid only for INTERRUPT type\r
389                          *  endpoints).\r
390                          *\r
391                          *  \param[in] Address  Address of the endpoint whose interrupt flag should be tested.\r
392                          *\r
393                          *  \return Boolean \c true if the specified endpoint has interrupted, \c false otherwise.\r
394                          */\r
395                         static inline bool Endpoint_HasEndpointInterrupted(const uint8_t Address) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
396                         static inline bool Endpoint_HasEndpointInterrupted(const uint8_t Address)\r
397                         {\r
398                                 return ((Endpoint_GetEndpointInterrupts() & (AVR32_USBB_EP0INT_MASK << (Address & ENDPOINT_EPNUM_MASK))) ? true : false);\r
399                         }\r
400 \r
401                         /** Determines if the selected IN endpoint is ready for a new packet to be sent to the host.\r
402                          *\r
403                          *  \ingroup Group_EndpointPacketManagement_UC3\r
404                          *\r
405                          *  \return Boolean \c true if the current endpoint is ready for an IN packet, \c false otherwise.\r
406                          */\r
407                         static inline bool Endpoint_IsINReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
408                         static inline bool Endpoint_IsINReady(void)\r
409                         {\r
410                                 return (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].txini;\r
411                         }\r
412 \r
413                         /** Determines if the selected OUT endpoint has received new packet from the host.\r
414                          *\r
415                          *  \ingroup Group_EndpointPacketManagement_UC3\r
416                          *\r
417                          *  \return Boolean \c true if current endpoint is has received an OUT packet, \c false otherwise.\r
418                          */\r
419                         static inline bool Endpoint_IsOUTReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
420                         static inline bool Endpoint_IsOUTReceived(void)\r
421                         {\r
422                                 return (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].rxouti;\r
423                         }\r
424 \r
425                         /** Determines if the current CONTROL type endpoint has received a SETUP packet.\r
426                          *\r
427                          *  \ingroup Group_EndpointPacketManagement_UC3\r
428                          *\r
429                          *  \return Boolean \c true if the selected endpoint has received a SETUP packet, \c false otherwise.\r
430                          */\r
431                         static inline bool Endpoint_IsSETUPReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
432                         static inline bool Endpoint_IsSETUPReceived(void)\r
433                         {\r
434                                 return (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].rxstpi;\r
435                         }\r
436 \r
437                         /** Clears a received SETUP packet on the currently selected CONTROL type endpoint, freeing up the\r
438                          *  endpoint for the next packet.\r
439                          *\r
440                          *  \ingroup Group_EndpointPacketManagement_UC3\r
441                          *\r
442                          *  \note This is not applicable for non CONTROL type endpoints.\r
443                          */\r
444                         static inline void Endpoint_ClearSETUP(void) ATTR_ALWAYS_INLINE;\r
445                         static inline void Endpoint_ClearSETUP(void)\r
446                         {\r
447                                 (&AVR32_USBB.UESTA0CLR)[USB_Endpoint_SelectedEndpoint].rxstpic = true;\r
448                                 USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint] = &AVR32_USBB_SLAVE[USB_Endpoint_SelectedEndpoint * ENDPOINT_HSB_ADDRESS_SPACE_SIZE];\r
449                         }\r
450 \r
451                         /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the\r
452                          *  next packet and switching to the alternative endpoint bank if double banked.\r
453                          *\r
454                          *  \ingroup Group_EndpointPacketManagement_UC3\r
455                          */\r
456                         static inline void Endpoint_ClearIN(void) ATTR_ALWAYS_INLINE;\r
457                         static inline void Endpoint_ClearIN(void)\r
458                         {\r
459                                 (&AVR32_USBB.UESTA0CLR)[USB_Endpoint_SelectedEndpoint].txinic   = true;\r
460                                 (&AVR32_USBB.UECON0CLR)[USB_Endpoint_SelectedEndpoint].fifoconc = true;\r
461                                 USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint] = &AVR32_USBB_SLAVE[USB_Endpoint_SelectedEndpoint * ENDPOINT_HSB_ADDRESS_SPACE_SIZE];\r
462                         }\r
463 \r
464                         /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint\r
465                          *  for the next packet and switching to the alternative endpoint bank if double banked.\r
466                          *\r
467                          *  \ingroup Group_EndpointPacketManagement_UC3\r
468                          */\r
469                         static inline void Endpoint_ClearOUT(void) ATTR_ALWAYS_INLINE;\r
470                         static inline void Endpoint_ClearOUT(void)\r
471                         {\r
472                                 (&AVR32_USBB.UESTA0CLR)[USB_Endpoint_SelectedEndpoint].rxoutic  = true;\r
473                                 (&AVR32_USBB.UECON0CLR)[USB_Endpoint_SelectedEndpoint].fifoconc = true;\r
474                                 USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint] = &AVR32_USBB_SLAVE[USB_Endpoint_SelectedEndpoint * ENDPOINT_HSB_ADDRESS_SPACE_SIZE];\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_UC3\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                                 (&AVR32_USBB.UECON0SET)[USB_Endpoint_SelectedEndpoint].stallrqs = true;\r
492                         }\r
493 \r
494                         /** Clears the STALL condition on the currently selected endpoint.\r
495                          *\r
496                          *  \ingroup Group_EndpointPacketManagement_UC3\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                                 (&AVR32_USBB.UECON0CLR)[USB_Endpoint_SelectedEndpoint].stallrqc = true;\r
502                         }\r
503 \r
504                         /** Determines if the currently selected endpoint is stalled, false otherwise.\r
505                          *\r
506                          *  \ingroup Group_EndpointPacketManagement_UC3\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 (&AVR32_USBB.UECON0)[USB_Endpoint_SelectedEndpoint].stallrq;\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                                 (&AVR32_USBB.UECON0SET)[USB_Endpoint_SelectedEndpoint].rstdts = true;\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 uint32_t DirectionMask) ATTR_ALWAYS_INLINE;\r
528                         static inline void Endpoint_SetEndpointDirection(const uint32_t DirectionMask)\r
529                         {\r
530                                 (&AVR32_USBB.UECFG0)[USB_Endpoint_SelectedEndpoint].epdir = (DirectionMask == ENDPOINT_DIR_IN);\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_UC3\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 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\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_UC3\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                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = 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_UC3\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 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\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_UC3\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                                 uint16_t Byte0 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
582                                 uint16_t Byte1 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
583 \r
584                                 return ((Byte1 << 8) | Byte0);\r
585                         }\r
586 \r
587                         /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT\r
588                          *  direction endpoints.\r
589                          *\r
590                          *  \ingroup Group_EndpointPrimitiveRW_UC3\r
591                          *\r
592                          *  \return Next two bytes in the currently selected endpoint's FIFO buffer.\r
593                          */\r
594                         static inline uint16_t Endpoint_Read_16_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
595                         static inline uint16_t Endpoint_Read_16_BE(void)\r
596                         {\r
597                                 uint16_t Byte0 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
598                                 uint16_t Byte1 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
599 \r
600                                 return ((Byte0 << 8) | Byte1);\r
601                         }\r
602 \r
603                         /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN\r
604                          *  direction endpoints.\r
605                          *\r
606                          *  \ingroup Group_EndpointPrimitiveRW_UC3\r
607                          *\r
608                          *  \param[in] Data  Data to write to the currently selected endpoint's FIFO buffer.\r
609                          */\r
610                         static inline void Endpoint_Write_16_LE(const uint16_t Data) ATTR_ALWAYS_INLINE;\r
611                         static inline void Endpoint_Write_16_LE(const uint16_t Data)\r
612                         {\r
613                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data & 0xFF);\r
614                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 8);\r
615                         }\r
616 \r
617                         /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN\r
618                          *  direction endpoints.\r
619                          *\r
620                          *  \ingroup Group_EndpointPrimitiveRW_UC3\r
621                          *\r
622                          *  \param[in] Data  Data to write to the currently selected endpoint's FIFO buffer.\r
623                          */\r
624                         static inline void Endpoint_Write_16_BE(const uint16_t Data) ATTR_ALWAYS_INLINE;\r
625                         static inline void Endpoint_Write_16_BE(const uint16_t Data)\r
626                         {\r
627                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 8);\r
628                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data & 0xFF);\r
629                         }\r
630 \r
631                         /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints.\r
632                          *\r
633                          *  \ingroup Group_EndpointPrimitiveRW_UC3\r
634                          */\r
635                         static inline void Endpoint_Discard_16(void) ATTR_ALWAYS_INLINE;\r
636                         static inline void Endpoint_Discard_16(void)\r
637                         {\r
638                                 uint8_t Dummy;\r
639 \r
640                                 Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
641                                 Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
642 \r
643                                 (void)Dummy;\r
644                         }\r
645 \r
646                         /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT\r
647                          *  direction endpoints.\r
648                          *\r
649                          *  \ingroup Group_EndpointPrimitiveRW_UC3\r
650                          *\r
651                          *  \return Next four bytes in the currently selected endpoint's FIFO buffer.\r
652                          */\r
653                         static inline uint32_t Endpoint_Read_32_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
654                         static inline uint32_t Endpoint_Read_32_LE(void)\r
655                         {\r
656                                 uint32_t Byte0 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
657                                 uint32_t Byte1 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
658                                 uint32_t Byte2 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
659                                 uint32_t Byte3 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
660 \r
661                                 return ((Byte3 << 24) | (Byte2 << 16) | (Byte1 << 8) | Byte0);\r
662                         }\r
663 \r
664                         /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT\r
665                          *  direction endpoints.\r
666                          *\r
667                          *  \ingroup Group_EndpointPrimitiveRW_UC3\r
668                          *\r
669                          *  \return Next four bytes in the currently selected endpoint's FIFO buffer.\r
670                          */\r
671                         static inline uint32_t Endpoint_Read_32_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
672                         static inline uint32_t Endpoint_Read_32_BE(void)\r
673                         {\r
674                                 uint32_t Byte0 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
675                                 uint32_t Byte1 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
676                                 uint32_t Byte2 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
677                                 uint32_t Byte3 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
678 \r
679                                 return ((Byte0 << 24) | (Byte1 << 16) | (Byte2 << 8) | Byte3);\r
680                         }\r
681 \r
682                         /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN\r
683                          *  direction endpoints.\r
684                          *\r
685                          *  \ingroup Group_EndpointPrimitiveRW_UC3\r
686                          *\r
687                          *  \param[in] Data  Data to write to the currently selected endpoint's FIFO buffer.\r
688                          */\r
689                         static inline void Endpoint_Write_32_LE(const uint32_t Data) ATTR_ALWAYS_INLINE;\r
690                         static inline void Endpoint_Write_32_LE(const uint32_t Data)\r
691                         {\r
692                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data &  0xFF);\r
693                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 8);\r
694                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 16);\r
695                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 24);\r
696                         }\r
697 \r
698                         /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN\r
699                          *  direction endpoints.\r
700                          *\r
701                          *  \ingroup Group_EndpointPrimitiveRW_UC3\r
702                          *\r
703                          *  \param[in] Data  Data to write to the currently selected endpoint's FIFO buffer.\r
704                          */\r
705                         static inline void Endpoint_Write_32_BE(const uint32_t Data) ATTR_ALWAYS_INLINE;\r
706                         static inline void Endpoint_Write_32_BE(const uint32_t Data)\r
707                         {\r
708                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 24);\r
709                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 16);\r
710                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 8);\r
711                                 *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data &  0xFF);\r
712                         }\r
713 \r
714                         /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.\r
715                          *\r
716                          *  \ingroup Group_EndpointPrimitiveRW_UC3\r
717                          */\r
718                         static inline void Endpoint_Discard_32(void) ATTR_ALWAYS_INLINE;\r
719                         static inline void Endpoint_Discard_32(void)\r
720                         {\r
721                                 uint8_t Dummy;\r
722 \r
723                                 Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
724                                 Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
725                                 Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
726                                 Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);\r
727 \r
728                                 (void)Dummy;\r
729                         }\r
730 \r
731                 /* External Variables: */\r
732                         /** Global indicating the maximum packet size of the default control endpoint located at address\r
733                          *  0 in the device. This value is set to the value indicated in the device descriptor in the user\r
734                          *  project once the USB interface is initialized into device mode.\r
735                          *\r
736                          *  If space is an issue, it is possible to fix this to a static value by defining the control\r
737                          *  endpoint size in the \c FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile\r
738                          *  via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically\r
739                          *  read from the descriptors at runtime and instead fixed to the given value. When used, it is\r
740                          *  important that the descriptor control endpoint size value matches the size given as the\r
741                          *  \c FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the \c FIXED_CONTROL_ENDPOINT_SIZE token\r
742                          *  be used in the device descriptors to ensure this.\r
743                          *\r
744                          *  \attention This variable should be treated as read-only in the user application, and never manually\r
745                          *             changed in value.\r
746                          */\r
747                         #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))\r
748                                 extern uint8_t USB_Device_ControlEndpointSize;\r
749                         #else\r
750                                 #define USB_Device_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE\r
751                         #endif\r
752 \r
753                 /* Function Prototypes: */\r
754                         /** Configures a table of endpoint descriptions, in sequence. This function can be used to configure multiple\r
755                          *  endpoints at the same time.\r
756                          *\r
757                          *  \note Endpoints with a zero address will be ignored, thus this function cannot be used to configure the\r
758                          *        control endpoint.\r
759                          *\r
760                          *  \param[in] Table    Pointer to a table of endpoint descriptions.\r
761                          *  \param[in] Entries  Number of entries in the endpoint table to configure.\r
762                          *\r
763                          *  \return Boolean \c true if all endpoints configured successfully, \c false otherwise.\r
764                          */\r
765                         bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,\r
766                                                              const uint8_t Entries);\r
767 \r
768                         /** Completes the status stage of a control transfer on a CONTROL type endpoint automatically,\r
769                          *  with respect to the data direction. This is a convenience function which can be used to\r
770                          *  simplify user control request handling.\r
771                          *\r
772                          *  \note This routine should not be called on non CONTROL type endpoints.\r
773                          */\r
774                         void Endpoint_ClearStatusStage(void);\r
775 \r
776                         /** Spin-loops until the currently selected non-control endpoint is ready for the next packet of data\r
777                          *  to be read or written to it.\r
778                          *\r
779                          *  \note This routine should not be called on CONTROL type endpoints.\r
780                          *\r
781                          *  \ingroup Group_EndpointRW_UC3\r
782                          *\r
783                          *  \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.\r
784                          */\r
785                         uint8_t Endpoint_WaitUntilReady(void);\r
786 \r
787         /* Disable C linkage for C++ Compilers: */\r
788                 #if defined(__cplusplus)\r
789                         }\r
790                 #endif\r
791 \r
792 #endif\r
793 \r
794 /** @} */\r
795 \r