USB_IsConnected is now cleared before the USB_Disconnect() event is fired in response...
[pub/USBasp.git] / LUFA / Drivers / USB / HighLevel / Events.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 /** \ingroup Group_USB
32 * @defgroup Group_Events USB Events
33 *
34 * This module 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.
38 *
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. This trasparent hook system is achieved through the use of the GCC compiler's
42 * "alias" attribute.
43 *
44 * Each event must only have one associated event handler, but can be raised by multiple sources.
45 *
46 * @{
47 */
48
49 #ifndef __USBEVENTS_H__
50 #define __USBEVENTS_H__
51
52 /* Includes: */
53 #include <avr/io.h>
54
55 #include "../../../Common/Common.h"
56 #include "USBMode.h"
57
58 /* Enable C linkage for C++ Compilers: */
59 #if defined(__cplusplus)
60 extern "C" {
61 #endif
62
63 /* Public Interface - May be used in end-application: */
64 /* Pseudo-Functions for Doxygen: */
65 #if !defined(INCLUDE_FROM_EVENTS_C) || defined(__DOXYGEN__)
66 /** Event for VBUS level change. This event fires when the VBUS line of the USB AVR changes from
67 * high to low or vice-versa, before the new VBUS level is sampled and the appropriate action
68 * taken.
69 *
70 * \note This event is only available on USB AVR models which support VBUS notification interrupts.
71 */
72 void EVENT_USB_VBUSChange(void);
73
74 /** Event for VBUS attachment. On the AVR models with a dedicated VBUS pin, this event fires when
75 * the VBUS line of the USB AVR changes from low to high (after the VBUS events have been handled),
76 * signalling the attachment of the USB device to a host, before the enumeration process has begun.
77 *
78 * \note This event is only available on USB AVR models which support VBUS notification interrupts.
79 */
80 void EVENT_USB_VBUSConnect(void);
81
82 /** Event for VBUS attachment. On the AVR models with a dedicated VBUS pin, this event fires when
83 * the VBUS line of the USB AVR changes from high to low (after the VBUS events have been handled),
84 * signalling the detatchment of the USB device from a host, regardless of its enumeration state.
85 *
86 * \note This event is only available on USB AVR models which support VBUS notification interrupts.
87 */
88 void EVENT_USB_VBUSDisconnect(void);
89
90 /** Event for USB device connection. This event fires when the AVR is in USB host mode and a device
91 * has been attached (but not yet fully enumerated), or when in device mode and the device is connected
92 * to a host, beginning the enumeration process.
93 *
94 * When in device mode, this can be used to programmatically start the USB management task to reduce
95 * CPU usage.
96 *
97 * \note For the smaller USB AVRs (AT90USBXX2) with limited USB controllers, VBUS is not available to the USB controller.
98 * this means that the current connection state is derived from the bus suspension and wake up events by default,
99 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
100 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
101 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
102 * and disconnection events may be manually fired, and the \ref USB_IsConnected global changed manually.
103 *
104 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
105 */
106 void EVENT_USB_Connect(void);
107
108 /** Event for USB device disconnection. This event fires when the AVR is in USB host mode and an
109 * attached and enumerated device has been disconnected, or when in device mode and the device is
110 * disconnected from the host.
111 *
112 * When in device mode, this can be used to programmatically stop the USB management task to reduce
113 * CPU usage.
114 *
115 * \note For the smaller USB AVRs (AT90USBXX2) with limited USB controllers, VBUS is not available to the USB controller.
116 * this means that the current connection state is derived from the bus suspension and wake up events by default,
117 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
118 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
119 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
120 * and disconnection events may be manually fired, and the \ref USB_IsConnected global changed manually.
121 *
122 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
123 */
124 void EVENT_USB_Disconnect(void);
125
126 /** Event for USB initialization failure. This event fires when the USB interface fails to
127 * initialize correctly due to a hardware or software fault.
128 *
129 * \note This event only exists on USB AVR models which support dual role modes.
130 *
131 * \param ErrorCode Error code indicating the failure reason, a value in \ref USB_InitErrorCodes_t
132 */
133 void EVENT_USB_InitFailure(const uint8_t ErrorCode);
134
135 /** Event for USB mode pin level change. This event fires when the USB interface is set to dual role
136 * mode, and the UID pin level has changed to indicate a new mode (device or host). This event fires
137 * before the mode is switched to the newly indicated mode but after the \ref USB_Disconnect event has
138 * fired (if connected before the role change).
139 *
140 * \note This event only exists on USB AVR models which support dual role modes.
141 *
142 * \note This event does not exist if the USB_DEVICE_ONLY or USB_HOST_ONLY tokens have been supplied
143 * to the compiler (see \ref Group_USBManagement documentation).
144 */
145 void EVENT_USB_UIDChange(void);
146
147 /** Event for USB host error. This event fires when a hardware fault has occurred whilst the USB
148 * interface is in host mode.
149 *
150 * \param ErrorCode Error code indicating the failure reason, a value in \ref USB_Host_ErrorCodes_t
151 *
152 * \note This event only exists on USB AVR models which supports host mode.
153 *
154 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
155 * \ref Group_USBManagement documentation).
156 */
157 void EVENT_USB_HostError(const uint8_t ErrorCode);
158
159 /** Event for USB device attachment. This event fires when a the USB interface is in host mode, and
160 * a USB device has been connected to the USB interface. This is interrupt driven, thus fires before
161 * the standard \ref EVENT_USB_Connect event and so can be used to programmatically start the USB management
162 * task to reduce CPU consumption.
163 *
164 * \note This event only exists on USB AVR models which supports host mode.
165 *
166 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
167 * \ref Group_USBManagement documentation).
168 *
169 * \see \ref TASK(USB_USBTask) for more information on the USB management task and reducing CPU usage.
170 */
171 void EVENT_USB_DeviceAttached(void);
172
173 /** Event for USB device removal. This event fires when a the USB interface is in host mode, and
174 * a USB device has been removed the USB interface whether or not it has been enumerated. This
175 * can be used to programmatically stop the USB management task to reduce CPU consumption.
176 *
177 * \note This event only exists on USB AVR models which supports host mode.
178 *
179 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
180 * \ref Group_USBManagement documentation).
181 *
182 * \see \ref TASK(USB_USBTask) for more information on the USB management task and reducing CPU usage.
183 */
184 void EVENT_USB_DeviceUnattached(void);
185
186 /** Event for USB device enumeration failure. This event fires when a the USB interface is
187 * in host mode, and an attached USB device has failed to enumerate completely.
188 *
189 * \param ErrorCode Error code indicating the failure reason, a value in
190 * \ref USB_Host_EnumerationErrorCodes_t
191 *
192 * \param SubErrorCode Sub error code indicating the reason for failure - for example, if the
193 * ErrorCode parameter indicates a control error, this will give the error
194 * code returned by the \ref USB_Host_SendControlRequest() function.
195 *
196 * \note This event only exists on USB AVR models which supports host mode.
197 *
198 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
199 * \ref Group_USBManagement documentation).
200 */
201 void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode);
202
203 /** Event for USB device enumeration completion. This event fires when a the USB interface is
204 * in host mode and an attached USB device has been completely enumerated and is ready to be
205 * controlled by the user application, or when the library is in device mode, and the Host
206 * has finished enumerating the device.
207 */
208 void EVENT_USB_DeviceEnumerationComplete(void);
209
210 /** Event for unhandled control requests. This event fires when a the USB host issues a control
211 * request to the control endpoint (address 0) that the library does not handle. This may either
212 * be a standard request that the library has no handler code for, or a class specific request
213 * issued to the device which must be handled appropriately. Due to the strict timing requirements
214 * on control transfers, interrupts are disabled during control request processing.
215 *
216 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
217 * \ref Group_USBManagement documentation).
218 *
219 * \note Requests should be handled in the same manner as described in the USB 2.0 Specification,
220 * or appropriate class specification. In all instances, the library has already read the
221 * request SETUP parameters into the \ref USB_ControlRequest structure which should then be used
222 * by the application to determine how to handle the issued request.
223 */
224 void EVENT_USB_UnhandledControlPacket(void);
225
226 /** Event for USB configuration number changed. This event fires when a the USB host changes the
227 * selected configuration number while in device mode. This event should be hooked in device
228 * applications to create the endpoints and configure the device for the selected configuration.
229 *
230 * This event fires after the value of \ref USB_ConfigurationNumber has been changed.
231 *
232 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
233 * \ref Group_USBManagement documentation).
234 */
235 void EVENT_USB_ConfigurationChanged(void);
236
237 /** Event for USB suspend. This event fires when a the USB host suspends the device by halting its
238 * transmission of Start Of Frame pulses to the device. This is generally hooked in order to move
239 * the device over to a low power state until the host wakes up the device. If the USB interface is
240 * enumerated with the \ref USB_OPT_AUTO_PLL option set, the library will automatically suspend the
241 * USB PLL before the event is fired to save power.
242 *
243 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
244 * \ref Group_USBManagement documentation).
245 *
246 * \see \ref EVENT_USB_WakeUp() event for accompanying Wake Up event.
247 */
248 void EVENT_USB_Suspend(void);
249
250 /** Event for USB wake up. This event fires when a the USB interface is suspended while in device
251 * mode, and the host wakes up the device by supplying Start Of Frame pulses. This is generally
252 * hooked to pull the user application out of a lowe power state and back into normal operating
253 * mode. If the USB interface is enumerated with the \ref USB_OPT_AUTO_PLL option set, the library
254 * will automatically restart the USB PLL before the event is fired.
255 *
256 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
257 * \ref Group_USBManagement documentation).
258 *
259 * \see \ref EVENT_USB_Suspend() event for accompanying Suspend event.
260 */
261 void EVENT_USB_WakeUp(void);
262
263 /** Event for USB interface reset. This event fires when the USB interface is in device mode, and
264 * a the USB host requests that the device reset its interface. This event fires after the control
265 * endpoint has been automatically configured by the library.
266 *
267 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
268 * \ref Group_USBManagement documentation).
269 */
270 void EVENT_USB_Reset(void);
271
272 /** Event for USB device mode error. This event fires when the USB interface is in device mode,
273 * and an error occurs which prevents it from operating normally.
274 *
275 * \param ErrorCode Error code indicating the source of the error, a value in
276 * \ref USB_Device_ErrorCodes_t
277 *
278 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
279 * \ref Group_USBManagement documentation).
280 */
281 void EVENT_USB_DeviceError(const uint8_t ErrorCode);
282 #endif
283
284 /* Private Interface - For use in library only: */
285 #if !defined(__DOXYGEN__)
286 /* Function Prototypes: */
287 #if defined(INCLUDE_FROM_EVENTS_C)
288 void USB_Event_Stub(void) ATTR_CONST;
289
290 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
291 void EVENT_USB_VBUSChange(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
292 void EVENT_USB_VBUSConnect(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
293 void EVENT_USB_VBUSDisconnect(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
294 #endif
295
296 void EVENT_USB_Connect(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
297 void EVENT_USB_Disconnect(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
298 void EVENT_USB_DeviceEnumerationComplete(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
299
300 #if defined(USB_CAN_BE_BOTH)
301 void EVENT_USB_InitFailure(const uint8_t ErrorCode) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
302 void EVENT_USB_UIDChange(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
303 #endif
304
305 #if defined(USB_CAN_BE_HOST)
306 void EVENT_USB_HostError(const uint8_t ErrorCode) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
307 void EVENT_USB_DeviceAttached(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
308 void EVENT_USB_DeviceUnattached(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
309 void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
310 ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
311 #endif
312
313 void EVENT_USB_UnhandledControlPacket(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
314 void EVENT_USB_ConfigurationChanged(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
315 void EVENT_USB_Suspend(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
316 void EVENT_USB_WakeUp(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
317 void EVENT_USB_Reset(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
318 void EVENT_USB_DeviceError(const uint8_t ErrorCode) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
319 #endif
320 #endif
321
322 /* Disable C linkage for C++ Compilers: */
323 #if defined(__cplusplus)
324 }
325 #endif
326
327 #endif
328
329 /** @} */