3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 /** \ingroup Group_USB
32 * @defgroup Group_Events USB Events
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.
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 is achieved through the use of the GCC compiler's "alias" attribute.
43 * Each event must only have one associated event handler, but can be raised by multiple sources.
48 #ifndef __USBEVENTS_H__
49 #define __USBEVENTS_H__
54 #include "../../../Common/Common.h"
57 /* Enable C linkage for C++ Compilers: */
58 #if defined(__cplusplus)
62 /* Public Interface - May be used in end-application: */
63 /* Pseudo-Functions for Doxygen: */
64 #if !defined(INCLUDE_FROM_EVENTS_C) || defined(__DOXYGEN__)
65 /** Event for VBUS level change. This event fires when the VBUS line of the USB AVR changes from
66 * high to low or vice-versa.
68 * \note This event is only available on USB AVR models which support VBUS notification interrupts.
70 void EVENT_USB_VBUSChange(void);
72 /** Event for VBUS attachment. This event fires when the VBUS line of the USB AVR changes from
73 * low to high, signalling the attachment of the USB device to a host, before the enumeration
76 * \note This event is only available on USB AVR models which support VBUS notification interrupts.
78 void EVENT_USB_VBUSConnect(void);
80 /** Event for VBUS detachment. This event fires when the VBUS line of the USB AVR changes from
81 * high to low, signalling the USB device has been removed from a host whether it has been enumerated
84 * \note This event is only available on USB AVR models which support VBUS notification interrupts.
86 void EVENT_USB_VBUSDisconnect(void);
88 /** Event for USB device connection. This event fires when the AVR is in USB host mode and a device
89 * has been attached (but not yet fully enumerated), or when in device mode and the device is connected
90 * to a host, beginning the enumeration process.
92 * When in device mode, this can be used to programmatically start the USB management task to reduce
95 * \note For the smaller USB AVRs (AT90USBXX2) with limited USB controllers, VBUS is not available to the USB controller.
96 * this means that the current connection state is derived from the bus suspension and wake up events by default,
97 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
98 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
99 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
100 * and disconnection events may be manually fired, and the \ref USB_IsConnected global changed manually.
102 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
104 void EVENT_USB_Connect(void);
106 /** Event for USB device disconnection. This event fires when the AVR is in USB host mode and an
107 * attached and enumerated device has been disconnected, or when in device mode and the device is
108 * disconnected from the host.
110 * When in device mode, this can be used to programmatically stop the USB management task to reduce
113 * \note For the smaller USB AVRs (AT90USBXX2) with limited USB controllers, VBUS is not available to the USB controller.
114 * this means that the current connection state is derived from the bus suspension and wake up events by default,
115 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
116 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
117 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
118 * and disconnection events may be manually fired, and the \ref USB_IsConnected global changed manually.
120 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
122 void EVENT_USB_Disconnect(void);
124 /** Event for USB initialization failure. This event fires when the USB interface fails to
125 * initialize correctly due to a hardware or software fault.
127 * \note This event only exists on USB AVR models which support dual role modes.
129 * \param ErrorCode Error code indicating the failure reason, a value in \ref USB_InitErrorCodes_t
131 void EVENT_USB_InitFailure(const uint8_t ErrorCode
);
133 /** Event for USB mode pin level change. This event fires when the USB interface is set to dual role
134 * mode, and the UID pin level has changed to indicate a new mode (device or host). This event fires
135 * before the mode is switched to the newly indicated mode.
137 * \note This event only exists on USB AVR models which support dual role modes.
139 * \note This event does not exist if the USB_DEVICE_ONLY or USB_HOST_ONLY tokens have been supplied
140 * to the compiler (see \ref Group_USBManagement documentation).
142 void EVENT_USB_UIDChange(void);
144 /** Event for USB host error. This event fires when a hardware fault has occurred whilst the USB
145 * interface is in host mode.
147 * \param ErrorCode Error code indicating the failure reason, a value in \ref USB_Host_ErrorCodes_t
149 * \note This event only exists on USB AVR models which supports host mode.
151 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
152 * \ref Group_USBManagement documentation).
154 void EVENT_USB_HostError(const uint8_t ErrorCode
);
156 /** Event for USB device attachment. This event fires when a the USB interface is in host mode, and
157 * a USB device has been connected to the USB interface. This is interrupt driven, thus fires before
158 * the standard \ref EVENT_USB_Connect event and so can be used to programmatically start the USB management
159 * task to reduce CPU consumption.
161 * \note This event only exists on USB AVR models which supports host mode.
163 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
164 * \ref Group_USBManagement documentation).
166 * \see \ref TASK(USB_USBTask) for more information on the USB management task and reducing CPU usage.
168 void EVENT_USB_DeviceAttached(void);
170 /** Event for USB device removal. This event fires when a the USB interface is in host mode, and
171 * a USB device has been removed the USB interface whether or not it has been enumerated. This
172 * can be used to programmatically stop the USB management task to reduce CPU consumption.
174 * \note This event only exists on USB AVR models which supports host mode.
176 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
177 * \ref Group_USBManagement documentation).
179 * \see \ref TASK(USB_USBTask) for more information on the USB management task and reducing CPU usage.
181 void EVENT_USB_DeviceUnattached(void);
183 /** Event for USB device enumeration failure. This event fires when a the USB interface is
184 * in host mode, and an attached USB device has failed to enumerate completely.
186 * \param ErrorCode Error code indicating the failure reason, a value in
187 * \ref USB_Host_EnumerationErrorCodes_t
189 * \param SubErrorCode Sub error code indicating the reason for failure - for example, if the
190 * ErrorCode parameter indicates a control error, this will give the error
191 * code returned by the \ref USB_Host_SendControlRequest() function.
193 * \note This event only exists on USB AVR models which supports host mode.
195 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
196 * \ref Group_USBManagement documentation).
198 void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
);
200 /** Event for USB device enumeration completion. This event fires when a the USB interface is
201 * in host mode and an attached USB device has been completely enumerated and is ready to be
202 * controlled by the user application, or when the library is in device mode, and the Host
203 * has finished enumerating the device.
205 void EVENT_USB_DeviceEnumerationComplete(void);
207 /** Event for unhandled control requests. This event fires when a the USB host issues a control
208 * request to the control endpoint (address 0) that the library does not handle. This may either
209 * be a standard request that the library has no handler code for, or a class specific request
210 * issued to the device which must be handled appropriately. Due to the strict timing requirements
211 * on control transfers, interrupts are disabled during control request processing.
213 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
214 * \ref Group_USBManagement documentation).
216 * \note Requests should be handled in the same manner as described in the USB 2.0 Specification,
217 * or appropriate class specification. In all instances, the library has already read the
218 * request SETUP parameters into the \ref USB_ControlRequest structure which should then be used
219 * by the application to determine how to handle the issued request.
221 void EVENT_USB_UnhandledControlPacket(void);
223 /** Event for USB configuration number changed. This event fires when a the USB host changes the
224 * selected configuration number while in device mode. This event should be hooked in device
225 * applications to create the endpoints and configure the device for the selected configuration.
227 * This event fires after the value of \ref USB_ConfigurationNumber has been changed.
229 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
230 * \ref Group_USBManagement documentation).
232 void EVENT_USB_ConfigurationChanged(void);
234 /** Event for USB suspend. This event fires when a the USB host suspends the device by halting its
235 * transmission of Start Of Frame pulses to the device. This is generally hooked in order to move
236 * the device over to a low power state until the host wakes up the device.
238 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
239 * \ref Group_USBManagement documentation).
241 * \see \ref EVENT_USB_WakeUp() event for accompanying Wake Up event.
243 void EVENT_USB_Suspend(void);
245 /** Event for USB wake up. This event fires when a the USB interface is suspended while in device
246 * mode, and the host wakes up the device by supplying Start Of Frame pulses. This is generally
247 * hooked to pull the user application out of a lowe power state and back into normal operating
250 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
251 * \ref Group_USBManagement documentation).
253 * \see \ref EVENT_USB_Suspend() event for accompanying Suspend event.
255 void EVENT_USB_WakeUp(void);
257 /** Event for USB interface reset. This event fires when a the USB interface is in device mode, and
258 * a the USB host requests that the device reset its interface. This is generally hooked so that
259 * the USB control endpoint can be switched to interrupt driven mode, by selecting it and calling
260 * USB_INT_Enable(ENDPOINT_INT_SETUP). Before this event fires, all device endpoints are reset and
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_Reset(void);
268 /** Event for USB device mode error. This event fires when the USB interface is in device mode,
269 * and an error occurs which prevents it from operating normally.
271 * \param ErrorCode Error code indicating the source of the error, a value in
272 * \ref USB_Device_ErrorCodes_t
274 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
275 * \ref Group_USBManagement documentation).
277 void EVENT_USB_DeviceError(const uint8_t ErrorCode
);
280 /* Private Interface - For use in library only: */
281 #if !defined(__DOXYGEN__)
282 /* Function Prototypes: */
283 #if defined(INCLUDE_FROM_EVENTS_C)
284 void USB_Event_Stub(void) ATTR_CONST
;
286 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
287 void EVENT_USB_VBUSChange(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
288 void EVENT_USB_VBUSConnect(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
289 void EVENT_USB_VBUSDisconnect(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
292 void EVENT_USB_Connect(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
293 void EVENT_USB_Disconnect(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
294 void EVENT_USB_DeviceEnumerationComplete(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
296 #if defined(USB_CAN_BE_BOTH)
297 void EVENT_USB_InitFailure(const uint8_t ErrorCode
) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
298 void EVENT_USB_UIDChange(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
301 #if defined(USB_CAN_BE_HOST)
302 void EVENT_USB_HostError(const uint8_t ErrorCode
) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
303 void EVENT_USB_DeviceAttached(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
304 void EVENT_USB_DeviceUnattached(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
305 void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
)
306 ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
309 void EVENT_USB_UnhandledControlPacket(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
310 void EVENT_USB_ConfigurationChanged(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
311 void EVENT_USB_Suspend(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
312 void EVENT_USB_WakeUp(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
313 void EVENT_USB_Reset(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
314 void EVENT_USB_DeviceError(const uint8_t ErrorCode
) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
318 /* Disable C linkage for C++ Compilers: */
319 #if defined(__cplusplus)