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                   0x03 
 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_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || 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                         /** Interrupt definition for the endpoint SETUP interrupt (for CONTROL type endpoints). Should be 
 145                          *  used with the USB_INT_* macros located in USBInterrupt.h. 
 147                          *  This interrupt will fire if enabled on a CONTROL type endpoint if a new control packet is 
 148                          *  received from the host. 
 150                          *  \note This interrupt must be enabled and cleared on *each* endpoint which requires it (after the 
 151                          *        endpoint is selected), and will fire the common endpoint interrupt vector. 
 153                          *  \see \ref ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector. 
 155                         #define ENDPOINT_INT_SETUP                    UEIENX, (1 << RXSTPE), UEINTX, (1 << RXSTPI) 
 157                         /** Interrupt definition for the endpoint IN interrupt (for INTERRUPT type endpoints). Should be 
 158                          *  used with the USB_INT_* macros located in USBInterrupt.h. 
 160                          *  This interrupt will fire if enabled on an INTERRUPT type endpoint if a the endpoint interrupt 
 161                          *  period has elapsed and the endpoint is ready for a new packet to be written to its FIFO buffer 
 164                          *  \note This interrupt must be enabled and cleared on *each* endpoint which requires it (after the 
 165                          *        endpoint is selected), and will fire the common endpoint interrupt vector. 
 167                          *  \see \ref ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector. 
 169                         #define ENDPOINT_INT_IN                       UEIENX, (1 << TXINE) , UEINTX, (1 << TXINI) 
 171                         /** Interrupt definition for the endpoint OUT interrupt (for INTERRUPT type endpoints). Should be 
 172                          *  used with the USB_INT_* macros located in USBInterrupt.h. 
 174                          *  This interrupt will fire if enabled on an INTERRUPT type endpoint if a the endpoint interrupt 
 175                          *  period has elapsed and the endpoint is ready for a packet from the host to be read from its 
 176                          *  FIFO buffer (if received). 
 178                          *  \note This interrupt must be enabled and cleared on *each* endpoint which requires it (after the 
 179                          *        endpoint is selected), and will fire the common endpoint interrupt vector. 
 181                          *  \see \ref ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector. 
 183                         #define ENDPOINT_INT_OUT                      UEIENX, (1 << RXOUTE), UEINTX, (1 << RXOUTI) 
 185                 /* Pseudo-Function Macros: */ 
 186                         #if defined(__DOXYGEN__) 
 187                                 /** Indicates the number of bytes currently stored in the current endpoint's selected bank. 
 189                                  *  \note The return width of this function may differ, depending on the maximum endpoint bank size 
 190                                  *        of the selected AVR model. 
 192                                  *  \ingroup Group_EndpointRW 
 194                                  *  \return Total number of bytes in the currently selected Endpoint's FIFO buffer 
 196                                 static inline uint16_t Endpoint_BytesInEndpoint(void); 
 198                                 /** Get the endpoint address of the currently selected endpoint. This is typically used to save 
 199                                  *  the currently selected endpoint number so that it can be restored after another endpoint has 
 202                                  *  \return Index of the currently selected endpoint 
 204                                 static inline uint8_t Endpoint_GetCurrentEndpoint(void); 
 206                                 /** Selects the given endpoint number. If the address from the device descriptors is used, the 
 207                                  *  value should be masked with the \ref ENDPOINT_EPNUM_MASK constant to extract only the endpoint 
 208                                  *  number (and discarding the endpoint direction bit). 
 210                                  *  Any endpoint operations which do not require the endpoint number to be indicated will operate on 
 211                                  *  the currently selected endpoint. 
 213                                  *  \param EndpointNumber Endpoint number to select 
 215                                 static inline void Endpoint_SelectEndpoint(uint8_t EndpointNumber
); 
 217                                 /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's 
 218                                  *  In and Out pointers to the bank's contents. 
 220                                  *  \param EndpointNumber Endpoint number whose FIFO buffers are to be reset 
 222                                 static inline void Endpoint_ResetFIFO(uint8_t EndpointNumber
); 
 224                                 /** Enables the currently selected endpoint so that data can be sent and received through it to 
 227                                  *  \note Endpoints must first be configured properly via \ref Endpoint_ConfigureEndpoint(). 
 229                                 static inline void Endpoint_EnableEndpoint(void); 
 231                                 /** Disables the currently selected endpoint so that data cannot be sent and received through it 
 232                                  *  to and from a host. 
 234                                 static inline void Endpoint_DisableEndpoint(void); 
 236                                 /** Determines if the currently selected endpoint is enabled, but not necessarily configured. 
 238                                  * \return Boolean True if the currently selected endpoint is enabled, false otherwise 
 240                                 static inline bool Endpoint_IsEnabled(void); 
 242                                 /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint 
 243                                  *  bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN 
 244                                  *  direction). This function will return false if an error has occurred in the endpoint, if the endpoint 
 245                                  *  is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN 
 246                                  *  direction and the endpoint bank is full. 
 248                                  *  \ingroup Group_EndpointPacketManagement 
 250                                  *  \return Boolean true if the currently selected endpoint may be read from or written to, depending on its direction 
 252                                 static inline bool Endpoint_IsReadWriteAllowed(void); 
 254                                 /** Determines if the currently selected endpoint is configured. 
 256                                  *  \return Boolean true if the currently selected endpoint has been configured, false otherwise 
 258                                 static inline bool Endpoint_IsConfigured(void); 
 260                                 /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their 
 261                                  *  interrupt duration has elapsed. Which endpoints have interrupted can be determined by 
 262                                  *  masking the return value against (1 << {Endpoint Number}). 
 264                                  *  \return Mask whose bits indicate which endpoints have interrupted 
 266                                 static inline uint8_t Endpoint_GetEndpointInterrupts(void); 
 268                                 /** Clears the endpoint interrupt flag. This clears the specified endpoint number's interrupt 
 269                                  *  mask in the endpoint interrupt flag register. 
 271                                  *  \param EndpointNumber  Index of the endpoint whose interrupt flag should be cleared 
 273                                 static inline void Endpoint_ClearEndpointInterrupt(uint8_t EndpointNumber
); 
 275                                 /** Determines if the specified endpoint number has interrupted (valid only for INTERRUPT type 
 278                                  *  \param EndpointNumber  Index of the endpoint whose interrupt flag should be tested 
 280                                  *  \return Boolean true if the specified endpoint has interrupted, false otherwise 
 282                                 static inline bool Endpoint_HasEndpointInterrupted(uint8_t EndpointNumber
); 
 284                                 /** Determines if the selected IN endpoint is ready for a new packet. 
 286                                  *  \ingroup Group_EndpointPacketManagement 
 288                                  *  \return Boolean true if the current endpoint is ready for an IN packet, false otherwise. 
 290                                 static inline bool Endpoint_IsINReady(void); 
 292                                 /** Determines if the selected OUT endpoint has received new packet. 
 294                                  *  \ingroup Group_EndpointPacketManagement 
 296                                  *  \return Boolean true if current endpoint is has received an OUT packet, false otherwise. 
 298                                 static inline bool Endpoint_IsOUTReceived(void); 
 300                                 /** Determines if the current CONTROL type endpoint has received a SETUP packet. 
 302                                  *  \ingroup Group_EndpointPacketManagement 
 304                                  *  \return Boolean true if the selected endpoint has received a SETUP packet, false otherwise. 
 306                                 static inline bool Endpoint_IsSETUPReceived(void); 
 308                                 /** Clears a received SETUP packet on the currently selected CONTROL type endpoint, freeing up the 
 309                                  *  endpoint for the next packet. 
 311                                  *  \ingroup Group_EndpointPacketManagement 
 313                                  *  \note This is not applicable for non CONTROL type endpoints.                          
 315                                 static inline void Endpoint_ClearSETUP(void); 
 317                                 /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the 
 318                                  *  next packet and switching to the alternative endpoint bank if double banked. 
 320                                  *  \ingroup Group_EndpointPacketManagement 
 322                                 static inline void Endpoint_ClearIN(void); 
 324                                 /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint 
 325                                  *  for the next packet and switching to the alternative endpoint bank if double banked. 
 327                                  *  \ingroup Group_EndpointPacketManagement 
 329                                 static inline void Endpoint_ClearOUT(void); 
 331                                 /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the 
 332                                  *  indicated endpoint and that the current transfer sequence should be aborted. This provides a 
 333                                  *  way for devices to indicate invalid commands to the host so that the current transfer can be 
 334                                  *  aborted and the host can begin its own recovery sequence. 
 336                                  *  The currently selected endpoint remains stalled until either the \ref Endpoint_ClearStall() macro 
 337                                  *  is called, or the host issues a CLEAR FEATURE request to the device for the currently selected 
 340                                  *  \ingroup Group_EndpointPacketManagement 
 342                                 static inline void Endpoint_StallTransaction(void); 
 344                                 /** Clears the STALL condition on the currently selected endpoint. 
 346                                  *  \ingroup Group_EndpointPacketManagement 
 348                                 static inline void Endpoint_ClearStall(void); 
 350                                 /** Determines if the currently selected endpoint is stalled, false otherwise. 
 352                                  *  \ingroup Group_EndpointPacketManagement 
 354                                  *  \return Boolean true if the currently selected endpoint is stalled, false otherwise 
 356                                 static inline bool Endpoint_IsStalled(void); 
 358                                 /** Resets the data toggle of the currently selected endpoint. */ 
 359                                 static inline void Endpoint_ResetDataToggle(void); 
 361                                 /** Determines the currently selected endpoint's direction. 
 363                                  *  \return The currently selected endpoint's direction, as a ENDPOINT_DIR_* mask. 
 365                                 static inline uint8_t Endpoint_GetEndpointDirection(void); 
 367                                 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__) 
 368                                         #define Endpoint_BytesInEndpoint()        UEBCX 
 370                                         #define Endpoint_BytesInEndpoint()        UEBCLX 
 373                                 #if !defined(CONTROL_ONLY_DEVICE) 
 374                                         #define Endpoint_GetCurrentEndpoint()     (UENUM & ENDPOINT_EPNUM_MASK) 
 376                                         #define Endpoint_GetCurrentEndpoint()     ENDPOINT_CONTROLEP 
 379                                 #if !defined(CONTROL_ONLY_DEVICE) 
 380                                         #define Endpoint_SelectEndpoint(epnum)    MACROS{ UENUM = epnum; }MACROE 
 382                                         #define Endpoint_SelectEndpoint(epnum)    (void)epnum 
 385                                 #define Endpoint_ResetFIFO(epnum)             MACROS{ UERST = (1 << epnum); UERST = 0; }MACROE 
 387                                 #define Endpoint_EnableEndpoint()             MACROS{ UECONX |= (1 << EPEN); }MACROE 
 389                                 #define Endpoint_DisableEndpoint()            MACROS{ UECONX &= ~(1 << EPEN); }MACROE 
 391                                 #define Endpoint_IsEnabled()                  ((UECONX & (1 << EPEN)) ? true : false) 
 393                                 #if !defined(CONTROL_ONLY_DEVICE) 
 394                                         #define Endpoint_IsReadWriteAllowed()     ((UEINTX & (1 << RWAL)) ? true : false) 
 397                                 #define Endpoint_IsConfigured()               ((UESTA0X & (1 << CFGOK)) ? true : false) 
 399                                 #define Endpoint_GetEndpointInterrupts()      UEINT 
 401                                 #define Endpoint_ClearEndpointInterrupt(n)    MACROS{ UEINT &= ~(1 << n); }MACROE 
 403                                 #define Endpoint_HasEndpointInterrupted(n)    ((UEINT & (1 << n)) ? true : false) 
 405                                 #define Endpoint_IsINReady()                  ((UEINTX & (1 << TXINI))  ? true : false) 
 407                                 #define Endpoint_IsOUTReceived()              ((UEINTX & (1 << RXOUTI)) ? true : false) 
 409                                 #define Endpoint_IsSETUPReceived()            ((UEINTX & (1 << RXSTPI)) ? true : false) 
 411                                 #define Endpoint_ClearSETUP()                 MACROS{ UEINTX &= ~(1 << RXSTPI); }MACROE 
 413                                 #if !defined(CONTROL_ONLY_DEVICE) 
 414                                         #define Endpoint_ClearIN()                MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << TXINI)); \ 
 415                                                                                           UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE 
 417                                         #define Endpoint_ClearIN()                MACROS{ UEINTX &= ~(1 << TXINI); }MACROE 
 420                                 #if !defined(CONTROL_ONLY_DEVICE) 
 421                                         #define Endpoint_ClearOUT()               MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << RXOUTI)); \ 
 422                                                                                           UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE 
 424                                         #define Endpoint_ClearOUT()               MACROS{ UEINTX &= ~(1 << RXOUTI); }MACROE                      
 427                                 #define Endpoint_StallTransaction()           MACROS{ UECONX |= (1 << STALLRQ); }MACROE 
 429                                 #define Endpoint_ClearStall()                 MACROS{ UECONX |= (1 << STALLRQC); }MACROE 
 431                                 #define Endpoint_IsStalled()                  ((UECONX & (1 << STALLRQ)) ? true : false) 
 433                                 #define Endpoint_ResetDataToggle()            MACROS{ UECONX |= (1 << RSTDT); }MACROE 
 435                                 #define Endpoint_GetEndpointDirection()       (UECFG0X & ENDPOINT_DIR_IN) 
 439                         /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function. 
 441                          *  \ingroup Group_EndpointRW 
 443                         enum Endpoint_WaitUntilReady_ErrorCodes_t
 
 445                                 ENDPOINT_READYWAIT_NoError                 
= 0, /**< Endpoint is ready for next packet, no error. */ 
 446                                 ENDPOINT_READYWAIT_EndpointStalled         
