3      Copyright (C) Dean Camera, 2018. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2018  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 disclaims 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 AVR8 microcontrollers. 
  33  *  \copydetails Group_Host_AVR8 
  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_AVR8 Host Management (AVR8) 
  41  *  \brief USB Host definitions for the AVR8 microcontrollers. 
  43  *  Architecture specific USB Host definitions for the Atmel 8-bit AVR microcontrollers. 
  48 #ifndef __USBHOST_AVR8_H__ 
  49 #define __USBHOST_AVR8_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                 #if defined(INVERTED_VBUS_ENABLE_LINE) && !defined(NO_AUTO_VBUS_MANAGEMENT) 
  68                         #error The INVERTED_VBUS_ENABLE_LINE compile option requires NO_AUTO_VBUS_MANAGEMENT for the AVR8 architecture. 
  71         /* Public Interface - May be used in end-application: */ 
  73                         /** Indicates the fixed USB device address which any attached device is enumerated to when in 
  74                          *  host mode. As only one USB device may be attached to the AVR in host mode at any one time 
  75                          *  and that the address used is not important (other than the fact that it is non-zero), a 
  76                          *  fixed value is specified by the library. 
  78                         #define USB_HOST_DEVICEADDRESS                 1 
  80                         #if !defined(HOST_DEVICE_SETTLE_DELAY_MS) || defined(__DOXYGEN__) 
  81                                 /** Constant for the delay in milliseconds after a device is connected before the library 
  82                                  *  will start the enumeration process. Some devices require a delay of up to 5 seconds 
  83                                  *  after connection before the enumeration process can start or incorrect operation will 
  86                                  *  The default delay value may be overridden in the user project makefile by defining the 
  87                                  *  \c HOST_DEVICE_SETTLE_DELAY_MS token to the required delay in milliseconds, and passed to the 
  88                                  *  compiler using the -D switch. 
  90                                 #define HOST_DEVICE_SETTLE_DELAY_MS        1000 
  93                         /** Enum for the error codes for the \ref EVENT_USB_Host_HostError() event. 
  95                          *  \see \ref Group_Events for more information on this event. 
  97                         enum USB_Host_ErrorCodes_t
 
  99                                 HOST_ERROR_VBusVoltageDip       
= 0, /**< VBUS voltage dipped to an unacceptable level. This 
 100                                                                       *   error may be the result of an attached device drawing 
 101                                                                       *   too much current from the VBUS line, or due to the 
 102                                                                       *   AVR's power source being unable to supply sufficient 
 107                         /** Enum for the error codes for the \ref EVENT_USB_Host_DeviceEnumerationFailed() event. 
 109                          *  \see \ref Group_Events for more information on this event. 
 111                         enum USB_Host_EnumerationErrorCodes_t
 
 113                                 HOST_ENUMERROR_NoError          
= 0, /**< No error occurred. Used internally, this is not a valid 
 114                                                                       *   ErrorCode parameter value for the \ref EVENT_USB_Host_DeviceEnumerationFailed() 
 117                                 HOST_ENUMERROR_WaitStage        
= 1, /**< One of the delays between enumeration steps failed 
 118                                                                       *   to complete successfully, due to a timeout or other 
 121                                 HOST_ENUMERROR_NoDeviceDetected 
= 2, /**< No device was detected, despite the USB data lines 
 122                                                                       *   indicating the attachment of a device. 
 124                                 HOST_ENUMERROR_ControlError     
= 3, /**< One of the enumeration control requests failed to 
 125                                                                       *   complete successfully. 
 127                                 HOST_ENUMERROR_PipeConfigError  
= 4, /**< The default control pipe (address 0) failed to 
 128                                                                       *   configure correctly. 
 132                 /* Inline Functions: */ 
 133                         /** Returns the current USB frame number, when in host mode. Every millisecond the USB bus is active (i.e. not suspended) 
 134                          *  the frame number is incremented by one. 
 136                          *  \return Current USB frame number from the USB controller. 
 138                         static inline uint16_t USB_Host_GetFrameNumber(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 139                         static inline uint16_t USB_Host_GetFrameNumber(void) 
 144                         #if !defined(NO_SOF_EVENTS) 
 145                                 /** Enables the host mode Start Of Frame events. When enabled, this causes the 
 146                                  *  \ref EVENT_USB_Host_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus, 
 147                                  *  at the start of each USB frame when a device is enumerated while in host mode. 
 149                                  *  \note This function is not available when the \c NO_SOF_EVENTS compile time token is defined. 
 151                                 static inline void USB_Host_EnableSOFEvents(void) ATTR_ALWAYS_INLINE
; 
 152                                 static inline void USB_Host_EnableSOFEvents(void) 
 154                                         USB_INT_Enable(USB_INT_HSOFI
); 
 157                                 /** Disables the host mode Start Of Frame events. When disabled, this stops the firing of the 
 158                                  *  \ref EVENT_USB_Host_StartOfFrame() event when enumerated in host mode. 
 160                                  *  \note This function is not available when the \c NO_SOF_EVENTS compile time token is defined. 
 162                                 static inline void USB_Host_DisableSOFEvents(void) ATTR_ALWAYS_INLINE
; 
 163                                 static inline void USB_Host_DisableSOFEvents(void) 
 165                                         USB_INT_Disable(USB_INT_HSOFI
); 
 169                         /** Resets the USB bus, including the endpoints in any attached device and pipes on the AVR host. 
 170                          *  USB bus resets leave the default control pipe configured (if already configured). 
 172                          *  If the USB bus has been suspended prior to issuing a bus reset, the attached device will be 
 173                          *  woken up automatically and the bus resumed after the reset has been correctly issued. 
 175                         static inline void USB_Host_ResetBus(void) ATTR_ALWAYS_INLINE
; 
 176                         static inline void USB_Host_ResetBus(void) 
 178                                 UHCON 
|=  (1 << RESET
); 
 181                         /** Determines if a previously issued bus reset (via the \ref USB_Host_ResetBus() macro) has 
 184                          *  \return Boolean \c true if no bus reset is currently being sent, \c false otherwise. 
 186                         static inline bool USB_Host_IsBusResetComplete(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 187                         static inline bool USB_Host_IsBusResetComplete(void) 
 189                                 return ((UHCON 
& (1 << RESET
)) ? 
false : true); 
 192                         /** Resumes USB communications with an attached and enumerated device, by resuming the transmission 
 193                          *  of the 1MS Start Of Frame messages to the device. When resumed, USB communications between the 
 194                          *  host and attached device may occur. 
 196                         static inline void USB_Host_ResumeBus(void) ATTR_ALWAYS_INLINE
; 
 197                         static inline void USB_Host_ResumeBus(void) 
 199                                 UHCON 
|=  (1 << SOFEN
); 
 202                         /** Suspends the USB bus, preventing any communications from occurring between the host and attached 
 203                          *  device until the bus has been resumed. This stops the transmission of the 1MS Start Of Frame 
 204                          *  messages to the device. 
 206                          *  \attention While the USB bus is suspended, all USB interrupt sources are also disabled; this means that 
 207                          *             some events (such as device disconnections) will not fire until the bus is resumed. 
 209                         static inline void USB_Host_SuspendBus(void) ATTR_ALWAYS_INLINE
; 
 210                         static inline void USB_Host_SuspendBus(void) 
 212                                 UHCON 
&= ~(1 << SOFEN
); 
 215                         /** Determines if the USB bus has been suspended via the use of the \ref USB_Host_SuspendBus() macro, 
 216                          *  false otherwise. While suspended, no USB communications can occur until the bus is resumed, 
 217                          *  except for the Remote Wakeup event from the device if supported. 
 219                          *  \return Boolean \c true if the bus is currently suspended, \c false otherwise. 
 221                         static inline bool USB_Host_IsBusSuspended(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 222                         static inline bool USB_Host_IsBusSuspended(void) 
 224                                 return ((UHCON 
& (1 << SOFEN
)) ? 
false : true); 
 227                         /** Determines if the attached device is currently enumerated in Full Speed mode (12Mb/s), or 
 228                          *  false if the attached device is enumerated in Low Speed mode (1.5Mb/s). 
 230                          *  \return Boolean \c true if the attached device is enumerated in Full Speed mode, \c false otherwise. 
 232                         static inline bool USB_Host_IsDeviceFullSpeed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 233                         static inline bool USB_Host_IsDeviceFullSpeed(void) 
 235                                 return ((USBSTA 
& (1 << SPEED
)) ? 
true : false); 
 238                         /** Determines if the attached device is currently issuing a Remote Wakeup request, requesting 
 239                          *  that the host resume the USB bus and wake up the device, \c false otherwise. 
 241                          *  \return Boolean \c true if the attached device has sent a Remote Wakeup request, \c false otherwise. 
 243                         static inline bool USB_Host_IsRemoteWakeupSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 244                         static inline bool USB_Host_IsRemoteWakeupSent(void) 
 246                                 return ((UHINT 
& (1 << RXRSMI
)) ? 
true : false); 
 249                         /** Clears the flag indicating that a Remote Wakeup request has been issued by an attached device. */ 
 250                         static inline void USB_Host_ClearRemoteWakeupSent(void) ATTR_ALWAYS_INLINE
