3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
32 * \brief USB controller events manager.
34 * This file contains macros and functions relating to the management of library events, which are small
35 * pieces of code similar to ISRs which are run when a given condition is met. Each event can be fired from
36 * multiple places in the user or library code, which may or may not be inside an ISR, thus each handler
37 * should be written to be as small and fast as possible to prevent possible problems.
39 * Events can be hooked by the user application by declaring a handler function with the same name and parameters
40 * listed here. If an event with no user-associated handler is fired within the library, it by default maps to an
41 * internal empty stub function.
43 * Each event must only have one associated event handler, but can be raised by multiple sources by calling the
44 * event handler function (with any required event parameters).
46 * \note This file should not be included directly. It is automatically included as needed by the USB driver
47 * dispatch header located in LUFA/Drivers/USB/USB.h.
50 /** \ingroup Group_USB
51 * @defgroup Group_Events USB Events
53 * This module contains macros and functions relating to the management of library events, which are small
54 * pieces of code similar to ISRs which are run when a given condition is met. Each event can be fired from
55 * multiple places in the user or library code, which may or may not be inside an ISR, thus each handler
56 * should be written to be as small and fast as possible to prevent possible problems.
58 * Events can be hooked by the user application by declaring a handler function with the same name and parameters
59 * listed here. If an event with no user-associated handler is fired within the library, it by default maps to an
60 * internal empty stub function.
62 * Each event must only have one associated event handler, but can be raised by multiple sources by calling the
63 * event handler function (with any required event parameters).
68 #ifndef __USBEVENTS_H__
69 #define __USBEVENTS_H__
74 #include "../../../Common/Common.h"
77 /* Enable C linkage for C++ Compilers: */
78 #if defined(__cplusplus)
82 /* Preprocessor Checks: */
83 #if !defined(__INCLUDE_FROM_USB_DRIVER)
84 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
87 /* Public Interface - May be used in end-application: */
88 /* Pseudo-Functions for Doxygen: */
89 #if !defined(__INCLUDE_FROM_EVENTS_C) || defined(__DOXYGEN__)
90 /** Event for USB mode pin level change. This event fires when the USB interface is set to dual role
91 * mode, and the UID pin level has changed to indicate a new mode (device or host). This event fires
92 * before the mode is switched to the newly indicated mode but after the \ref EVENT_USB_Device_Disconnect
93 * event has fired (if connected before the role change).
95 * \note This event only exists on USB AVR models which support dual role modes.
98 * \note This event does not exist if the USB_DEVICE_ONLY or USB_HOST_ONLY tokens have been supplied
99 * to the compiler (see \ref Group_USBManagement documentation).
101 void EVENT_USB_UIDChange(void);
103 /** Event for USB host error. This event fires when a hardware fault has occurred whilst the USB
104 * interface is in host mode.
106 * \param[in] ErrorCode Error code indicating the failure reason, a value in \ref USB_Host_ErrorCodes_t.
108 * \note This event only exists on USB AVR models which supports host mode.
111 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
112 * \ref Group_USBManagement documentation).
114 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
);
116 /** Event for USB device attachment. This event fires when a the USB interface is in host mode, and
117 * a USB device has been connected to the USB interface. This is interrupt driven, thus fires before
118 * the standard \ref EVENT_USB_Device_Connect() event and so can be used to programmatically start the USB
119 * management task to reduce CPU consumption.
121 * \note This event only exists on USB AVR models which supports host mode.
124 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
125 * \ref Group_USBManagement documentation).
127 * \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage.
129 void EVENT_USB_Host_DeviceAttached(void);
131 /** Event for USB device removal. This event fires when a the USB interface is in host mode, and
132 * a USB device has been removed the USB interface whether or not it has been enumerated. This
133 * can be used to programmatically stop the USB management task to reduce CPU consumption.
135 * \note This event only exists on USB AVR models which supports host mode.
138 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
139 * \ref Group_USBManagement documentation).
141 * \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage.
143 void EVENT_USB_Host_DeviceUnattached(void);
145 /** Event for USB device enumeration failure. This event fires when a the USB interface is
146 * in host mode, and an attached USB device has failed to enumerate completely.
148 * \param[in] ErrorCode Error code indicating the failure reason, a value in
149 * \ref USB_Host_EnumerationErrorCodes_t.
151 * \param[in] SubErrorCode Sub error code indicating the reason for failure - for example, if the
152 * ErrorCode parameter indicates a control error, this will give the error
153 * code returned by the \ref USB_Host_SendControlRequest() function.
155 * \note This event only exists on USB AVR models which supports host mode.
158 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
159 * \ref Group_USBManagement documentation).
161 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
,
162 const uint8_t SubErrorCode
);
164 /** Event for USB device enumeration completion. This event fires when a the USB interface is
165 * in host mode and an attached USB device has been completely enumerated and is ready to be
166 * controlled by the user application.
168 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
169 * 1 second) when a transaction is waiting to be processed by the device will prevent break communications
170 * and cause the host to reset the USB bus.
172 void EVENT_USB_Host_DeviceEnumerationComplete(void);
174 /** Event for USB Start Of Frame detection, when enabled. This event fires at the start of each USB
175 * frame, once per millisecond, and is synchronized to the USB bus. This can be used as an accurate
176 * millisecond timer source when the USB bus is not suspended while in host mode.
178 * This event is time-critical; it is run once per millisecond and thus long handlers will significantly
179 * degrade device performance. This event should only be enabled when needed to reduce device wake-ups.
181 * \note This event is not normally active - it must be manually enabled and disabled via the
182 * \ref USB_Host_EnableSOFEvents() and \ref USB_Host_DisableSOFEvents() commands after enumeration of
186 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
187 * \ref Group_USBManagement documentation).
189 void EVENT_USB_Host_StartOfFrame(void);
191 /** Event for USB device connection. This event fires when the AVR in device mode and the device is connected
192 * to a host, beginning the enumeration process, measured by a rising level on the AVR's VBUS pin.
194 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
195 * two seconds) will prevent the device from enumerating correctly.
197 * \note For the smaller series 2 USB AVRs with limited USB controllers, VBUS is not available to the USB controller.
198 * this means that the current connection state is derived from the bus suspension and wake up events by default,
199 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
200 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
201 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
202 * and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually.
205 * \note This event may fire multiple times during device enumeration on the series 2 USB AVRs with limited USB controllers
206 * if NO_LIMITED_CONTROLLER_CONNECT is not defined.
208 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
210 void EVENT_USB_Device_Connect(void);
212 /** Event for USB device disconnection. This event fires when the AVR in device mode and the device is disconnected
213 * from a host, measured by a falling level on the AVR's VBUS pin.
215 * \note For the smaller series 2 USB AVRs with limited USB controllers, VBUS is not available to the USB controller.
216 * this means that the current connection state is derived from the bus suspension and wake up events by default,
217 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
218 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
219 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
220 * and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually.
223 * \note This event may fire multiple times during device enumeration on the series 2 USB AVRs with limited USB controllers
224 * if NO_LIMITED_CONTROLLER_CONNECT is not defined.
226 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
228 void EVENT_USB_Device_Disconnect(void);
230 /** Event for unhandled control requests. This event fires when a the USB host issues a control
231 * request to the control endpoint (address 0) that the library does not handle. This may either
232 * be a standard request that the library has no handler code for, or a class specific request
233 * issued to the device which must be handled appropriately.
235 * This event is time-critical; each packet within the request transaction must be acknowledged or
236 * sent within 50ms or the host will abort the transfer.
238 * The library internally handles all standard control requests with the exceptions of SYNC FRAME,
239 * SET DESCRIPTOR and SET INTERFACE. These and all other non-standard control requests will be left
240 * for the user to process via this event if desired. If not handled in the user application, requests
241 * are automatically STALLed.
243 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
244 * \ref Group_USBManagement documentation).
247 * \note Requests should be handled in the same manner as described in the USB 2.0 Specification,
248 * or appropriate class specification. In all instances, the library has already read the
249 * request SETUP parameters into the \ref USB_ControlRequest structure which should then be used
250 * by the application to determine how to handle the issued request.
252 void EVENT_USB_Device_UnhandledControlRequest(void);
254 /** Event for USB configuration number changed. This event fires when a the USB host changes the
255 * selected configuration number while in device mode. This event should be hooked in device
256 * applications to create the endpoints and configure the device for the selected configuration.
258 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
259 * one second) will prevent the device from enumerating correctly.
261 * This event fires after the value of \ref USB_ConfigurationNumber has been changed.
263 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
264 * \ref Group_USBManagement documentation).
266 void EVENT_USB_Device_ConfigurationChanged(void);
268 /** Event for USB suspend. This event fires when a the USB host suspends the device by halting its
269 * transmission of Start Of Frame pulses to the device. This is generally hooked in order to move
270 * the device over to a low power state until the host wakes up the device. If the USB interface is
271 * enumerated with the \ref USB_OPT_AUTO_PLL option set, the library will automatically suspend the
272 * USB PLL before the event is fired to save power.
274 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
275 * \ref Group_USBManagement documentation).
278 * \note This event does not exist on the series 2 USB AVRs when the NO_LIMITED_CONTROLLER_CONNECT
279 * compile time token is not set - see \ref EVENT_USB_Device_Disconnect.
281 * \see \ref EVENT_USB_Device_WakeUp() event for accompanying Wake Up event.
283 void EVENT_USB_Device_Suspend(void);
285 /** Event for USB wake up. This event fires when a the USB interface is suspended while in device
286 * mode, and the host wakes up the device by supplying Start Of Frame pulses. This is generally
287 * hooked to pull the user application out of a low power state and back into normal operating
288 * mode. If the USB interface is enumerated with the \ref USB_OPT_AUTO_PLL option set, the library
289 * will automatically restart the USB PLL before the event is fired.
291 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
292 * \ref Group_USBManagement documentation).
295 * \note This event does not exist on the series 2 USB AVRs when the NO_LIMITED_CONTROLLER_CONNECT
296 * compile time token is not set - see \ref EVENT_USB_Device_Connect.
298 * \see \ref EVENT_USB_Device_Suspend() event for accompanying Suspend event.
300 void EVENT_USB_Device_WakeUp(void);
302 /** Event for USB interface reset. This event fires when the USB interface is in device mode, and
303 * a the USB host requests that the device reset its interface. This event fires after the control
304 * endpoint has been automatically configured by the library.
306 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
307 * two seconds) will prevent the device from enumerating correctly.
309 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
310 * \ref Group_USBManagement documentation).
312 void EVENT_USB_Device_Reset(void);
314 /** Event for USB Start Of Frame detection, when enabled. This event fires at the start of each USB
315 * frame, once per millisecond, and is synchronized to the USB bus. This can be used as an accurate
316 * millisecond timer source when the USB bus is enumerated in device mode to a USB host.
318 * This event is time-critical; it is run once per millisecond and thus long handlers will significantly
319 * degrade device performance. This event should only be enabled when needed to reduce device wake-ups.
321 * \note This event is not normally active - it must be manually enabled and disabled via the
322 * \ref USB_Device_EnableSOFEvents() and \ref USB_Device_DisableSOFEvents() commands after enumeration.
325 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
326 * \ref Group_USBManagement documentation).
328 void EVENT_USB_Device_StartOfFrame(void);
331 /* Private Interface - For use in library only: */
332 #if !defined(__DOXYGEN__)
333 /* Function Prototypes: */
334 #if defined(__INCLUDE_FROM_EVENTS_C)
335 void USB_Event_Stub(void) ATTR_CONST
;
337 #if defined(USB_CAN_BE_BOTH)
338 void EVENT_USB_UIDChange(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
341 #if defined(USB_CAN_BE_HOST)
342 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
343 void EVENT_USB_Host_DeviceAttached(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
344 void EVENT_USB_Host_DeviceUnattached(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
345 void EVENT_USB_Host_DeviceEnumerationComplete(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
346 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
,
347 const uint8_t SubErrorCode
)
348 ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
349 void EVENT_USB_Host_StartOfFrame(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
352 #if defined(USB_CAN_BE_DEVICE)
353 void EVENT_USB_Device_Connect(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
354 void EVENT_USB_Device_Disconnect(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
355 void EVENT_USB_Device_UnhandledControlRequest(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
356 void EVENT_USB_Device_ConfigurationChanged(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
357 void EVENT_USB_Device_Suspend(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
358 void EVENT_USB_Device_WakeUp(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
359 void EVENT_USB_Device_Reset(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
360 void EVENT_USB_Device_StartOfFrame(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
365 /* Disable C linkage for C++ Compilers: */
366 #if defined(__cplusplus)