3      Copyright (C) Dean Camera, 2014. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2014  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 disclaims 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 Event management definitions. 
  33  *  \copydetails Group_Events 
  35  *  \note This file should not be included directly. It is automatically included as needed by the USB driver 
  36  *        dispatch header located in LUFA/Drivers/USB/USB.h. 
  39 /** \ingroup Group_USB 
  40  *  \defgroup Group_Events USB Events 
  41  *  \brief USB Event management definitions. 
  43  *  This module contains macros and functions relating to the management of library events, which are small 
  44  *  pieces of code similar to ISRs which are run when a given condition is met. Each event can be fired from 
  45  *  multiple places in the user or library code, which may or may not be inside an ISR, thus each handler 
  46  *  should be written to be as small and fast as possible to prevent possible problems. 
  48  *  Events can be hooked by the user application by declaring a handler function with the same name and parameters 
  49  *  listed here. If an event with no user-associated handler is fired within the library, it by default maps to an 
  50  *  internal empty stub function. 
  52  *  Each event must only have one associated event handler, but can be raised by multiple sources by calling the 
  53  *  event handler function (with any required event parameters). 
  58 #ifndef __USBEVENTS_H__ 
  59 #define __USBEVENTS_H__ 
  62                 #include "../../../Common/Common.h" 
  65         /* Enable C linkage for C++ Compilers: */ 
  66                 #if defined(__cplusplus) 
  70         /* Preprocessor Checks: */ 
  71                 #if !defined(__INCLUDE_FROM_USB_DRIVER) 
  72                         #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead. 
  75         /* Public Interface - May be used in end-application: */ 
  76                 /* Pseudo-Functions for Doxygen: */ 
  77                 #if !defined(__INCLUDE_FROM_EVENTS_C) || defined(__DOXYGEN__) 
  78                         /** Event for USB mode pin level change. This event fires when the USB interface is set to dual role 
  79                          *  mode, and the UID pin level has changed to indicate a new mode (device or host). This event fires 
  80                          *  before the mode is switched to the newly indicated mode but after the \ref EVENT_USB_Device_Disconnect 
  81                          *  event has fired (if disconnected before the role change). 
  83                          *  \note This event only exists on microcontrollers that support dual role USB modes. 
  86                          *  \note This event does not exist if the \c USB_DEVICE_ONLY or \c USB_HOST_ONLY tokens have been supplied 
  87                          *        to the compiler (see \ref Group_USBManagement documentation). 
  89                         void EVENT_USB_UIDChange(void); 
  91                         /** Event for USB host error. This event fires when a hardware fault has occurred whilst the USB 
  92                          *  interface is in host mode. 
  94                          *  \param[in] ErrorCode  Error code indicating the failure reason, a value in \ref USB_Host_ErrorCodes_t. 
  96                          *  \note This event only exists on microcontrollers that supports USB host mode. 
  99                          *  \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see 
 100                          *        \ref Group_USBManagement documentation). 
 102                         void EVENT_USB_Host_HostError(const uint8_t ErrorCode
); 
 104                         /** Event for USB device attachment. This event fires when a the USB interface is in host mode, and 
 105                          *  a USB device has been connected to the USB interface. This is interrupt driven, thus fires before 
 106                          *  the standard \ref EVENT_USB_Device_Connect() event and so can be used to programmatically start the USB 
 107                          *  management task to reduce CPU consumption. 
 109                          *  \note This event only exists on microcontrollers that supports USB host mode. 
 112                          *  \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see 
 113                          *        \ref Group_USBManagement documentation). 
 115                          *  \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage. 
 117                         void EVENT_USB_Host_DeviceAttached(void); 
 119                         /** Event for USB device removal. This event fires when a the USB interface is in host mode, and 
 120                          *  a USB device has been removed the USB interface whether or not it has been enumerated. This 
 121                          *  can be used to programmatically stop the USB management task to reduce CPU consumption. 
 123                          *  \note This event only exists on microcontrollers that supports USB host mode. 
 126                          *  \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see 
 127                          *        \ref Group_USBManagement documentation). 
 129                          *  \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage. 
 131                         void EVENT_USB_Host_DeviceUnattached(void); 
 133                         /** Event for USB device enumeration failure. This event fires when a the USB interface is 
 134                          *  in host mode, and an attached USB device has failed to enumerate completely. 
 136                          *  \param[in] ErrorCode     Error code indicating the failure reason, a value in 
 137                          *                           \ref USB_Host_EnumerationErrorCodes_t. 
 139                          *  \param[in] SubErrorCode  Sub error code indicating the reason for failure - for example, if the 
 140                          *                           ErrorCode parameter indicates a control error, this will give the error 
 141                          *                           code returned by the \ref USB_Host_SendControlRequest() function. 
 143                          *  \note This event only exists on microcontrollers that supports USB host mode. 
 146                          *  \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see 
 147                          *        \ref Group_USBManagement documentation). 
 149                         void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, 
 150                                                                     const uint8_t SubErrorCode
); 
 152                         /** Event for USB device enumeration completion. This event fires when a the USB interface is 
 153                          *  in host mode and an attached USB device has been completely enumerated and is ready to be 
 154                          *  controlled by the user application. 
 156                          *  This event is time-critical; exceeding OS-specific delays within this event handler (typically of around 
 157                          *  1 second) when a transaction is waiting to be processed by the device will prevent break communications 
 158                          *  and cause the host to reset the USB bus. 
 160                          *  \note This event only exists on microcontrollers that supports USB host mode. 
 163                          *  \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see 
 164                          *        \ref Group_USBManagement documentation). 
 166                         void EVENT_USB_Host_DeviceEnumerationComplete(void); 
 168                         /** Event for USB Start Of Frame detection, when enabled. This event fires at the start of each USB 
 169                          *  frame, once per millisecond, and is synchronized to the USB bus. This can be used as an accurate 
 170                          *  millisecond timer source when the USB bus is not suspended while in host mode. 
 172                          *  This event is time-critical; it is run once per millisecond and thus long handlers will significantly 
 173                          *  degrade device performance. This event should only be enabled when needed to reduce device wake-ups. 
 175                          *  \note This event is not normally active - it must be manually enabled and disabled via the 
 176                          *        \ref USB_Host_EnableSOFEvents() and \ref USB_Host_DisableSOFEvents() commands after enumeration of 
 180                          *  \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see 
 181                          *        \ref Group_USBManagement documentation). 
 183                         void EVENT_USB_Host_StartOfFrame(void); 
 185                         /** Event for USB device connection. This event fires when the microcontroller is in USB Device mode 
 186                          *  and the device is connected to a USB host, beginning the enumeration process measured by a rising 
 187                          *  level on the microcontroller's VBUS sense pin. 
 189                          *  This event is time-critical; exceeding OS-specific delays within this event handler (typically of around 
 190                          *  two seconds) will prevent the device from enumerating correctly. 
 192                          *  \attention This event may fire multiple times during device enumeration on the microcontrollers with limited USB controllers 
 193                          *             if \c NO_LIMITED_CONTROLLER_CONNECT is not defined. 
 195                          *  \note For the microcontrollers with limited USB controller functionality, VBUS sensing is not available. 
 196                          *        this means that the current connection state is derived from the bus suspension and wake up events by default, 
 197                          *        which is not always accurate (host may suspend the bus while still connected). If the actual connection state 
 198                          *        needs to be determined, VBUS should be routed to an external pin, and the auto-detect behavior turned off by 
 199                          *        passing the \c NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection 
 200                          *        and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually. 
 203                          *  \see \ref Group_USBManagement for more information on the USB management task and reducing CPU usage. 
 205                         void EVENT_USB_Device_Connect(void); 
 207                         /** Event for USB device disconnection. This event fires when the microcontroller is in USB Device mode and the device is 
 208                          *  disconnected from a host, measured by a falling level on the microcontroller's VBUS sense pin. 
 210                          *  \attention This event may fire multiple times during device enumeration on the microcontrollers with limited USB controllers 
 211                          *             if \c NO_LIMITED_CONTROLLER_CONNECT is not defined. 
 213                          *  \note For the microcontrollers with limited USB controllers, VBUS sense is not available to the USB controller. 
 214                          *        this means that the current connection state is derived from the bus suspension and wake up events by default, 
 215                          *        which is not always accurate (host may suspend the bus while still connected). If the actual connection state 
 216                          *        needs to be determined, VBUS should be routed to an external pin, and the auto-detect behavior turned off by 
 217                          *        passing the \c NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection 
 218                          *        and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually. 
 221                          *  \see \ref Group_USBManagement for more information on the USB management task and reducing CPU usage. 
 223                         void EVENT_USB_Device_Disconnect(void); 
 225                         /** Event for control requests. This event fires when a the USB host issues a control request 
 226                          *  to the mandatory device control endpoint (of address 0). This may either be a standard 
 227                          *  request that the library may have a handler code for internally, or a class specific request 
 228                          *  issued to the device which must be handled appropriately. If a request is not processed in the 
 229                          *  user application via this event, it will be passed to the library for processing internally 
 230                          *  if a suitable handler exists. 
 232                          *  This event is time-critical; each packet within the request transaction must be acknowledged or 
 233                          *  sent within 50ms or the host will abort the transfer. 
 235                          *  The library internally handles all standard control requests with the exceptions of SYNC FRAME, 
 236                          *  SET DESCRIPTOR and SET INTERFACE. These and all other non-standard control requests will be left 
 237                          *  for the user to process via this event if desired. If not handled in the user application or by 
 238                          *  the library internally, unknown requests are automatically STALLed. 
 240                          *  \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see 
 241                          *        \ref Group_USBManagement documentation). 
 244                          *  \note Requests should be handled in the same manner as described in the USB 2.0 Specification, 
 245                          *        or appropriate class specification. In all instances, the library has already read the 
 246                          *        request SETUP parameters into the \ref USB_ControlRequest structure which should then be used 
 247                          *        by the application to determine how to handle the issued request. 
 249                         void EVENT_USB_Device_ControlRequest(void); 
 251                         /** Event for USB configuration number changed. This event fires when a the USB host changes the 
 252                          *  selected configuration number while in device mode. This event should be hooked in device 
 253                          *  applications to create the endpoints and configure the device for the selected configuration. 
 255                          *  This event is time-critical; exceeding OS-specific delays within this event handler (typically of around 
 256                          *  one second) will prevent the device from enumerating correctly. 
 258                          *  This event fires after the value of \ref USB_Device_ConfigurationNumber has been changed. 
 260                          *  \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see 
 261                          *        \ref Group_USBManagement documentation). 
 263                         void EVENT_USB_Device_ConfigurationChanged(void); 
 265                         /** Event for USB suspend. This event fires when a the USB host suspends the device by halting its 
 266                          *  transmission of Start Of Frame pulses to the device. This is generally hooked in order to move 
 267                          *  the device over to a low power state until the host wakes up the device. If the USB interface is 
 268                          *  enumerated with the \ref USB_OPT_AUTO_PLL option set, the library will automatically suspend the 
 269                          *  USB PLL before the event is fired to save power. 
 271                          *  \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see 
 272                          *        \ref Group_USBManagement documentation). 
 275                          *  \note This event does not exist on the microcontrollers with limited USB VBUS sensing abilities 
 276                          *        when the \c NO_LIMITED_CONTROLLER_CONNECT compile time token is not set - see 
 277                          *        \ref EVENT_USB_Device_Disconnect. 
 279                          *  \see \ref EVENT_USB_Device_WakeUp() event for accompanying Wake Up event. 
 281                         void EVENT_USB_Device_Suspend(void); 
 283                         /** Event for USB wake up. This event fires when a the USB interface is suspended while in device 
 284                          *  mode, and the host wakes up the device by supplying Start Of Frame pulses. This is generally 
 285                          *  hooked to pull the user application out of a low power state and back into normal operating 
 286                          *  mode. If the USB interface is enumerated with the \ref USB_OPT_AUTO_PLL option set, the library 
 287                          *  will automatically restart the USB PLL before the event is fired. 
 289                          *  \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see 
 290                          *        \ref Group_USBManagement documentation). 
 293                          *  \note This event does not exist on the microcontrollers with limited USB VBUS sensing abilities 
 294                          *        when the \c NO_LIMITED_CONTROLLER_CONNECT compile time token is not set - see 
 295                          *        \ref EVENT_USB_Device_Disconnect. 
 297                          *  \see \ref EVENT_USB_Device_Suspend() event for accompanying Suspend event. 
 299                         void EVENT_USB_Device_WakeUp(void); 
 301                         /** Event for USB interface reset. This event fires when the USB interface is in device mode, and 
 302                          *  a the USB host requests that the device reset its interface. This event fires after the control 
 303                          *  endpoint has been automatically configured by the library. 
 305                          *  This event is time-critical; exceeding OS-specific delays within this event handler (typically of around 
 306                          *  two seconds) will prevent the device from enumerating correctly. 
 308                          *  \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see 
 309                          *        \ref Group_USBManagement documentation). 
 311                         void EVENT_USB_Device_Reset(void); 
 313                         /** Event for USB Start Of Frame detection, when enabled. This event fires at the start of each USB 
 314                          *  frame, once per millisecond, and is synchronized to the USB bus. This can be used as an accurate 
 315                          *  millisecond timer source when the USB bus is enumerated in device mode to a USB host. 
 317                          *  This event is time-critical; it is run once per millisecond and thus long handlers will significantly 
 318                          *  degrade device performance. This event should only be enabled when needed to reduce device wake-ups. 
 320                          *  \pre This event is not normally active - it must be manually enabled and disabled via the 
 321                          *       \ref USB_Device_EnableSOFEvents() and \ref USB_Device_DisableSOFEvents() commands after enumeration. 
 324                          *  \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see 
 325                          *        \ref Group_USBManagement documentation). 
 327                         void EVENT_USB_Device_StartOfFrame(void); 
 330         /* Private Interface - For use in library only: */ 
 331         #if !defined(__DOXYGEN__) 
 332                 /* Function Prototypes: */ 
 333                         #if defined(__INCLUDE_FROM_EVENTS_C) 
 334                                 void USB_Event_Stub(void) ATTR_CONST
