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 definitions for the AVR32 UC3B microcontrollers. 
  33  *  \copydetails Group_Host_UC3B 
  35  *  \note This file should not be included directly. It is automatically included as needed by the USB driver 
  36  *        dispatch header located in LUFA/Drivers/USB/USB.h. 
  39 /** \ingroup Group_Host 
  40  *  \defgroup Group_Host_UC3B Host Management (UC3B) 
  41  *  \brief USB Host definitions for the AVR32 UC3B microcontrollers. 
  43  *  Architecture specific USB Host definitions for the Atmel 32-bit AVR UC3B microcontrollers. 
  48 #ifndef __USBHOST_UC3B_H__ 
  49 #define __USBHOST_UC3B_H__ 
  52                 #include "../../../../Common/Common.h" 
  53                 #include "../StdDescriptors.h" 
  55                 #include "../USBInterrupt.h" 
  57         /* Enable C linkage for C++ Compilers: */ 
  58                 #if defined(__cplusplus) 
  62         /* Preprocessor Checks: */ 
  63                 #if !defined(__INCLUDE_FROM_USB_DRIVER) 
  64                         #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead. 
  67         /* Public Interface - May be used in end-application: */ 
  69                         /** Indicates the fixed USB device address which any attached device is enumerated to when in 
  70                          *  host mode. As only one USB device may be attached to the AVR in host mode at any one time 
  71                          *  and that the address used is not important (other than the fact that it is non-zero), a 
  72                          *  fixed value is specified by the library. 
  74                         #define USB_HOST_DEVICEADDRESS                 1 
  76                         #if !defined(USB_HOST_TIMEOUT_MS) || defined(__DOXYGEN__) 
  77                                 /** Constant for the maximum software timeout period of sent USB control transactions to an attached 
  78                                  *  device. If a device fails to respond to a sent control request within this period, the 
  79                                  *  library will return a timeout error code. 
  81                                  *  This value may be overridden in the user project makefile as the value of the 
  82                                  *  \ref USB_HOST_TIMEOUT_MS token, and passed to the compiler using the -D switch. 
  84                                 #define USB_HOST_TIMEOUT_MS                1000 
  87                         #if !defined(HOST_DEVICE_SETTLE_DELAY_MS) || defined(__DOXYGEN__) 
  88                                 /** Constant for the delay in milliseconds after a device is connected before the library 
  89                                  *  will start the enumeration process. Some devices require a delay of up to 5 seconds 
  90                                  *  after connection before the enumeration process can start or incorrect operation will 
  93                                  *  The default delay value may be overridden in the user project makefile by defining the 
  94                                  *  \c HOST_DEVICE_SETTLE_DELAY_MS token to the required delay in milliseconds, and passed to the 
  95                                  *  compiler using the -D switch. 
  97                                 #define HOST_DEVICE_SETTLE_DELAY_MS        1000 
 101                         /** Enum for the various states of the USB Host state machine. Only some states are 
 102                          *  implemented in the LUFA library - other states are left to the user to implement. 
 104                          *  For information on each possible USB host state, refer to the USB 2.0 specification. 
 105                          *  Several of the USB host states are broken up further into multiple smaller sub-states, 
 106                          *  so that they can be internally implemented inside the library in an efficient manner. 
 108                          *  \see \ref USB_HostState, which stores the current host state machine state. 
 110                         enum USB_Host_States_t
 
 112                                 HOST_STATE_WaitForDeviceRemoval         
= 0,  /**< Internally implemented by the library. This state can be 
 113                                                                                *   used by the library to wait until the attached device is 
 114                                                                                *   removed by the user - useful for when an error occurs or 
 115                                                                                *   further communication with the device is not needed. This 
 116                                                                                *   allows for other code to run while the state machine is 
 117                                                                                *   effectively disabled. 
 119                                 HOST_STATE_WaitForDevice                
= 1,  /**< Internally implemented by the library. This state indicates 
 120                                                                                *   that the stack is waiting for an interval to elapse before 
 121                                                                                *   continuing with the next step of the device enumeration 
 124                                                                                *   \note Do not manually change to this state in the user code. 
 126                                 HOST_STATE_Unattached                   
= 2,  /**< Internally implemented by the library. This state indicates 
 127                                                                                *   that the host state machine is waiting for a device to be 
 128                                                                                *   attached so that it can start the enumeration process. 
 130                                                                                *   \note Do not manually change to this state in the user code. 
 132                                 HOST_STATE_Powered                      
= 3,  /**< Internally implemented by the library. This state indicates 
 133                                                                                *   that a device has been attached, and the library's internals 
 134                                                                                *   are being configured to begin the enumeration process. 
 136                                                                                *   \note Do not manually change to this state in the user code. 
 138                                 HOST_STATE_Powered_WaitForDeviceSettle  
= 4,  /**< Internally implemented by the library. This state indicates 
 139                                                                                *   that the stack is waiting for the initial settling period to 
 140                                                                                *   elapse before beginning the enumeration process. 
 142                                                                                *   \note Do not manually change to this state in the user code. 
 144                                 HOST_STATE_Powered_WaitForConnect       
= 5,  /**< Internally implemented by the library. This state indicates 
 145                                                                                *   that the stack is waiting for a connection event from the USB 
 146                                                                                *   controller to indicate a valid USB device has been attached to 
 147                                                                                *   the bus and is ready to be enumerated. 
 149                                                                                *   \note Do not manually change to this state in the user code. 
 151                                 HOST_STATE_Powered_DoReset              
= 6,  /**< Internally implemented by the library. This state indicates 
 152                                                                                *   that a valid USB device has been attached, and that it is 
 153                                                                                *   will now be reset to ensure it is ready for enumeration. 
 155                                                                                *   \note Do not manually change to this state in the user code. 
 157                                 HOST_STATE_Powered_ConfigPipe           
= 7,  /**< Internally implemented by the library. This state indicates 
 158                                                                                *   that the attached device is currently powered and reset, and 
 159                                                                                *   that the control pipe is now being configured by the stack. 
 161                                                                                *   \note Do not manually change to this state in the user code. 
 163                                 HOST_STATE_Default                      
= 8,  /**< Internally implemented by the library. This state indicates 
 164                                                                                *   that the stack is currently retrieving the control endpoint's 
 165                                                                                *   size from the device, so that the control pipe can be altered 
 168                                                                                *   \note Do not manually change to this state in the user code. 
 170                                 HOST_STATE_Default_PostReset            
= 9,  /**< Internally implemented by the library. This state indicates that 
 171                                                                                *   the control pipe is being reconfigured to match the retrieved 
 172                                                                                *   control endpoint size from the device, and the device's USB bus 
 173                                                                                *   address is being set. 
 175                                                                                *   \note Do not manually change to this state in the user code. 
 177                                 HOST_STATE_Default_PostAddressSet       
= 10, /**< Internally implemented by the library. This state indicates that 
 178                                                                                *   the device's address has now been set, and the stack is has now 
 179                                                                                *   completed the device enumeration process. This state causes the 
 180                                                                                *   stack to change the current USB device address to that set for 
 181                                                                                *   the connected device, before progressing to the user-implemented 
 182                                                                                *   \ref HOST_STATE_Addressed state for further communications. 
 184                                                                                *   \note Do not manually change to this state in the user code. 
 186                                 HOST_STATE_Addressed                    
= 11, /**< May be implemented by the user project. This state should 
 187                                                                                *   set the device configuration before progressing to the 
 188                                                                                *   \ref HOST_STATE_Configured state. Other processing (such as the 
 189                                                                                *   retrieval and processing of the device descriptor) should also 
 190                                                                                *   be placed in this state. 
 192                                 HOST_STATE_Configured                   
= 12, /**< May be implemented by the user project. This state should implement the 
 193                                                                                *   actual work performed on the attached device and changed to the 
 194                                                                                *   \ref HOST_STATE_Suspended or \ref HOST_STATE_WaitForDeviceRemoval states as needed. 
 196                                 HOST_STATE_Suspended                    
= 15, /**< May be implemented by the user project. This state should be maintained 
 197                                                                                *   while the bus is suspended, and changed to either the \ref HOST_STATE_Configured 
 198                                                                                *   (after resuming the bus with the USB_Host_ResumeBus() macro) or the 
 199                                                                                *   \ref HOST_STATE_WaitForDeviceRemoval states as needed. 
 203                         /** Enum for the error codes for the \ref EVENT_USB_Host_HostError() event. 
 205                          *  \see \ref Group_Events for more information on this event. 
 207                         enum USB_Host_ErrorCodes_t
 
 209                                 HOST_ERROR_VBusVoltageDip       
= 0, /**< VBUS voltage dipped to an unacceptable level. This 
 210                                                                       *   error may be the result of an attached device drawing 
 211                                                                       *   too much current from the VBUS line, or due to the 
 212                                                                       *   AVR's power source being unable to supply sufficient 
 217                         /** Enum for the error codes for the \ref EVENT_USB_Host_DeviceEnumerationFailed() event. 
 219                          *  \see \ref Group_Events for more information on this event. 
 221                         enum USB_Host_EnumerationErrorCodes_t
 
 223                                 HOST_ENUMERROR_NoError          
= 0, /**< No error occurred. Used internally, this is not a valid 
 224                                                                       *   ErrorCode parameter value for the \ref EVENT_USB_Host_DeviceEnumerationFailed() 
 227                                 HOST_ENUMERROR_WaitStage        
= 1, /**< One of the delays between enumeration steps failed 
 228                                                                       *   to complete successfully, due to a timeout or other 
 231                                 HOST_ENUMERROR_NoDeviceDetected 
= 2, /**< No device was detected, despite the USB data lines 
 232                                                                       *   indicating the attachment of a device. 
 234                                 HOST_ENUMERROR_ControlError     
= 3, /**< One of the enumeration control requests failed to 
 235                                                                       *   complete successfully. 
 237                                 HOST_ENUMERROR_PipeConfigError  
= 4, /**< The default control pipe (address 0) failed to 
 238                                                                       *   configure correctly. 
 242                 /* Inline Functions: */ 
 243                         /** Returns the current USB frame number, when in host mode. Every millisecond the USB bus is active (i.e. not suspended) 
 244                          *  the frame number is incremented by one. 
 246                         static inline uint16_t USB_Host_GetFrameNumber(void) 
 251                         #if !defined(NO_SOF_EVENTS) 
 252                                 /** Enables the host mode Start Of Frame events. When enabled, this causes the 
 253                                  *  \ref EVENT_USB_Host_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus, 
 254                                  *  at the start of each USB frame when a device is enumerated while in host mode. 
 256                                  *  \note Not available when the \c NO_SOF_EVENTS compile time token is defined. 
 258                                 static inline void USB_Host_EnableSOFEvents(void) ATTR_ALWAYS_INLINE
; 
 259                                 static inline void USB_Host_EnableSOFEvents(void) 
 261                                         USB_INT_Enable(USB_INT_HSOFI
); 
 264                                 /** Disables the host mode Start Of Frame events. When disabled, this stops the firing of the 
 265                                  *  \ref EVENT_USB_Host_StartOfFrame() event when enumerated in host mode. 
 267                                  *  \note Not available when the NO_SOF_EVENTS compile time token is defined. 
 269                                 static inline void USB_Host_DisableSOFEvents(void) ATTR_ALWAYS_INLINE
; 
 270                                 static inline void USB_Host_DisableSOFEvents(void) 
 272                                         USB_INT_Disable(USB_INT_HSOFI
); 
 276                         /** Resets the USB bus, including the endpoints in any attached device and pipes on the AVR host. 
 277                          *  USB bus resets leave the default control pipe configured (if already configured). 
 279                          *  If the USB bus has been suspended prior to issuing a bus reset, the attached device will be 
 280                          *  woken up automatically and the bus resumed after the reset has been correctly issued. 
 282                         static inline void USB_Host_ResetBus(void) ATTR_ALWAYS_INLINE
; 
 283                         static inline void USB_Host_ResetBus(void) 
 285                                 UHCON 
|=  (1 << RESET
); 
 288                         /** Determines if a previously issued bus reset (via the \ref USB_Host_ResetBus() macro) has 
 291                          *  \return Boolean \c true if no bus reset is currently being sent, \c false otherwise. 
 293                         static inline bool USB_Host_IsBusResetComplete(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 294                         static inline bool USB_Host_IsBusResetComplete(void) 
 296                                 return ((UHCON 
& (1 << RESET
)) ? 
false : true); 
 299                         /** Resumes USB communications with an attached and enumerated device, by resuming the transmission 
 300                          *  of the 1MS Start Of Frame messages to the device. When resumed, USB communications between the 
 301                          *  host and attached device may occur. 
 303                         static inline void USB_Host_ResumeBus(void) ATTR_ALWAYS_INLINE
; 
 304                         static inline void USB_Host_ResumeBus(void) 
 306                                 UHCON 
|=  (1 << SOFEN
); 
 309                         /** Suspends the USB bus, preventing any communications from occurring between the host and attached 
 310                          *  device until the bus has been resumed. This stops the transmission of the 1MS Start Of Frame 
 311                          *  messages to the device. 
 313                         static inline void USB_Host_SuspendBus(void) ATTR_ALWAYS_INLINE
; 
 314                         static inline void USB_Host_SuspendBus(void) 
 316                                 UHCON 
&= ~(1 << SOFEN
); 
 319                         /** Determines if the USB bus has been suspended via the use of the \ref USB_Host_SuspendBus() macro, 
 320                          *  false otherwise. While suspended, no USB communications can occur until the bus is resumed, 
 321                          *  except for the Remote Wakeup event from the device if supported. 
 323                          *  \return Boolean \c true if the bus is currently suspended, \c false otherwise. 
 325                         static inline bool USB_Host_IsBusSuspended(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 326                         static inline bool USB_Host_IsBusSuspended(void) 
 328                                 return ((UHCON 
& (1 << SOFEN
)) ? 
false : true); 
 331                         /** Determines if the attached device is currently enumerated in Full Speed mode (12Mb/s), or 
 332                          *  false if the attached device is enumerated in Low Speed mode (1.5Mb/s). 
 334                          *  \return Boolean \c true if the attached device is enumerated in Full Speed mode, \c false otherwise. 
 336                         static inline bool USB_Host_IsDeviceFullSpeed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 337                         static inline bool USB_Host_IsDeviceFullSpeed(void) 
 339                                 return ((USBSTA 
& (1 << SPEED
)) ? 
true : false); 
 342                         /** Determines if the attached device is currently issuing a Remote Wakeup request, requesting 
 343                          *  that the host resume the USB bus and wake up the device, false otherwise. 
 345                          *  \return Boolean \c true if the attached device has sent a Remote Wakeup request, \c false otherwise. 
 347                         static inline bool USB_Host_IsRemoteWakeupSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 348                         static inline bool USB_Host_IsRemoteWakeupSent(void) 
 350                                 return ((UHINT 
& (1 << RXRSMI
)) ? 
true : false); 
 353                         /** Clears the flag indicating that a Remote Wakeup request has been issued by an attached device. */ 
 354                         static inline void USB_Host_ClearRemoteWakeupSent(void) ATTR_ALWAYS_INLINE
