3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all 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 
  32  *  \brief USB host mode definitions. 
  34  *  This file contains structures, function prototypes and macros related to USB host mode. 
  36  *  \note This file should not be included directly. It is automatically included as needed by the USB driver 
  37  *        dispatch header located in LUFA/Drivers/USB/USB.h. 
  40 /** \ingroup Group_USB 
  41  *  @defgroup Group_Host Host Management 
  43  *  USB Host mode related macros and enums. This module contains macros and enums which are used when 
  44  *  the USB controller is initialized in host mode. 
  55                 #include <util/delay.h> 
  57                 #include "../../../Common/Common.h" 
  58                 #include "../HighLevel/StdDescriptors.h" 
  60                 #include "USBInterrupt.h" 
  62         /* Enable C linkage for C++ Compilers: */ 
  63                 #if defined(__cplusplus) 
  67         /* Preprocessor Checks: */ 
  68                 #if !defined(__INCLUDE_FROM_USB_DRIVER) 
  69                         #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead. 
  72         /* Public Interface - May be used in end-application: */ 
  74                         /** Indicates the fixed USB device address which any attached device is enumerated to when in 
  75                          *  host mode. As only one USB device may be attached to the AVR in host mode at any one time 
  76                          *  and that the address used is not important (other than the fact that it is non-zero), a 
  77                          *  fixed value is specified by the library. 
  79                         #define USB_HOST_DEVICEADDRESS                 1 
  81                         #if !defined(USB_HOST_TIMEOUT_MS) || defined(__DOXYGEN__) 
  82                                 /** Constant for the maximum software timeout period of sent USB control transactions to an attached 
  83                                  *  device. If a device fails to respond to a sent control request within this period, the 
  84                                  *  library will return a timeout error code. 
  86                                  *  This value may be overridden in the user project makefile as the value of the 
  87                                  *  \ref USB_HOST_TIMEOUT_MS token, and passed to the compiler using the -D switch. 
  89                                 #define USB_HOST_TIMEOUT_MS                1000 
  92                         #if !defined(HOST_DEVICE_SETTLE_DELAY_MS) || defined(__DOXYGEN__) 
  93                                 /** Constant for the delay in milliseconds after a device is connected before the library 
  94                                  *  will start the enumeration process. Some devices require a delay of up to 5 seconds 
  95                                  *  after connection before the enumeration process can start or incorrect operation will 
  98                                  *  The default delay value may be overridden in the user project makefile by definining the 
  99                                  *  HOST_DEVICE_SETTLE_DELAY_MS token to tbe required delay in milliseconds, and passed to the 
 100                                  *  compiler using the -D switch. 
 102                                 #define HOST_DEVICE_SETTLE_DELAY_MS        1000 
 106                         /** Enum for the various states of the USB Host state machine. Only some states are 
 107                          *  implemented in the LUFA library - other states are left to the user to implement. 
 109                          *  For information on each possible USB host state, refer to the USB 2.0 specification. 
 110                          *  Several of the USB host states are broken up further into multiple smaller sub-states, 
 111                          *  so that they can be internally implemented inside the library in an efficient manner. 
 113                          *  \see \ref USB_HostState, which stores the current host state machine state. 
 115                         enum USB_Host_States_t
 
 117                                 HOST_STATE_WaitForDeviceRemoval         
= 0,  /**< Internally implemented by the library. This state can be 
 118                                                                                *   used by the library to wait until the attached device is 
 119                                                                                *   removed by the user - useful for when an error occurs or 
 120                                                                                *   further communication with the device is not needed. This 
 121                                                                                *   allows for other code to run while the state machine is 
 122                                                                                *   effectively disabled. 
 124                                 HOST_STATE_WaitForDevice                
= 1,  /**< Internally implemented by the library. This state indicates 
 125                                                                                *   that the stack is waiting for an interval to elapse before 
 126                                                                                *   continuing with the next step of the device enumeration 
 129                                                                                *   \note Do not manually change to this state in the user code. 
 131                                 HOST_STATE_Unattached                   
= 2,  /**< Internally implemented by the library. This state indicates 
 132                                                                                *   that the host state machine is waiting for a device to be 
 133                                                                                *   attached so that it can start the enumeration process. 
 135                                                                                *   \note Do not manually change to this state in the user code. 
 137                                 HOST_STATE_Powered                      
= 3,  /**< Internally implemented by the library. This state indicates 
 138                                                                                *   that a device has been attached, and the library's internals 
 139                                                                                *   are being configured to begin the enumeration process. 
 141                                                                                *   \note Do not manually change to this state in the user code. 
 143                                 HOST_STATE_Powered_WaitForDeviceSettle  
= 4,  /**< Internally implemented by the library. This state indicates 
 144                                                                                *   that the stack is waiting for the initial settling period to 
 145                                                                                *   elapse before beginning the enumeration process. 
 147                                                                                *   \note Do not manually change to this state in the user code. 
 149                                 HOST_STATE_Powered_WaitForConnect       
= 5,  /**< Internally implemented by the library. This state indicates 
 150                                                                                *   that the stack is waiting for a connection event from the USB 
 151                                                                                *   controller to indicate a valid USB device has been attached to 
 152                                                                                *   the bus and is ready to be enumerated. 
 154                                                                                *   \note Do not manually change to this state in the user code. 
 156                                 HOST_STATE_Powered_DoReset              
= 6,  /**< Internally implemented by the library. This state indicates 
 157                                                                                *   that a valid USB device has been attached, and that it is 
 158                                                                                *   will now be reset to ensure it is ready for enumeration. 
 160                                                                                *   \note Do not manually change to this state in the user code. 
 162                                 HOST_STATE_Powered_ConfigPipe           
= 7,  /**< Internally implemented by the library. This state indicates 
 163                                                                                *   that the attached device is currently powered and reset, and 
 164                                                                                *   that the control pipe is now being configured by the stack. 
 166                                                                                *   \note Do not manually change to this state in the user code. 
 168                                 HOST_STATE_Default                      
= 8,  /**< Internally implemented by the library. This state indicates 
 169                                                                                *   that the stack is currently retrieving the control endpoint's 
 170                                                                                *   size from the device, so that the control pipe can be altered 
 173                                                                                *   \note Do not manually change to this state in the user code. 
 175                                 HOST_STATE_Default_PostReset            
= 9,  /**< Internally implemented by the library. This state indicates that 
 176                                                                                *   the control pipe is being reconfigured to match the retrieved 
 177                                                                                *   control endpoint size from the device, and the device's USB bus 
 178                                                                                *   address is being set. 
 180                                                                                *   \note Do not manually change to this state in the user code. 
 182                                 HOST_STATE_Default_PostAddressSet       
= 10, /**< Internally implemented by the library. This state indicates that 
 183                                                                                *   the device's address has now been set, and the stack is has now 
 184                                                                                *   completed the device enumeration process. This state causes the 
 185                                                                                *   stack to change the current USB device address to that set for 
 186                                                                                *   the connected device, before progressing to the user-implemented 
 187                                                                                *   \ref HOST_STATE_Addressed state for further communications. 
 189                                                                                *   \note Do not manually change to this state in the user code. 
 191                                 HOST_STATE_Addressed                    
= 11, /**< May be implemented by the user project. This state should 
 192                                                                                *   set the device configuration before progressing to the 
 193                                                                                *   \ref HOST_STATE_Configured state. Other processing (such as the 
 194                                                                                *   retrieval and processing of the device descriptor) should also 
 195                                                                                *   be placed in this state. 
 197                                 HOST_STATE_Configured                   
= 12, /**< May be implemented by the user project. This state should implement the 
 198                                                                                *   actual work performed on the attached device and changed to the 
 199                                                                                *   \ref HOST_STATE_Suspended or \ref HOST_STATE_WaitForDeviceRemoval states as needed. 
 201                                 HOST_STATE_Suspended                    
= 15, /**< May be implemented by the user project. This state should be maintained 
 202                                                                                *   while the bus is suspended, and changed to either the \ref HOST_STATE_Configured 
 203                                                                                *   (after resuming the bus with the USB_Host_ResumeBus() macro) or the 
 204                                                                                *   \ref HOST_STATE_WaitForDeviceRemoval states as needed. 
 208                         /** Enum for the error codes for the \ref EVENT_USB_Host_HostError() event. 
 210                          *  \see \ref Group_Events for more information on this event. 
 212                         enum USB_Host_ErrorCodes_t
 
 214                                 HOST_ERROR_VBusVoltageDip       
= 0, /**< VBUS voltage dipped to an unacceptable level. This 
 215                                                                       *   error may be the result of an attached device drawing 
 216                                                                       *   too much current from the VBUS line, or due to the 
 217                                                                       *   AVR's power source being unable to supply sufficient 
 222                         /** Enum for the error codes for the \ref EVENT_USB_Host_DeviceEnumerationFailed() event. 
 224                          *  \see \ref Group_Events for more information on this event. 
 226                         enum USB_Host_EnumerationErrorCodes_t
 
 228                                 HOST_ENUMERROR_NoError          
= 0, /**< No error occurred. Used internally, this is not a valid 
 229                                                                       *   ErrorCode parameter value for the \ref EVENT_USB_Host_DeviceEnumerationFailed() 
 232                                 HOST_ENUMERROR_WaitStage        
= 1, /**< One of the delays between enumeration steps failed 
 233                                                                       *   to complete successfully, due to a timeout or other 
 236                                 HOST_ENUMERROR_NoDeviceDetected 
= 2, /**< No device was detected, despite the USB data lines 
 237                                                                       *   indicating the attachment of a device. 
 239                                 HOST_ENUMERROR_ControlError     
= 3, /**< One of the enumeration control requests failed to 
 240                                                                       *   complete successfully. 
 242                                 HOST_ENUMERROR_PipeConfigError  
= 4, /**< The default control pipe (address 0) failed to 
 243                                                                       *   configure correctly. 
 247                 /* Inline Functions: */ 
 248                         /** Returns the current USB frame number, when in host mode. Every millisecond the USB bus is active (i.e. not suspended) 
 249                          *  the frame number is incremented by one. 
 251                         static inline uint16_t USB_Host_GetFrameNumber(void) 
 256                         #if !defined(NO_SOF_EVENTS) 
 257                                 /** Enables the host mode Start Of Frame events. When enabled, this causes the 
 258                                  *  \ref EVENT_USB_Host_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus, 
 259                                  *  at the start of each USB frame when a device is enumerated while in host mode. 
 261                                  *  \note Not available when the NO_SOF_EVENTS compile time token is defined. 
 263                                 static inline void USB_Host_EnableSOFEvents(void) ATTR_ALWAYS_INLINE
; 
 264                                 static inline void USB_Host_EnableSOFEvents(void) 
 266                                         USB_INT_Enable(USB_INT_HSOFI
); 
 269                                 /** Disables the host mode Start Of Frame events. When disabled, this stops the firing of the 
 270                                  *  \ref EVENT_USB_Host_StartOfFrame() event when enumerated in host mode. 
 272                                  *  \note Not available when the NO_SOF_EVENTS compile time token is defined. 
 274                                 static inline void USB_Host_DisableSOFEvents(void) ATTR_ALWAYS_INLINE
; 
 275                                 static inline void USB_Host_DisableSOFEvents(void) 
 277                                         USB_INT_Disable(USB_INT_HSOFI
); 
 281                         /** Resets the USB bus, including the endpoints in any attached device and pipes on the AVR host. 
 282                          *  USB bus resets leave the default control pipe configured (if already configured). 
 284                          *  If the USB bus has been suspended prior to issuing a bus reset, the attached device will be 
 285                          *  woken up automatically and the bus resumed after the reset has been correctly issued. 
 287                         static inline void USB_Host_ResetBus(void) ATTR_ALWAYS_INLINE
; 
 288                         static inline void USB_Host_ResetBus(void) 
 290                                 UHCON 
|=  (1 << RESET
); 
 293                         /** Determines if a previously issued bus reset (via the \ref USB_Host_ResetBus() macro) has 
 296                          *  \return Boolean true if no bus reset is currently being sent, false otherwise. 
 298                         static inline bool USB_Host_IsBusResetComplete(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 299                         static inline bool USB_Host_IsBusResetComplete(void) 
 301                                 return ((UHCON 
& (1 << RESET
)) ? 
false : true); 
 304                         /** Resumes USB communications with an attached and enumerated device, by resuming the transmission 
 305                          *  of the 1MS Start Of Frame messages to the device. When resumed, USB communications between the 
 306                          *  host and attached device may occur. 
 308                         static inline void USB_Host_ResumeBus(void) ATTR_ALWAYS_INLINE
; 
 309                         static inline void USB_Host_ResumeBus(void) 
 311                                 UHCON 
|=  (1 << SOFEN
); 
 314                         /** Suspends the USB bus, preventing any communications from occurring between the host and attached 
 315                          *  device until the bus has been resumed. This stops the transmission of the 1MS Start Of Frame 
 316                          *  messages to the device. 
 318                         static inline void USB_Host_SuspendBus(void) ATTR_ALWAYS_INLINE
; 
 319                         static inline void USB_Host_SuspendBus(void) 
 321                                 UHCON 
&= ~(1 << SOFEN
); 
 324                         /** Determines if the USB bus has been suspended via the use of the \ref USB_Host_SuspendBus() macro, 
 325                          *  false otherwise. While suspended, no USB communications can occur until the bus is resumed, 
 326                          *  except for the Remote Wakeup event from the device if supported. 
 328                          *  \return Boolean true if the bus is currently suspended, false otherwise. 
 330                         static inline bool USB_Host_IsBusSuspended(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 331                         static inline bool USB_Host_IsBusSuspended(void) 
 333                                 return ((UHCON 
& (1 << SOFEN
)) ? 
false : true); 
 336                         /** Determines if the attached device is currently enumerated in Full Speed mode (12Mb/s), or 
 337                          *  false if the attached device is enumerated in Low Speed mode (1.5Mb/s). 
 339                          *  \return Boolean true if the attached device is enumerated in Full Speed mode, false otherwise. 
 341                         static inline bool USB_Host_IsDeviceFullSpeed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 342                         static inline bool USB_Host_IsDeviceFullSpeed(void) 
 344                                 return ((USBSTA 
& (1 << SPEED
)) ? 
true : false); 
 347                         /** Determines if the attached device is currently issuing a Remote Wakeup request, requesting 
 348                          *  that the host resume the USB bus and wake up the device, false otherwise. 
 350                          *  \return Boolean true if the attached device has sent a Remote Wakeup request, false otherwise. 
 352                         static inline bool USB_Host_IsRemoteWakeupSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 353                         static inline bool USB_Host_IsRemoteWakeupSent(void) 
 355                                 return ((UHINT 
& (1 << RXRSMI
)) ? 
true : false); 
 358                         /** Clears the flag indicating that a Remote Wakeup request has been issued by an attached device. */ 
 359                         static inline void USB_Host_ClearRemoteWakeupSent(void) ATTR_ALWAYS_INLINE
