3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  31 /** \ingroup Group_USB 
  32  *  @defgroup Group_EndpointManagement Endpoint Management 
  34  *  Functions, macros and enums related to endpoint management when in USB Device mode. This 
  35  *  module contains the endpoint management macros, as well as endpoint interrupt and data 
  36  *  send/recieve functions for various data types. 
  41 /** @defgroup Group_EndpointRW Endpoint Data Reading and Writing 
  43  *  Functions, macros, variables, enums and types related to data reading and writing from and to endpoints. 
  46 /** @defgroup Group_EndpointPacketManagement Endpoint Packet Management 
  48  *  Functions, macros, variables, enums and types related to packet management of endpoints. 
  51 #ifndef __ENDPOINT_H__ 
  52 #define __ENDPOINT_H__ 
  58                 #include "../../../Common/Common.h" 
  59                 #include "../HighLevel/USBTask.h" 
  61                 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__) 
  62                         #include "../HighLevel/StreamCallbacks.h" 
  65         /* Enable C linkage for C++ Compilers: */ 
  66                 #if defined(__cplusplus) 
  70         /* Public Interface - May be used in end-application: */ 
  72                         /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint 
  73                          *  should be initialized in the OUT direction - i.e. data flows from host to device. 
  75                         #define ENDPOINT_DIR_OUT                      (0 << EPDIR) 
  77                         /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint 
  78                          *  should be initialized in the IN direction - i.e. data flows from device to host. 
  80                         #define ENDPOINT_DIR_IN                       (1 << EPDIR) 
  82                         /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates 
  83                          *  that the endpoint should have one single bank, which requires less USB FIFO memory but results 
  84                          *  in slower transfers as only one USB device (the AVR or the host) can access the endpoint's 
  85                          *  bank at the one time. 
  87                         #define ENDPOINT_BANK_SINGLE                  (0 << EPBK0) 
  89                         /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates 
  90                          *  that the endpoint should have two banks, which requires more USB FIFO memory but results 
  91                          *  in faster transfers as one USB device (the AVR or the host) can access one bank while the other 
  92                          *  accesses the second bank. 
  94                         #define ENDPOINT_BANK_DOUBLE                  (1 << EPBK0) 
  96                         /** Endpoint address for the default control endpoint, which always resides in address 0. This is 
  97                          *  defined for convenience to give more readable code when used with the endpoint macros. 
  99                         #define ENDPOINT_CONTROLEP                    0 
 101                         #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__)) 
 102                                 /** Default size of the default control endpoint's bank, until altered by the Endpoint0Size value  
 103                                  *  in the device descriptor. Not available if the FIXED_CONTROL_ENDPOINT_SIZE token is defined. 
 105                                 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE   8 
 108                         /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's 
 109                          *  numerical address in the device. 
 111                         #define ENDPOINT_EPNUM_MASK                   0x07 
 113                         /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's 
 114                          *  bank size in the device. 
 116                         #define ENDPOINT_EPSIZE_MASK                  0x7FF 
 118                         /** Maximum size in bytes of a given endpoint. 
 120                          *  \param n  Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1) 
 122                         #define ENDPOINT_MAX_SIZE(n)                  _ENDPOINT_GET_MAXSIZE(n) 
 124                         /** Indicates if the given endpoint supports double banking. 
 126                          *  \param n  Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1) 
 128                         #define ENDPOINT_DOUBLEBANK_SUPPORTED(n)      _ENDPOINT_GET_DOUBLEBANK(n) 
 130                         #if !defined(CONTROL_ONLY_DEVICE) 
 131                                 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) || defined(__DOXYGEN__) 
 132                                         /** Total number of endpoints (including the default control endpoint at address 0) which may 
 133                                          *  be used in the device. Different USB AVR models support different amounts of endpoints, 
 134                                          *  this value reflects the maximum number of endpoints for the currently selected AVR model. 
 136                                         #define ENDPOINT_TOTAL_ENDPOINTS          7 
 138                                         #define ENDPOINT_TOTAL_ENDPOINTS          5                      
 141                                 #define ENDPOINT_TOTAL_ENDPOINTS              1 
 144                 /* Pseudo-Function Macros: */ 
 145                         #if defined(__DOXYGEN__) 
 146                                 /** Indicates the number of bytes currently stored in the current endpoint's selected bank. 
 148                                  *  \note The return width of this function may differ, depending on the maximum endpoint bank size 
 149                                  *        of the selected AVR model. 
 151                                  *  \ingroup Group_EndpointRW 
 153                                  *  \return Total number of bytes in the currently selected Endpoint's FIFO buffer 
 155                                 static inline uint16_t Endpoint_BytesInEndpoint(void); 
 157                                 /** Get the endpoint address of the currently selected endpoint. This is typically used to save 
 158                                  *  the currently selected endpoint number so that it can be restored after another endpoint has 
 161                                  *  \return Index of the currently selected endpoint 
 163                                 static inline uint8_t Endpoint_GetCurrentEndpoint(void); 
 165                                 /** Selects the given endpoint number. If the address from the device descriptors is used, the 
 166                                  *  value should be masked with the \ref ENDPOINT_EPNUM_MASK constant to extract only the endpoint 
 167                                  *  number (and discarding the endpoint direction bit). 
 169                                  *  Any endpoint operations which do not require the endpoint number to be indicated will operate on 
 170                                  *  the currently selected endpoint. 
 172                                  *  \param EndpointNumber Endpoint number to select 
 174                                 static inline void Endpoint_SelectEndpoint(uint8_t EndpointNumber
); 
 176                                 /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's 
 177                                  *  In and Out pointers to the bank's contents. 
 179                                  *  \param EndpointNumber Endpoint number whose FIFO buffers are to be reset 
 181                                 static inline void Endpoint_ResetFIFO(uint8_t EndpointNumber
); 
 183                                 /** Enables the currently selected endpoint so that data can be sent and received through it to 
 186                                  *  \note Endpoints must first be configured properly via \ref Endpoint_ConfigureEndpoint(). 
 188                                 static inline void Endpoint_EnableEndpoint(void); 
 190                                 /** Disables the currently selected endpoint so that data cannot be sent and received through it 
 191                                  *  to and from a host. 
 193                                 static inline void Endpoint_DisableEndpoint(void); 
 195                                 /** Determines if the currently selected endpoint is enabled, but not necessarily configured. 
 197                                  * \return Boolean True if the currently selected endpoint is enabled, false otherwise 
 199                                 static inline bool Endpoint_IsEnabled(void); 
 201                                 /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint 
 202                                  *  bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN 
 203                                  *  direction). This function will return false if an error has occurred in the endpoint, if the endpoint 
 204                                  *  is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN 
 205                                  *  direction and the endpoint bank is full. 
 207                                  *  \ingroup Group_EndpointPacketManagement 
 209                                  *  \return Boolean true if the currently selected endpoint may be read from or written to, depending on its direction 
 211                                 static inline bool Endpoint_IsReadWriteAllowed(void); 
 213                                 /** Determines if the currently selected endpoint is configured. 
 215                                  *  \return Boolean true if the currently selected endpoint has been configured, false otherwise 
 217                                 static inline bool Endpoint_IsConfigured(void); 
 219                                 /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their 
 220                                  *  interrupt duration has elapsed. Which endpoints have interrupted can be determined by 
 221                                  *  masking the return value against (1 << {Endpoint Number}). 
 223                                  *  \return Mask whose bits indicate which endpoints have interrupted 
 225                                 static inline uint8_t Endpoint_GetEndpointInterrupts(void); 
 227                                 /** Determines if the specified endpoint number has interrupted (valid only for INTERRUPT type 
 230                                  *  \param EndpointNumber  Index of the endpoint whose interrupt flag should be tested 
 232                                  *  \return Boolean true if the specified endpoint has interrupted, false otherwise 
 234                                 static inline bool Endpoint_HasEndpointInterrupted(uint8_t EndpointNumber
); 
 236                                 /** Determines if the selected IN endpoint is ready for a new packet. 
 238                                  *  \ingroup Group_EndpointPacketManagement 
 240                                  *  \return Boolean true if the current endpoint is ready for an IN packet, false otherwise. 
 242                                 static inline bool Endpoint_IsINReady(void); 
 244                                 /** Determines if the selected OUT endpoint has received new packet. 
 246                                  *  \ingroup Group_EndpointPacketManagement 
 248                                  *  \return Boolean true if current endpoint is has received an OUT packet, false otherwise. 
 250                                 static inline bool Endpoint_IsOUTReceived(void); 
 252                                 /** Determines if the current CONTROL type endpoint has received a SETUP packet. 
 254                                  *  \ingroup Group_EndpointPacketManagement 
 256                                  *  \return Boolean true if the selected endpoint has received a SETUP packet, false otherwise. 
 258                                 static inline bool Endpoint_IsSETUPReceived(void); 
 260                                 /** Clears a received SETUP packet on the currently selected CONTROL type endpoint, freeing up the 
 261                                  *  endpoint for the next packet. 
 263                                  *  \ingroup Group_EndpointPacketManagement 
 265                                  *  \note This is not applicable for non CONTROL type endpoints.                          
 267                                 static inline void Endpoint_ClearSETUP(void); 
 269                                 /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the 
 270                                  *  next packet and switching to the alternative endpoint bank if double banked. 
 272                                  *  \ingroup Group_EndpointPacketManagement 
 274                                 static inline void Endpoint_ClearIN(void); 
 276                                 /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint 
 277                                  *  for the next packet and switching to the alternative endpoint bank if double banked. 
 279                                  *  \ingroup Group_EndpointPacketManagement 
 281                                 static inline void Endpoint_ClearOUT(void); 
 283                                 /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the 
 284                                  *  indicated endpoint and that the current transfer sequence should be aborted. This provides a 
 285                                  *  way for devices to indicate invalid commands to the host so that the current transfer can be 
 286                                  *  aborted and the host can begin its own recovery sequence. 
 288                                  *  The currently selected endpoint remains stalled until either the \ref Endpoint_ClearStall() macro 
 289                                  *  is called, or the host issues a CLEAR FEATURE request to the device for the currently selected 
 292                                  *  \ingroup Group_EndpointPacketManagement 
 294                                 static inline void Endpoint_StallTransaction(void); 
 296                                 /** Clears the STALL condition on the currently selected endpoint. 
 298                                  *  \ingroup Group_EndpointPacketManagement 
 300                                 static inline void Endpoint_ClearStall(void); 
 302                                 /** Determines if the currently selected endpoint is stalled, false otherwise. 
 304                                  *  \ingroup Group_EndpointPacketManagement 
 306                                  *  \return Boolean true if the currently selected endpoint is stalled, false otherwise 
 308                                 static inline bool Endpoint_IsStalled(void); 
 310                                 /** Resets the data toggle of the currently selected endpoint. */ 
 311                                 static inline void Endpoint_ResetDataToggle(void); 
 313                                 /** Determines the currently selected endpoint's direction. 
 315                                  *  \return The currently selected endpoint's direction, as a ENDPOINT_DIR_* mask. 
 317                                 static inline uint8_t Endpoint_GetEndpointDirection(void); 
 319                                 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) || defined(__DOXYGEN__) 
 320                                         #define Endpoint_BytesInEndpoint()        UEBCX 
 322                                         #define Endpoint_BytesInEndpoint()        UEBCLX 
 325                                 #if !defined(CONTROL_ONLY_DEVICE) 
 326                                         #define Endpoint_GetCurrentEndpoint()     (UENUM & ENDPOINT_EPNUM_MASK) 
 328                                         #define Endpoint_GetCurrentEndpoint()     ENDPOINT_CONTROLEP 
 331                                 #if !defined(CONTROL_ONLY_DEVICE) 
 332                                         #define Endpoint_SelectEndpoint(epnum)    MACROS{ UENUM = epnum; }MACROE 
 334                                         #define Endpoint_SelectEndpoint(epnum)    (void)epnum 
 337                                 #define Endpoint_ResetFIFO(epnum)             MACROS{ UERST = (1 << epnum); UERST = 0; }MACROE 
 339                                 #define Endpoint_EnableEndpoint()             MACROS{ UECONX |= (1 << EPEN); }MACROE 
 341                                 #define Endpoint_DisableEndpoint()            MACROS{ UECONX &= ~(1 << EPEN); }MACROE 
 343                                 #define Endpoint_IsEnabled()                  ((UECONX & (1 << EPEN)) ? true : false) 
 345                                 #if !defined(CONTROL_ONLY_DEVICE) 
 346                                         #define Endpoint_IsReadWriteAllowed()     ((UEINTX & (1 << RWAL)) ? true : false) 
 349                                 #define Endpoint_IsConfigured()               ((UESTA0X & (1 << CFGOK)) ? true : false) 
 351                                 #define Endpoint_GetEndpointInterrupts()      UEINT 
 353                                 #define Endpoint_HasEndpointInterrupted(n)    ((UEINT & (1 << n)) ? true : false) 
 355                                 #define Endpoint_IsINReady()                  ((UEINTX & (1 << TXINI))  ? true : false) 
 357                                 #define Endpoint_IsOUTReceived()              ((UEINTX & (1 << RXOUTI)) ? true : false) 
 359                                 #define Endpoint_IsSETUPReceived()            ((UEINTX & (1 << RXSTPI)) ? true : false) 
 361                                 #define Endpoint_ClearSETUP()                 MACROS{ UEINTX &= ~(1 << RXSTPI); }MACROE 
 363                                 #if !defined(CONTROL_ONLY_DEVICE) 
 364                                         #define Endpoint_ClearIN()                MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << TXINI)); \ 
 365                                                                                           UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE 
 367                                         #define Endpoint_ClearIN()                MACROS{ UEINTX &= ~(1 << TXINI); }MACROE 
 370                                 #if !defined(CONTROL_ONLY_DEVICE) 
 371                                         #define Endpoint_ClearOUT()               MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << RXOUTI)); \ 
 372                                                                                           UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE 
 374                                         #define Endpoint_ClearOUT()               MACROS{ UEINTX &= ~(1 << RXOUTI); }MACROE                      
 377                                 #define Endpoint_StallTransaction()           MACROS{ UECONX |= (1 << STALLRQ); }MACROE 
 379                                 #define Endpoint_ClearStall()                 MACROS{ UECONX |= (1 << STALLRQC); }MACROE 
 381                                 #define Endpoint_IsStalled()                  ((UECONX & (1 << STALLRQ)) ? true : false) 
 383                                 #define Endpoint_ResetDataToggle()            MACROS{ UECONX |= (1 << RSTDT); }MACROE 
 385                                 #define Endpoint_GetEndpointDirection()       (UECFG0X & ENDPOINT_DIR_IN) 
 389                         /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function. 
 391                          *  \ingroup Group_EndpointRW 
 393                         enum Endpoint_WaitUntilReady_ErrorCodes_t
 
 395                                 ENDPOINT_READYWAIT_NoError                 
= 0, /**< Endpoint is ready for next packet, no error. */ 
 396                                 ENDPOINT_READYWAIT_EndpointStalled         