; 
 355                         static inline void USB_Host_ClearRemoteWakeupSent(void) 
 357                                 UHINT 
&= ~(1 << RXRSMI
); 
 360                         /** Accepts a Remote Wakeup request from an attached device. This must be issued in response to 
 361                          *  a device's Remote Wakeup request within 2ms for the request to be accepted and the bus to 
 364                         static inline void USB_Host_ResumeFromWakeupRequest(void) ATTR_ALWAYS_INLINE
; 
 365                         static inline void USB_Host_ResumeFromWakeupRequest(void) 
 367                                 UHCON 
|=  (1 << RESUME
); 
 370                         /** Determines if a resume from Remote Wakeup request is currently being sent to an attached 
 373                          *  \return Boolean \c true if no resume request is currently being sent, \c false otherwise. 
 375                         static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 376                         static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) 
 378                                 return ((UHCON 
& (1 << RESUME
)) ? 
false : true); 
 381                 /* Function Prototypes: */ 
 382                         /** Convenience function. This routine sends a SET CONFIGURATION standard request to the attached 
 383                          *  device, with the given configuration index. This can be used to easily set the device 
 384                          *  configuration without creating and sending the request manually. 
 386                          *  \note After this routine returns, the control pipe will be selected. 
 388                          *  \param[in] ConfigNumber  Configuration index to send to the device. 
 390                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result. 
 392                         uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber
); 
 394                         /** Convenience function. This routine sends a GET DESCRIPTOR standard request to the attached 
 395                          *  device, requesting the device descriptor. This can be used to easily retrieve information 
 396                          *  about the device such as its VID, PID and power requirements. 
 398                          *  \note After this routine returns, the control pipe will be selected. 
 400                          *  \param[out] DeviceDescriptorPtr  Pointer to the destination device descriptor structure where 
 401                          *                                   the read data is to be stored. 
 403                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result. 
 405                         uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr
); 
 407                         /** Convenience function. This routine sends a GET DESCRIPTOR standard request to the attached 
 408                          *  device, requesting the string descriptor of the specified index. This can be used to easily 
 409                          *  retrieve string descriptors from the device by index, after the index is obtained from the 
 410                          *  Device or Configuration descriptors. 
 412                          *  \note After this routine returns, the control pipe will be selected. 
 414                          *  \param[in]  Index        Index of the string index to retrieve. 
 415                          *  \param[out] Buffer       Pointer to the destination buffer where the retrieved string descriptor is 
 417                          *  \param[in] BufferLength  Maximum size of the string descriptor which can be stored into the buffer. 
 419                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result. 
 421                         uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index
, 
 423                                                                    const uint8_t BufferLength
); 
 425                         /** Clears a stall condition on the given pipe, via a CLEAR FEATURE standard request to the attached device. 
 427                          *  \note After this routine returns, the control pipe will be selected. 
 429                          *  \param[in] EndpointIndex  Index of the endpoint to clear, including the endpoint's direction. 
 431                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result. 
 433                         uint8_t USB_Host_ClearPipeStall(const uint8_t EndpointIndex
); 
 435         /* Private Interface - For use in library only: */ 
 436         #if !defined(__DOXYGEN__) 
 438                         static inline void USB_Host_HostMode_On(void) ATTR_ALWAYS_INLINE
