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 using the \ref EVENT_HANDLER() and \ref HANDLES_EVENT() macros. If an
40 * event with no associated handler is fired within the library, it by default fires an internal empty stub
41 * 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: */
64 /** Raises a given event name, with the specified parameters. For events with no parameters the
65 * only argument to the macro is the event name, events with parameters list the parameter values
66 * after the name as a comma separated list.
68 * When a given event is fired, its corresponding event handler code is executed.
72 * // Raise the USB_VBUSChange event, which takes no parameters
73 * RAISE_EVENT(USB_VBUSChange);
75 * // Raise the USB_UnhandledControlPacket event which takes two parameters
76 * RAISE_EVENT(USB_UnhandledControlPacket, 0, 1);
81 #define RAISE_EVENT(e, ...) Event_ ## e (__VA_ARGS__)
83 /** Indicates that a given module can raise a given event. This is the equivalent of putting the
84 * event function's prototype into the module, but in a cleaner way. Each event which may be
85 * fired via the \ref RAISE_EVENT macro in the module should have an accompanying \ref RAISES_EVENT
86 * prototype in the module's header file.
90 * // Module can raise the USB_VBUSChange event
91 * RAISES_EVENT(USB_VBUSChange);
94 * // Inside a block of code in a function of the module, raise the USB_VBUSChange event
95 * RAISE_EVENT(USB_VBUSChange);
100 #define RAISES_EVENT(e) HANDLES_EVENT(e)
102 /** Defines an event handler for the given event. Event handlers should be short in length, as they
103 * may be raised from inside an ISR. The user application can react to each event as it sees fit,
104 * such as logging the event, indicating the change to the user or performing some other action.
106 * Only one event handler may be defined in any user project for each individual event. Events may
107 * or may not have parameters - for each event, refer to its documentation elsewhere in this module
108 * to determine the presence and purpose of any event parameters.
112 * // Create an event handler for the USB_VBUSChange event
113 * EVENT_HANDLER(USB_VBUSChange)
115 * // Code to execute when the VBUS level changes
119 * \see HANDLES_EVENT()
121 #define EVENT_HANDLER(e) void Event_ ## e e ## _P
123 /** Indicates that a given module handles an event. This is the equivalent of putting the
124 * event function's prototype into the module, but in a cleaner way. Each event which may be
125 * handled via the \ref EVENT_HANDLER macro in the module should have an accompanying \ref HANDLES_EVENT
126 * prototype in the module's header file.
130 * // Module handles the USB_VBUSChange event
131 * HANDLES_EVENT(USB_VBUSChange);
133 * // Create the USB_VBUSChange event handler
134 * EVENT_HANDLER(USB_VBUSChange)
136 * // Event handler code here
140 * \see EVENT_HANDLER()
142 #define HANDLES_EVENT(e) EVENT_HANDLER(e)
144 /* Pseudo-Functions for Doxygen: */
145 #if defined(__DOXYGEN__)
146 /** Event for VBUS level change. This event fires when the VBUS line of the USB AVR changes from
147 * high to low or vice-versa.
149 * \note This event is only available on USB AVR models which support VBUS notification interrupts.
151 void USB_VBUSChange(void);
153 /** Event for VBUS attachment. This event fires when the VBUS line of the USB AVR changes from
154 * low to high, signalling the attachment of the USB device to a host, before the enumeration
157 * \note This event is only available on USB AVR models which support VBUS notification interrupts.
159 void USB_VBUSConnect(void);
161 /** Event for VBUS detachment. This event fires when the VBUS line of the USB AVR changes from
162 * high to low, signalling the USB device has been removed from a host whether it has been enumerated
165 * \note This event is only available on USB AVR models which support VBUS notification interrupts.
167 void USB_VBUSDisconnect(void);
169 /** Event for USB device connection. This event fires when the AVR is in USB host mode and a device
170 * has been attached (but not yet fully enumerated), or when in device mode and the device is connected
171 * to a host, beginning the enumeration process.
173 * When in device mode, this can be used to programmatically start the USB management task to reduce
176 * \note For the smaller USB AVRs (AT90USBXX2) with limited USB controllers, VBUS is not available to the USB controller.
177 * this means that the current connection state is derived from the bus suspension and wake up events by default,
178 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
179 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
180 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
181 * and disconnection events may be manually fired by \ref RAISE_EVENT(), and the \ref USB_IsConnected global changed manually.
183 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
185 void USB_Connect(void);
187 /** Event for USB device disconnection. This event fires when the AVR is in USB host mode and an
188 * attached and enumerated device has been disconnected, or when in device mode and the device is
189 * disconnected from the host.
191 * When in device mode, this can be used to programmatically stop the USB management task to reduce
194 * \note For the smaller USB AVRs (AT90USBXX2) with limited USB controllers, VBUS is not available to the USB controller.
195 * this means that the current connection state is derived from the bus suspension and wake up events by default,
196 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
197 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
198 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
199 * and disconnection events may be manually fired by \ref RAISE_EVENT(), and the \ref USB_IsConnected global changed manually.
201 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
203 void USB_Disconnect(void);
205 /** Event for USB initialization failure. This event fires when the USB interface fails to
206 * initialize correctly due to a hardware or software fault.
208 * \note This event only exists on USB AVR models which support dual role modes.
210 * \param ErrorCode Error code indicating the failure reason, a value in \ref USB_InitErrorCodes_t
211 * located in LowLevel.h.
213 void USB_InitFailure(const uint8_t ErrorCode
);
215 /** Event for USB mode pin level change. This event fires when the USB interface is set to dual role
216 * mode, and the UID pin level has changed to indicate a new mode (device or host). This event fires
217 * before the mode is switched to the newly indicated mode.
219 * \note This event only exists on USB AVR models which support dual role modes.
221 * \note This event does not exist if the USB_DEVICE_ONLY or USB_HOST_ONLY tokens have been supplied
222 * to the compiler (see \ref Group_USBManagement documentation).
224 void USB_UIDChange(void);
226 /** Event for USB host error. This event fires when a hardware fault has occurred whilst the USB
227 * interface is in host mode.
229 * \param ErrorCode Error code indicating the failure reason, a value in \ref USB_Host_ErrorCodes_t
232 * \note This event only exists on USB AVR models which supports host mode.
234 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
235 * \ref Group_USBManagement documentation).
237 void USB_HostError(const uint8_t ErrorCode
);
239 /** Event for USB device attachment. This event fires when a the USB interface is in host mode, and
240 * a USB device has been connected to the USB interface. This is interrupt driven, thus fires before
241 * the standard \ref USB_Connect event and so can be used to programmatically start the USB management
242 * task to reduce CPU consumption.
244 * \note This event only exists on USB AVR models which supports host mode.
246 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
247 * \ref Group_USBManagement documentation).
249 * \see \ref TASK(USB_USBTask) for more information on the USB management task and reducing CPU usage.
251 void USB_DeviceAttached(void);
253 /** Event for USB device removal. This event fires when a the USB interface is in host mode, and
254 * a USB device has been removed the USB interface whether or not it has been enumerated. This
255 * can be used to programmatically stop the USB management task to reduce CPU consumption.
257 * \note This event only exists on USB AVR models which supports host mode.
259 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
260 * \ref Group_USBManagement documentation).
262 * \see \ref TASK(USB_USBTask) for more information on the USB management task and reducing CPU usage.
264 void USB_DeviceUnattached(void);
266 /** Event for USB device enumeration failure. This event fires when a the USB interface is
267 * in host mode, and an attached USB device has failed to enumerate completely.
269 * \param ErrorCode Error code indicating the failure reason, a value in
270 * \ref USB_Host_EnumerationErrorCodes_t located in Host.h.
272 * \param SubErrorCode Sub error code indicating the reason for failure - for example, if the
273 * ErrorCode parameter indicates a control error, this will give the error
274 * code returned by the \ref USB_Host_SendControlRequest() function.
276 * \note This event only exists on USB AVR models which supports host mode.
278 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
279 * \ref Group_USBManagement documentation).
281 void USB_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
);
283 /** Event for USB device enumeration completion. This event fires when a the USB interface is
284 * in host mode and an attached USB device has been completely enumerated and is ready to be
285 * controlled by the user application, or when the library is in device mode, and the Host
286 * has finished enumerating the device.
288 void USB_DeviceEnumerationComplete(void);
290 /** Event for unhandled control requests. This event fires when a the USB host issues a control
291 * request to the control endpoint (address 0) that the library does not handle. This may either
292 * be a standard request that the library has no handler code for, or a class specific request
293 * issued to the device which must be handled appropriately. Due to the strict timing requirements
294 * on control transfers, interrupts are disabled during control request processing.
296 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
297 * \ref Group_USBManagement documentation).
299 * \note Requests should be handled in the same manner as described in the USB 2.0 Specification,
300 * or appropriate class specification. In all instances, the library has already read the
301 * request SETUP parameters into the \ref USB_ControlRequest structure which should then be used
302 * by the application to determine how to handle the issued request.
304 void USB_UnhandledControlPacket(void);
306 /** Event for USB configuration number changed. This event fires when a the USB host changes the
307 * selected configuration number while in device mode. This event should be hooked in device
308 * applications to create the endpoints and configure the device for the selected configuration.
310 * This event fires after the value of \ref USB_ConfigurationNumber has been changed.
312 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
313 * \ref Group_USBManagement documentation).
315 void USB_ConfigurationChanged(void);
317 /** Event for USB suspend. This event fires when a the USB host suspends the device by halting its
318 * transmission of Start Of Frame pulses to the device. This is generally hooked in order to move
319 * the device over to a low power state until the host wakes up the device.
321 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
322 * \ref Group_USBManagement documentation).
324 * \see \ref USB_WakeUp() event for accompanying Wake Up event.
326 void USB_Suspend(void);
328 /** Event for USB wake up. This event fires when a the USB interface is suspended while in device
329 * mode, and the host wakes up the device by supplying Start Of Frame pulses. This is generally
330 * hooked to pull the user application out of a lowe power state and back into normal operating
333 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
334 * \ref Group_USBManagement documentation).
336 * \see \ref USB_Suspend() event for accompanying Suspend event.
338 void USB_WakeUp(void);
340 /** Event for USB interface reset. This event fires when a the USB interface is in device mode, and
341 * a the USB host requests that the device reset its interface. This is generally hooked so that
342 * the USB control endpoint can be switched to interrupt driven mode, by selecting it and calling
343 * USB_INT_Enable(ENDPOINT_INT_SETUP). Before this event fires, all device endpoints are reset and
346 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
347 * \ref Group_USBManagement documentation).
349 void USB_Reset(void);
351 /** Event for USB device mode error. This event fires when the USB interface is in device mode,
352 * and an error occurs which prevents it from operating normally.
354 * \param ErrorCode Error code indicating the source of the error. One of the values in the
355 * \ref USB_Device_ErrorCodes_t enum located in Device.h.
357 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
358 * \ref Group_USBManagement documentation).
360 void USB_DeviceError(const uint8_t ErrorCode
);
363 /* Private Interface - For use in library only: */
364 #if !defined(__DOXYGEN__)
366 #define ALIAS_STUB(e) EVENT_HANDLER(e) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub)
368 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
369 #define USB_VBUSChange_P (void)
370 #define USB_VBUSConnect_P (void)
371 #define USB_VBUSDisconnect_P (void)
374 #define USB_Connect_P (void)
375 #define USB_Disconnect_P (void)
376 #define USB_DeviceEnumerationComplete_P (void)
378 #if defined(USB_CAN_BE_BOTH)
379 #define USB_InitFailure_P (const uint8_t ErrorCode)
380 #define USB_UIDChange_P (void)
383 #if defined(USB_CAN_BE_HOST)
384 #define USB_HostError_P (const uint8_t ErrorCode)
385 #define USB_DeviceAttached_P (void)
386 #define USB_DeviceUnattached_P (void)
387 #define USB_DeviceEnumerationFailed_P (const uint8_t ErrorCode, const uint8_t SubErrorCode)
390 #if defined(USB_CAN_BE_DEVICE)
391 #define USB_UnhandledControlPacket_P (void)
392 #define USB_ConfigurationChanged_P (void)
393 #define USB_Suspend_P (void)
394 #define USB_WakeUp_P (void)
395 #define USB_Reset_P (void)
396 #define USB_DeviceError_P (const uint8_t ErrorCode)
399 /* Function Prototypes: */
400 #if defined(INCLUDE_FROM_EVENTS_C)
401 void USB_Event_Stub(void) ATTR_CONST
;
403 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
404 ALIAS_STUB(USB_VBUSChange
);
405 ALIAS_STUB(USB_VBUSConnect
);
406 ALIAS_STUB(USB_VBUSDisconnect
);
409 ALIAS_STUB(USB_Connect
);
410 ALIAS_STUB(USB_Disconnect
);
411 ALIAS_STUB(USB_DeviceEnumerationComplete
);
413 #if defined(USB_CAN_BE_BOTH)
414 ALIAS_STUB(USB_InitFailure
);
415 ALIAS_STUB(USB_UIDChange
);
418 #if defined(USB_CAN_BE_HOST)
419 ALIAS_STUB(USB_HostError
);
420 ALIAS_STUB(USB_DeviceAttached
);
421 ALIAS_STUB(USB_DeviceUnattached
);
422 ALIAS_STUB(USB_DeviceEnumerationFailed
);
425 #if defined(USB_CAN_BE_DEVICE)
426 ALIAS_STUB(USB_UnhandledControlPacket
);
427 ALIAS_STUB(USB_ConfigurationChanged
);
428 ALIAS_STUB(USB_Suspend
);
429 ALIAS_STUB(USB_WakeUp
);
430 ALIAS_STUB(USB_Reset
);
431 ALIAS_STUB(USB_DeviceError
);
436 /* Disable C linkage for C++ Compilers: */
437 #if defined(__cplusplus)