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.

USBTask.h 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 Main USB service task management.
  28. *
  29. * This file contains the function definitions required for the main USB service task, which must be called
  30. * from the user application to ensure that the USB connection to or from a connected USB device is maintained.
  31. *
  32. * \note This file should not be included directly. It is automatically included as needed by the USB driver
  33. * dispatch header located in LUFA/Drivers/USB/USB.h.
  34. */
  35. #ifndef __USBTASK_H__
  36. #define __USBTASK_H__
  37. /* Includes: */
  38. #include "../../../Common/Common.h"
  39. #include "USBMode.h"
  40. #include "USBController.h"
  41. #include "Events.h"
  42. #include "StdRequestType.h"
  43. #include "StdDescriptors.h"
  44. #if defined(USB_CAN_BE_DEVICE)
  45. #include "DeviceStandardReq.h"
  46. #endif
  47. #if defined(USB_CAN_BE_HOST)
  48. #include "HostStandardReq.h"
  49. #endif
  50. /* Enable C linkage for C++ Compilers: */
  51. #if defined(__cplusplus)
  52. extern "C" {
  53. #endif
  54. /* Preprocessor Checks: */
  55. #if !defined(__INCLUDE_FROM_USB_DRIVER)
  56. #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
  57. #endif
  58. /* Public Interface - May be used in end-application: */
  59. /* Global Variables: */
  60. /** Indicates if the USB interface is currently initialized but not necessarily connected to a host
  61. * or device (i.e. if \ref USB_Init() has been run). If this is false, all other library globals related
  62. * to the USB driver are invalid.
  63. *
  64. * \attention This variable should be treated as read-only in the user application, and never manually
  65. * changed in value.
  66. *
  67. * \ingroup Group_USBManagement
  68. */
  69. extern volatile bool USB_IsInitialized;
  70. /** Structure containing the last received Control request when in Device mode (for use in user-applications
  71. * inside of the \ref EVENT_USB_Device_ControlRequest() event, or for filling up with a control request to
  72. * issue when in Host mode before calling \ref USB_Host_SendControlRequest().
  73. *
  74. * \note The contents of this structure is automatically endian-corrected for the current CPU architecture.
  75. *
  76. * \ingroup Group_USBManagement
  77. */
  78. extern USB_Request_Header_t USB_ControlRequest;
  79. #if defined(USB_CAN_BE_HOST) || defined(__DOXYGEN__)
  80. #if !defined(HOST_STATE_AS_GPIOR) || defined(__DOXYGEN__)
  81. /** Indicates the current host state machine state. When in host mode, this indicates the state
  82. * via one of the values of the \ref USB_Host_States_t enum values.
  83. *
  84. * This value should not be altered by the user application as it is handled automatically by the
  85. * library.
  86. *
  87. * To reduce program size and speed up checks of this global on the AVR8 architecture, it can be
  88. * placed into one of the AVR's \c GPIOR hardware registers instead of RAM by defining the
  89. * \c HOST_STATE_AS_GPIOR token to a value between 0 and 2 in the project makefile and passing it to
  90. * the compiler via the -D switch. When defined, the corresponding GPIOR register should not be used
  91. * in the user application except implicitly via the library APIs.
  92. *
  93. * \note This global is only present if the user application can be a USB host.
  94. *
  95. * \see \ref USB_Host_States_t for a list of possible device states.
  96. *
  97. * \ingroup Group_Host
  98. */
  99. extern volatile uint8_t USB_HostState;
  100. #else
  101. #define USB_HostState CONCAT_EXPANDED(GPIOR, HOST_STATE_AS_GPIOR)
  102. #endif
  103. #endif
  104. #if defined(USB_CAN_BE_DEVICE) || defined(__DOXYGEN__)
  105. #if !defined(DEVICE_STATE_AS_GPIOR) || defined(__DOXYGEN__)
  106. /** Indicates the current device state machine state. When in device mode, this indicates the state
  107. * via one of the values of the \ref USB_Device_States_t enum values.
  108. *
  109. * This value should not be altered by the user application as it is handled automatically by the
  110. * library. The only exception to this rule is if the NO_LIMITED_CONTROLLER_CONNECT token is used
  111. * (see \ref EVENT_USB_Device_Connect() and \ref EVENT_USB_Device_Disconnect() events).
  112. *
  113. * To reduce program size and speed up checks of this global on the AVR8 architecture, it can be
  114. * placed into one of the AVR's \c GPIOR hardware registers instead of RAM by defining the
  115. * \c DEVICE_STATE_AS_GPIOR token to a value between 0 and 2 in the project makefile and passing it to
  116. * the compiler via the -D switch. When defined, the corresponding GPIOR register should not be used
  117. * in the user application except implicitly via the library APIs.
  118. *
  119. * \attention This variable should be treated as read-only in the user application, and never manually
  120. * changed in value except in the circumstances outlined above.
  121. *
  122. * \note This global is only present if the user application can be a USB device.
  123. * \n\n
  124. *
  125. * \see \ref USB_Device_States_t for a list of possible device states.
  126. *
  127. * \ingroup Group_Device
  128. */
  129. extern volatile uint8_t USB_DeviceState;
  130. #else
  131. #define USB_DeviceState CONCAT_EXPANDED(GPIOR, DEVICE_STATE_AS_GPIOR)
  132. #endif
  133. #endif
  134. /* Function Prototypes: */
  135. /** This is the main USB management task. The USB driver requires this task to be executed
  136. * continuously when the USB system is active (device attached in host mode, or attached to a host
  137. * in device mode) in order to manage USB communications. This task may be executed inside an RTOS,
  138. * fast timer ISR or the main user application loop.
  139. *
  140. * The USB task must be serviced within 30ms while in device mode, or within 1ms while in host mode.
  141. * The task may be serviced at all times, or (for minimum CPU consumption):
  142. *
  143. * - In device mode, it may be disabled at start-up, enabled on the firing of the \ref EVENT_USB_Device_Connect()
  144. * event and disabled again on the firing of the \ref EVENT_USB_Device_Disconnect() event.
  145. *
  146. * - In host mode, it may be disabled at start-up, enabled on the firing of the \ref EVENT_USB_Host_DeviceAttached()
  147. * event and disabled again on the firing of the \ref EVENT_USB_Host_DeviceEnumerationComplete() or
  148. * \ref EVENT_USB_Host_DeviceEnumerationFailed() events.
  149. *
  150. * If in device mode (only), the control endpoint can instead be managed via interrupts entirely by the library
  151. * by defining the INTERRUPT_CONTROL_ENDPOINT token and passing it to the compiler via the -D switch.
  152. *
  153. * \see \ref Group_Events for more information on the USB events.
  154. *
  155. * \ingroup Group_USBManagement
  156. */
  157. void USB_USBTask(void);
  158. /* Private Interface - For use in library only: */
  159. #if !defined(__DOXYGEN__)
  160. /* Function Prototypes: */
  161. #if defined(__INCLUDE_FROM_USBTASK_C)
  162. #if defined(USB_CAN_BE_HOST)
  163. static void USB_HostTask(void);
  164. #endif
  165. #if defined(USB_CAN_BE_DEVICE)
  166. static void USB_DeviceTask(void);
  167. #endif
  168. #endif
  169. /* Macros: */
  170. #define HOST_TASK_NONBLOCK_WAIT(Duration, NextState) do { USB_HostState = HOST_STATE_WaitForDevice; \
  171. WaitMSRemaining = (Duration); \
  172. PostWaitState = (NextState); } while (0)
  173. #endif
  174. /* Disable C linkage for C++ Compilers: */
  175. #if defined(__cplusplus)
  176. }
  177. #endif
  178. #endif