= 1, /**< The endpoint was stalled during the stream 
 447                                                                                  *   transfer by the host or device. 
 449                                 ENDPOINT_READYWAIT_DeviceDisconnected      
= 2, /**< Device was disconnected from the host while 
 450                                                                                  *   waiting for the endpoint to become ready. 
 452                                 ENDPOINT_READYWAIT_Timeout                 
= 3, /**< The host failed to accept or send the next packet 
 453                                                                                  *   within the software timeout period set by the 
 454                                                                                  *   \ref USB_STREAM_TIMEOUT_MS macro. 
 458                         /** Enum for the possible error return codes of the Endpoint_*_Stream_* functions. 
 460                          *  \ingroup Group_EndpointRW 
 462                         enum Endpoint_Stream_RW_ErrorCodes_t
 
 464                                 ENDPOINT_RWSTREAM_NoError            
= 0, /**< Command completed successfully, no error. */ 
 465                                 ENDPOINT_RWSTREAM_EndpointStalled    
= 1, /**< The endpoint was stalled during the stream 
 466                                                                            *   transfer by the host or device. 
 468                                 ENDPOINT_RWSTREAM_DeviceDisconnected 
= 1, /**< Device was disconnected from the host during 
 471                                 ENDPOINT_RWSTREAM_Timeout            
= 2, /**< The host failed to accept or send the next packet 
 472                                                                            *   within the software timeout period set by the 
 473                                                                            *   \ref USB_STREAM_TIMEOUT_MS macro. 
 475                                 ENDPOINT_RWSTREAM_CallbackAborted    
= 3, /**< Indicates that the stream's callback function 
 476                                                                        *   aborted the transfer early. 
 480                         /** Enum for the possible error return codes of the Endpoint_*_Control_Stream_* functions.. 
 482                          *  \ingroup Group_EndpointRW 
 484                         enum Endpoint_ControlStream_RW_ErrorCodes_t
 
 486                                 ENDPOINT_RWCSTREAM_NoError            
= 0, /**< Command completed successfully, no error. */ 
 487                                 ENDPOINT_RWCSTREAM_HostAborted        
