]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/AVR8/Host_AVR8.h
Squashed 'tmk_core/' content from commit 05caacc
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / AVR8 / Host_AVR8.h
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 /** \file\r
32  *  \brief USB Host definitions for the AVR8 microcontrollers.\r
33  *  \copydetails Group_Host_AVR8\r
34  *\r
35  *  \note This file should not be included directly. It is automatically included as needed by the USB driver\r
36  *        dispatch header located in LUFA/Drivers/USB/USB.h.\r
37  */\r
38 \r
39 /** \ingroup Group_Host\r
40  *  \defgroup Group_Host_AVR8 Host Management (AVR8)\r
41  *  \brief USB Host definitions for the AVR8 microcontrollers.\r
42  *\r
43  *  Architecture specific USB Host definitions for the Atmel 8-bit AVR microcontrollers.\r
44  *\r
45  *  @{\r
46  */\r
47 \r
48 #ifndef __USBHOST_AVR8_H__\r
49 #define __USBHOST_AVR8_H__\r
50 \r
51         /* Includes: */\r
52                 #include "../../../../Common/Common.h"\r
53                 #include "../StdDescriptors.h"\r
54                 #include "../Pipe.h"\r
55                 #include "../USBInterrupt.h"\r
56 \r
57         /* Enable C linkage for C++ Compilers: */\r
58                 #if defined(__cplusplus)\r
59                         extern "C" {\r
60                 #endif\r
61 \r
62         /* Preprocessor Checks: */\r
63                 #if !defined(__INCLUDE_FROM_USB_DRIVER)\r
64                         #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.\r
65                 #endif\r
66 \r
67                 #if defined(INVERTED_VBUS_ENABLE_LINE) && !defined(NO_AUTO_VBUS_MANAGEMENT)\r
68                         #error The INVERTED_VBUS_ENABLE_LINE compile option requires NO_AUTO_VBUS_MANAGEMENT for the AVR8 architecture.\r
69                 #endif\r
70 \r
71         /* Public Interface - May be used in end-application: */\r
72                 /* Macros: */\r
73                         /** Indicates the fixed USB device address which any attached device is enumerated to when in\r
74                          *  host mode. As only one USB device may be attached to the AVR in host mode at any one time\r
75                          *  and that the address used is not important (other than the fact that it is non-zero), a\r
76                          *  fixed value is specified by the library.\r
77                          */\r
78                         #define USB_HOST_DEVICEADDRESS                 1\r
79 \r
80                         #if !defined(HOST_DEVICE_SETTLE_DELAY_MS) || defined(__DOXYGEN__)\r
81                                 /** Constant for the delay in milliseconds after a device is connected before the library\r
82                                  *  will start the enumeration process. Some devices require a delay of up to 5 seconds\r
83                                  *  after connection before the enumeration process can start or incorrect operation will\r
84                                  *  occur.\r
85                                  *\r
86                                  *  The default delay value may be overridden in the user project makefile by defining the\r
87                                  *  \c HOST_DEVICE_SETTLE_DELAY_MS token to the required delay in milliseconds, and passed to the\r
88                                  *  compiler using the -D switch.\r
89                                  */\r
90                                 #define HOST_DEVICE_SETTLE_DELAY_MS        1000\r
91                         #endif\r
92 \r
93                         /** Enum for the error codes for the \ref EVENT_USB_Host_HostError() event.\r
94                          *\r
95                          *  \see \ref Group_Events for more information on this event.\r
96                          */\r
97                         enum USB_Host_ErrorCodes_t\r
98                         {\r
99                                 HOST_ERROR_VBusVoltageDip       = 0, /**< VBUS voltage dipped to an unacceptable level. This\r
100                                                                       *   error may be the result of an attached device drawing\r
101                                                                       *   too much current from the VBUS line, or due to the\r
102                                                                       *   AVR's power source being unable to supply sufficient\r
103                                                                       *   current.\r
104                                                                       */\r
105                         };\r
106 \r
107                         /** Enum for the error codes for the \ref EVENT_USB_Host_DeviceEnumerationFailed() event.\r
108                          *\r
109                          *  \see \ref Group_Events for more information on this event.\r
110                          */\r
111                         enum USB_Host_EnumerationErrorCodes_t\r
112                         {\r
113                                 HOST_ENUMERROR_NoError          = 0, /**< No error occurred. Used internally, this is not a valid\r
114                                                                       *   ErrorCode parameter value for the \ref EVENT_USB_Host_DeviceEnumerationFailed()\r
115                                                                       *   event.\r
116                                                                       */\r
117                                 HOST_ENUMERROR_WaitStage        = 1, /**< One of the delays between enumeration steps failed\r
118                                                                       *   to complete successfully, due to a timeout or other\r
119                                                                       *   error.\r
120                                                                       */\r
121                                 HOST_ENUMERROR_NoDeviceDetected = 2, /**< No device was detected, despite the USB data lines\r
122                                                                       *   indicating the attachment of a device.\r
123                                                                       */\r
124                                 HOST_ENUMERROR_ControlError     = 3, /**< One of the enumeration control requests failed to\r
125                                                                       *   complete successfully.\r
126                                                                       */\r
127                                 HOST_ENUMERROR_PipeConfigError  = 4, /**< The default control pipe (address 0) failed to\r
128                                                                       *   configure correctly.\r
129                                                                       */\r
130                         };\r
131 \r
132                 /* Inline Functions: */\r
133                         /** Returns the current USB frame number, when in host mode. Every millisecond the USB bus is active (i.e. not suspended)\r
134                          *  the frame number is incremented by one.\r
135                          *\r
136                          *  \return Current USB frame number from the USB controller.\r
137                          */\r
138                         static inline uint16_t USB_Host_GetFrameNumber(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
139                         static inline uint16_t USB_Host_GetFrameNumber(void)\r
140                         {\r
141                                 return UHFNUM;\r
142                         }\r
143 \r
144                         #if !defined(NO_SOF_EVENTS)\r
145                                 /** Enables the host mode Start Of Frame events. When enabled, this causes the\r
146                                  *  \ref EVENT_USB_Host_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus,\r
147                                  *  at the start of each USB frame when a device is enumerated while in host mode.\r
148                                  *\r
149                                  *  \note This function is not available when the \c NO_SOF_EVENTS compile time token is defined.\r
150                                  */\r
151                                 static inline void USB_Host_EnableSOFEvents(void) ATTR_ALWAYS_INLINE;\r
152                                 static inline void USB_Host_EnableSOFEvents(void)\r
153                                 {\r
154                                         USB_INT_Enable(USB_INT_HSOFI);\r
155                                 }\r
156 \r
157                                 /** Disables the host mode Start Of Frame events. When disabled, this stops the firing of the\r
158                                  *  \ref EVENT_USB_Host_StartOfFrame() event when enumerated in host mode.\r
159                                  *\r
160                                  *  \note This function is not available when the \c NO_SOF_EVENTS compile time token is defined.\r
161                                  */\r
162                                 static inline void USB_Host_DisableSOFEvents(void) ATTR_ALWAYS_INLINE;\r
163                                 static inline void USB_Host_DisableSOFEvents(void)\r
164                                 {\r
165                                         USB_INT_Disable(USB_INT_HSOFI);\r
166                                 }\r
167                         #endif\r
168 \r
169                         /** Resets the USB bus, including the endpoints in any attached device and pipes on the AVR host.\r
170                          *  USB bus resets leave the default control pipe configured (if already configured).\r
171                          *\r
172                          *  If the USB bus has been suspended prior to issuing a bus reset, the attached device will be\r
173                          *  woken up automatically and the bus resumed after the reset has been correctly issued.\r
174                          */\r
175                         static inline void USB_Host_ResetBus(void) ATTR_ALWAYS_INLINE;\r
176                         static inline void USB_Host_ResetBus(void)\r
177                         {\r
178                                 UHCON |=  (1 << RESET);\r
179                         }\r
180 \r
181                         /** Determines if a previously issued bus reset (via the \ref USB_Host_ResetBus() macro) has\r
182                          *  completed.\r
183                          *\r
184                          *  \return Boolean \c true if no bus reset is currently being sent, \c false otherwise.\r
185                          */\r
186                         static inline bool USB_Host_IsBusResetComplete(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
187                         static inline bool USB_Host_IsBusResetComplete(void)\r
188                         {\r
189                                 return ((UHCON & (1 << RESET)) ? false : true);\r
190                         }\r
191 \r
192                         /** Resumes USB communications with an attached and enumerated device, by resuming the transmission\r
193                          *  of the 1MS Start Of Frame messages to the device. When resumed, USB communications between the\r
194                          *  host and attached device may occur.\r
195                          */\r
196                         static inline void USB_Host_ResumeBus(void) ATTR_ALWAYS_INLINE;\r
197                         static inline void USB_Host_ResumeBus(void)\r
198                         {\r
199                                 UHCON |=  (1 << SOFEN);\r
200                         }\r
201 \r
202                         /** Suspends the USB bus, preventing any communications from occurring between the host and attached\r
203                          *  device until the bus has been resumed. This stops the transmission of the 1MS Start Of Frame\r
204                          *  messages to the device.\r
205                          *\r
206                          *  \attention While the USB bus is suspended, all USB interrupt sources are also disabled; this means that\r
207                          *             some events (such as device disconnections) will not fire until the bus is resumed.\r
208                          */\r
209                         static inline void USB_Host_SuspendBus(void) ATTR_ALWAYS_INLINE;\r
210                         static inline void USB_Host_SuspendBus(void)\r
211                         {\r
212                                 UHCON &= ~(1 << SOFEN);\r
213                         }\r
214 \r
215                         /** Determines if the USB bus has been suspended via the use of the \ref USB_Host_SuspendBus() macro,\r
216                          *  false otherwise. While suspended, no USB communications can occur until the bus is resumed,\r
217                          *  except for the Remote Wakeup event from the device if supported.\r
218                          *\r
219                          *  \return Boolean \c true if the bus is currently suspended, \c false otherwise.\r
220                          */\r
221                         static inline bool USB_Host_IsBusSuspended(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
222                         static inline bool USB_Host_IsBusSuspended(void)\r
223                         {\r
224                                 return ((UHCON & (1 << SOFEN)) ? false : true);\r
225                         }\r
226 \r
227                         /** Determines if the attached device is currently enumerated in Full Speed mode (12Mb/s), or\r
228                          *  false if the attached device is enumerated in Low Speed mode (1.5Mb/s).\r
229                          *\r
230                          *  \return Boolean \c true if the attached device is enumerated in Full Speed mode, \c false otherwise.\r
231                          */\r
232                         static inline bool USB_Host_IsDeviceFullSpeed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
233                         static inline bool USB_Host_IsDeviceFullSpeed(void)\r
234                         {\r
235                                 return ((USBSTA & (1 << SPEED)) ? true : false);\r
236                         }\r
237 \r
238                         /** Determines if the attached device is currently issuing a Remote Wakeup request, requesting\r
239                          *  that the host resume the USB bus and wake up the device, false otherwise.\r
240                          *\r
241                          *  \return Boolean \c true if the attached device has sent a Remote Wakeup request, \c false otherwise.\r
242                          */\r
243                         static inline bool USB_Host_IsRemoteWakeupSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
244                         static inline bool USB_Host_IsRemoteWakeupSent(void)\r
245                         {\r
246                                 return ((UHINT & (1 << RXRSMI)) ? true : false);\r
247                         }\r
248 \r
249                         /** Clears the flag indicating that a Remote Wakeup request has been issued by an attached device. */\r
250                         static inline void USB_Host_ClearRemoteWakeupSent(void) ATTR_ALWAYS_INLINE;\r
251                         static inline void USB_Host_ClearRemoteWakeupSent(void)\r
252                         {\r
253                                 UHINT &= ~(1 << RXRSMI);\r
254                         }\r
255 \r
256                         /** Accepts a Remote Wakeup request from an attached device. This must be issued in response to\r
257                          *  a device's Remote Wakeup request within 2ms for the request to be accepted and the bus to\r
258                          *  be resumed.\r
259                          */\r
260                         static inline void USB_Host_ResumeFromWakeupRequest(void) ATTR_ALWAYS_INLINE;\r
261                         static inline void USB_Host_ResumeFromWakeupRequest(void)\r
262                         {\r
263                                 UHCON |=  (1 << RESUME);\r
264                         }\r
265 \r
266                         /** Determines if a resume from Remote Wakeup request is currently being sent to an attached\r
267                          *  device.\r
268                          *\r
269                          *  \return Boolean \c true if no resume request is currently being sent, \c false otherwise.\r
270                          */\r
271                         static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;\r
272                         static inline bool USB_Host_IsResumeFromWakeupRequestSent(void)\r
273                         {\r
274                                 return ((UHCON & (1 << RESUME)) ? false : true);\r
275                         }\r
276 \r
277         /* Private Interface - For use in library only: */\r
278         #if !defined(__DOXYGEN__)\r
279                 /* Macros: */\r
280                         static inline void USB_Host_HostMode_On(void) ATTR_ALWAYS_INLINE;\r
281                         static inline void USB_Host_HostMode_On(void)\r
282                         {\r
283                                 USBCON |=  (1 << HOST);\r
284                         }\r
285 \r
286                         static inline void USB_Host_HostMode_Off(void) ATTR_ALWAYS_INLINE;\r
287                         static inline void USB_Host_HostMode_Off(void)\r
288                         {\r
289                                 USBCON &= ~(1 << HOST);\r
290                         }\r
291 \r
292                         static inline void USB_Host_VBUS_Auto_Enable(void) ATTR_ALWAYS_INLINE;\r
293                         static inline void USB_Host_VBUS_Auto_Enable(void)\r
294                         {\r
295                                 OTGCON &= ~(1 << VBUSHWC);\r
296                                 UHWCON |=  (1 << UVCONE);\r
297                         }\r
298 \r
299                         static inline void USB_Host_VBUS_Manual_Enable(void) ATTR_ALWAYS_INLINE;\r
300                         static inline void USB_Host_VBUS_Manual_Enable(void)\r
301                         {\r
302                                 OTGCON |=  (1 << VBUSHWC);\r
303                                 UHWCON &= ~(1 << UVCONE);\r
304 \r
305                                 DDRE   |=  (1 << 7);\r
306                         }\r
307 \r
308                         static inline void USB_Host_VBUS_Auto_On(void) ATTR_ALWAYS_INLINE;\r
309                         static inline void USB_Host_VBUS_Auto_On(void)\r
310                         {\r
311                                 OTGCON |=  (1 << VBUSREQ);\r
312                         }\r
313 \r
314                         static inline void USB_Host_VBUS_Manual_On(void) ATTR_ALWAYS_INLINE;\r
315                         static inline void USB_Host_VBUS_Manual_On(void)\r
316                         {\r
317                                 #if defined(INVERTED_VBUS_ENABLE_LINE)\r
318                                 PORTE  &= ~(1 << 7);\r
319                                 #else\r
320                                 PORTE  |=  (1 << 7);\r
321                                 #endif\r
322                         }\r
323 \r
324                         static inline void USB_Host_VBUS_Auto_Off(void) ATTR_ALWAYS_INLINE;\r
325                         static inline void USB_Host_VBUS_Auto_Off(void)\r
326                         {\r
327                                 OTGCON |=  (1 << VBUSRQC);\r
328                         }\r
329 \r
330                         static inline void USB_Host_VBUS_Manual_Off(void) ATTR_ALWAYS_INLINE;\r
331                         static inline void USB_Host_VBUS_Manual_Off(void)\r
332                         {\r
333                                 #if defined(INVERTED_VBUS_ENABLE_LINE)\r
334                                 PORTE  |=  (1 << 7);\r
335                                 #else\r
336                                 PORTE  &= ~(1 << 7);\r
337                                 #endif\r
338                         }\r
339 \r
340                         static inline void USB_Host_SetDeviceAddress(const uint8_t Address) ATTR_ALWAYS_INLINE;\r
341                         static inline void USB_Host_SetDeviceAddress(const uint8_t Address)\r
342                         {\r
343                                 UHADDR  =  (Address & 0x7F);\r
344                         }\r
345 \r
346                 /* Enums: */\r
347                         enum USB_Host_WaitMSErrorCodes_t\r
348                         {\r
349                                 HOST_WAITERROR_Successful       = 0,\r
350                                 HOST_WAITERROR_DeviceDisconnect = 1,\r
351                                 HOST_WAITERROR_PipeError        = 2,\r
352                                 HOST_WAITERROR_SetupStalled     = 3,\r
353                         };\r
354 \r
355                 /* Function Prototypes: */\r
356                         void    USB_Host_ProcessNextHostState(void);\r
357                         uint8_t USB_Host_WaitMS(uint8_t MS);\r
358 \r
359                         #if defined(__INCLUDE_FROM_HOST_C)\r
360                                 static void USB_Host_ResetDevice(void);\r
361                         #endif\r
362         #endif\r
363 \r
364         /* Disable C linkage for C++ Compilers: */\r
365                 #if defined(__cplusplus)\r
366                         }\r
367                 #endif\r
368 \r
369 #endif\r
370 \r
371 /** @} */\r
372 \r