Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Events.h 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. * \brief USB Event management definitions.
  28. * \copydetails Group_Events
  29. *
  30. * \note This file should not be included directly. It is automatically included as needed by the USB driver
  31. * dispatch header located in LUFA/Drivers/USB/USB.h.
  32. */
  33. /** \ingroup Group_USB
  34. * \defgroup Group_Events USB Events
  35. * \brief USB Event management definitions.
  36. *
  37. * This module contains macros and functions relating to the management of library events, which are small
  38. * pieces of code similar to ISRs which are run when a given condition is met. Each event can be fired from
  39. * multiple places in the user or library code, which may or may not be inside an ISR, thus each handler
  40. * should be written to be as small and fast as possible to prevent possible problems.
  41. *
  42. * Events can be hooked by the user application by declaring a handler function with the same name and parameters
  43. * listed here. If an event with no user-associated handler is fired within the library, it by default maps to an
  44. * internal empty stub function.
  45. *
  46. * Each event must only have one associated event handler, but can be raised by multiple sources by calling the
  47. * event handler function (with any required event parameters).
  48. *
  49. * @{
  50. */
  51. #ifndef __USBEVENTS_H__
  52. #define __USBEVENTS_H__
  53. /* Includes: */
  54. #include "../../../Common/Common.h"
  55. #include "USBMode.h"
  56. /* Enable C linkage for C++ Compilers: */
  57. #if defined(__cplusplus)
  58. extern "C" {
  59. #endif
  60. /* Preprocessor Checks: */
  61. #if !defined(__INCLUDE_FROM_USB_DRIVER)
  62. #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
  63. #endif
  64. /* Public Interface - May be used in end-application: */
  65. /* Pseudo-Functions for Doxygen: */
  66. #if !defined(__INCLUDE_FROM_EVENTS_C) || defined(__DOXYGEN__)
  67. /** Event for USB mode pin level change. This event fires when the USB interface is set to dual role
  68. * mode, and the UID pin level has changed to indicate a new mode (device or host). This event fires
  69. * before the mode is switched to the newly indicated mode but after the \ref EVENT_USB_Device_Disconnect
  70. * event has fired (if disconnected before the role change).
  71. *
  72. * \note This event only exists on microcontrollers that support dual role USB modes.
  73. * \n\n
  74. *
  75. * \note This event does not exist if the \c USB_DEVICE_ONLY or \c USB_HOST_ONLY tokens have been supplied
  76. * to the compiler (see \ref Group_USBManagement documentation).
  77. */
  78. void EVENT_USB_UIDChange(void);
  79. /** Event for USB host error. This event fires when a hardware fault has occurred whilst the USB
  80. * interface is in host mode.
  81. *
  82. * \param[in] ErrorCode Error code indicating the failure reason, a value in \ref USB_Host_ErrorCodes_t.
  83. *
  84. * \note This event only exists on microcontrollers that supports USB host mode.
  85. * \n\n
  86. *
  87. * \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see
  88. * \ref Group_USBManagement documentation).
  89. */
  90. void EVENT_USB_Host_HostError(const uint8_t ErrorCode);
  91. /** Event for USB device attachment. This event fires when a the USB interface is in host mode, and
  92. * a USB device has been connected to the USB interface. This is interrupt driven, thus fires before
  93. * the standard \ref EVENT_USB_Device_Connect() event and so can be used to programmatically start the USB
  94. * management task to reduce CPU consumption.
  95. *
  96. * \note This event only exists on microcontrollers that supports USB host mode.
  97. * \n\n
  98. *
  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).
  101. *
  102. * \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage.
  103. */
  104. void EVENT_USB_Host_DeviceAttached(void);
  105. /** Event for USB device removal. This event fires when a the USB interface is in host mode, and
  106. * a USB device has been removed the USB interface whether or not it has been enumerated. This
  107. * can be used to programmatically stop the USB management task to reduce CPU consumption.
  108. *
  109. * \note This event only exists on microcontrollers that supports USB host mode.
  110. * \n\n
  111. *
  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).
  114. *
  115. * \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage.
  116. */
  117. void EVENT_USB_Host_DeviceUnattached(void);
  118. /** Event for USB device enumeration failure. This event fires when a the USB interface is
  119. * in host mode, and an attached USB device has failed to enumerate completely.
  120. *
  121. * \param[in] ErrorCode Error code indicating the failure reason, a value in
  122. * \ref USB_Host_EnumerationErrorCodes_t.
  123. *
  124. * \param[in] SubErrorCode Sub error code indicating the reason for failure - for example, if the
  125. * ErrorCode parameter indicates a control error, this will give the error
  126. * code returned by the \ref USB_Host_SendControlRequest() function.
  127. *
  128. * \note This event only exists on microcontrollers that supports USB host mode.
  129. * \n\n
  130. *
  131. * \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see
  132. * \ref Group_USBManagement documentation).
  133. */
  134. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  135. const uint8_t SubErrorCode);
  136. /** Event for USB device enumeration completion. This event fires when a the USB interface is
  137. * in host mode and an attached USB device has been completely enumerated and is ready to be
  138. * controlled by the user application.
  139. *
  140. * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
  141. * 1 second) when a transaction is waiting to be processed by the device will prevent break communications
  142. * and cause the host to reset the USB bus.
  143. *
  144. * \note This event only exists on microcontrollers that supports USB host mode.
  145. * \n\n
  146. *
  147. * \note This event does not exist if the \c USB_DEVICE_ONLY token is supplied to the compiler (see
  148. * \ref Group_USBManagement documentation).
  149. */
  150. void EVENT_USB_Host_DeviceEnumerationComplete(void);
  151. /** Event for USB Start Of Frame detection, when enabled. This event fires at the start of each USB
  152. * frame, once per millisecond, and is synchronized to the USB bus. This can be used as an accurate
  153. * millisecond timer source when the USB bus is not suspended while in host mode.
  154. *
  155. * This event is time-critical; it is run once per millisecond and thus long handlers will significantly
  156. * degrade device performance. This event should only be enabled when needed to reduce device wake-ups.
  157. *
  158. * \note This event is not normally active - it must be manually enabled and disabled via the
  159. * \ref USB_Host_EnableSOFEvents() and \ref USB_Host_DisableSOFEvents() commands after enumeration of
  160. * a USB device.
  161. * \n\n
  162. *
  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).
  165. */
  166. void EVENT_USB_Host_StartOfFrame(void);
  167. /** Event for USB device connection. This event fires when the microcontroller is in USB Device mode
  168. * and the device is connected to a USB host, beginning the enumeration process measured by a rising
  169. * level on the microcontroller's VBUS sense pin.
  170. *
  171. * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
  172. * two seconds) will prevent the device from enumerating correctly.
  173. *
  174. * \attention This event may fire multiple times during device enumeration on the microcontrollers with limited USB controllers
  175. * if \c NO_LIMITED_CONTROLLER_CONNECT is not defined.
  176. *
  177. * \note For the microcontrollers with limited USB controller functionality, VBUS sensing is not available.
  178. * this means that the current connection state is derived from the bus suspension and wake up events by default,
  179. * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
  180. * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behavior turned off by
  181. * passing the \c NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
  182. * and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually.
  183. * \n\n
  184. *
  185. * \see \ref Group_USBManagement for more information on the USB management task and reducing CPU usage.
  186. */
  187. void EVENT_USB_Device_Connect(void);
  188. /** Event for USB device disconnection. This event fires when the microcontroller is in USB Device mode and the device is
  189. * disconnected from a host, measured by a falling level on the microcontroller's VBUS sense pin.
  190. *
  191. * \attention This event may fire multiple times during device enumeration on the microcontrollers with limited USB controllers
  192. * if \c NO_LIMITED_CONTROLLER_CONNECT is not defined.
  193. *
  194. * \note For the microcontrollers with limited USB controllers, VBUS sense 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 behavior turned off by
  198. * passing the \c 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, and the \ref USB_DeviceState global changed manually.
  200. * \n\n
  201. *
  202. * \see \ref Group_USBManagement for more information on the USB management task and reducing CPU usage.
  203. */
  204. void EVENT_USB_Device_Disconnect(void);
  205. /** Event for control requests. This event fires when a the USB host issues a control request
  206. * to the mandatory device control endpoint (of address 0). This may either be a standard
  207. * request that the library may have a handler code for internally, or a class specific request
  208. * issued to the device which must be handled appropriately. If a request is not processed in the
  209. * user application via this event, it will be passed to the library for processing internally
  210. * if a suitable handler exists.
  211. *
  212. * This event is time-critical; each packet within the request transaction must be acknowledged or
  213. * sent within 50ms or the host will abort the transfer.
  214. *
  215. * The library internally handles all standard control requests with the exceptions of SYNC FRAME,
  216. * SET DESCRIPTOR and SET INTERFACE. These and all other non-standard control requests will be left
  217. * for the user to process via this event if desired. If not handled in the user application or by
  218. * the library internally, unknown requests are automatically STALLed.
  219. *
  220. * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
  221. * \ref Group_USBManagement documentation).
  222. * \n\n
  223. *
  224. * \note Requests should be handled in the same manner as described in the USB 2.0 Specification,
  225. * or appropriate class specification. In all instances, the library has already read the
  226. * request SETUP parameters into the \ref USB_ControlRequest structure which should then be used
  227. * by the application to determine how to handle the issued request.
  228. */
  229. void EVENT_USB_Device_ControlRequest(void);
  230. /** Event for USB configuration number changed. This event fires when a the USB host changes the
  231. * selected configuration number while in device mode. This event should be hooked in device
  232. * applications to create the endpoints and configure the device for the selected configuration.
  233. *
  234. * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
  235. * one second) will prevent the device from enumerating correctly.
  236. *
  237. * This event fires after the value of \ref USB_Device_ConfigurationNumber has been changed.
  238. *
  239. * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
  240. * \ref Group_USBManagement documentation).
  241. */
  242. void EVENT_USB_Device_ConfigurationChanged(void);
  243. /** Event for USB suspend. This event fires when a the USB host suspends the device by halting its
  244. * transmission of Start Of Frame pulses to the device. This is generally hooked in order to move
  245. * the device over to a low power state until the host wakes up the device. If the USB interface is
  246. * enumerated with the \ref USB_OPT_AUTO_PLL option set, the library will automatically suspend the
  247. * USB PLL before the event is fired to save power.
  248. *
  249. * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
  250. * \ref Group_USBManagement documentation).
  251. * \n\n
  252. *
  253. * \note This event does not exist on the microcontrollers with limited USB VBUS sensing abilities
  254. * when the \c NO_LIMITED_CONTROLLER_CONNECT compile time token is not set - see
  255. * \ref EVENT_USB_Device_Disconnect.
  256. *
  257. * \see \ref EVENT_USB_Device_WakeUp() event for accompanying Wake Up event.
  258. */
  259. void EVENT_USB_Device_Suspend(void);
  260. /** Event for USB wake up. This event fires when a the USB interface is suspended while in device
  261. * mode, and the host wakes up the device by supplying Start Of Frame pulses. This is generally
  262. * hooked to pull the user application out of a low power state and back into normal operating
  263. * mode. If the USB interface is enumerated with the \ref USB_OPT_AUTO_PLL option set, the library
  264. * will automatically restart the USB PLL before the event is fired.
  265. *
  266. * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
  267. * \ref Group_USBManagement documentation).
  268. * \n\n
  269. *
  270. * \note This event does not exist on the microcontrollers with limited USB VBUS sensing abilities
  271. * when the \c NO_LIMITED_CONTROLLER_CONNECT compile time token is not set - see
  272. * \ref EVENT_USB_Device_Disconnect.
  273. *
  274. * \see \ref EVENT_USB_Device_Suspend() event for accompanying Suspend event.
  275. */
  276. void EVENT_USB_Device_WakeUp(void);
  277. /** Event for USB interface reset. This event fires when the USB interface is in device mode, and
  278. * a the USB host requests that the device reset its interface. This event fires after the control
  279. * endpoint has been automatically configured by the library.
  280. *
  281. * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
  282. * two seconds) will prevent the device from enumerating correctly.
  283. *
  284. * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
  285. * \ref Group_USBManagement documentation).
  286. */
  287. void EVENT_USB_Device_Reset(void);
  288. /** Event for USB Start Of Frame detection, when enabled. This event fires at the start of each USB
  289. * frame, once per millisecond, and is synchronized to the USB bus. This can be used as an accurate
  290. * millisecond timer source when the USB bus is enumerated in device mode to a USB host.
  291. *
  292. * This event is time-critical; it is run once per millisecond and thus long handlers will significantly
  293. * degrade device performance. This event should only be enabled when needed to reduce device wake-ups.
  294. *
  295. * \pre This event is not normally active - it must be manually enabled and disabled via the
  296. * \ref USB_Device_EnableSOFEvents() and \ref USB_Device_DisableSOFEvents() commands after enumeration.
  297. * \n\n
  298. *
  299. * \note This event does not exist if the \c USB_HOST_ONLY token is supplied to the compiler (see
  300. * \ref Group_USBManagement documentation).
  301. */
  302. void EVENT_USB_Device_StartOfFrame(void);
  303. #endif
  304. /* Private Interface - For use in library only: */
  305. #if !defined(__DOXYGEN__)
  306. /* Function Prototypes: */
  307. #if defined(__INCLUDE_FROM_EVENTS_C)
  308. void USB_Event_Stub(void) ATTR_CONST;
  309. #if defined(USB_CAN_BE_BOTH)
  310. void EVENT_USB_UIDChange(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  311. #endif
  312. #if defined(USB_CAN_BE_HOST)
  313. void EVENT_USB_Host_HostError(const uint8_t ErrorCode) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  314. void EVENT_USB_Host_DeviceAttached(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  315. void EVENT_USB_Host_DeviceUnattached(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  316. void EVENT_USB_Host_DeviceEnumerationComplete(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  317. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  318. const uint8_t SubErrorCode)
  319. ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  320. void EVENT_USB_Host_StartOfFrame(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  321. #endif
  322. #if defined(USB_CAN_BE_DEVICE)
  323. void EVENT_USB_Device_Connect(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  324. void EVENT_USB_Device_Disconnect(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  325. void EVENT_USB_Device_ControlRequest(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  326. void EVENT_USB_Device_ConfigurationChanged(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  327. void EVENT_USB_Device_Suspend(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  328. void EVENT_USB_Device_WakeUp(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  329. void EVENT_USB_Device_Reset(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  330. void EVENT_USB_Device_StartOfFrame(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
  331. #endif
  332. #endif
  333. #endif
  334. /* Disable C linkage for C++ Compilers: */
  335. #if defined(__cplusplus)
  336. }
  337. #endif
  338. #endif
  339. /** @} */