; 
 251                         static inline void USB_Host_ClearRemoteWakeupSent(void) 
 253                                 UHINT 
&= ~(1 << RXRSMI
); 
 256                         /** Accepts a Remote Wakeup request from an attached device. This must be issued in response to 
 257                          *  a device's Remote Wakeup request within 2ms for the request to be accepted and the bus to 
 260                         static inline void USB_Host_ResumeFromWakeupRequest(void) ATTR_ALWAYS_INLINE
; 
 261                         static inline void USB_Host_ResumeFromWakeupRequest(void) 
 263                                 UHCON 
|=  (1 << RESUME
); 
 266                         /** Determines if a resume from Remote Wakeup request is currently being sent to an attached 
 269                          *  \return Boolean \c true if no resume request is currently being sent, \c false otherwise. 
 271                         static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 272                         static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) 
 274                                 return ((UHCON 
& (1 << RESUME
)) ? 
false : true); 
 277         /* Private Interface - For use in library only: */ 
 278         #if !defined(__DOXYGEN__) 
 280                         static inline void USB_Host_HostMode_On(void) ATTR_ALWAYS_INLINE
; 
 281                         static inline void USB_Host_HostMode_On(void) 
 283                                 USBCON 
|=  (1 << HOST
); 
 286                         static inline void USB_Host_HostMode_Off(void) ATTR_ALWAYS_INLINE