= 1, /**< The endpoint was stalled during the stream 
 397                                                                                  *   transfer by the host or device. 
 399                                 ENDPOINT_READYWAIT_DeviceDisconnected      
= 2, /**< Device was disconnected from the host while 
 400                                                                                  *   waiting for the endpoint to become ready. 
 402                                 ENDPOINT_READYWAIT_Timeout                 
= 3, /**< The host failed to accept or send the next packet 
 403                                                                                  *   within the software timeout period set by the 
 404                                                                                  *   \ref USB_STREAM_TIMEOUT_MS macro. 
 408                         /** Enum for the possible error return codes of the Endpoint_*_Stream_* functions. 
 410                          *  \ingroup Group_EndpointRW 
 412                         enum Endpoint_Stream_RW_ErrorCodes_t
 
 414                                 ENDPOINT_RWSTREAM_NoError            
= 0, /**< Command completed successfully, no error. */ 
 415                                 ENDPOINT_RWSTREAM_EndpointStalled    
= 1, /**< The endpoint was stalled during the stream 
 416                                                                            *   transfer by the host or device. 
 418                                 ENDPOINT_RWSTREAM_DeviceDisconnected 
= 1, /**< Device was disconnected from the host during 
 421                                 ENDPOINT_RWSTREAM_Timeout            
= 2, /**< The host failed to accept or send the next packet 
 422                                                                            *   within the software timeout period set by the 
 423                                                                            *   \ref USB_STREAM_TIMEOUT_MS macro. 
 425                                 ENDPOINT_RWSTREAM_CallbackAborted    
= 3, /**< Indicates that the stream's callback function 
 426                                                                        *   aborted the transfer early. 
 430                         /** Enum for the possible error return codes of the Endpoint_*_Control_Stream_* functions.. 
 432                          *  \ingroup Group_EndpointRW 
 434                         enum Endpoint_ControlStream_RW_ErrorCodes_t
 
 436                                 ENDPOINT_RWCSTREAM_NoError            
= 0, /**< Command completed successfully, no error. */ 
 437                                 ENDPOINT_RWCSTREAM_HostAborted        
