Reduced HOST_DEVICE_SETTLE_DELAY_MS to 1000ms down from 1500ms to improve device...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Host.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 * \brief USB host mode definitions.
33 *
34 * This file contains structures, function prototypes and macros related to USB host mode.
35 *
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.
38 */
39
40 /** \ingroup Group_USB
41 * @defgroup Group_Host Host Management
42 *
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.
45 *
46 * @{
47 */
48
49 #ifndef __USBHOST_H__
50 #define __USBHOST_H__
51
52 /* Includes: */
53 #include <avr/io.h>
54 #include <stdbool.h>
55 #include <util/delay.h>
56
57 #include "../../../Common/Common.h"
58 #include "../HighLevel/StdDescriptors.h"
59 #include "Pipe.h"
60 #include "USBInterrupt.h"
61
62 /* Enable C linkage for C++ Compilers: */
63 #if defined(__cplusplus)
64 extern "C" {
65 #endif
66
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.
70 #endif
71
72 /* Public Interface - May be used in end-application: */
73 /* Macros: */
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.
78 */
79 #define USB_HOST_DEVICEADDRESS 1
80
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.
85 *
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.
88 */
89 #define USB_HOST_TIMEOUT_MS 1000
90 #endif
91
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
96 * occur.
97 *
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.
101 */
102 #define HOST_DEVICE_SETTLE_DELAY_MS 1000
103 #endif
104
105 /* Enums: */
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.
108 *
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.
112 *
113 * \see \ref USB_HostState, which stores the current host state machine state.
114 */
115 enum USB_Host_States_t
116 {
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.
123 */
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
127 * process.
128 *
129 * \note Do not manually change to this state in the user code.
130 */
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.
134 *
135 * \note Do not manually change to this state in the user code.
136 */
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.
140 *
141 * \note Do not manually change to this state in the user code.
142 */
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.
146 *
147 * \note Do not manually change to this state in the user code.
148 */
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.
153 *
154 * \note Do not manually change to this state in the user code.
155 */
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.
159 *
160 * \note Do not manually change to this state in the user code.
161 */
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.
165 *
166 * \note Do not manually change to this state in the user code.
167 */
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
171 * to match.
172 *
173 * \note Do not manually change to this state in the user code.
174 */
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.
179 *
180 * \note Do not manually change to this state in the user code.
181 */
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 * HOST_STATE_Addressed state for further communications.
188 *
189 * \note Do not manually change to this state in the user code.
190 */
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 * 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.
196 */
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 * HOST_STATE_Suspended or HOST_STATE_WaitForDeviceRemoval states as needed.
200 */
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 HOST_STATE_Configured
203 * (after resuming the bus with the USB_Host_ResumeBus() macro) or the
204 * HOST_STATE_WaitForDeviceRemoval states as needed.
205 */
206 };
207
208 /** Enum for the error codes for the \ref EVENT_USB_Host_HostError() event.
209 *
210 * \see \ref Group_Events for more information on this event.
211 */
212 enum USB_Host_ErrorCodes_t
213 {
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
218 * current.
219 */
220 };
221
222 /** Enum for the error codes for the \ref EVENT_USB_Host_DeviceEnumerationFailed() event.
223 *
224 * \see \ref Group_Events for more information on this event.
225 */
226 enum USB_Host_EnumerationErrorCodes_t
227 {
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()
230 * event.
231 */
232 HOST_ENUMERROR_WaitStage = 1, /**< One of the delays between enumeration steps failed
233 * to complete successfully, due to a timeout or other
234 * error.
235 */
236 HOST_ENUMERROR_NoDeviceDetected = 2, /**< No device was detected, despite the USB data lines
237 * indicating the attachment of a device.
238 */
239 HOST_ENUMERROR_ControlError = 3, /**< One of the enumeration control requests failed to
240 * complete successfully.
241 */
242 HOST_ENUMERROR_PipeConfigError = 4, /**< The default control pipe (address 0) failed to
243 * configure correctly.
244 */
245 };
246
247 /* Inline Functions: */
248 /** Resets the USB bus, including the endpoints in any attached device and pipes on the AVR host.
249 * USB bus resets leave the default control pipe configured (if already configured).
250 *
251 * If the USB bus has been suspended prior to issuing a bus reset, the attached device will be
252 * woken up automatically and the bus resumed after the reset has been correctly issued.
253 */
254 static inline void USB_Host_ResetBus(void) ATTR_ALWAYS_INLINE;
255 static inline void USB_Host_ResetBus(void)
256 {
257 UHCON |= (1 << RESET);
258 }
259
260 /** Determines if a previously issued bus reset (via the \ref USB_Host_ResetBus() macro) has
261 * completed.
262 *
263 * \return Boolean true if no bus reset is currently being sent, false otherwise.
264 */
265 static inline bool USB_Host_IsBusResetComplete(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
266 static inline bool USB_Host_IsBusResetComplete(void)
267 {
268 return ((UHCON & (1 << RESET)) ? false : true);
269 }
270
271 /** Resumes USB communications with an attached and enumerated device, by resuming the transmission
272 * of the 1MS Start Of Frame messages to the device. When resumed, USB communications between the
273 * host and attached device may occur.
274 */
275 static inline void USB_Host_ResumeBus(void) ATTR_ALWAYS_INLINE;
276 static inline void USB_Host_ResumeBus(void)
277 {
278 UHCON |= (1 << SOFEN);
279 }
280
281 /** Suspends the USB bus, preventing any communications from occurring between the host and attached
282 * device until the bus has been resumed. This stops the transmission of the 1MS Start Of Frame
283 * messages to the device.
284 */
285 static inline void USB_Host_SuspendBus(void) ATTR_ALWAYS_INLINE;
286 static inline void USB_Host_SuspendBus(void)
287 {
288 UHCON &= ~(1 << SOFEN);
289 }
290
291 /** Determines if the USB bus has been suspended via the use of the \ref USB_Host_SuspendBus() macro,
292 * false otherwise. While suspended, no USB communications can occur until the bus is resumed,
293 * except for the Remote Wakeup event from the device if supported.
294 *
295 * \return Boolean true if the bus is currently suspended, false otherwise.
296 */
297 static inline bool USB_Host_IsBusSuspended(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
298 static inline bool USB_Host_IsBusSuspended(void)
299 {
300 return ((UHCON & (1 << SOFEN)) ? false : true);
301 }
302
303 /** Determines if the attached device is currently enumerated in Full Speed mode (12Mb/s), or
304 * false if the attached device is enumerated in Low Speed mode (1.5Mb/s).
305 *
306 * \return Boolean true if the attached device is enumerated in Full Speed mode, false otherwise.
307 */
308 static inline bool USB_Host_IsDeviceFullSpeed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
309 static inline bool USB_Host_IsDeviceFullSpeed(void)
310 {
311 return ((USBSTA & (1 << SPEED)) ? true : false);
312 }
313
314 /** Determines if the attached device is currently issuing a Remote Wakeup request, requesting
315 * that the host resume the USB bus and wake up the device, false otherwise.
316 *
317 * \return Boolean true if the attached device has sent a Remote Wakeup request, false otherwise.
318 */
319 static inline bool USB_Host_IsRemoteWakeupSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
320 static inline bool USB_Host_IsRemoteWakeupSent(void)
321 {
322 return ((UHINT & (1 << RXRSMI)) ? true : false);
323 }
324
325 /** Clears the flag indicating that a Remote Wakeup request has been issued by an attached device. */
326 static inline void USB_Host_ClearRemoteWakeupSent(void) ATTR_ALWAYS_INLINE;
327 static inline void USB_Host_ClearRemoteWakeupSent(void)
328 {
329 UHINT &= ~(1 << RXRSMI);
330 }
331
332 /** Accepts a Remote Wakeup request from an attached device. This must be issued in response to
333 * a device's Remote Wakeup request within 2ms for the request to be accepted and the bus to
334 * be resumed.
335 */
336 static inline void USB_Host_ResumeFromWakeupRequest(void) ATTR_ALWAYS_INLINE;
337 static inline void USB_Host_ResumeFromWakeupRequest(void)
338 {
339 UHCON |= (1 << RESUME);
340 }
341
342 /** Determines if a resume from Remote Wakeup request is currently being sent to an attached
343 * device.
344 *
345 * \return Boolean true if no resume request is currently being sent, false otherwise.
346 */
347 static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
348 static inline bool USB_Host_IsResumeFromWakeupRequestSent(void)
349 {
350 return ((UHCON & (1 << RESUME)) ? false : true);
351 }
352
353 /* Function Prototypes: */
354 /** Convenience function. This routine sends a SetConfiguration standard request to the attached
355 * device, with the given configuration index. This can be used to easily set the device
356 * configuration without creating and sending the request manually.
357 *
358 * \note After this routine returns, the control pipe will be selected.
359 *
360 * \param[in] ConfigNumber Configuration index to send to the device.
361 *
362 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
363 */
364 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber);
365
366 /** Convenience function. This routine sends a GetDescriptor standard request to the attached
367 * device, requesting the device descriptor. This can be used to easily retrieve information
368 * about the device such as its VID, PID and power requirements.
369 *
370 * \note After this routine returns, the control pipe will be selected.
371 *
372 * \param[out] DeviceDescriptorPtr Pointer to the destination device descriptor structure where
373 * the read data is to be stored.
374 *
375 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
376 */
377 uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr);
378
379 /** Convenience function. This routine sends a GetDescriptor standard request to the attached
380 * device, requesting the string descriptor of the specified index. This can be used to easily
381 * retrieve string descriptors from the device by index, after the index is obtained from the
382 * Device or Configuration descriptors.
383 *
384 * \note After this routine returns, the control pipe will be selected.
385 *
386 * \param[in] Index Index of the string index to retrieve.
387 * \param[out] Buffer Pointer to the destination buffer where the retrieved string descriptor is
388 * to be stored.
389 * \param[in] BufferLength Maximum size of the string descriptor which can be stored into the buffer.
390 *
391 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
392 */
393 uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index,
394 void* const Buffer,
395 const uint8_t BufferLength);
396
397 /** Clears a stall condition on the given pipe, via a ClearFeature request to the attached device.
398 *
399 * \note After this routine returns, the control pipe will be selected.
400 *
401 * \param[in] EndpointIndex Index of the endpoint to clear.
402 *
403 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
404 */
405 uint8_t USB_Host_ClearPipeStall(uint8_t EndpointIndex);
406
407 /* Private Interface - For use in library only: */
408 #if !defined(__DOXYGEN__)
409 /* Macros: */
410 static inline void USB_Host_HostMode_On(void) ATTR_ALWAYS_INLINE;
411 static inline void USB_Host_HostMode_On(void)
412 {
413 USBCON |= (1 << HOST);
414 }
415
416 static inline void USB_Host_HostMode_Off(void) ATTR_ALWAYS_INLINE;
417 static inline void USB_Host_HostMode_Off(void)
418 {
419 USBCON &= ~(1 << HOST);
420 }
421
422 static inline void USB_Host_VBUS_Auto_Enable(void) ATTR_ALWAYS_INLINE;
423 static inline void USB_Host_VBUS_Auto_Enable(void)
424 {
425 OTGCON &= ~(1 << VBUSHWC);
426 UHWCON |= (1 << UVCONE);
427 }
428
429 static inline void USB_Host_VBUS_Manual_Enable(void) ATTR_ALWAYS_INLINE;
430 static inline void USB_Host_VBUS_Manual_Enable(void)
431 {
432 OTGCON |= (1 << VBUSHWC);
433 UHWCON &= ~(1 << UVCONE);
434
435 DDRE |= (1 << 7);
436 }
437
438 static inline void USB_Host_VBUS_Auto_On(void) ATTR_ALWAYS_INLINE;
439 static inline void USB_Host_VBUS_Auto_On(void)
440 {
441 OTGCON |= (1 << VBUSREQ);
442 }
443
444 static inline void USB_Host_VBUS_Manual_On(void) ATTR_ALWAYS_INLINE;
445 static inline void USB_Host_VBUS_Manual_On(void)
446 {
447 PORTE |= (1 << 7);
448 }
449
450 static inline void USB_Host_VBUS_Auto_Off(void) ATTR_ALWAYS_INLINE;
451 static inline void USB_Host_VBUS_Auto_Off(void)
452 {
453 OTGCON |= (1 << VBUSRQC);
454 }
455
456 static inline void USB_Host_VBUS_Manual_Off(void) ATTR_ALWAYS_INLINE;
457 static inline void USB_Host_VBUS_Manual_Off(void)
458 {
459 PORTE &= ~(1 << 7);
460 }
461
462 static inline void USB_Host_SetDeviceAddress(const uint8_t Address) ATTR_ALWAYS_INLINE;
463 static inline void USB_Host_SetDeviceAddress(const uint8_t Address)
464 {
465 UHADDR = (Address & 0x7F);
466 }
467
468 /* Enums: */
469 enum USB_Host_WaitMSErrorCodes_t
470 {
471 HOST_WAITERROR_Successful = 0,
472 HOST_WAITERROR_DeviceDisconnect = 1,
473 HOST_WAITERROR_PipeError = 2,
474 HOST_WAITERROR_SetupStalled = 3,
475 };
476
477 /* Function Prototypes: */
478 void USB_Host_ProcessNextHostState(void);
479 uint8_t USB_Host_WaitMS(uint8_t MS);
480
481 #if defined(__INCLUDE_FROM_HOST_C)
482 static void USB_Host_ResetDevice(void);
483 #endif
484 #endif
485
486 /* Disable C linkage for C++ Compilers: */
487 #if defined(__cplusplus)
488 }
489 #endif
490
491 #endif
492
493 /** @} */