; 
 439                         static inline void USB_Host_HostMode_On(void) 
 441                                 USBCON 
|=  (1 << HOST
); 
 444                         static inline void USB_Host_HostMode_Off(void) ATTR_ALWAYS_INLINE
; 
 445                         static inline void USB_Host_HostMode_Off(void) 
 447                                 USBCON 
&= ~(1 << HOST
); 
 450                         static inline void USB_Host_VBUS_Auto_Enable(void) ATTR_ALWAYS_INLINE
; 
 451                         static inline void USB_Host_VBUS_Auto_Enable(void) 
 453                                 OTGCON 
&= ~(1 << VBUSHWC
); 
 454                                 UHWCON 
|=  (1 << UVCONE
); 
 457                         static inline void USB_Host_VBUS_Manual_Enable(void) ATTR_ALWAYS_INLINE
; 
 458                         static inline void USB_Host_VBUS_Manual_Enable(void) 
 460                                 OTGCON 
|=  (1 << VBUSHWC
); 
 461                                 UHWCON 
&= ~(1 << UVCONE
); 
 466                         static inline void USB_Host_VBUS_Auto_On(void) ATTR_ALWAYS_INLINE
; 
 467                         static inline void USB_Host_VBUS_Auto_On(void) 
 469                                 OTGCON 