; 
 360                         static inline void USB_Host_ClearRemoteWakeupSent(void) 
 362                                 UHINT 
&= ~(1 << RXRSMI
); 
 365                         /** Accepts a Remote Wakeup request from an attached device. This must be issued in response to 
 366                          *  a device's Remote Wakeup request within 2ms for the request to be accepted and the bus to 
 369                         static inline void USB_Host_ResumeFromWakeupRequest(void) ATTR_ALWAYS_INLINE
; 
 370                         static inline void USB_Host_ResumeFromWakeupRequest(void) 
 372                                 UHCON 
|=  (1 << RESUME
); 
 375                         /** Determines if a resume from Remote Wakeup request is currently being sent to an attached 
 378                          *  \return Boolean true if no resume request is currently being sent, false otherwise. 
 380                         static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 381                         static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) 
 383                                 return ((UHCON 
& (1 << RESUME
)) ? 
false : true); 
 386                 /* Function Prototypes: */ 
 387                         /** Convenience function. This routine sends a SetConfiguration standard request to the attached 
 388                          *  device, with the given configuration index. This can be used to easily set the device 
 389                          *  configuration without creating and sending the request manually. 
 391                          *  \note After this routine returns, the control pipe will be selected. 
 393                          *  \param[in] ConfigNumber  Configuration index to send to the device. 
 395                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result. 
 397                         uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber
); 
 399                         /** Convenience function. This routine sends a GetDescriptor standard request to the attached 
 400                          *  device, requesting the device descriptor. This can be used to easily retrieve information 
 401                          *  about the device such as its VID, PID and power requirements. 
 403                          *  \note After this routine returns, the control pipe will be selected. 
 405                          *  \param[out] DeviceDescriptorPtr  Pointer to the destination device descriptor structure where 
 406                          *                                   the read data is to be stored. 
 408                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result. 
 410                         uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr
); 
 412                         /** Convenience function. This routine sends a GetDescriptor standard request to the attached 
 413                          *  device, requesting the string descriptor of the specified index. This can be used to easily 
 414                          *  retrieve string descriptors from the device by index, after the index is obtained from the 
 415                          *  Device or Configuration descriptors. 
 417                          *  \note After this routine returns, the control pipe will be selected. 
 419                          *  \param[in]  Index        Index of the string index to retrieve. 
 420                          *  \param[out] Buffer       Pointer to the destination buffer where the retrieved string descriptor is 
 422                          *  \param[in] BufferLength  Maximum size of the string descriptor which can be stored into the buffer. 
 424                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result. 
 426                         uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index
, 
 428                                                                    const uint8_t BufferLength
); 
 430                         /** Clears a stall condition on the given pipe, via a ClearFeature request to the attached device. 
 432                          *  \note After this routine returns, the control pipe will be selected. 
 434                          *  \param[in] EndpointIndex  Index of the endpoint to clear, including the endpoint's direction. 
 436                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result. 
 438                         uint8_t USB_Host_ClearPipeStall(const uint8_t EndpointIndex
); 
 440         /* Private Interface - For use in library only: */ 
 441         #if !defined(__DOXYGEN__) 
 443                         static inline void USB_Host_HostMode_On(void) ATTR_ALWAYS_INLINE
