Minor documentation improvements.
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Host.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 * \c 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 * \ref 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 * \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.
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 * \ref HOST_STATE_Suspended or \ref 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 \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.
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 /** 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.
250 */
251 static inline uint16_t USB_Host_GetFrameNumber(void)
252 {
253 return UHFNUM;
254 }
255
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.
260 *
261 * \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
262 */
263 static inline void USB_Host_EnableSOFEvents(void) ATTR_ALWAYS_INLINE;
264 static inline void USB_Host_EnableSOFEvents(void)
265 {
266 USB_INT_Enable(USB_INT_HSOFI);
267 }
268
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.
271 *
272 * \note Not available when the NO_SOF_EVENTS compile time token is defined.
273 */
274 static inline void USB_Host_DisableSOFEvents(void) ATTR_ALWAYS_INLINE;
275 static inline void USB_Host_DisableSOFEvents(void)
276 {
277 USB_INT_Disable(USB_INT_HSOFI);
278 }
279 #endif
280
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).
283 *
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.
286 */
287 static inline void USB_Host_ResetBus(void) ATTR_ALWAYS_INLINE;
288 static inline void USB_Host_ResetBus(void)
289 {
290 UHCON |= (1 << RESET);
291 }
292
293 /** Determines if a previously issued bus reset (via the \ref USB_Host_ResetBus() macro) has
294 * completed.
295 *
296 * \return Boolean \c true if no bus reset is currently being sent, \c false otherwise.
297 */
298 static inline bool USB_Host_IsBusResetComplete(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
299 static inline bool USB_Host_IsBusResetComplete(void)
300 {
301 return ((UHCON & (1 << RESET)) ? false : true);
302 }
303
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.
307 */
308 static inline void USB_Host_ResumeBus(void) ATTR_ALWAYS_INLINE;
309 static inline void USB_Host_ResumeBus(void)
310 {
311 UHCON |= (1 << SOFEN);
312 }
313
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.
317 */
318 static inline void USB_Host_SuspendBus(void) ATTR_ALWAYS_INLINE;
319 static inline void USB_Host_SuspendBus(void)
320 {
321 UHCON &= ~(1 << SOFEN);
322 }
323
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.
327 *
328 * \return Boolean \c true if the bus is currently suspended, \c false otherwise.
329 */
330 static inline bool USB_Host_IsBusSuspended(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
331 static inline bool USB_Host_IsBusSuspended(void)
332 {
333 return ((UHCON & (1 << SOFEN)) ? false : true);
334 }
335
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).
338 *
339 * \return Boolean \c true if the attached device is enumerated in Full Speed mode, \c false otherwise.
340 */
341 static inline bool USB_Host_IsDeviceFullSpeed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
342 static inline bool USB_Host_IsDeviceFullSpeed(void)
343 {
344 return ((USBSTA & (1 << SPEED)) ? true : false);
345 }
346
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.
349 *
350 * \return Boolean \c true if the attached device has sent a Remote Wakeup request, \c false otherwise.
351 */
352 static inline bool USB_Host_IsRemoteWakeupSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
353 static inline bool USB_Host_IsRemoteWakeupSent(void)
354 {
355 return ((UHINT & (1 << RXRSMI)) ? true : false);
356 }
357
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)
361 {
362 UHINT &= ~(1 << RXRSMI);
363 }
364
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
367 * be resumed.
368 */
369 static inline void USB_Host_ResumeFromWakeupRequest(void) ATTR_ALWAYS_INLINE;
370 static inline void USB_Host_ResumeFromWakeupRequest(void)
371 {
372 UHCON |= (1 << RESUME);
373 }
374
375 /** Determines if a resume from Remote Wakeup request is currently being sent to an attached
376 * device.
377 *
378 * \return Boolean \c true if no resume request is currently being sent, \c false otherwise.
379 */
380 static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
381 static inline bool USB_Host_IsResumeFromWakeupRequestSent(void)
382 {
383 return ((UHCON & (1 << RESUME)) ? false : true);
384 }
385
386 /* Function Prototypes: */
387 /** Convenience function. This routine sends a SET CONFIGURATION 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.
390 *
391 * \note After this routine returns, the control pipe will be selected.
392 *
393 * \param[in] ConfigNumber Configuration index to send to the device.
394 *
395 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
396 */
397 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber);
398
399 /** Convenience function. This routine sends a GET DESCRIPTOR 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.
402 *
403 * \note After this routine returns, the control pipe will be selected.
404 *
405 * \param[out] DeviceDescriptorPtr Pointer to the destination device descriptor structure where
406 * the read data is to be stored.
407 *
408 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
409 */
410 uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr);
411
412 /** Convenience function. This routine sends a GET DESCRIPTOR 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.
416 *
417 * \note After this routine returns, the control pipe will be selected.
418 *
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
421 * to be stored.
422 * \param[in] BufferLength Maximum size of the string descriptor which can be stored into the buffer.
423 *
424 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
425 */
426 uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index,
427 void* const Buffer,
428 const uint8_t BufferLength);
429
430 /** Clears a stall condition on the given pipe, via a CLEAR FEATURE standard request to the attached device.
431 *
432 * \note After this routine returns, the control pipe will be selected.
433 *
434 * \param[in] EndpointIndex Index of the endpoint to clear, including the endpoint's direction.
435 *
436 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
437 */
438 uint8_t USB_Host_ClearPipeStall(const uint8_t EndpointIndex);
439
440 /* Private Interface - For use in library only: */
441 #if !defined(__DOXYGEN__)
442 /* Macros: */
443 static inline void USB_Host_HostMode_On(void) ATTR_ALWAYS_INLINE;
444 static inline void USB_Host_HostMode_On(void)
445 {
446 USBCON |= (1 << HOST);
447 }
448
449 static inline void USB_Host_HostMode_Off(void) ATTR_ALWAYS_INLINE;
450 static inline void USB_Host_HostMode_Off(void)
451 {
452 USBCON &= ~(1 << HOST);
453 }
454
455 static inline void USB_Host_VBUS_Auto_Enable(void) ATTR_ALWAYS_INLINE;
456 static inline void USB_Host_VBUS_Auto_Enable(void)
457 {
458 OTGCON &= ~(1 << VBUSHWC);
459 UHWCON |= (1 << UVCONE);
460 }
461
462 static inline void USB_Host_VBUS_Manual_Enable(void) ATTR_ALWAYS_INLINE;
463 static inline void USB_Host_VBUS_Manual_Enable(void)
464 {
465 OTGCON |= (1 << VBUSHWC);
466 UHWCON &= ~(1 << UVCONE);
467
468 DDRE |= (1 << 7);
469 }
470
471 static inline void USB_Host_VBUS_Auto_On(void) ATTR_ALWAYS_INLINE;
472 static inline void USB_Host_VBUS_Auto_On(void)
473 {
474 OTGCON |= (1 << VBUSREQ);
475 }
476
477 static inline void USB_Host_VBUS_Manual_On(void) ATTR_ALWAYS_INLINE;
478 static inline void USB_Host_VBUS_Manual_On(void)
479 {
480 PORTE |= (1 << 7);
481 }
482
483 static inline void USB_Host_VBUS_Auto_Off(void) ATTR_ALWAYS_INLINE;
484 static inline void USB_Host_VBUS_Auto_Off(void)
485 {
486 OTGCON |= (1 << VBUSRQC);
487 }
488
489 static inline void USB_Host_VBUS_Manual_Off(void) ATTR_ALWAYS_INLINE;
490 static inline void USB_Host_VBUS_Manual_Off(void)
491 {
492 PORTE &= ~(1 << 7);
493 }
494
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)
497 {
498 UHADDR = (Address & 0x7F);
499 }
500
501 /* Enums: */
502 enum USB_Host_WaitMSErrorCodes_t
503 {
504 HOST_WAITERROR_Successful = 0,
505 HOST_WAITERROR_DeviceDisconnect = 1,
506 HOST_WAITERROR_PipeError = 2,
507 HOST_WAITERROR_SetupStalled = 3,
508 };
509
510 /* Function Prototypes: */
511 void USB_Host_ProcessNextHostState(void);
512 uint8_t USB_Host_WaitMS(uint8_t MS);
513
514 #if defined(__INCLUDE_FROM_HOST_C)
515 static void USB_Host_ResetDevice(void);
516 #endif
517 #endif
518
519 /* Disable C linkage for C++ Compilers: */
520 #if defined(__cplusplus)
521 }
522 #endif
523
524 #endif
525
526 /** @} */
527