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.

Host_UC3.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 Host definitions for the AVR32 UC3B microcontrollers.
  28. * \copydetails Group_Host_UC3B
  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_Host
  34. * \defgroup Group_Host_UC3B Host Management (UC3B)
  35. * \brief USB Host definitions for the AVR32 UC3B microcontrollers.
  36. *
  37. * Architecture specific USB Host definitions for the Atmel 32-bit AVR UC3B microcontrollers.
  38. *
  39. * @{
  40. */
  41. #ifndef __USBHOST_UC3B_H__
  42. #define __USBHOST_UC3B_H__
  43. /* Includes: */
  44. #include "../../../../Common/Common.h"
  45. #include "../StdDescriptors.h"
  46. #include "../Pipe.h"
  47. #include "../USBInterrupt.h"
  48. /* Enable C linkage for C++ Compilers: */
  49. #if defined(__cplusplus)
  50. extern "C" {
  51. #endif
  52. /* Preprocessor Checks: */
  53. #if !defined(__INCLUDE_FROM_USB_DRIVER)
  54. #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
  55. #endif
  56. /* Public Interface - May be used in end-application: */
  57. /* Macros: */
  58. /** Indicates the fixed USB device address which any attached device is enumerated to when in
  59. * host mode. As only one USB device may be attached to the AVR in host mode at any one time
  60. * and that the address used is not important (other than the fact that it is non-zero), a
  61. * fixed value is specified by the library.
  62. */
  63. #define USB_HOST_DEVICEADDRESS 1
  64. #if !defined(HOST_DEVICE_SETTLE_DELAY_MS) || defined(__DOXYGEN__)
  65. /** Constant for the delay in milliseconds after a device is connected before the library
  66. * will start the enumeration process. Some devices require a delay of up to 5 seconds
  67. * after connection before the enumeration process can start or incorrect operation will
  68. * occur.
  69. *
  70. * The default delay value may be overridden in the user project makefile by defining the
  71. * \c HOST_DEVICE_SETTLE_DELAY_MS token to the required delay in milliseconds, and passed to the
  72. * compiler using the -D switch.
  73. */
  74. #define HOST_DEVICE_SETTLE_DELAY_MS 1000
  75. #endif
  76. /* Enums: */
  77. /** Enum for the error codes for the \ref EVENT_USB_Host_HostError() event.
  78. *
  79. * \see \ref Group_Events for more information on this event.
  80. */
  81. enum USB_Host_ErrorCodes_t
  82. {
  83. HOST_ERROR_VBusVoltageDip = 0, /**< VBUS voltage dipped to an unacceptable level. This
  84. * error may be the result of an attached device drawing
  85. * too much current from the VBUS line, or due to the
  86. * AVR's power source being unable to supply sufficient
  87. * current.
  88. */
  89. };
  90. /** Enum for the error codes for the \ref EVENT_USB_Host_DeviceEnumerationFailed() event.
  91. *
  92. * \see \ref Group_Events for more information on this event.
  93. */
  94. enum USB_Host_EnumerationErrorCodes_t
  95. {
  96. HOST_ENUMERROR_NoError = 0, /**< No error occurred. Used internally, this is not a valid
  97. * ErrorCode parameter value for the \ref EVENT_USB_Host_DeviceEnumerationFailed()
  98. * event.
  99. */
  100. HOST_ENUMERROR_WaitStage = 1, /**< One of the delays between enumeration steps failed
  101. * to complete successfully, due to a timeout or other
  102. * error.
  103. */
  104. HOST_ENUMERROR_NoDeviceDetected = 2, /**< No device was detected, despite the USB data lines
  105. * indicating the attachment of a device.
  106. */
  107. HOST_ENUMERROR_ControlError = 3, /**< One of the enumeration control requests failed to
  108. * complete successfully.
  109. */
  110. HOST_ENUMERROR_PipeConfigError = 4, /**< The default control pipe (address 0) failed to
  111. * configure correctly.
  112. */
  113. };
  114. /* Inline Functions: */
  115. /** Returns the current USB frame number, when in host mode. Every millisecond the USB bus is active (i.e. not suspended)
  116. * the frame number is incremented by one.
  117. *
  118. * \return Current USB frame number from the USB controller.
  119. */
  120. static inline uint16_t USB_Host_GetFrameNumber(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  121. static inline uint16_t USB_Host_GetFrameNumber(void)
  122. {
  123. return AVR32_USBB_UHFNUM;
  124. }
  125. #if !defined(NO_SOF_EVENTS)
  126. /** Enables the host mode Start Of Frame events. When enabled, this causes the
  127. * \ref EVENT_USB_Host_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus,
  128. * at the start of each USB frame when a device is enumerated while in host mode.
  129. *
  130. * \note This function is not available when the \c NO_SOF_EVENTS compile time token is defined.
  131. */
  132. static inline void USB_Host_EnableSOFEvents(void) ATTR_ALWAYS_INLINE;
  133. static inline void USB_Host_EnableSOFEvents(void)
  134. {
  135. USB_INT_Enable(USB_INT_HSOFI);
  136. }
  137. /** Disables the host mode Start Of Frame events. When disabled, this stops the firing of the
  138. * \ref EVENT_USB_Host_StartOfFrame() event when enumerated in host mode.
  139. *
  140. * \note This function is not available when the \c NO_SOF_EVENTS compile time token is defined.
  141. */
  142. static inline void USB_Host_DisableSOFEvents(void) ATTR_ALWAYS_INLINE;
  143. static inline void USB_Host_DisableSOFEvents(void)
  144. {
  145. USB_INT_Disable(USB_INT_HSOFI);
  146. }
  147. #endif
  148. /** Resets the USB bus, including the endpoints in any attached device and pipes on the AVR host.
  149. * USB bus resets leave the default control pipe configured (if already configured).
  150. *
  151. * If the USB bus has been suspended prior to issuing a bus reset, the attached device will be
  152. * woken up automatically and the bus resumed after the reset has been correctly issued.
  153. */
  154. static inline void USB_Host_ResetBus(void) ATTR_ALWAYS_INLINE;
  155. static inline void USB_Host_ResetBus(void)
  156. {
  157. AVR32_USBB.UHCON.reset = true;
  158. }
  159. /** Determines if a previously issued bus reset (via the \ref USB_Host_ResetBus() macro) has
  160. * completed.
  161. *
  162. * \return Boolean \c true if no bus reset is currently being sent, \c false otherwise.
  163. */
  164. static inline bool USB_Host_IsBusResetComplete(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  165. static inline bool USB_Host_IsBusResetComplete(void)
  166. {
  167. return AVR32_USBB.UHCON.reset;
  168. }
  169. /** Resumes USB communications with an attached and enumerated device, by resuming the transmission
  170. * of the 1MS Start Of Frame messages to the device. When resumed, USB communications between the
  171. * host and attached device may occur.
  172. */
  173. static inline void USB_Host_ResumeBus(void) ATTR_ALWAYS_INLINE;
  174. static inline void USB_Host_ResumeBus(void)
  175. {
  176. AVR32_USBB.UHCON.sofe = true;
  177. }
  178. /** Suspends the USB bus, preventing any communications from occurring between the host and attached
  179. * device until the bus has been resumed. This stops the transmission of the 1MS Start Of Frame
  180. * messages to the device.
  181. *
  182. * \note While the USB bus is suspended, all USB interrupt sources are also disabled; this means that
  183. * some events (such as device disconnections) will not fire until the bus is resumed.
  184. */
  185. static inline void USB_Host_SuspendBus(void) ATTR_ALWAYS_INLINE;
  186. static inline void USB_Host_SuspendBus(void)
  187. {
  188. AVR32_USBB.UHCON.sofe = false;
  189. }
  190. /** Determines if the USB bus has been suspended via the use of the \ref USB_Host_SuspendBus() macro,
  191. * false otherwise. While suspended, no USB communications can occur until the bus is resumed,
  192. * except for the Remote Wakeup event from the device if supported.
  193. *
  194. * \return Boolean \c true if the bus is currently suspended, \c false otherwise.
  195. */
  196. static inline bool USB_Host_IsBusSuspended(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  197. static inline bool USB_Host_IsBusSuspended(void)
  198. {
  199. return AVR32_USBB.UHCON.sofe;
  200. }
  201. /** Determines if the attached device is currently enumerated in Full Speed mode (12Mb/s), or
  202. * false if the attached device is enumerated in Low Speed mode (1.5Mb/s).
  203. *
  204. * \return Boolean \c true if the attached device is enumerated in Full Speed mode, \c false otherwise.
  205. */
  206. static inline bool USB_Host_IsDeviceFullSpeed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  207. static inline bool USB_Host_IsDeviceFullSpeed(void)
  208. {
  209. return (AVR32_USBB.USBSTA.speed == AVR32_USBB_SPEED_FULL);
  210. }
  211. /** Determines if the attached device is currently issuing a Remote Wakeup request, requesting
  212. * that the host resume the USB bus and wake up the device, \c false otherwise.
  213. *
  214. * \return Boolean \c true if the attached device has sent a Remote Wakeup request, \c false otherwise.
  215. */
  216. static inline bool USB_Host_IsRemoteWakeupSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  217. static inline bool USB_Host_IsRemoteWakeupSent(void)
  218. {
  219. return AVR32_USBB.UHINT.rxrsmi;
  220. }
  221. /** Clears the flag indicating that a Remote Wakeup request has been issued by an attached device. */
  222. static inline void USB_Host_ClearRemoteWakeupSent(void) ATTR_ALWAYS_INLINE;
  223. static inline void USB_Host_ClearRemoteWakeupSent(void)
  224. {
  225. AVR32_USBB.UHINTCLR.rxrsmic = true;
  226. }
  227. /** Accepts a Remote Wakeup request from an attached device. This must be issued in response to
  228. * a device's Remote Wakeup request within 2ms for the request to be accepted and the bus to
  229. * be resumed.
  230. */
  231. static inline void USB_Host_ResumeFromWakeupRequest(void) ATTR_ALWAYS_INLINE;
  232. static inline void USB_Host_ResumeFromWakeupRequest(void)
  233. {
  234. AVR32_USBB.UHCON.resume = true;
  235. }
  236. /** Determines if a resume from Remote Wakeup request is currently being sent to an attached
  237. * device.
  238. *
  239. * \return Boolean \c true if no resume request is currently being sent, \c false otherwise.
  240. */
  241. static inline bool USB_Host_IsResumeFromWakeupRequestSent(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  242. static inline bool USB_Host_IsResumeFromWakeupRequestSent(void)
  243. {
  244. return AVR32_USBB.UHCON.resume;
  245. }
  246. /* Private Interface - For use in library only: */
  247. #if !defined(__DOXYGEN__)
  248. /* Macros: */
  249. static inline void USB_Host_HostMode_On(void) ATTR_ALWAYS_INLINE;
  250. static inline void USB_Host_HostMode_On(void)
  251. {
  252. // Not required for UC3B
  253. }
  254. static inline void USB_Host_HostMode_Off(void) ATTR_ALWAYS_INLINE;
  255. static inline void USB_Host_HostMode_Off(void)
  256. {
  257. // Not required for UC3B
  258. }
  259. static inline void USB_Host_VBUS_Auto_Enable(void) ATTR_ALWAYS_INLINE;
  260. static inline void USB_Host_VBUS_Auto_Enable(void)
  261. {
  262. AVR32_USBB.USBCON.vbushwc = false;
  263. }
  264. static inline void USB_Host_VBUS_Manual_Enable(void) ATTR_ALWAYS_INLINE;
  265. static inline void USB_Host_VBUS_Manual_Enable(void)
  266. {
  267. AVR32_USBB.USBCON.vbushwc = true;
  268. }
  269. static inline void USB_Host_VBUS_Auto_On(void) ATTR_ALWAYS_INLINE;
  270. static inline void USB_Host_VBUS_Auto_On(void)
  271. {
  272. AVR32_USBB.USBSTASET.vbusrqs = true;
  273. }
  274. static inline void USB_Host_VBUS_Manual_On(void) ATTR_ALWAYS_INLINE;
  275. static inline void USB_Host_VBUS_Manual_On(void)
  276. {
  277. AVR32_USBB.USBSTASET.vbusrqs = true;
  278. }
  279. static inline void USB_Host_VBUS_Auto_Off(void) ATTR_ALWAYS_INLINE;
  280. static inline void USB_Host_VBUS_Auto_Off(void)
  281. {
  282. AVR32_USBB.USBSTACLR.vbusrqc = true;
  283. }
  284. static inline void USB_Host_VBUS_Manual_Off(void) ATTR_ALWAYS_INLINE;
  285. static inline void USB_Host_VBUS_Manual_Off(void)
  286. {
  287. AVR32_USBB.USBSTACLR.vbusrqc = true;
  288. }
  289. static inline void USB_Host_SetDeviceAddress(const uint8_t Address) ATTR_ALWAYS_INLINE;
  290. static inline void USB_Host_SetDeviceAddress(const uint8_t Address)
  291. {
  292. AVR32_USBB.UHADDR1.uhaddr_p0 = Address;
  293. AVR32_USBB.UHADDR1.uhaddr_p1 = Address;
  294. AVR32_USBB.UHADDR1.uhaddr_p2 = Address;
  295. AVR32_USBB.UHADDR1.uhaddr_p3 = Address;
  296. AVR32_USBB.UHADDR2.uhaddr_p4 = Address;
  297. AVR32_USBB.UHADDR2.uhaddr_p5 = Address;
  298. AVR32_USBB.UHADDR2.uhaddr_p6 = Address;
  299. }
  300. /* Enums: */
  301. enum USB_Host_WaitMSErrorCodes_t
  302. {
  303. HOST_WAITERROR_Successful = 0,
  304. HOST_WAITERROR_DeviceDisconnect = 1,
  305. HOST_WAITERROR_PipeError = 2,
  306. HOST_WAITERROR_SetupStalled = 3,
  307. };
  308. /* Function Prototypes: */
  309. void USB_Host_ProcessNextHostState(void);
  310. uint8_t USB_Host_WaitMS(uint8_t MS);
  311. #if defined(__INCLUDE_FROM_HOST_C)
  312. static void USB_Host_ResetDevice(void);
  313. #endif
  314. #endif
  315. /* Disable C linkage for C++ Compilers: */
  316. #if defined(__cplusplus)
  317. }
  318. #endif
  319. #endif
  320. /** @} */