|=  (1 << VBUSREQ
); 
 472                         static inline void USB_Host_VBUS_Manual_On(void) ATTR_ALWAYS_INLINE
; 
 473                         static inline void USB_Host_VBUS_Manual_On(void) 
 478                         static inline void USB_Host_VBUS_Auto_Off(void) ATTR_ALWAYS_INLINE
; 
 479                         static inline void USB_Host_VBUS_Auto_Off(void) 
 481                                 OTGCON 
|=  (1 << VBUSRQC
); 
 484                         static inline void USB_Host_VBUS_Manual_Off(void) ATTR_ALWAYS_INLINE
; 
 485                         static inline void USB_Host_VBUS_Manual_Off(void) 
 490                         static inline void USB_Host_SetDeviceAddress(const uint8_t Address
) ATTR_ALWAYS_INLINE
; 
 491                         static inline void USB_Host_SetDeviceAddress(const uint8_t Address
) 
 493                                 UHADDR  
=  (Address 
& 0x7F); 
 497                         enum USB_Host_WaitMSErrorCodes_t
 
 499                                 HOST_WAITERROR_Successful       
= 0, 
 500                                 HOST_WAITERROR_DeviceDisconnect 
= 1, 
 501                                 HOST_WAITERROR_PipeError        
= 2, 
 502                                 HOST_WAITERROR_SetupStalled     
= 3, 
 505                 /* Function Prototypes: */ 
 506                         void    USB_Host_ProcessNextHostState(void); 
 507                         uint8_t USB_Host_WaitMS(uint8_t MS
); 
 509                         #if defined(__INCLUDE_FROM_HOST_C) 
 510                                 static void USB_Host_ResetDevice(void); 
 514         /* Disable C linkage for C++ Compilers: */ 
 515                 #if defined(__cplusplus)