Conditionally add available address spaces to the address space enum, for multiple...
[pub/USBasp.git] / LUFA / Drivers / USB / Core / AVR8 / Host_AVR8.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 definitions for the AVR8 microcontrollers.
33 * \copydetails Group_Host_AVR8
34 *
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.
37 */
38
39 /** \ingroup Group_Host
40 * \defgroup Group_Host_AVR8 Host Management (AVR8)
41 * \brief USB Host definitions for the AVR8 microcontrollers.
42 *
43 * Architecture specific USB Host definitions for the Atmel 8-bit AVR microcontrollers.
44 *
45 * @{
46 */
47
48 #ifndef __USBHOST_AVR8_H__
49 #define __USBHOST_AVR8_H__
50
51 /* Includes: */
52 #include "../../../../Common/Common.h"
53 #include "../StdDescriptors.h"
54 #include "../Pipe.h"
55 #include "../USBInterrupt.h"
56
57 /* Enable C linkage for C++ Compilers: */
58 #if defined(__cplusplus)
59 extern "C" {
60 #endif
61
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.
65 #endif
66
67 /* Public Interface - May be used in end-application: */
68 /* Macros: */
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.
73 */
74 #define USB_HOST_DEVICEADDRESS 1
75
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.
80 *
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.
83 */
84 #define USB_HOST_TIMEOUT_MS 1000
85 #endif
86
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
91 * occur.
92 *
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.
96 */
97 #define HOST_DEVICE_SETTLE_DELAY_MS 1000
98 #endif
99
100 /** Enum for the error codes for the \ref EVENT_USB_Host_HostError() event.
101 *
102 * \see \ref Group_Events for more information on this event.
103 */
104 enum USB_Host_ErrorCodes_t
105 {
106 HOST_ERROR_VBusVoltageDip = 0, /**< VBUS voltage dipped to an unacceptable level. This
107 * error may be the result of an attached device drawing
108 * too much current from the VBUS line, or due to the
109 * AVR's power source being unable to supply sufficient
110 * current.
111 */
112 };
113
114 /** Enum for the error codes for the \ref EVENT_USB_Host_DeviceEnumerationFailed() event.
115 *
116 * \see \ref Group_Events for more information on this event.
117 */
118 enum USB_Host_EnumerationErrorCodes_t
119 {
120 HOST_ENUMERROR_NoError = 0, /**< No error occurred. Used internally, this is not a valid
121 * ErrorCode parameter value for the \ref EVENT_USB_Host_DeviceEnumerationFailed()
122 * event.
123 */
124 HOST_ENUMERROR_WaitStage = 1, /**< One of the delays between enumeration steps failed
125 * to complete successfully, due to a timeout or other
126 * error.
127 */
128 HOST_ENUMERROR_NoDeviceDetected = 2, /**< No device was detected, despite the USB data lines
129 * indicating the attachment of a device.
130 */
131 HOST_ENUMERROR_ControlError = 3, /**< One of the enumeration control requests failed to
132 * complete successfully.
133 */
134 HOST_ENUMERROR_PipeConfigError = 4, /**< The default control pipe (address 0) failed to
135 * configure correctly.
136 */
137 };
138
139 /* Inline Functions: */
140 /** Returns the current USB frame number, when in host mode. Every millisecond the USB bus is active (i.e. not suspended)
141 * the frame number is incremented by one.
142 */
143 static inline uint16_t USB_Host_GetFrameNumber(void)
144 {
145 return UHFNUM;
146 }
147
148 #if !defined(NO_SOF_EVENTS)
149 /** Enables the host mode Start Of Frame events. When enabled, this causes the
150 * \ref EVENT_USB_Host_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus,
151 * at the start of each USB frame when a device is enumerated while in host mode.
152 *
153 * \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
154 */
155 static inline void USB_Host_EnableSOFEvents(void) ATTR_ALWAYS_INLINE;
156 static inline void USB_Host_EnableSOFEvents(void)
157 {
158 USB_INT_Enable(USB_INT_HSOFI);
159 }
160
161 /** Disables the host mode Start Of Frame events. When disabled, this stops the firing of the
162 * \ref EVENT_USB_Host_StartOfFrame() event when enumerated in host mode.
163 *
164 * \note Not available when the NO_SOF_EVENTS compile time token is defined.
165 */
166 static inline void USB_Host_DisableSOFEvents(void) ATTR_ALWAYS_INLINE;
167 static inline void USB_Host_DisableSOFEvents(void)
168 {
169 USB_INT_Disable(USB_INT_HSOFI);
170 }
171 #endif
172
173 /** Resets the USB bus, including the endpoints in any attached device and pipes on the AVR host.
174 * USB bus resets leave the default control pipe configured (if already configured).
175 *
176 * If the USB bus has been suspended prior to issuing a bus reset, the attached device will be
177 * woken up automatically and the bus resumed after the reset has been correctly issued.
178 */
179 static inline void USB_Host_ResetBus(void) ATTR_ALWAYS_INLINE;
180 static inline void USB_Host_ResetBus(void)
181 {
182 UHCON |= (1 << RESET);
183 }
184
185 /** Determines if a previously issued bus reset (via the \ref USB_Host_ResetBus() macro) has
186 * completed.
187 *
188 * \return Boolean \c true if no bus reset is currently being sent, \c false otherwise.
189 */
190 static inline bool USB_Host_IsBusResetComplete(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
191 static inline bool USB_Host_IsBusResetComplete(void)
192 {
193 return ((UHCON & (1 << RESET)) ? false : true);
194 }
195
196 /** Resumes USB communications with an attached and enumerated device, by resuming the transmission
197 * of the 1MS Start Of Frame messages to the device. When resumed, USB communications between the
198 * host and attached device may occur.
199 */
200 static inline void USB_Host_ResumeBus(void) ATTR_ALWAYS_INLINE;
201 static inline void USB_Host_ResumeBus(void)
202 {
203 UHCON |= (1 << SOFEN);
204 }
205
206 /** Suspends the USB bus, preventing any communications from occurring between the host and attached
207 * device until the bus has been resumed. This stops the transmission of the 1MS Start Of Frame
208 * messages to the device.
209 */
210 static inline void USB_Host_SuspendBus(void) ATTR_ALWAYS_INLINE;
211 static inline void USB_Host_SuspendBus(void)
212 {
213 UHCON &= ~(1 << SOFEN);
214 }
215
216 /** Determines if the USB bus has been suspended via the use of the \ref USB_Host_SuspendBus() macro,
217 * false otherwise. While suspended, no USB communications can occur until the bus is resumed,
218 * except for the Remote Wakeup event from the device if supported.
219 *
220 * \return Boolean \c true if the bus is currently suspended, \c false otherwise.
221 */
222 static inline bool USB_Host_IsBusSuspended(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
223 static inline bool USB_Host_IsBusSuspended(void)
224 {
225 return ((UHCON & (1 << SOFEN)) ? false : true);
226 }
227
228 /** Determines if the attached device is currently enumerated in Full Speed mode (12Mb/s), or
229 * false if the attached device is enumerated in Low Speed mode (1.5Mb/s).
230 *
231 * \return Boolean \c true if the attached device is enumerated in Full Speed mode, \c false otherwise.
232 */
233 static inline bool USB_Host_IsDeviceFullSpeed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
234 static inline bool USB_Host_IsDeviceFullSpeed(void)
235 {
236 return ((USBSTA & (1 << SPEED)) ? true : false);
237 }
238
239 /** Determines if the attached device is currently issuing a Remote Wakeup request, requesting
240 * that the host resume the USB bus and wake up the device, false otherwise.
241 *
242 * \return Boolean \c true if the attached device has sent a Remote Wakeup request, \c false otherwise.
243 */
244 static inline bool USB_Host_IsRemoteWakeupSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
245 static inline bool USB_Host_IsRemoteWakeupSent(void)
246 {
247 return ((UHINT & (1 << RXRSMI)) ? true : false);
248 }
249
250 /** Clears the flag indicating that a Remote Wakeup request has been issued by an attached device. */
251 static inline void USB_Host_ClearRemoteWakeupSent(void) ATTR_ALWAYS_INLINE;
252 static inline void USB_Host_ClearRemoteWakeupSent(void)
253 {
254 UHINT &= ~(1 << RXRSMI);
255 }
256
257 /** Accepts a Remote Wakeup request from an attached device. This must be issued in response to
258 * a device's Remote Wakeup request within 2ms for the request to be accepted and the bus to
259 * be resumed.
260 */
261 static inline void USB_Host_ResumeFromWakeupRequest(void) ATTR_ALWAYS_INLINE;
262 static inline void USB_Host_ResumeFromWakeupRequest(void)
263 {
264 UHCON |= (1 << RESUME);
265 }
266
267 /** Determines if a resume from Remote Wakeup request is currently being sent to an attached
268 * device.
269 *
270 * \return Boolean \c true if no resume request is currently being sent, \c false otherwise.
271 */
272 static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
273 static inline bool USB_Host_IsResumeFromWakeupRequestSent(void)
274 {
275 return ((UHCON & (1 << RESUME)) ? false : true);
276 }
277
278 /* Function Prototypes: */
279 /** Convenience function. This routine sends a SET CONFIGURATION standard request to the attached
280 * device, with the given configuration index. This can be used to easily set the device
281 * configuration without creating and sending the request manually.
282 *
283 * \note After this routine returns, the control pipe will be selected.
284 *
285 * \param[in] ConfigNumber Configuration index to send to the device.
286 *
287 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
288 */
289 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber);
290
291 /** Convenience function. This routine sends a GET DESCRIPTOR standard request to the attached
292 * device, requesting the device descriptor. This can be used to easily retrieve information
293 * about the device such as its VID, PID and power requirements.
294 *
295 * \note After this routine returns, the control pipe will be selected.
296 *
297 * \param[out] DeviceDescriptorPtr Pointer to the destination device descriptor structure where
298 * the read data is to be stored.
299 *
300 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
301 */
302 uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr);
303
304 /** Convenience function. This routine sends a GET DESCRIPTOR standard request to the attached
305 * device, requesting the string descriptor of the specified index. This can be used to easily
306 * retrieve string descriptors from the device by index, after the index is obtained from the
307 * Device or Configuration descriptors.
308 *
309 * \note After this routine returns, the control pipe will be selected.
310 *
311 * \param[in] Index Index of the string index to retrieve.
312 * \param[out] Buffer Pointer to the destination buffer where the retrieved string descriptor is
313 * to be stored.
314 * \param[in] BufferLength Maximum size of the string descriptor which can be stored into the buffer.
315 *
316 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
317 */
318 uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index,
319 void* const Buffer,
320 const uint8_t BufferLength);
321
322 /** Clears a stall condition on the given pipe, via a CLEAR FEATURE standard request to the attached device.
323 *
324 * \note After this routine returns, the control pipe will be selected.
325 *
326 * \param[in] EndpointIndex Index of the endpoint to clear, including the endpoint's direction.
327 *
328 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
329 */
330 uint8_t USB_Host_ClearPipeStall(const uint8_t EndpointIndex);
331
332 /* Private Interface - For use in library only: */
333 #if !defined(__DOXYGEN__)
334 /* Macros: */
335 static inline void USB_Host_HostMode_On(void) ATTR_ALWAYS_INLINE;
336 static inline void USB_Host_HostMode_On(void)
337 {
338 USBCON |= (1 << HOST);
339 }
340
341 static inline void USB_Host_HostMode_Off(void) ATTR_ALWAYS_INLINE;
342 static inline void USB_Host_HostMode_Off(void)
343 {
344 USBCON &= ~(1 << HOST);
345 }
346
347 static inline void USB_Host_VBUS_Auto_Enable(void) ATTR_ALWAYS_INLINE;
348 static inline void USB_Host_VBUS_Auto_Enable(void)
349 {
350 OTGCON &= ~(1 << VBUSHWC);
351 UHWCON |= (1 << UVCONE);
352 }
353
354 static inline void USB_Host_VBUS_Manual_Enable(void) ATTR_ALWAYS_INLINE;
355 static inline void USB_Host_VBUS_Manual_Enable(void)
356 {
357 OTGCON |= (1 << VBUSHWC);
358 UHWCON &= ~(1 << UVCONE);
359
360 DDRE |= (1 << 7);
361 }
362
363 static inline void USB_Host_VBUS_Auto_On(void) ATTR_ALWAYS_INLINE;
364 static inline void USB_Host_VBUS_Auto_On(void)
365 {
366 OTGCON |= (1 << VBUSREQ);
367 }
368
369 static inline void USB_Host_VBUS_Manual_On(void) ATTR_ALWAYS_INLINE;
370 static inline void USB_Host_VBUS_Manual_On(void)
371 {
372 PORTE |= (1 << 7);
373 }
374
375 static inline void USB_Host_VBUS_Auto_Off(void) ATTR_ALWAYS_INLINE;
376 static inline void USB_Host_VBUS_Auto_Off(void)
377 {
378 OTGCON |= (1 << VBUSRQC);
379 }
380
381 static inline void USB_Host_VBUS_Manual_Off(void) ATTR_ALWAYS_INLINE;
382 static inline void USB_Host_VBUS_Manual_Off(void)
383 {
384 PORTE &= ~(1 << 7);
385 }
386
387 static inline void USB_Host_SetDeviceAddress(const uint8_t Address) ATTR_ALWAYS_INLINE;
388 static inline void USB_Host_SetDeviceAddress(const uint8_t Address)
389 {
390 UHADDR = (Address & 0x7F);
391 }
392
393 /* Enums: */
394 enum USB_Host_WaitMSErrorCodes_t
395 {
396 HOST_WAITERROR_Successful = 0,
397 HOST_WAITERROR_DeviceDisconnect = 1,
398 HOST_WAITERROR_PipeError = 2,
399 HOST_WAITERROR_SetupStalled = 3,
400 };
401
402 /* Function Prototypes: */
403 void USB_Host_ProcessNextHostState(void);
404 uint8_t USB_Host_WaitMS(uint8_t MS);
405
406 #if defined(__INCLUDE_FROM_HOST_C)
407 static void USB_Host_ResetDevice(void);
408 #endif
409 #endif
410
411 /* Disable C linkage for C++ Compilers: */
412 #if defined(__cplusplus)
413 }
414 #endif
415
416 #endif
417
418 /** @} */
419