= 1, /**< The aborted the transfer prematurely. */ 
 490                 /* Inline Functions: */ 
 491                         /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints. 
 493                          *  \ingroup Group_EndpointRW 
 495                          *  \return Next byte in the currently selected endpoint's FIFO buffer 
 497                         static inline uint8_t Endpoint_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 498                         static inline uint8_t Endpoint_Read_Byte(void) 
 503                         /** Writes one byte from the currently selected endpoint's bank, for IN direction endpoints. 
 505                          *  \ingroup Group_EndpointRW 
 507                          *  \param Byte  Next byte to write into the the currently selected endpoint's FIFO buffer 
 509                         static inline void Endpoint_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
; 
 510                         static inline void Endpoint_Write_Byte(const uint8_t Byte
) 
 515                         /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints. 
 517                          *  \ingroup Group_EndpointRW 
 519                         static inline void Endpoint_Discard_Byte(void) ATTR_ALWAYS_INLINE
; 
 520                         static inline void Endpoint_Discard_Byte(void) 
 527                         /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT 
 528                          *  direction endpoints. 
 530                          *  \ingroup Group_EndpointRW 
 532                          *  \return Next word in the currently selected endpoint's FIFO buffer 
 534                         static inline uint16_t Endpoint_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 535                         static inline uint16_t Endpoint_Read_Word_LE(void) 
 540                                 Data 
|= (((uint16_t)UEDATX
) << 8); 
 545                         /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT 
 546                          *  direction endpoints. 
 548                          *  \ingroup Group_EndpointRW 
 550                          *  \return Next word in the currently selected endpoint's FIFO buffer 
 552                         static inline uint16_t Endpoint_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 553                         static inline uint16_t Endpoint_Read_Word_BE(void) 
 557                                 Data  
= (((uint16_t)UEDATX
) << 8); 
 563                         /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN 
 564                          *  direction endpoints. 
 566                          *  \ingroup Group_EndpointRW 
 568                          *  \param Word  Next word to write to the currently selected endpoint's FIFO buffer 
 570                         static inline void Endpoint_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
; 
 571                         static inline void Endpoint_Write_Word_LE(const uint16_t Word
) 
 573                                 UEDATX 
= (Word 
& 0xFF); 
 574                                 UEDATX 
= (Word 
>> 8); 
 577                         /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN 
 578                          *  direction endpoints. 
 580                          *  \ingroup Group_EndpointRW 
 582                          *  \param Word  Next word to write to the currently selected endpoint's FIFO buffer 
 584                         static inline void Endpoint_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
; 
 585                         static inline void Endpoint_Write_Word_BE(const uint16_t Word
) 
 587                                 UEDATX 
= (Word 
>> 8); 
 588                                 UEDATX 
= (Word 
& 0xFF); 
 591                         /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints. 
 593                          *  \ingroup Group_EndpointRW 
 595                         static inline void Endpoint_Discard_Word(void) ATTR_ALWAYS_INLINE
; 
 596                         static inline void Endpoint_Discard_Word(void) 
 604                         /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT 
 605                          *  direction endpoints. 
 607                          *  \ingroup Group_EndpointRW 
 609                          *  \return Next double word in the currently selected endpoint's FIFO buffer 
 611                         static inline uint32_t Endpoint_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 612                         static inline uint32_t Endpoint_Read_DWord_LE(void) 
 620                                 Data
.Bytes
[0] = UEDATX
; 
 621                                 Data
.Bytes
[1] = UEDATX
; 
 622                                 Data
.Bytes
[2] = UEDATX
; 
 623                                 Data
.Bytes
[3] = UEDATX
; 
 628                         /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT 
 629                          *  direction endpoints. 
 631                          *  \ingroup Group_EndpointRW 
 633                          *  \return Next double word in the currently selected endpoint's FIFO buffer 
 635                         static inline uint32_t Endpoint_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 636                         static inline uint32_t Endpoint_Read_DWord_BE(void) 
 644                                 Data
.Bytes
[3] = UEDATX
; 
 645                                 Data
.Bytes
[2] = UEDATX
; 
 646                                 Data
.Bytes
[1] = UEDATX
; 
 647                                 Data
.Bytes
[0] = UEDATX
; 
 652                         /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN 
 653                          *  direction endpoints. 
 655                          *  \ingroup Group_EndpointRW 
 657                          *  \param DWord  Next double word to write to the currently selected endpoint's FIFO buffer 
 659                         static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
; 
 660                         static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
) 
 662                                 UEDATX 
= (DWord 
&  0xFF); 
 663                                 UEDATX 
= (DWord 
>> 8); 
 664                                 UEDATX 
= (DWord 
>> 16); 
 665                                 UEDATX 
= (DWord 
>> 24); 
 668                         /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN 
 669                          *  direction endpoints. 
 671                          *  \ingroup Group_EndpointRW 
 673                          *  \param DWord  Next double word to write to the currently selected endpoint's FIFO buffer 
 675                         static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
; 
 676                         static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
) 
 678                                 UEDATX 
= (DWord 
>> 24); 
 679                                 UEDATX 
= (DWord 
>> 16); 
 680                                 UEDATX 
= (DWord 
>> 8); 
 681                                 UEDATX 
= (DWord 
&  0xFF); 
 684                         /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.        
 686                          *  \ingroup Group_EndpointRW 
 688                         static inline void Endpoint_Discard_DWord(void) ATTR_ALWAYS_INLINE
; 
 689                         static inline void Endpoint_Discard_DWord(void) 
 699                 /* External Variables: */ 
 700                         /** Global indicating the maximum packet size of the default control endpoint located at address 
 701                          *  0 in the device. This value is set to the value indicated in the device descriptor in the user 
 702                          *  project once the USB interface is initialized into device mode. 
 704                          *  If space is an issue, it is possible to fix this to a static value by defining the control 
 705                          *  endpoint size in the FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile 
 706                          *  via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically 
 707                          *  read from the descriptors at runtime and instead fixed to the given value. When used, it is 
 708                          *  important that the descriptor control endpoint size value matches the size given as the 
 709                          *  FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the FIXED_CONTROL_ENDPOINT_SIZE token 
 710                          *  be used in the descriptors to ensure this. 
 712                          *  \note This variable should be treated as read-only in the user application, and never manually 
 715                         #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__)) 
 716                                 extern uint8_t USB_ControlEndpointSize
; 
 718                                 #define USB_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE 
 721                 /* Function Prototypes: */ 
 722                         /** Configures the specified endpoint number with the given endpoint type, direction, bank size 
 723                          *  and banking mode. Endpoints should be allocated in ascending order by their address in the 
 724                          *  device (i.e. endpoint 1 should be configured before endpoint 2 and so on). 
 726                          *  The endpoint type may be one of the EP_TYPE_* macros listed in LowLevel.h and the direction 
 727                          *  may be either \ref ENDPOINT_DIR_OUT or \ref ENDPOINT_DIR_IN. 
 729                          *  The bank size must indicate the maximum packet size that the endpoint can handle. Different 
 730                          *  endpoint numbers can handle different maximum packet sizes - refer to the chosen USB AVR's 
 731                          *  datasheet to determine the maximum bank size for each endpoint. 
 733                          *  The banking mode may be either \ref ENDPOINT_BANK_SINGLE or \ref ENDPOINT_BANK_DOUBLE. 
 735                          *  The success of this routine can be determined via the \ref Endpoint_IsConfigured() macro. 
 737                          *  \note This routine will select the specified endpoint, and the endpoint will remain selected 
 738                          *        once the routine completes regardless of if the endpoint configuration succeeds. 
 740                          *  \return Boolean true if the configuration succeeded, false otherwise 
 742                         bool Endpoint_ConfigureEndpoint(const uint8_t  Number
, const uint8_t Type
, const uint8_t Direction
, 
 743                                                         const uint16_t Size
, const uint8_t Banks
); 
 745                         #if !defined(CONTROL_ONLY_DEVICE) 
 747                         /** Spinloops until the currently selected non-control endpoint is ready for the next packet of data 
 748                          *  to be read or written to it. 
 750                          *  \note This routine should not be called on CONTROL type endpoints. 
 752                          *  \ingroup Group_EndpointRW 
 754                          *  \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum. 
 756                         uint8_t Endpoint_WaitUntilReady(void); 
 758                         /** Reads and discards the given number of bytes from the endpoint from the given buffer, 
 759                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
 760                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
 761                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between 
 762                          *  each USB packet, the given stream callback function is executed repeatedly until the next 
 763                          *  packet is ready, allowing for early aborts of stream transfers. 
 765                          *      The callback routine should be created using the \ref STREAM_CALLBACK() macro. If the token 
 766                          *  NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled 
 767                          *  and this function has the Callback parameter omitted. 
 769                          *  \note This routine should not be used on CONTROL type endpoints. 
 771                          *  \ingroup Group_EndpointRW 
 773                          *  \param Length    Number of bytes to send via the currently selected endpoint. 
 774                          *  \param Callback  Name of a callback routine to call between successive USB packet transfers, NULL if no callback 
 776                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 778                         uint8_t Endpoint_Discard_Stream(uint16_t Length
 
 779                         #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__) 
 780                                                         , uint8_t (* const Callback
)(void) 
 784                         /** Writes the given number of bytes to the endpoint from the given buffer in little endian, 
 785                          *  sending full packets to the host as needed. The last packet filled is not automatically sent; 
 786                          *  the user is responsible for manually sending the last written packet to the host via the 
 787                          *  \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function 
 788                          *  is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early 
 789                          *  aborts of stream transfers. 
 791                          *      The callback routine should be created using the \ref STREAM_CALLBACK() macro. If the token 
 792                          *  NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled 
 793                          *  and this function has the Callback parameter omitted. 
 795                          *  \note This routine should not be used on CONTROL type endpoints. 
 797                          *  \ingroup Group_EndpointRW 
 799                          *  \param Buffer    Pointer to the source data buffer to read from. 
 800                          *  \param Length    Number of bytes to read for the currently selected endpoint into the buffer. 
 801                          *  \param Callback  Name of a callback routine to call between successive USB packet transfers, NULL if no callback 
 803                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 805                         uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
 
 806                         #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__) 
 807                                                          , uint8_t (* const Callback
)(void) 
 809                                                          ) ATTR_NON_NULL_PTR_ARG(1); 
 811                         /** Writes the given number of bytes to the endpoint from the given buffer in big endian, 
 812                          *  sending full packets to the host as needed. The last packet filled is not automatically sent; 
 813                          *  the user is responsible for manually sending the last written packet to the host via the 
 814                          *  \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function 
 815                          *  is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early 
 816                          *  aborts of stream transfers. 
 818                          *      The callback routine should be created using the \ref STREAM_CALLBACK() macro. If the token 
 819                          *  NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled 
 820                          *  and this function has the Callback parameter omitted. 
 822                          *  \note This routine should not be used on CONTROL type endpoints. 
 824                          *  \ingroup Group_EndpointRW 
 826                          *  \param Buffer    Pointer to the source data buffer to read from. 
 827                          *  \param Length    Number of bytes to read for the currently selected endpoint into the buffer. 
 828                          *  \param Callback  Name of a callback routine to call between successive USB packet transfers, NULL if no callback 
 830                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 832                         uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
 
 833                         #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__) 
 834                                                          , uint8_t (* const Callback
)(void) 
 836                                                          ) ATTR_NON_NULL_PTR_ARG(1); 
 838                         /** Reads the given number of bytes from the endpoint from the given buffer in little endian, 
 839                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
 840                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
 841                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between 
 842                          *  each USB packet, the given stream callback function is executed repeatedly until the endpoint 
 843                          *  is ready to accept the next packet, allowing for early aborts of stream transfers. 
 845                          *      The callback routine should be created using the \ref STREAM_CALLBACK() macro. If the token 
 846                          *  NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled 
 847                          *  and this function has the Callback parameter omitted. 
 849                          *  \note This routine should not be used on CONTROL type endpoints. 
 851                          *  \ingroup Group_EndpointRW 
 853                          *  \param Buffer    Pointer to the destination data buffer to write to. 
 854                          *  \param Length    Number of bytes to send via the currently selected endpoint. 
 855                          *  \param Callback  Name of a callback routine to call between successive USB packet transfers, NULL if no callback 
 857                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 859                         uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
 
 860                         #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__) 
 861                                                         , uint8_t (* const Callback
)(void) 
 863                                                         ) ATTR_NON_NULL_PTR_ARG(1); 
 865                         /** Reads the given number of bytes from the endpoint from the given buffer in big endian, 
 866                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
 867                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
 868                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between 
 869                          *  each USB packet, the given stream callback function is executed repeatedly until the endpoint 
 870                          *  is ready to accept the next packet, allowing for early aborts of stream transfers. 
 872                          *      The callback routine should be created using the \ref STREAM_CALLBACK() macro. If the token 
 873                          *  NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled 
 874                          *  and this function has the Callback parameter omitted. 
 876                          *  \note This routine should not be used on CONTROL type endpoints. 
 878                          *  \ingroup Group_EndpointRW 
 880                          *  \param Buffer    Pointer to the destination data buffer to write to. 
 881                          *  \param Length    Number of bytes to send via the currently selected endpoint. 
 882                          *  \param Callback  Name of a callback routine to call between successive USB packet transfers, NULL if no callback 
 884                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 886                         uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
 
 887                         #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__) 
 888                                                         , uint8_t (* const Callback
)(void) 
 890                                                         ) ATTR_NON_NULL_PTR_ARG(1); 
 894                         /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian, 
 895                          *  sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared 
 896                          *  in both failure and success states; the user is responsible for manually clearing the setup OUT to 
 897                          *  finalize the transfer via the \ref Endpoint_ClearOUT() macro. 
 899                          *  \note This routine should only be used on CONTROL type endpoints. 
 901                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 902                          *           together; i.e. the entire stream data must be read or written at the one time. 
 904                          *  \ingroup Group_EndpointRW 
 906                          *  \param Buffer  Pointer to the source data buffer to read from. 
 907                          *  \param Length  Number of bytes to read for the currently selected endpoint into the buffer. 
 909                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 911                         uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 913                         /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian, 
 914                          *  sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared 
 915                          *  in both failure and success states; the user is responsible for manually clearing the setup OUT to 
 916                          *  finalize the transfer via the \ref Endpoint_ClearOUT() macro. 
 918                          *  \note This routine should only be used on CONTROL type endpoints. 
 920                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 921                          *           together; i.e. the entire stream data must be read or written at the one time. 
 923                          *  \ingroup Group_EndpointRW 
 925                          *  \param Buffer  Pointer to the source data buffer to read from. 
 926                          *  \param Length  Number of bytes to read for the currently selected endpoint into the buffer. 
 928                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 930                         uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 932                         /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian, 
 933                          *  discarding fully read packets from the host as needed. The device IN acknowledgement is not 
 934                          *  automatically sent after success or failure states; the user is responsible for manually sending the 
 935                          *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro. 
 937                          *  \note This routine should only be used on CONTROL type endpoints. 
 939                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 940                          *           together; i.e. the entire stream data must be read or written at the one time. 
 942                          *  \ingroup Group_EndpointRW 
 944                          *  \param Buffer  Pointer to the destination data buffer to write to. 
 945                          *  \param Length  Number of bytes to send via the currently selected endpoint. 
 947                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 949                         uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 951                         /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian, 
 952                          *  discarding fully read packets from the host as needed. The device IN acknowledgement is not 
 953                          *  automatically sent after success or failure states; the user is responsible for manually sending the 
 954                          *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro. 
 956                          *  \note This routine should only be used on CONTROL type endpoints. 
 958                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 959                          *           together; i.e. the entire stream data must be read or written at the one time. 
 961                          *  \ingroup Group_EndpointRW 
 963                          *  \param Buffer  Pointer to the destination data buffer to write to. 
 964                          *  \param Length  Number of bytes to send via the currently selected endpoint. 
 966                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 968                         uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);                 
 970         /* Private Interface - For use in library only: */ 
 971         #if !defined(__DOXYGEN__) 
 973                         #define Endpoint_AllocateMemory()              MACROS{ UECFG1X |=  (1 << ALLOC); }MACROE 
 974                         #define Endpoint_DeallocateMemory()            MACROS{ UECFG1X &= ~(1 << ALLOC); }MACROE 
 976                         #define _ENDPOINT_GET_MAXSIZE(n)               _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## n) 
 977                         #define _ENDPOINT_GET_MAXSIZE2(details)        _ENDPOINT_GET_MAXSIZE3(details) 
 978                         #define _ENDPOINT_GET_MAXSIZE3(maxsize, db)    maxsize 
 980                         #define _ENDPOINT_GET_DOUBLEBANK(n)            _ENDPOINT_GET_DOUBLEBANK2(ENDPOINT_DETAILS_EP ## n) 
 981                         #define _ENDPOINT_GET_DOUBLEBANK2(details)     _ENDPOINT_GET_DOUBLEBANK3(details) 
 982                         #define _ENDPOINT_GET_DOUBLEBANK3(maxsize, db) db 
 984                         #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) 
 985                                 #define ENDPOINT_DETAILS_EP0               64,  true 
 986                                 #define ENDPOINT_DETAILS_EP1               256, true 
 987                                 #define ENDPOINT_DETAILS_EP2               64,  true 
 988                                 #define ENDPOINT_DETAILS_EP3               64,  true 
 989                                 #define ENDPOINT_DETAILS_EP4               64,  true 
 990                                 #define ENDPOINT_DETAILS_EP5               64,  true 
 991                                 #define ENDPOINT_DETAILS_EP6               64,  true 
 993                                 #define ENDPOINT_DETAILS_EP0               64,  true 
 994                                 #define ENDPOINT_DETAILS_EP1               64,  false 
 995                                 #define ENDPOINT_DETAILS_EP2               64,  false 
 996                                 #define ENDPOINT_DETAILS_EP3               64,  true 
 997                                 #define ENDPOINT_DETAILS_EP4               64,  true                     
1000                         #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks)            \ 
1001                                                             Endpoint_ConfigureEndpoint_Prv(Number,          \ 
1002                                                                       ((Type << EPTYPE0) | Direction),      \ 
1003                                                                       ((1 << ALLOC) | Banks |               \ 
1004                                                                         (__builtin_constant_p(Size) ?       \ 
1005                                                                          Endpoint_BytesToEPSizeMask(Size) :  \ 
1006                                                                          Endpoint_BytesToEPSizeMaskDynamic(Size)))) 
1008                 /* Function Prototypes: */ 
1009                         void    Endpoint_ClearEndpoints(void); 
1010                         uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size
); 
1011                         bool    Endpoint_ConfigureEndpoint_Prv(const uint8_t Number
, const uint8_t UECFG0XData
, const uint8_t UECFG1XData
); 
1013                 /* Inline Functions: */ 
1014                         static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
; 
1015                         static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
) 
1017                                 uint8_t  MaskVal    
= 0; 
1018                                 uint16_t CheckBytes 
= 8; 
1020                                 while (CheckBytes 
< Bytes
) 
1026                                 return (MaskVal 
<< EPSIZE0
); 
1031         /* Disable C linkage for C++ Compilers: */ 
1032                 #if defined(__cplusplus)