]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/USB/Core/Events.h
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / USB / Core / Events.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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 disclaims 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 Event management definitions.
33 * \copydetails Group_Events
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_USB
40 * \defgroup Group_Events USB Events
41 * \brief USB Event management definitions.
42 *
43 * This module contains macros and functions relating to the management of library events, which are small
44 * pieces of code similar to ISRs which are run when a given condition is met. Each event can be fired from
45 * multiple places in the user or library code, which may or may not be inside an ISR, thus each handler
46 * should be written to be as small and fast as possible to prevent possible problems.
47 *
48 * Events can be hooked by the user application by declaring a handler function with the same name and parameters
49 * listed here. If an event with no user-associated handler is fired within the library, it by default maps to an
50 * internal empty stub function.
51 *
52 * Each event must only have one associated event handler, but can be raised by multiple sources by calling the
53 * event handler function (with any required event parameters).
54 *
55 * @{
56 */
57
58 #ifndef __USBEVENTS_H__
59 #define __USBEVENTS_H__
60
61 /* Includes: */
62 #include "../../../Common/Common.h"
63 #include "USBMode.h"
64
65 /* Enable C linkage for C++ Compilers: */
66 #if defined(__cplusplus)
67 extern "C" {
68 #endif
69
70 /* Preprocessor Checks: */
71 #if !defined(__INCLUDE_FROM_USB_DRIVER)
72 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
73 #endif
74
75 /* Public Interface - May be used in end-application: */
76 /* Pseudo-Functions for Doxygen: */
77 #if !defined(__INCLUDE_FROM_EVENTS_C) || defined(__DOXYGEN__)
78 /** Event for USB mode pin level change. This event fires when the USB interface is set to dual role
79 * mode, and the UID pin level has changed to indicate a new mode (device or host). This event fires
80 * before the mode is switched to the newly indicated mode but after the \ref EVENT_USB_Device_Disconnect
81 * event has fired (if disconnected before the role change).
82 *
83 * \note This event only exists on microcontrollers that support dual role USB modes.
84 * \n\n
85 *
86 * \note This event does not exist if the \c USB_DEVICE_ONLY or \c USB_HOST_ONLY tokens have been supplied
87 * to the compiler (see \ref Group_USBManagement documentation).
88 */
89 void EVENT_USB_UIDChange(void);
90
91 /** Event for USB host error. This event fires when a hardware fault has occurred whilst the USB
92 * interface is in host mode.
93 *
94 * \param[in] ErrorCode Error code indicating the failure reason, a value in \ref USB_Host_ErrorCodes_t.
95 *
96 * \note This event only exists on microcontrollers that supports USB host mode.
97 * \n\n
98 *
99 * \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see
100 * \ref Group_USBManagement documentation).
101 */
102 void EVENT_USB_Host_HostError(const uint8_t ErrorCode);
103
104 /** Event for USB device attachment. This event fires when a the USB interface is in host mode, and
105 * a USB device has been connected to the USB interface. This is interrupt driven, thus fires before
106 * the standard \ref EVENT_USB_Device_Connect() event and so can be used to programmatically start the USB
107 * management task to reduce CPU consumption.
108 *
109 * \note This event only exists on microcontrollers that supports USB host mode.
110 * \n\n
111 *
112 * \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see
113 * \ref Group_USBManagement documentation).
114 *
115 * \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage.
116 */
117 void EVENT_USB_Host_DeviceAttached(void);
118
119 /** Event for USB device removal. This event fires when a the USB interface is in host mode, and
120 * a USB device has been removed the USB interface whether or not it has been enumerated. This
121 * can be used to programmatically stop the USB management task to reduce CPU consumption.
122 *
123 * \note This event only exists on microcontrollers that supports USB host mode.
124 * \n\n
125 *
126 * \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see
127 * \ref Group_USBManagement documentation).
128 *
129 * \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage.
130 */
131 void EVENT_USB_Host_DeviceUnattached(void);
132
133 /** Event for USB device enumeration failure. This event fires when a the USB interface is
134 * in host mode, and an attached USB device has failed to enumerate completely.
135 *
136 * \param[in] ErrorCode Error code indicating the failure reason, a value in
137 * \ref USB_Host_EnumerationErrorCodes_t.
138 *
139 * \param[in] SubErrorCode Sub error code indicating the reason for failure - for example, if the
140 * ErrorCode parameter indicates a control error, this will give the error
141 * code returned by the \ref USB_Host_SendControlRequest() function.
142 *
143 * \note This event only exists on microcontrollers that supports USB host mode.
144 * \n\n
145 *
146 * \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see
147 * \ref Group_USBManagement documentation).
148 */
149 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
150 const uint8_t SubErrorCode);
151
152 /** Event for USB device enumeration completion. This event fires when a the USB interface is
153 * in host mode and an attached USB device has been completely enumerated and is ready to be
154 * controlled by the user application.
155 *
156 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
157 * 1 second) when a transaction is waiting to be processed by the device will prevent break communications
158 * and cause the host to reset the USB bus.
159 *
160 * \note This event only exists on microcontrollers that supports USB host mode.
161 * \n\n
162 *
163 * \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see
164 * \ref Group_USBManagement documentation).
165 */
166 void EVENT_USB_Host_DeviceEnumerationComplete(void);
167
168 /** Event for USB Start Of Frame detection, when enabled. This event fires at the start of each USB
169 * frame, once per millisecond, and is synchronized to the USB bus. This can be used as an accurate
170 * millisecond timer source when the USB bus is not suspended while in host mode.
171 *
172 * This event is time-critical; it is run once per millisecond and thus long handlers will significantly
173 * degrade device performance. This event should only be enabled when needed to reduce device wake-ups.
174 *
175 * \note This event is not normally active - it must be manually enabled and disabled via the
176 * \ref USB_Host_EnableSOFEvents() and \ref USB_Host_DisableSOFEvents() commands after enumeration of
177 * a USB device.
178 * \n\n
179 *
180 * \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see
181 * \ref Group_USBManagement documentation).
182 */
183 void EVENT_USB_Host_StartOfFrame(void);
184
185 /** Event for USB device connection. This event fires when the microcontroller is in USB Device mode
186 * and the device is connected to a USB host, beginning the enumeration process measured by a rising
187 * level on the microcontroller's VBUS sense pin.
188 *
189 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
190 * two seconds) will prevent the device from enumerating correctly.
191 *
192 * \attention This event may fire multiple times during device enumeration on the microcontrollers with limited USB controllers
193 * if \c NO_LIMITED_CONTROLLER_CONNECT is not defined.
194 *
195 * \note For the microcontrollers with limited USB controller functionality, VBUS sensing is not available.
196 * this means that the current connection state is derived from the bus suspension and wake up events by default,
197 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
198 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behavior turned off by
199 * passing the \c NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
200 * and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually.
201 * \n\n
202 *
203 * \see \ref Group_USBManagement for more information on the USB management task and reducing CPU usage.
204 */
205 void EVENT_USB_Device_Connect(void);
206
207 /** Event for USB device disconnection. This event fires when the microcontroller is in USB Device mode and the device is
208 * disconnected from a host, measured by a falling level on the microcontroller's VBUS sense pin.
209 *
210 * \attention This event may fire multiple times during device enumeration on the microcontrollers with limited USB controllers
211 * if \c NO_LIMITED_CONTROLLER_CONNECT is not defined.
212 *
213 * \note For the microcontrollers with limited USB controllers, VBUS sense is not available to the USB controller.
214 * this means that the current connection state is derived from the bus suspension and wake up events by default,
215 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
216 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behavior turned off by
217 * passing the \c NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
218 * and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually.
219 * \n\n
220 *
221 * \see \ref Group_USBManagement for more information on the USB management task and reducing CPU usage.
222 */
223 void EVENT_USB_Device_Disconnect(void);
224
225 /** Event for control requests. This event fires when a the USB host issues a control request
226 * to the mandatory device control endpoint (of address 0). This may either be a standard
227 * request that the library may have a handler code for internally, or a class specific request
228 * issued to the device which must be handled appropriately. If a request is not processed in the
229 * user application via this event, it will be passed to the library for processing internally
230 * if a suitable handler exists.
231 *
232 * This event is time-critical; each packet within the request transaction must be acknowledged or
233 * sent within 50ms or the host will abort the transfer.
234 *
235 * The library internally handles all standard control requests with the exceptions of SYNC FRAME,
236 * SET DESCRIPTOR and SET INTERFACE. These and all other non-standard control requests will be left
237 * for the user to process via this event if desired. If not handled in the user application or by
238 * the library internally, unknown requests are automatically STALLed.
239 *
240 * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
241 * \ref Group_USBManagement documentation).
242 * \n\n
243 *
244 * \note Requests should be handled in the same manner as described in the USB 2.0 Specification,
245 * or appropriate class specification. In all instances, the library has already read the
246 * request SETUP parameters into the \ref USB_ControlRequest structure which should then be used
247 * by the application to determine how to handle the issued request.
248 */
249 void EVENT_USB_Device_ControlRequest(void);
250
251 /** Event for USB configuration number changed. This event fires when a the USB host changes the
252 * selected configuration number while in device mode. This event should be hooked in device
253 * applications to create the endpoints and configure the device for the selected configuration.
254 *
255 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
256 * one second) will prevent the device from enumerating correctly.
257 *
258 * This event fires after the value of \ref USB_Device_ConfigurationNumber has been changed.
259 *
260 * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
261 * \ref Group_USBManagement documentation).
262 */
263 void EVENT_USB_Device_ConfigurationChanged(void);
264
265 /** Event for USB suspend. This event fires when a the USB host suspends the device by halting its
266 * transmission of Start Of Frame pulses to the device. This is generally hooked in order to move
267 * the device over to a low power state until the host wakes up the device. If the USB interface is
268 * enumerated with the \ref USB_OPT_AUTO_PLL option set, the library will automatically suspend the
269 * USB PLL before the event is fired to save power.
270 *
271 * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
272 * \ref Group_USBManagement documentation).
273 * \n\n
274 *
275 * \note This event does not exist on the microcontrollers with limited USB VBUS sensing abilities
276 * when the \c NO_LIMITED_CONTROLLER_CONNECT compile time token is not set - see
277 * \ref EVENT_USB_Device_Disconnect.
278 *
279 * \see \ref EVENT_USB_Device_WakeUp() event for accompanying Wake Up event.
280 */
281 void EVENT_USB_Device_Suspend(void);
282
283 /** Event for USB wake up. This event fires when a the USB interface is suspended while in device
284 * mode, and the host wakes up the device by supplying Start Of Frame pulses. This is generally
285 * hooked to pull the user application out of a low power state and back into normal operating
286 * mode. If the USB interface is enumerated with the \ref USB_OPT_AUTO_PLL option set, the library
287 * will automatically restart the USB PLL before the event is fired.
288 *
289 * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
290 * \ref Group_USBManagement documentation).
291 * \n\n
292 *
293 * \note This event does not exist on the microcontrollers with limited USB VBUS sensing abilities
294 * when the \c NO_LIMITED_CONTROLLER_CONNECT compile time token is not set - see
295 * \ref EVENT_USB_Device_Disconnect.
296 *
297 * \see \ref EVENT_USB_Device_Suspend() event for accompanying Suspend event.
298 */
299 void EVENT_USB_Device_WakeUp(void);
300
301 /** Event for USB interface reset. This event fires when the USB interface is in device mode, and
302 * a the USB host requests that the device reset its interface. This event fires after the control
303 * endpoint has been automatically configured by the library.
304 *
305 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
306 * two seconds) will prevent the device from enumerating correctly.
307 *
308 * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
309 * \ref Group_USBManagement documentation).
310 */
311 void EVENT_USB_Device_Reset(void);
312
313 /** Event for USB Start Of Frame detection, when enabled. This event fires at the start of each USB
314 * frame, once per millisecond, and is synchronized to the USB bus. This can be used as an accurate
315 * millisecond timer source when the USB bus is enumerated in device mode to a USB host.
316 *
317 * This event is time-critical; it is run once per millisecond and thus long handlers will significantly
318 * degrade device performance. This event should only be enabled when needed to reduce device wake-ups.
319 *
320 * \pre This event is not normally active - it must be manually enabled and disabled via the
321 * \ref USB_Device_EnableSOFEvents() and \ref USB_Device_DisableSOFEvents() commands after enumeration.
322 * \n\n
323 *
324 * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
325 * \ref Group_USBManagement documentation).
326 */
327 void EVENT_USB_Device_StartOfFrame(void);
328 #endif
329
330 /* Private Interface - For use in library only: */
331 #if !defined(__DOXYGEN__)
332 /* Function Prototypes: */
333 #if defined(__INCLUDE_FROM_EVENTS_C)
334 void USB_Event_Stub(void) ATTR_CONST;
335
336 #if defined(USB_CAN_BE_BOTH)
337 void EVENT_USB_UIDChange(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
338 #endif
339
340 #if defined(USB_CAN_BE_HOST)
341 void EVENT_USB_Host_HostError(const uint8_t ErrorCode) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
342 void EVENT_USB_Host_DeviceAttached(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
343 void EVENT_USB_Host_DeviceUnattached(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
344 void EVENT_USB_Host_DeviceEnumerationComplete(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
345 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
346 const uint8_t SubErrorCode)
347 ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
348 void EVENT_USB_Host_StartOfFrame(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
349 #endif
350
351 #if defined(USB_CAN_BE_DEVICE)
352 void EVENT_USB_Device_Connect(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
353 void EVENT_USB_Device_Disconnect(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
354 void EVENT_USB_Device_ControlRequest(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
355 void EVENT_USB_Device_ConfigurationChanged(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
356 void EVENT_USB_Device_Suspend(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
357 void EVENT_USB_Device_WakeUp(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
358 void EVENT_USB_Device_Reset(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
359 void EVENT_USB_Device_StartOfFrame(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
360 #endif
361 #endif
362 #endif
363
364 /* Disable C linkage for C++ Compilers: */
365 #if defined(__cplusplus)
366 }
367 #endif
368
369 #endif
370
371 /** @} */
372
Imprint / Impressum