= 1, /**< The aborted the transfer prematurely. */ 
 440                 /* Inline Functions: */ 
 441                         /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints. 
 443                          *  \ingroup Group_EndpointRW 
 445                          *  \return Next byte in the currently selected endpoint's FIFO buffer 
 447                         static inline uint8_t Endpoint_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 448                         static inline uint8_t Endpoint_Read_Byte(void) 
 453                         /** Writes one byte from the currently selected endpoint's bank, for IN direction endpoints. 
 455                          *  \ingroup Group_EndpointRW 
 457                          *  \param Byte  Next byte to write into the the currently selected endpoint's FIFO buffer 
 459                         static inline void Endpoint_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
; 
 460                         static inline void Endpoint_Write_Byte(const uint8_t Byte
) 
 465                         /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints. 
 467                          *  \ingroup Group_EndpointRW 
 469                         static inline void Endpoint_Discard_Byte(void) ATTR_ALWAYS_INLINE
; 
 470                         static inline void Endpoint_Discard_Byte(void) 
 477                         /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT 
 478                          *  direction endpoints. 
 480                          *  \ingroup Group_EndpointRW 
 482                          *  \return Next word in the currently selected endpoint's FIFO buffer 
 484                         static inline uint16_t Endpoint_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 485                         static inline uint16_t Endpoint_Read_Word_LE(void) 
 490                                 Data 
|= (((uint16_t)UEDATX
) << 8); 
 495                         /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT 
 496                          *  direction endpoints. 
 498                          *  \ingroup Group_EndpointRW 
 500                          *  \return Next word in the currently selected endpoint's FIFO buffer 
 502                         static inline uint16_t Endpoint_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 503                         static inline uint16_t Endpoint_Read_Word_BE(void) 
 507                                 Data  
= (((uint16_t)UEDATX
) << 8); 
 513                         /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN 
 514                          *  direction endpoints. 
 516                          *  \ingroup Group_EndpointRW 
 518                          *  \param Word  Next word to write to the currently selected endpoint's FIFO buffer 
 520                         static inline void Endpoint_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
; 
 521                         static inline void Endpoint_Write_Word_LE(const uint16_t Word
) 
 523                                 UEDATX 
= (Word 
& 0xFF); 
 524                                 UEDATX 
= (Word 
>> 8); 
 527                         /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN 
 528                          *  direction endpoints. 
 530                          *  \ingroup Group_EndpointRW 
 532                          *  \param Word  Next word to write to the currently selected endpoint's FIFO buffer 
 534                         static inline void Endpoint_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
; 
 535                         static inline void Endpoint_Write_Word_BE(const uint16_t Word
) 
 537                                 UEDATX 
= (Word 
>> 8); 
 538                                 UEDATX 
= (Word 
& 0xFF); 
 541                         /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints. 
 543                          *  \ingroup Group_EndpointRW 
 545                         static inline void Endpoint_Discard_Word(void) ATTR_ALWAYS_INLINE
; 
 546                         static inline void Endpoint_Discard_Word(void) 
 554                         /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT 
 555                          *  direction endpoints. 
 557                          *  \ingroup Group_EndpointRW 
 559                          *  \return Next double word in the currently selected endpoint's FIFO buffer 
 561                         static inline uint32_t Endpoint_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 562                         static inline uint32_t Endpoint_Read_DWord_LE(void) 
 570                                 Data
.Bytes
[0] = UEDATX
; 
 571                                 Data
.Bytes
[1] = UEDATX
; 
 572                                 Data
.Bytes
[2] = UEDATX
; 
 573                                 Data
.Bytes
[3] = UEDATX
; 
 578                         /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT 
 579                          *  direction endpoints. 
 581                          *  \ingroup Group_EndpointRW 
 583                          *  \return Next double word in the currently selected endpoint's FIFO buffer 
 585                         static inline uint32_t Endpoint_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 586                         static inline uint32_t Endpoint_Read_DWord_BE(void) 
 594                                 Data
.Bytes
[3] = UEDATX
; 
 595                                 Data
.Bytes
[2] = UEDATX
; 
 596                                 Data
.Bytes
[1] = UEDATX
; 
 597                                 Data
.Bytes
[0] = UEDATX
; 
 602                         /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN 
 603                          *  direction endpoints. 
 605                          *  \ingroup Group_EndpointRW 
 607                          *  \param DWord  Next double word to write to the currently selected endpoint's FIFO buffer 
 609                         static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
; 
 610                         static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
) 
 612                                 UEDATX 
= (DWord 
&  0xFF); 
 613                                 UEDATX 
= (DWord 
>> 8); 
 614                                 UEDATX 
= (DWord 
>> 16); 
 615                                 UEDATX 
= (DWord 
>> 24); 
 618                         /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN 
 619                          *  direction endpoints. 
 621                          *  \ingroup Group_EndpointRW 
 623                          *  \param DWord  Next double word to write to the currently selected endpoint's FIFO buffer 
 625                         static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
; 
 626                         static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
) 
 628                                 UEDATX 
= (DWord 
>> 24); 
 629                                 UEDATX 
= (DWord 
>> 16); 
 630                                 UEDATX 
= (DWord 
>> 8); 
 631                                 UEDATX 
= (DWord 
&  0xFF); 
 634                         /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.        
 636                          *  \ingroup Group_EndpointRW 
 638                         static inline void Endpoint_Discard_DWord(void) ATTR_ALWAYS_INLINE
; 
 639                         static inline void Endpoint_Discard_DWord(void) 
 649                 /* External Variables: */ 
 650                         /** Global indicating the maximum packet size of the default control endpoint located at address 
 651                          *  0 in the device. This value is set to the value indicated in the device descriptor in the user 
 652                          *  project once the USB interface is initialized into device mode. 
 654                          *  If space is an issue, it is possible to fix this to a static value by defining the control 
 655                          *  endpoint size in the FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile 
 656                          *  via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically 
 657                          *  read from the descriptors at runtime and instead fixed to the given value. When used, it is 
 658                          *  important that the descriptor control endpoint size value matches the size given as the 
 659                          *  FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the FIXED_CONTROL_ENDPOINT_SIZE token 
 660                          *  be used in the descriptors to ensure this. 
 662                          *  \note This variable should be treated as read-only in the user application, and never manually 
 665                         #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__)) 
 666                                 extern uint8_t USB_ControlEndpointSize
; 
 668                                 #define USB_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE 
 671                 /* Function Prototypes: */ 
 672                         /** Configures the specified endpoint number with the given endpoint type, direction, bank size 
 673                          *  and banking mode. Endpoints should be allocated in ascending order by their address in the 
 674                          *  device (i.e. endpoint 1 should be configured before endpoint 2 and so on) to prevent fragmentation 
 675                          *  of the USB FIFO memory. 
 677                          *  The endpoint type may be one of the EP_TYPE_* macros listed in LowLevel.h and the direction 
 678                          *  may be either \ref ENDPOINT_DIR_OUT or \ref ENDPOINT_DIR_IN. 
 680                          *  The bank size must indicate the maximum packet size that the endpoint can handle. Different 
 681                          *  endpoint numbers can handle different maximum packet sizes - refer to the chosen USB AVR's 
 682                          *  datasheet to determine the maximum bank size for each endpoint. 
 684                          *  The banking mode may be either \ref ENDPOINT_BANK_SINGLE or \ref ENDPOINT_BANK_DOUBLE. 
 686                          *  \note The default control endpoint does not have to be manually configured, as it is automatically 
 687                          *  configured by the library internally. 
 689                          *  \note This routine will select the specified endpoint, and the endpoint will remain selected 
 690                          *        once the routine completes regardless of if the endpoint configuration succeeds. 
 692                          *  \return Boolean true if the configuration succeeded, false otherwise 
 694                         bool Endpoint_ConfigureEndpoint(const uint8_t  Number
, const uint8_t Type
, const uint8_t Direction
, 
 695                                                         const uint16_t Size
, const uint8_t Banks
); 
 697                         #if !defined(CONTROL_ONLY_DEVICE) 
 699                         /** Spinloops until the currently selected non-control endpoint is ready for the next packet of data 
 700                          *  to be read or written to it. 
 702                          *  \note This routine should not be called on CONTROL type endpoints. 
 704                          *  \ingroup Group_EndpointRW 
 706                          *  \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum. 
 708                         uint8_t Endpoint_WaitUntilReady(void); 
 710                         /** Reads and discards the given number of bytes from the endpoint from the given buffer, 
 711                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
 712                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
 713                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between 
 714                          *  each USB packet, the given stream callback function is executed repeatedly until the next 
 715                          *  packet is ready, allowing for early aborts of stream transfers. 
 717                          *      The callback routine should be created according to the information in \ref Group_StreamCallbacks. 
 718                          *  If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are 
 719                          *  disabled and this function has the Callback parameter omitted. 
 721                          *  \note This routine should not be used on CONTROL type endpoints. 
 723                          *  \ingroup Group_EndpointRW 
 725                          *  \param Length    Number of bytes to send via the currently selected endpoint. 
 726                          *  \param Callback  Name of a callback routine to call between successive USB packet transfers, NULL if no callback 
 728                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 730                         uint8_t Endpoint_Discard_Stream(uint16_t Length
 
 731                         #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__) 
 732                                                         , StreamCallbackPtr_t Callback
 
 736                         /** Writes the given number of bytes to the endpoint from the given buffer in little endian, 
 737                          *  sending full packets to the host as needed. The last packet filled is not automatically sent; 
 738                          *  the user is responsible for manually sending the last written packet to the host via the 
 739                          *  \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function 
 740                          *  is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early 
 741                          *  aborts of stream transfers. 
 743                          *      The callback routine should be created according to the information in \ref Group_StreamCallbacks. 
 744                          *  If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are 
 745                          *  disabled and this function has the Callback parameter omitted. 
 747                          *  \note This routine should not be used on CONTROL type endpoints. 
 749                          *  \ingroup Group_EndpointRW 
 751                          *  \param Buffer    Pointer to the source data buffer to read from. 
 752                          *  \param Length    Number of bytes to read for the currently selected endpoint into the buffer. 
 753                          *  \param Callback  Name of a callback routine to call between successive USB packet transfers, NULL if no callback 
 755                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 757                         uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
 
 758                         #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__) 
 759                                                          , StreamCallbackPtr_t Callback
 
 761                                                          ) ATTR_NON_NULL_PTR_ARG(1); 
 763                         /** Writes the given number of bytes to the endpoint from the given buffer in big endian, 
 764                          *  sending full packets to the host as needed. The last packet filled is not automatically sent; 
 765                          *  the user is responsible for manually sending the last written packet to the host via the 
 766                          *  \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function 
 767                          *  is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early 
 768                          *  aborts of stream transfers. 
 770                          *      The callback routine should be created according to the information in \ref Group_StreamCallbacks. 
 771                          *  If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are 
 772                          *  disabled and this function has the Callback parameter omitted. 
 774                          *  \note This routine should not be used on CONTROL type endpoints. 
 776                          *  \ingroup Group_EndpointRW 
 778                          *  \param Buffer    Pointer to the source data buffer to read from. 
 779                          *  \param Length    Number of bytes to read for the currently selected endpoint into the buffer. 
 780                          *  \param Callback  Name of a callback routine to call between successive USB packet transfers, NULL if no callback 
 782                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 784                         uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
 
 785                         #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__) 
 786                                                          , StreamCallbackPtr_t Callback
 
 788                                                          ) ATTR_NON_NULL_PTR_ARG(1); 
 790                         /** Reads the given number of bytes from the endpoint from the given buffer in little endian, 
 791                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
 792                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
 793                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between 
 794                          *  each USB packet, the given stream callback function is executed repeatedly until the endpoint 
 795                          *  is ready to accept the next packet, allowing for early aborts of stream transfers. 
 797                          *      The callback routine should be created according to the information in \ref Group_StreamCallbacks. 
 798                          *  If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are 
 799                          *  disabled and this function has the Callback parameter omitted. 
 801                          *  \note This routine should not be used on CONTROL type endpoints. 
 803                          *  \ingroup Group_EndpointRW 
 805                          *  \param Buffer    Pointer to the destination data buffer to write to. 
 806                          *  \param Length    Number of bytes to send via the currently selected endpoint. 
 807                          *  \param Callback  Name of a callback routine to call between successive USB packet transfers, NULL if no callback 
 809                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 811                         uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
 
 812                         #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__) 
 813                                                         , StreamCallbackPtr_t Callback
 
 815                                                         ) ATTR_NON_NULL_PTR_ARG(1); 
 817                         /** Reads the given number of bytes from the endpoint from the given buffer in big endian, 
 818                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
 819                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
 820                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between 
 821                          *  each USB packet, the given stream callback function is executed repeatedly until the endpoint 
 822                          *  is ready to accept the next packet, allowing for early aborts of stream transfers. 
 824                          *      The callback routine should be created according to the information in \ref Group_StreamCallbacks. 
 825                          *  If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are 
 826                          *  disabled and this function has the Callback parameter omitted. 
 828                          *  \note This routine should not be used on CONTROL type endpoints. 
 830                          *  \ingroup Group_EndpointRW 
 832                          *  \param Buffer    Pointer to the destination data buffer to write to. 
 833                          *  \param Length    Number of bytes to send via the currently selected endpoint. 
 834                          *  \param Callback  Name of a callback routine to call between successive USB packet transfers, NULL if no callback 
 836                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 838                         uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
 
 839                         #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__) 
 840                                                         , StreamCallbackPtr_t Callback
 
 842                                                         ) ATTR_NON_NULL_PTR_ARG(1); 
 846                         /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian, 
 847                          *  sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared 
 848                          *  in both failure and success states; the user is responsible for manually clearing the setup OUT to 
 849                          *  finalize the transfer via the \ref Endpoint_ClearOUT() macro. 
 851                          *  \note This routine should only be used on CONTROL type endpoints. 
 853                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 854                          *           together; i.e. the entire stream data must be read or written at the one time. 
 856                          *  \ingroup Group_EndpointRW 
 858                          *  \param Buffer  Pointer to the source data buffer to read from. 
 859                          *  \param Length  Number of bytes to read for the currently selected endpoint into the buffer. 
 861                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 863                         uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 865                         /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian, 
 866                          *  sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared 
 867                          *  in both failure and success states; the user is responsible for manually clearing the setup OUT to 
 868                          *  finalize the transfer via the \ref Endpoint_ClearOUT() macro. 
 870                          *  \note This routine should only be used on CONTROL type endpoints. 
 872                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 873                          *           together; i.e. the entire stream data must be read or written at the one time. 
 875                          *  \ingroup Group_EndpointRW 
 877                          *  \param Buffer  Pointer to the source data buffer to read from. 
 878                          *  \param Length  Number of bytes to read for the currently selected endpoint into the buffer. 
 880                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 882                         uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 884                         /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian, 
 885                          *  discarding fully read packets from the host as needed. The device IN acknowledgement is not 
 886                          *  automatically sent after success or failure states; the user is responsible for manually sending the 
 887                          *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro. 
 889                          *  \note This routine should only be used on CONTROL type endpoints. 
 891                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 892                          *           together; i.e. the entire stream data must be read or written at the one time. 
 894                          *  \ingroup Group_EndpointRW 
 896                          *  \param Buffer  Pointer to the destination data buffer to write to. 
 897                          *  \param Length  Number of bytes to send via the currently selected endpoint. 
 899                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 901                         uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 903                         /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian, 
 904                          *  discarding fully read packets from the host as needed. The device IN acknowledgement is not 
 905                          *  automatically sent after success or failure states; the user is responsible for manually sending the 
 906                          *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro. 
 908                          *  \note This routine should only be used on CONTROL type endpoints. 
 910                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 911                          *           together; i.e. the entire stream data must be read or written at the one time. 
 913                          *  \ingroup Group_EndpointRW 
 915                          *  \param Buffer  Pointer to the destination data buffer to write to. 
 916                          *  \param Length  Number of bytes to send via the currently selected endpoint. 
 918                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 920                         uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);                 
 922         /* Private Interface - For use in library only: */ 
 923         #if !defined(__DOXYGEN__) 
 925                         #define Endpoint_AllocateMemory()              MACROS{ UECFG1X |=  (1 << ALLOC); }MACROE 
 926                         #define Endpoint_DeallocateMemory()            MACROS{ UECFG1X &= ~(1 << ALLOC); }MACROE 
 928                         #define _ENDPOINT_GET_MAXSIZE(n)               _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## n) 
 929                         #define _ENDPOINT_GET_MAXSIZE2(details)        _ENDPOINT_GET_MAXSIZE3(details) 
 930                         #define _ENDPOINT_GET_MAXSIZE3(maxsize, db)    maxsize 
 932                         #define _ENDPOINT_GET_DOUBLEBANK(n)            _ENDPOINT_GET_DOUBLEBANK2(ENDPOINT_DETAILS_EP ## n) 
 933                         #define _ENDPOINT_GET_DOUBLEBANK2(details)     _ENDPOINT_GET_DOUBLEBANK3(details) 
 934                         #define _ENDPOINT_GET_DOUBLEBANK3(maxsize, db) db 
 936                         #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) 
 937                                 #define ENDPOINT_DETAILS_EP0               64,  true 
 938                                 #define ENDPOINT_DETAILS_EP1               256, true 
 939                                 #define ENDPOINT_DETAILS_EP2               64,  true 
 940                                 #define ENDPOINT_DETAILS_EP3               64,  true 
 941                                 #define ENDPOINT_DETAILS_EP4               64,  true 
 942                                 #define ENDPOINT_DETAILS_EP5               64,  true 
 943                                 #define ENDPOINT_DETAILS_EP6               64,  true 
 945                                 #define ENDPOINT_DETAILS_EP0               64,  true 
 946                                 #define ENDPOINT_DETAILS_EP1               64,  false 
 947                                 #define ENDPOINT_DETAILS_EP2               64,  false 
 948                                 #define ENDPOINT_DETAILS_EP3               64,  true 
 949                                 #define ENDPOINT_DETAILS_EP4               64,  true                     
 952                         #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks)            \ 
 953                                                             Endpoint_ConfigureEndpoint_Prv(Number,          \ 
 954                                                                       ((Type << EPTYPE0) | Direction),      \ 
 955                                                                       ((1 << ALLOC) | Banks |               \ 
 956                                                                         (__builtin_constant_p(Size) ?       \ 
 957                                                                          Endpoint_BytesToEPSizeMask(Size) :  \ 
 958                                                                          Endpoint_BytesToEPSizeMaskDynamic(Size)))) 
 960                 /* Function Prototypes: */ 
 961                         void    Endpoint_ClearEndpoints(void); 
 962                         uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size
); 
 963                         bool    Endpoint_ConfigureEndpoint_Prv(const uint8_t Number
, const uint8_t UECFG0XData
, const uint8_t UECFG1XData
); 
 965                 /* Inline Functions: */ 
 966                         static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
; 
 967                         static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
) 
 970                                 uint16_t CheckBytes 
= 8; 
 972                                 while (CheckBytes 
< Bytes
) 
 978                                 return (MaskVal 
<< EPSIZE0
); 
 983         /* Disable C linkage for C++ Compilers: */ 
 984                 #if defined(__cplusplus)