; 
 444                         static inline void USB_Host_HostMode_On(void) 
 446                                 USBCON 
|=  (1 << HOST
); 
 449                         static inline void USB_Host_HostMode_Off(void) ATTR_ALWAYS_INLINE
; 
 450                         static inline void USB_Host_HostMode_Off(void) 
 452                                 USBCON 
&= ~(1 << HOST
); 
 455                         static inline void USB_Host_VBUS_Auto_Enable(void) ATTR_ALWAYS_INLINE
; 
 456                         static inline void USB_Host_VBUS_Auto_Enable(void) 
 458                                 OTGCON 
&= ~(1 << VBUSHWC
); 
 459                                 UHWCON 
|=  (1 << UVCONE
); 
 462                         static inline void USB_Host_VBUS_Manual_Enable(void) ATTR_ALWAYS_INLINE
; 
 463                         static inline void USB_Host_VBUS_Manual_Enable(void) 
 465                                 OTGCON 
|=  (1 << VBUSHWC
); 
 466                                 UHWCON 
&= ~(1 << UVCONE
); 
 471                         static inline void USB_Host_VBUS_Auto_On(void) ATTR_ALWAYS_INLINE
; 
 472                         static inline void USB_Host_VBUS_Auto_On(void) 
 474                                 OTGCON 