; 
 336                                 #if defined(USB_CAN_BE_BOTH) 
 337                                         void EVENT_USB_UIDChange(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 340                                 #if defined(USB_CAN_BE_HOST) 
 341                                         void EVENT_USB_Host_HostError(const uint8_t ErrorCode
) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 342                                         void EVENT_USB_Host_DeviceAttached(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 343                                         void EVENT_USB_Host_DeviceUnattached(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 344                                         void EVENT_USB_Host_DeviceEnumerationComplete(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 345                                         void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, 
 346                                                                 const uint8_t SubErrorCode
) 
 347                                                                                     ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 348                                         void EVENT_USB_Host_StartOfFrame(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 351                                 #if defined(USB_CAN_BE_DEVICE) 
 352                                         void EVENT_USB_Device_Connect(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 353                                         void EVENT_USB_Device_Disconnect(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 354                                         void EVENT_USB_Device_ControlRequest(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 355                                         void EVENT_USB_Device_ConfigurationChanged(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 356                                         void EVENT_USB_Device_Suspend(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 357                                         void EVENT_USB_Device_WakeUp(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 358                                         void EVENT_USB_Device_Reset(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 359                                         void EVENT_USB_Device_StartOfFrame(void) ATTR_WEAK 
ATTR_ALIAS(USB_Event_Stub
); 
 364         /* Disable C linkage for C++ Compilers: */ 
 365                 #if defined(__cplusplus)