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_XMEGA.h 9.9KB

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