|=  (1 << VBUSREQ
); 
 477                         static inline void USB_Host_VBUS_Manual_On(void) ATTR_ALWAYS_INLINE
; 
 478                         static inline void USB_Host_VBUS_Manual_On(void) 
 483                         static inline void USB_Host_VBUS_Auto_Off(void) ATTR_ALWAYS_INLINE
; 
 484                         static inline void USB_Host_VBUS_Auto_Off(void) 
 486                                 OTGCON 
|=  (1 << VBUSRQC
); 
 489                         static inline void USB_Host_VBUS_Manual_Off(void) ATTR_ALWAYS_INLINE
; 
 490                         static inline void USB_Host_VBUS_Manual_Off(void) 
 495                         static inline void USB_Host_SetDeviceAddress(const uint8_t Address
) ATTR_ALWAYS_INLINE
; 
 496                         static inline void USB_Host_SetDeviceAddress(const uint8_t Address
) 
 498                                 UHADDR  
=  (Address 
& 0x7F); 
 502                         enum USB_Host_WaitMSErrorCodes_t
 
 504                                 HOST_WAITERROR_Successful       
= 0, 
 505                                 HOST_WAITERROR_DeviceDisconnect 
= 1, 
 506                                 HOST_WAITERROR_PipeError        
= 2, 
 507                                 HOST_WAITERROR_SetupStalled     
= 3, 
 510                 /* Function Prototypes: */ 
 511                         void    USB_Host_ProcessNextHostState(void); 
 512                         uint8_t USB_Host_WaitMS(uint8_t MS
); 
 514                         #if defined(__INCLUDE_FROM_HOST_C) 
 515                                 static void USB_Host_ResetDevice(void); 
 519         /* Disable C linkage for C++ Compilers: */ 
 520                 #if defined(__cplusplus)