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 (AVR8)
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_Host
41 * \defgroup Group_Host_AVR8 Host Management (AVR8)
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.
49 #ifndef __USBHOST_AVR8_H__
50 #define __USBHOST_AVR8_H__
53 #include "../../../../Common/Common.h"
54 #include "../StdDescriptors.h"
56 #include "../USBInterrupt.h"
58 /* Enable C linkage for C++ Compilers: */
59 #if defined(__cplusplus)
63 /* Preprocessor Checks: */
64 #if !defined(__INCLUDE_FROM_USB_DRIVER)
65 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
68 /* Public Interface - May be used in end-application: */
70 /** Indicates the fixed USB device address which any attached device is enumerated to when in
71 * host mode. As only one USB device may be attached to the AVR in host mode at any one time
72 * and that the address used is not important (other than the fact that it is non-zero), a
73 * fixed value is specified by the library.
75 #define USB_HOST_DEVICEADDRESS 1
77 #if !defined(USB_HOST_TIMEOUT_MS) || defined(__DOXYGEN__)
78 /** Constant for the maximum software timeout period of sent USB control transactions to an attached
79 * device. If a device fails to respond to a sent control request within this period, the
80 * library will return a timeout error code.
82 * This value may be overridden in the user project makefile as the value of the
83 * \ref USB_HOST_TIMEOUT_MS token, and passed to the compiler using the -D switch.
85 #define USB_HOST_TIMEOUT_MS 1000
88 #if !defined(HOST_DEVICE_SETTLE_DELAY_MS) || defined(__DOXYGEN__)
89 /** Constant for the delay in milliseconds after a device is connected before the library
90 * will start the enumeration process. Some devices require a delay of up to 5 seconds
91 * after connection before the enumeration process can start or incorrect operation will
94 * The default delay value may be overridden in the user project makefile by defining the
95 * \c HOST_DEVICE_SETTLE_DELAY_MS token to the required delay in milliseconds, and passed to the
96 * compiler using the -D switch.
98 #define HOST_DEVICE_SETTLE_DELAY_MS 1000
102 /** Enum for the various states of the USB Host state machine. Only some states are
103 * implemented in the LUFA library - other states are left to the user to implement.
105 * For information on each possible USB host state, refer to the USB 2.0 specification.
106 * Several of the USB host states are broken up further into multiple smaller sub-states,
107 * so that they can be internally implemented inside the library in an efficient manner.
109 * \see \ref USB_HostState, which stores the current host state machine state.
111 enum USB_Host_States_t
113 HOST_STATE_WaitForDeviceRemoval
= 0, /**< Internally implemented by the library. This state can be
114 * used by the library to wait until the attached device is
115 * removed by the user - useful for when an error occurs or
116 * further communication with the device is not needed. This
117 * allows for other code to run while the state machine is
118 * effectively disabled.
120 HOST_STATE_WaitForDevice
= 1, /**< Internally implemented by the library. This state indicates
121 * that the stack is waiting for an interval to elapse before
122 * continuing with the next step of the device enumeration
125 * \note Do not manually change to this state in the user code.
127 HOST_STATE_Unattached
= 2, /**< Internally implemented by the library. This state indicates
128 * that the host state machine is waiting for a device to be
129 * attached so that it can start the enumeration process.
131 * \note Do not manually change to this state in the user code.
133 HOST_STATE_Powered
= 3, /**< Internally implemented by the library. This state indicates
134 * that a device has been attached, and the library's internals
135 * are being configured to begin the enumeration process.
137 * \note Do not manually change to this state in the user code.
139 HOST_STATE_Powered_WaitForDeviceSettle
= 4, /**< Internally implemented by the library. This state indicates
140 * that the stack is waiting for the initial settling period to
141 * elapse before beginning the enumeration process.
143 * \note Do not manually change to this state in the user code.
145 HOST_STATE_Powered_WaitForConnect
= 5, /**< Internally implemented by the library. This state indicates
146 * that the stack is waiting for a connection event from the USB
147 * controller to indicate a valid USB device has been attached to
148 * the bus and is ready to be enumerated.
150 * \note Do not manually change to this state in the user code.
152 HOST_STATE_Powered_DoReset
= 6, /**< Internally implemented by the library. This state indicates
153 * that a valid USB device has been attached, and that it is
154 * will now be reset to ensure it is ready for enumeration.
156 * \note Do not manually change to this state in the user code.
158 HOST_STATE_Powered_ConfigPipe
= 7, /**< Internally implemented by the library. This state indicates
159 * that the attached device is currently powered and reset, and
160 * that the control pipe is now being configured by the stack.
162 * \note Do not manually change to this state in the user code.
164 HOST_STATE_Default
= 8, /**< Internally implemented by the library. This state indicates
165 * that the stack is currently retrieving the control endpoint's
166 * size from the device, so that the control pipe can be altered
169 * \note Do not manually change to this state in the user code.
171 HOST_STATE_Default_PostReset
= 9, /**< Internally implemented by the library. This state indicates that
172 * the control pipe is being reconfigured to match the retrieved
173 * control endpoint size from the device, and the device's USB bus
174 * address is being set.
176 * \note Do not manually change to this state in the user code.
178 HOST_STATE_Default_PostAddressSet
= 10, /**< Internally implemented by the library. This state indicates that
179 * the device's address has now been set, and the stack is has now
180 * completed the device enumeration process. This state causes the
181 * stack to change the current USB device address to that set for
182 * the connected device, before progressing to the user-implemented
183 * \ref HOST_STATE_Addressed state for further communications.
185 * \note Do not manually change to this state in the user code.
187 HOST_STATE_Addressed
= 11, /**< May be implemented by the user project. This state should
188 * set the device configuration before progressing to the
189 * \ref HOST_STATE_Configured state. Other processing (such as the
190 * retrieval and processing of the device descriptor) should also
191 * be placed in this state.
193 HOST_STATE_Configured
= 12, /**< May be implemented by the user project. This state should implement the
194 * actual work performed on the attached device and changed to the
195 * \ref HOST_STATE_Suspended or \ref HOST_STATE_WaitForDeviceRemoval states as needed.
197 HOST_STATE_Suspended
= 15, /**< May be implemented by the user project. This state should be maintained
198 * while the bus is suspended, and changed to either the \ref HOST_STATE_Configured
199 * (after resuming the bus with the USB_Host_ResumeBus() macro) or the
200 * \ref HOST_STATE_WaitForDeviceRemoval states as needed.
204 /** Enum for the error codes for the \ref EVENT_USB_Host_HostError() event.
206 * \see \ref Group_Events for more information on this event.
208 enum USB_Host_ErrorCodes_t
210 HOST_ERROR_VBusVoltageDip
= 0, /**< VBUS voltage dipped to an unacceptable level. This
211 * error may be the result of an attached device drawing
212 * too much current from the VBUS line, or due to the
213 * AVR's power source being unable to supply sufficient
218 /** Enum for the error codes for the \ref EVENT_USB_Host_DeviceEnumerationFailed() event.
220 * \see \ref Group_Events for more information on this event.
222 enum USB_Host_EnumerationErrorCodes_t
224 HOST_ENUMERROR_NoError
= 0, /**< No error occurred. Used internally, this is not a valid
225 * ErrorCode parameter value for the \ref EVENT_USB_Host_DeviceEnumerationFailed()
228 HOST_ENUMERROR_WaitStage
= 1, /**< One of the delays between enumeration steps failed
229 * to complete successfully, due to a timeout or other
232 HOST_ENUMERROR_NoDeviceDetected
= 2, /**< No device was detected, despite the USB data lines
233 * indicating the attachment of a device.
235 HOST_ENUMERROR_ControlError
= 3, /**< One of the enumeration control requests failed to
236 * complete successfully.
238 HOST_ENUMERROR_PipeConfigError
= 4, /**< The default control pipe (address 0) failed to
239 * configure correctly.
243 /* Inline Functions: */
244 /** Returns the current USB frame number, when in host mode. Every millisecond the USB bus is active (i.e. not suspended)
245 * the frame number is incremented by one.
247 static inline uint16_t USB_Host_GetFrameNumber(void)
252 #if !defined(NO_SOF_EVENTS)
253 /** Enables the host mode Start Of Frame events. When enabled, this causes the
254 * \ref EVENT_USB_Host_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus,
255 * at the start of each USB frame when a device is enumerated while in host mode.
257 * \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
259 static inline void USB_Host_EnableSOFEvents(void) ATTR_ALWAYS_INLINE
;
260 static inline void USB_Host_EnableSOFEvents(void)
262 USB_INT_Enable(USB_INT_HSOFI
);
265 /** Disables the host mode Start Of Frame events. When disabled, this stops the firing of the
266 * \ref EVENT_USB_Host_StartOfFrame() event when enumerated in host mode.
268 * \note Not available when the NO_SOF_EVENTS compile time token is defined.
270 static inline void USB_Host_DisableSOFEvents(void) ATTR_ALWAYS_INLINE
;
271 static inline void USB_Host_DisableSOFEvents(void)
273 USB_INT_Disable(USB_INT_HSOFI
);
277 /** Resets the USB bus, including the endpoints in any attached device and pipes on the AVR host.
278 * USB bus resets leave the default control pipe configured (if already configured).
280 * If the USB bus has been suspended prior to issuing a bus reset, the attached device will be
281 * woken up automatically and the bus resumed after the reset has been correctly issued.
283 static inline void USB_Host_ResetBus(void) ATTR_ALWAYS_INLINE
;
284 static inline void USB_Host_ResetBus(void)
286 UHCON
|= (1 << RESET
);
289 /** Determines if a previously issued bus reset (via the \ref USB_Host_ResetBus() macro) has
292 * \return Boolean \c true if no bus reset is currently being sent, \c false otherwise.
294 static inline bool USB_Host_IsBusResetComplete(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
295 static inline bool USB_Host_IsBusResetComplete(void)
297 return ((UHCON
& (1 << RESET
)) ?
false : true);
300 /** Resumes USB communications with an attached and enumerated device, by resuming the transmission
301 * of the 1MS Start Of Frame messages to the device. When resumed, USB communications between the
302 * host and attached device may occur.
304 static inline void USB_Host_ResumeBus(void) ATTR_ALWAYS_INLINE
;
305 static inline void USB_Host_ResumeBus(void)
307 UHCON
|= (1 << SOFEN
);
310 /** Suspends the USB bus, preventing any communications from occurring between the host and attached
311 * device until the bus has been resumed. This stops the transmission of the 1MS Start Of Frame
312 * messages to the device.
314 static inline void USB_Host_SuspendBus(void) ATTR_ALWAYS_INLINE
;
315 static inline void USB_Host_SuspendBus(void)
317 UHCON
&= ~(1 << SOFEN
);
320 /** Determines if the USB bus has been suspended via the use of the \ref USB_Host_SuspendBus() macro,
321 * false otherwise. While suspended, no USB communications can occur until the bus is resumed,
322 * except for the Remote Wakeup event from the device if supported.
324 * \return Boolean \c true if the bus is currently suspended, \c false otherwise.
326 static inline bool USB_Host_IsBusSuspended(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
327 static inline bool USB_Host_IsBusSuspended(void)
329 return ((UHCON
& (1 << SOFEN
)) ?
false : true);
332 /** Determines if the attached device is currently enumerated in Full Speed mode (12Mb/s), or
333 * false if the attached device is enumerated in Low Speed mode (1.5Mb/s).
335 * \return Boolean \c true if the attached device is enumerated in Full Speed mode, \c false otherwise.
337 static inline bool USB_Host_IsDeviceFullSpeed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
338 static inline bool USB_Host_IsDeviceFullSpeed(void)
340 return ((USBSTA
& (1 << SPEED
)) ?
true : false);
343 /** Determines if the attached device is currently issuing a Remote Wakeup request, requesting
344 * that the host resume the USB bus and wake up the device, false otherwise.
346 * \return Boolean \c true if the attached device has sent a Remote Wakeup request, \c false otherwise.
348 static inline bool USB_Host_IsRemoteWakeupSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
349 static inline bool USB_Host_IsRemoteWakeupSent(void)
351 return ((UHINT
& (1 << RXRSMI
)) ?
true : false);
354 /** Clears the flag indicating that a Remote Wakeup request has been issued by an attached device. */
355 static inline void USB_Host_ClearRemoteWakeupSent(void) ATTR_ALWAYS_INLINE
;
356 static inline void USB_Host_ClearRemoteWakeupSent(void)
358 UHINT
&= ~(1 << RXRSMI
);
361 /** Accepts a Remote Wakeup request from an attached device. This must be issued in response to
362 * a device's Remote Wakeup request within 2ms for the request to be accepted and the bus to
365 static inline void USB_Host_ResumeFromWakeupRequest(void) ATTR_ALWAYS_INLINE
;
366 static inline void USB_Host_ResumeFromWakeupRequest(void)
368 UHCON
|= (1 << RESUME
);
371 /** Determines if a resume from Remote Wakeup request is currently being sent to an attached
374 * \return Boolean \c true if no resume request is currently being sent, \c false otherwise.
376 static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
377 static inline bool USB_Host_IsResumeFromWakeupRequestSent(void)
379 return ((UHCON
& (1 << RESUME
)) ?
false : true);
382 /* Function Prototypes: */
383 /** Convenience function. This routine sends a SET CONFIGURATION standard request to the attached
384 * device, with the given configuration index. This can be used to easily set the device
385 * configuration without creating and sending the request manually.
387 * \note After this routine returns, the control pipe will be selected.
389 * \param[in] ConfigNumber Configuration index to send to the device.
391 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
393 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber
);
395 /** Convenience function. This routine sends a GET DESCRIPTOR standard request to the attached
396 * device, requesting the device descriptor. This can be used to easily retrieve information
397 * about the device such as its VID, PID and power requirements.
399 * \note After this routine returns, the control pipe will be selected.
401 * \param[out] DeviceDescriptorPtr Pointer to the destination device descriptor structure where
402 * the read data is to be stored.
404 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
406 uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr
);
408 /** Convenience function. This routine sends a GET DESCRIPTOR standard request to the attached
409 * device, requesting the string descriptor of the specified index. This can be used to easily
410 * retrieve string descriptors from the device by index, after the index is obtained from the
411 * Device or Configuration descriptors.
413 * \note After this routine returns, the control pipe will be selected.
415 * \param[in] Index Index of the string index to retrieve.
416 * \param[out] Buffer Pointer to the destination buffer where the retrieved string descriptor is
418 * \param[in] BufferLength Maximum size of the string descriptor which can be stored into the buffer.
420 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
422 uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index
,
424 const uint8_t BufferLength
);
426 /** Clears a stall condition on the given pipe, via a CLEAR FEATURE standard request to the attached device.
428 * \note After this routine returns, the control pipe will be selected.
430 * \param[in] EndpointIndex Index of the endpoint to clear, including the endpoint's direction.
432 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
434 uint8_t USB_Host_ClearPipeStall(const uint8_t EndpointIndex
);
436 /* Private Interface - For use in library only: */
437 #if !defined(__DOXYGEN__)
439 static inline void USB_Host_HostMode_On(void) ATTR_ALWAYS_INLINE
;
440 static inline void USB_Host_HostMode_On(void)
442 USBCON
|= (1 << HOST
);
445 static inline void USB_Host_HostMode_Off(void) ATTR_ALWAYS_INLINE
;
446 static inline void USB_Host_HostMode_Off(void)
448 USBCON
&= ~(1 << HOST
);
451 static inline void USB_Host_VBUS_Auto_Enable(void) ATTR_ALWAYS_INLINE
;
452 static inline void USB_Host_VBUS_Auto_Enable(void)
454 OTGCON
&= ~(1 << VBUSHWC
);
455 UHWCON
|= (1 << UVCONE
);
458 static inline void USB_Host_VBUS_Manual_Enable(void) ATTR_ALWAYS_INLINE
;
459 static inline void USB_Host_VBUS_Manual_Enable(void)
461 OTGCON
|= (1 << VBUSHWC
);
462 UHWCON
&= ~(1 << UVCONE
);
467 static inline void USB_Host_VBUS_Auto_On(void) ATTR_ALWAYS_INLINE
;
468 static inline void USB_Host_VBUS_Auto_On(void)
470 OTGCON
|= (1 << VBUSREQ
);
473 static inline void USB_Host_VBUS_Manual_On(void) ATTR_ALWAYS_INLINE
;
474 static inline void USB_Host_VBUS_Manual_On(void)
479 static inline void USB_Host_VBUS_Auto_Off(void) ATTR_ALWAYS_INLINE
;
480 static inline void USB_Host_VBUS_Auto_Off(void)
482 OTGCON
|= (1 << VBUSRQC
);
485 static inline void USB_Host_VBUS_Manual_Off(void) ATTR_ALWAYS_INLINE
;
486 static inline void USB_Host_VBUS_Manual_Off(void)
491 static inline void USB_Host_SetDeviceAddress(const uint8_t Address
) ATTR_ALWAYS_INLINE
;
492 static inline void USB_Host_SetDeviceAddress(const uint8_t Address
)
494 UHADDR
= (Address
& 0x7F);
498 enum USB_Host_WaitMSErrorCodes_t
500 HOST_WAITERROR_Successful
= 0,
501 HOST_WAITERROR_DeviceDisconnect
= 1,
502 HOST_WAITERROR_PipeError
= 2,
503 HOST_WAITERROR_SetupStalled
= 3,
506 /* Function Prototypes: */
507 void USB_Host_ProcessNextHostState(void);
508 uint8_t USB_Host_WaitMS(uint8_t MS
);
510 #if defined(__INCLUDE_FROM_HOST_C)
511 static void USB_Host_ResetDevice(void);
515 /* Disable C linkage for C++ Compilers: */
516 #if defined(__cplusplus)