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.

Device_UC3.h 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 Device definitions for the AVR32 UC3 microcontrollers.
  28. * \copydetails Group_Device_UC3
  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_Device
  34. * \defgroup Group_Device_UC3 Device Management (UC3)
  35. * \brief USB Device definitions for the AVR32 UC3 microcontrollers.
  36. *
  37. * Architecture specific USB Device definitions for the Atmel 32-bit UC3 AVR microcontrollers.
  38. *
  39. * @{
  40. */
  41. #ifndef __USBDEVICE_UC3_H__
  42. #define __USBDEVICE_UC3_H__
  43. /* Includes: */
  44. #include "../../../../Common/Common.h"
  45. #include "../USBController.h"
  46. #include "../StdDescriptors.h"
  47. #include "../USBInterrupt.h"
  48. #include "../Endpoint.h"
  49. /* Enable C linkage for C++ Compilers: */
  50. #if defined(__cplusplus)
  51. extern "C" {
  52. #endif
  53. /* Preprocessor Checks: */
  54. #if !defined(__INCLUDE_FROM_USB_DRIVER)
  55. #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
  56. #endif
  57. /* Public Interface - May be used in end-application: */
  58. /* Macros: */
  59. /** \name USB Device Mode Option Masks */
  60. //@{
  61. /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
  62. * USB interface should be initialized in low speed (1.5Mb/s) mode.
  63. *
  64. * \note Restrictions apply on the number, size and type of endpoints which can be used
  65. * when running in low speed mode - please refer to the USB 2.0 specification.
  66. */
  67. #define USB_DEVICE_OPT_LOWSPEED (1 << 0)
  68. /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
  69. * USB interface should be initialized in full speed (12Mb/s) mode.
  70. */
  71. #define USB_DEVICE_OPT_FULLSPEED (0 << 0)
  72. #if defined(USB_SERIES_UC3A3_AVR32) || defined(USB_SERIES_UC3A4_AVR32) || defined(__DOXYGEN__)
  73. /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
  74. * USB interface should be initialized in high speed (480Mb/s) mode.
  75. */
  76. #define USB_DEVICE_OPT_HIGHSPEED (1 << 1)
  77. #endif
  78. //@}
  79. #if (!defined(NO_INTERNAL_SERIAL) && \
  80. (defined(USB_SERIES_UC3A3_AVR32) || defined(USB_SERIES_UC3A4_AVR32) || \
  81. defined(__DOXYGEN__)))
  82. /** String descriptor index for the device's unique serial number string descriptor within the device.
  83. * This unique serial number is used by the host to associate resources to the device (such as drivers or COM port
  84. * number allocations) to a device regardless of the port it is plugged in to on the host. Some microcontrollers contain
  85. * a unique serial number internally, and setting the device descriptors serial number string index to this value
  86. * will cause it to use the internal serial number.
  87. *
  88. * On unsupported devices, this will evaluate to \ref NO_DESCRIPTOR and so will force the host to create a pseudo-serial
  89. * number for the device.
  90. */
  91. #define USE_INTERNAL_SERIAL 0xDC
  92. /** Length of the device's unique internal serial number, in bits, if present on the selected microcontroller
  93. * model.
  94. */
  95. #define INTERNAL_SERIAL_LENGTH_BITS 120
  96. /** Start address of the internal serial number, in the appropriate address space, if present on the selected microcontroller
  97. * model.
  98. */
  99. #define INTERNAL_SERIAL_START_ADDRESS 0x80800204
  100. #else
  101. #define USE_INTERNAL_SERIAL NO_DESCRIPTOR
  102. #define INTERNAL_SERIAL_LENGTH_BITS 0
  103. #define INTERNAL_SERIAL_START_ADDRESS 0
  104. #endif
  105. /* Function Prototypes: */
  106. /** Sends a Remote Wakeup request to the host. This signals to the host that the device should
  107. * be taken out of suspended mode, and communications should resume.
  108. *
  109. * Typically, this is implemented so that HID devices (mice, keyboards, etc.) can wake up the
  110. * host computer when the host has suspended all USB devices to enter a low power state.
  111. *
  112. * \note This function should only be used if the device has indicated to the host that it
  113. * supports the Remote Wakeup feature in the device descriptors, and should only be
  114. * issued if the host is currently allowing remote wakeup events from the device (i.e.,
  115. * the \ref USB_Device_RemoteWakeupEnabled flag is set). When the \c NO_DEVICE_REMOTE_WAKEUP
  116. * compile time option is used, this function is unavailable.
  117. *
  118. * \note The USB clock must be running for this function to operate. If the stack is initialized with
  119. * the \ref USB_OPT_MANUAL_PLL option enabled, the user must ensure that the PLL is running
  120. * before attempting to call this function.
  121. *
  122. * \see \ref Group_StdDescriptors for more information on the RMWAKEUP feature and device descriptors.
  123. */
  124. void USB_Device_SendRemoteWakeup(void);
  125. /* Inline Functions: */
  126. /** Returns the current USB frame number, when in device mode. Every millisecond the USB bus is active (i.e. enumerated to a host)
  127. * the frame number is incremented by one.
  128. *
  129. * \return Current USB frame number from the USB controller.
  130. */
  131. static inline uint16_t USB_Device_GetFrameNumber(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  132. static inline uint16_t USB_Device_GetFrameNumber(void)
  133. {
  134. return AVR32_USBB.UDFNUM.fnum;
  135. }
  136. #if !defined(NO_SOF_EVENTS)
  137. /** Enables the device mode Start Of Frame events. When enabled, this causes the
  138. * \ref EVENT_USB_Device_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus,
  139. * at the start of each USB frame when enumerated in device mode.
  140. *
  141. * \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
  142. */
  143. static inline void USB_Device_EnableSOFEvents(void) ATTR_ALWAYS_INLINE;
  144. static inline void USB_Device_EnableSOFEvents(void)
  145. {
  146. USB_INT_Enable(USB_INT_SOFI);
  147. }
  148. /** Disables the device mode Start Of Frame events. When disabled, this stops the firing of the
  149. * \ref EVENT_USB_Device_StartOfFrame() event when enumerated in device mode.
  150. *
  151. * \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
  152. */
  153. static inline void USB_Device_DisableSOFEvents(void) ATTR_ALWAYS_INLINE;
  154. static inline void USB_Device_DisableSOFEvents(void)
  155. {
  156. USB_INT_Disable(USB_INT_SOFI);
  157. }
  158. #endif
  159. /* Private Interface - For use in library only: */
  160. #if !defined(__DOXYGEN__)
  161. /* Inline Functions: */
  162. static inline void USB_Device_SetLowSpeed(void) ATTR_ALWAYS_INLINE;
  163. static inline void USB_Device_SetLowSpeed(void)
  164. {
  165. AVR32_USBB.UDCON.ls = true;
  166. }
  167. static inline void USB_Device_SetFullSpeed(void) ATTR_ALWAYS_INLINE;
  168. static inline void USB_Device_SetFullSpeed(void)
  169. {
  170. AVR32_USBB.UDCON.ls = false;
  171. #if defined(USB_DEVICE_OPT_HIGHSPEED)
  172. AVR32_USBB.UDCON.spdconf = 3;
  173. #endif
  174. }
  175. #if defined(USB_DEVICE_OPT_HIGHSPEED)
  176. static inline void USB_Device_SetHighSpeed(void) ATTR_ALWAYS_INLINE;
  177. static inline void USB_Device_SetHighSpeed(void)
  178. {
  179. AVR32_USBB.UDCON.ls = false;
  180. AVR32_USBB.UDCON.spdconf = 0;
  181. }
  182. #endif
  183. static inline void USB_Device_SetDeviceAddress(const uint8_t Address) ATTR_ALWAYS_INLINE;
  184. static inline void USB_Device_SetDeviceAddress(const uint8_t Address)
  185. {
  186. AVR32_USBB.UDCON.uadd = Address;
  187. }
  188. static inline void USB_Device_EnableDeviceAddress(const uint8_t Address) ATTR_ALWAYS_INLINE;
  189. static inline void USB_Device_EnableDeviceAddress(const uint8_t Address)
  190. {
  191. (void)Address;
  192. AVR32_USBB.UDCON.adden = true;
  193. }
  194. static inline bool USB_Device_IsAddressSet(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  195. static inline bool USB_Device_IsAddressSet(void)
  196. {
  197. return AVR32_USBB.UDCON.adden;
  198. }
  199. #if (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
  200. static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString) ATTR_NON_NULL_PTR_ARG(1);
  201. static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString)
  202. {
  203. uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();
  204. GlobalInterruptDisable();
  205. uint8_t* SigReadAddress = (uint8_t*)INTERNAL_SERIAL_START_ADDRESS;
  206. for (uint8_t SerialCharNum = 0; SerialCharNum < (INTERNAL_SERIAL_LENGTH_BITS / 4); SerialCharNum++)
  207. {
  208. uint8_t SerialByte = *SigReadAddress;
  209. if (SerialCharNum & 0x01)
  210. {
  211. SerialByte >>= 4;
  212. SigReadAddress++;
  213. }
  214. SerialByte &= 0x0F;
  215. UnicodeString[SerialCharNum] = cpu_to_le16((SerialByte >= 10) ?
  216. (('A' - 10) + SerialByte) : ('0' + SerialByte));
  217. }
  218. SetGlobalInterruptMask(CurrentGlobalInt);
  219. }
  220. #endif
  221. #endif
  222. /* Disable C linkage for C++ Compilers: */
  223. #if defined(__cplusplus)
  224. }
  225. #endif
  226. #endif
  227. /** @} */