; 
 287                         static inline void USB_Host_HostMode_Off(void) 
 289                                 USBCON 
&= ~(1 << HOST
); 
 292                         static inline void USB_Host_VBUS_Auto_Enable(void) ATTR_ALWAYS_INLINE
; 
 293                         static inline void USB_Host_VBUS_Auto_Enable(void) 
 295                                 OTGCON 
&= ~(1 << VBUSHWC
); 
 296                                 UHWCON 
|=  (1 << UVCONE
); 
 299                         static inline void USB_Host_VBUS_Manual_Enable(void) ATTR_ALWAYS_INLINE
; 
 300                         static inline void USB_Host_VBUS_Manual_Enable(void) 
 302                                 OTGCON 
|=  (1 << VBUSHWC
); 
 303                                 UHWCON 
&= ~(1 << UVCONE
); 
 308                         static inline void USB_Host_VBUS_Auto_On(void) ATTR_ALWAYS_INLINE
; 
 309                         static inline void USB_Host_VBUS_Auto_On(void) 
 311                                 OTGCON 
|=  (1 << VBUSREQ
); 
 314                         static inline void USB_Host_VBUS_Manual_On(void) ATTR_ALWAYS_INLINE
; 
 315                         static inline void USB_Host_VBUS_Manual_On(void) 
 317                                 #if defined(INVERTED_VBUS_ENABLE_LINE) 
 324                         static inline void USB_Host_VBUS_Auto_Off(void) ATTR_ALWAYS_INLINE
; 
 325                         static inline void USB_Host_VBUS_Auto_Off(void) 
 327                                 OTGCON 
|=  (1 << VBUSRQC
); 
 330                         static inline void USB_Host_VBUS_Manual_Off(void) ATTR_ALWAYS_INLINE
; 
 331                         static inline void USB_Host_VBUS_Manual_Off(void) 
 333                                 #if defined(INVERTED_VBUS_ENABLE_LINE) 
 340                         static inline void USB_Host_SetDeviceAddress(const uint8_t Address
) ATTR_ALWAYS_INLINE
; 
 341                         static inline void USB_Host_SetDeviceAddress(const uint8_t Address
) 
 343                                 UHADDR  
=  (Address 
& 0x7F); 
 347                         enum USB_Host_WaitMSErrorCodes_t
 
 349                                 HOST_WAITERROR_Successful       
= 0, 
 350                                 HOST_WAITERROR_DeviceDisconnect 
= 1, 
 351                                 HOST_WAITERROR_PipeError        
= 2, 
 352                                 HOST_WAITERROR_SetupStalled     
= 3, 
 355                 /* Function Prototypes: */ 
 356                         void    USB_Host_ProcessNextHostState(void); 
 357                         uint8_t USB_Host_WaitMS(uint8_t MS
); 
 359                         #if defined(__INCLUDE_FROM_HOST_C) 
 360                                 static void USB_Host_ResetDevice(void); 
 364         /* Disable C linkage for C++ Compilers: */ 
 365                 #if defined(__cplusplus)