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_AVR8.h 10KB

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