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.
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 USB stack initialization failure. This event fires when the USB interface fails to
66 * initialize correctly due to a hardware or software fault.
68 * \note This event only exists on USB AVR models which support dual role modes.
70 * \param[in] ErrorCode Error code indicating the failure reason, a value in \ref USB_InitErrorCodes_t
72 void EVENT_USB_InitFailure(const uint8_t ErrorCode
);
74 /** Event for USB mode pin level change. This event fires when the USB interface is set to dual role
75 * mode, and the UID pin level has changed to indicate a new mode (device or host). This event fires
76 * before the mode is switched to the newly indicated mode but after the \ref EVENT_USB_Device_Disconnect
77 * event has fired (if connected before the role change).
79 * \note This event only exists on USB AVR models which support dual role modes.
81 * \note This event does not exist if the USB_DEVICE_ONLY or USB_HOST_ONLY tokens have been supplied
82 * to the compiler (see \ref Group_USBManagement documentation).
84 void EVENT_USB_UIDChange(void);
86 /** Event for USB host error. This event fires when a hardware fault has occurred whilst the USB
87 * interface is in host mode.
89 * \param[in] ErrorCode Error code indicating the failure reason, a value in \ref USB_Host_ErrorCodes_t
91 * \note This event only exists on USB AVR models which supports host mode.
93 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
94 * \ref Group_USBManagement documentation).
96 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
);
98 /** Event for USB device attachment. This event fires when a the USB interface is in host mode, and
99 * a USB device has been connected to the USB interface. This is interrupt driven, thus fires before
100 * the standard \ref EVENT_USB_Device_Connect() event and so can be used to programmatically start the USB
101 * management task to reduce CPU consumption.
103 * \note This event only exists on USB AVR models which supports host mode.
105 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
106 * \ref Group_USBManagement documentation).
108 * \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage.
110 void EVENT_USB_Host_DeviceAttached(void);
112 /** Event for USB device removal. This event fires when a the USB interface is in host mode, and
113 * a USB device has been removed the USB interface whether or not it has been enumerated. This
114 * can be used to programmatically stop the USB management task to reduce CPU consumption.
116 * \note This event only exists on USB AVR models which supports host mode.
118 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
119 * \ref Group_USBManagement documentation).
121 * \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage.
123 void EVENT_USB_Host_DeviceUnattached(void);
125 /** Event for USB device enumeration failure. This event fires when a the USB interface is
126 * in host mode, and an attached USB device has failed to enumerate completely.
128 * \param[in] ErrorCode Error code indicating the failure reason, a value in
129 * \ref USB_Host_EnumerationErrorCodes_t
131 * \param[in] SubErrorCode Sub error code indicating the reason for failure - for example, if the
132 * ErrorCode parameter indicates a control error, this will give the error
133 * code returned by the \ref USB_Host_SendControlRequest() function.
135 * \note This event only exists on USB AVR models which supports host mode.
137 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
138 * \ref Group_USBManagement documentation).
140 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
);
142 /** Event for USB device enumeration completion. This event fires when a the USB interface is
143 * in host mode and an attached USB device has been completely enumerated and is ready to be
144 * controlled by the user application.
146 void EVENT_USB_Host_DeviceEnumerationComplete(void);
148 /** Event for USB device connection. This event fires when the AVR in device mode and the device is connected
149 * to a host, beginning the enumeration process, measured by a rising level on the AVR's VBUS pin.
151 * \note For the smaller USB AVRs (AT90USBXX2) with limited USB controllers, VBUS is not available to the USB controller.
152 * this means that the current connection state is derived from the bus suspension and wake up events by default,
153 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
154 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
155 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
156 * and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually.
158 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
160 void EVENT_USB_Device_Connect(void);
162 /** Event for USB device disconnection. This event fires when the AVR in device mode and the device is disconnected
163 * from a host, measured by a falling level on the AVR's VBUS pin.
165 * \note For the smaller USB AVRs (AT90USBXX2) with limited USB controllers, VBUS is not available to the USB controller.
166 * this means that the current connection state is derived from the bus suspension and wake up events by default,
167 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
168 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
169 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
170 * and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually.
172 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
174 void EVENT_USB_Device_Disconnect(void);
176 /** Event for unhandled control requests. This event fires when a the USB host issues a control
177 * request to the control endpoint (address 0) that the library does not handle. This may either
178 * be a standard request that the library has no handler code for, or a class specific request
179 * issued to the device which must be handled appropriately. Due to the strict timing requirements
180 * on control transfers, interrupts are disabled during control request processing.
182 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
183 * \ref Group_USBManagement documentation).
185 * \note Requests should be handled in the same manner as described in the USB 2.0 Specification,
186 * or appropriate class specification. In all instances, the library has already read the
187 * request SETUP parameters into the \ref USB_ControlRequest structure which should then be used
188 * by the application to determine how to handle the issued request.
190 void EVENT_USB_Device_UnhandledControlRequest(void);
192 /** Event for USB configuration number changed. This event fires when a the USB host changes the
193 * selected configuration number while in device mode. This event should be hooked in device
194 * applications to create the endpoints and configure the device for the selected configuration.
196 * This event fires after the value of \ref USB_ConfigurationNumber has been changed.
198 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
199 * \ref Group_USBManagement documentation).
201 void EVENT_USB_Device_ConfigurationChanged(void);
203 /** Event for USB suspend. This event fires when a the USB host suspends the device by halting its
204 * transmission of Start Of Frame pulses to the device. This is generally hooked in order to move
205 * the device over to a low power state until the host wakes up the device. If the USB interface is
206 * enumerated with the \ref USB_OPT_AUTO_PLL option set, the library will automatically suspend the
207 * USB PLL before the event is fired to save power.
209 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
210 * \ref Group_USBManagement documentation).
212 * \see \ref EVENT_USB_Device_WakeUp() event for accompanying Wake Up event.
214 void EVENT_USB_Device_Suspend(void);
216 /** Event for USB wake up. This event fires when a the USB interface is suspended while in device
217 * mode, and the host wakes up the device by supplying Start Of Frame pulses. This is generally
218 * hooked to pull the user application out of a lowe power state and back into normal operating
219 * mode. If the USB interface is enumerated with the \ref USB_OPT_AUTO_PLL option set, the library
220 * will automatically restart the USB PLL before the event is fired.
222 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
223 * \ref Group_USBManagement documentation).
225 * \see \ref EVENT_USB_Device_Suspend() event for accompanying Suspend event.
227 void EVENT_USB_Device_WakeUp(void);
229 /** Event for USB interface reset. This event fires when the USB interface is in device mode, and
230 * a the USB host requests that the device reset its interface. This event fires after the control
231 * endpoint has been automatically configured by the library.
233 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
234 * \ref Group_USBManagement documentation).
236 void EVENT_USB_Device_Reset(void);
238 /** Event for USB Start Of Frame detection, when enabled. This event fires at the start of each USB
239 * frame, once per millisecond, and is synchronised to the USB bus. This can be used as an accurate
240 * millisecond timer source when the USB bus is enumerated in device mode to a USB host.
242 * This event is not normally active - it must be manually enabled and disabled via the
243 * \ref USB_Device_EnableSOFEvents() and \ref USB_Device_DisableSOFEvents() commands after enumeration.
245 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
246 * \ref Group_USBManagement documentation).
248 void EVENT_USB_Device_StartOfFrame(void);
251 /* Private Interface - For use in library only: */
252 #if !defined(__DOXYGEN__)
253 /* Function Prototypes: */
254 #if defined(INCLUDE_FROM_EVENTS_C)
255 void USB_Event_Stub(void) ATTR_CONST
;
257 #if defined(USB_CAN_BE_BOTH)
258 void EVENT_USB_InitFailure(const uint8_t ErrorCode
) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
259 void EVENT_USB_UIDChange(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
262 #if defined(USB_CAN_BE_HOST)
263 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
264 void EVENT_USB_Host_DeviceAttached(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
265 void EVENT_USB_Host_DeviceUnattached(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
266 void EVENT_USB_Host_DeviceEnumerationComplete(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
267 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
)
268 ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
271 #if defined(USB_CAN_BE_DEVICE)
272 void EVENT_USB_Device_Connect(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
273 void EVENT_USB_Device_Disconnect(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
274 void EVENT_USB_Device_UnhandledControlRequest(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
275 void EVENT_USB_Device_ConfigurationChanged(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
276 void EVENT_USB_Device_Suspend(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
277 void EVENT_USB_Device_WakeUp(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
278 void EVENT_USB_Device_Reset(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
279 void EVENT_USB_Device_StartOfFrame(void) ATTR_WEAK
ATTR_ALIAS(USB_Event_Stub
);
284 /* Disable C linkage for C++ Compilers: */
285 #if defined(__cplusplus)