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.

AVRISPDescriptors.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. *
  28. * USB Device Descriptors, for library use when in USB device mode. Descriptors are special
  29. * computer-readable structures which the host requests upon device enumeration, to determine
  30. * the device's capabilities and functions.
  31. */
  32. #include "AVRISPDescriptors.h"
  33. #if defined(RESET_TOGGLES_LIBUSB_COMPAT) || defined(__DOXYGEN__)
  34. /** Indicates if an external reset has occurred and the compatibility mode needs to be altered */
  35. static bool AVRISP_NeedCompatibilitySwitch ATTR_NO_INIT;
  36. /** Current AVRISP data IN endpoint address. */
  37. uint8_t AVRISP_CurrDataINEndpointAddress;
  38. /** Saved AVRISP data IN endpoint address in EEPROM. */
  39. uint8_t AVRISP_CurrDataINEndpointAddress_EEPROM EEMEM;
  40. #endif
  41. /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
  42. * device characteristics, including the supported USB version, control endpoint size and the
  43. * number of device configurations. The descriptor is read out by the USB host when the enumeration
  44. * process begins.
  45. */
  46. const USB_Descriptor_Device_t PROGMEM AVRISP_DeviceDescriptor =
  47. {
  48. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  49. .USBSpecification = VERSION_BCD(1,1,0),
  50. .Class = USB_CSCP_VendorSpecificClass,
  51. .SubClass = USB_CSCP_NoDeviceSubclass,
  52. .Protocol = USB_CSCP_NoDeviceProtocol,
  53. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  54. .VendorID = 0x03EB,
  55. .ProductID = 0x2104,
  56. .ReleaseNumber = VERSION_BCD(2,0,0),
  57. .ManufacturerStrIndex = AVRISP_STRING_ID_Manufacturer,
  58. .ProductStrIndex = AVRISP_STRING_ID_Product,
  59. .SerialNumStrIndex = AVRISP_STRING_ID_Serial,
  60. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  61. };
  62. /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
  63. * of the device in one of its supported configurations, including information about any device interfaces
  64. * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
  65. * a configuration so that the host may correctly communicate with the USB device.
  66. */
  67. AVRISP_USB_Descriptor_Configuration_t AVRISP_ConfigurationDescriptor =
  68. {
  69. .Config =
  70. {
  71. .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
  72. .TotalConfigurationSize = sizeof(AVRISP_USB_Descriptor_Configuration_t),
  73. .TotalInterfaces = 1,
  74. .ConfigurationNumber = 1,
  75. .ConfigurationStrIndex = NO_DESCRIPTOR,
  76. .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
  77. .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
  78. },
  79. .AVRISP_Interface =
  80. {
  81. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  82. .InterfaceNumber = INTERFACE_ID_AVRISP,
  83. .AlternateSetting = 0,
  84. .TotalEndpoints = 2,
  85. .Class = USB_CSCP_VendorSpecificClass,
  86. .SubClass = USB_CSCP_NoDeviceSubclass,
  87. .Protocol = USB_CSCP_NoDeviceProtocol,
  88. .InterfaceStrIndex = NO_DESCRIPTOR
  89. },
  90. .AVRISP_DataInEndpoint =
  91. {
  92. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  93. #if defined(RESET_TOGGLES_LIBUSB_COMPAT)
  94. .EndpointAddress = 0,
  95. #else
  96. .EndpointAddress = AVRISP_DATA_IN_EPADDR,
  97. #endif
  98. .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  99. .EndpointSize = AVRISP_DATA_EPSIZE,
  100. .PollingIntervalMS = 0x0A
  101. },
  102. .AVRISP_DataOutEndpoint =
  103. {
  104. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  105. .EndpointAddress = AVRISP_DATA_OUT_EPADDR,
  106. .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  107. .EndpointSize = AVRISP_DATA_EPSIZE,
  108. .PollingIntervalMS = 0x0A
  109. },
  110. };
  111. /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
  112. * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
  113. * via the language ID table available at USB.org what languages the device supports for its string descriptors.
  114. */
  115. const USB_Descriptor_String_t PROGMEM AVRISP_LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
  116. /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
  117. * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  118. * Descriptor.
  119. */
  120. const USB_Descriptor_String_t PROGMEM AVRISP_ManufacturerString = USB_STRING_DESCRIPTOR(L"ATMEL");
  121. /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
  122. * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  123. * Descriptor.
  124. */
  125. const USB_Descriptor_String_t PROGMEM AVRISP_ProductString = USB_STRING_DESCRIPTOR(L"AVRISP mkII");
  126. /** Serial number string. This is a Unicode string containing the device's unique serial number, expressed as a
  127. * series of uppercase hexadecimal digits.
  128. */
  129. USB_Descriptor_String_t AVRISP_SerialString = USB_STRING_DESCRIPTOR(L"000200012345\0"
  130. // Note: Real AVRISP-MKII has the embedded NUL byte, bug in firmware?
  131. );
  132. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  133. * documentation) by the application code so that the address and size of a requested descriptor can be given
  134. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  135. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  136. * USB host.
  137. */
  138. uint16_t AVRISP_GetDescriptor(const uint16_t wValue,
  139. const uint8_t wIndex,
  140. const void** const DescriptorAddress,
  141. uint8_t* DescriptorMemorySpace)
  142. {
  143. const uint8_t DescriptorType = (wValue >> 8);
  144. const uint8_t DescriptorNumber = (wValue & 0xFF);
  145. const void* Address = NULL;
  146. uint16_t Size = NO_DESCRIPTOR;
  147. *DescriptorMemorySpace = MEMSPACE_FLASH;
  148. switch (DescriptorType)
  149. {
  150. case DTYPE_Device:
  151. Address = &AVRISP_DeviceDescriptor;
  152. Size = sizeof(USB_Descriptor_Device_t);
  153. break;
  154. case DTYPE_Configuration:
  155. *DescriptorMemorySpace = MEMSPACE_RAM;
  156. #if defined(RESET_TOGGLES_LIBUSB_COMPAT)
  157. /* Update the configuration descriptor with the current endpoint address */
  158. AVRISP_ConfigurationDescriptor.AVRISP_DataInEndpoint.EndpointAddress = AVRISP_CurrDataINEndpointAddress;
  159. #endif
  160. Address = &AVRISP_ConfigurationDescriptor;
  161. Size = sizeof(AVRISP_USB_Descriptor_Configuration_t);
  162. break;
  163. case DTYPE_String:
  164. switch (DescriptorNumber)
  165. {
  166. case AVRISP_STRING_ID_Language:
  167. Address = &AVRISP_LanguageString;
  168. Size = pgm_read_byte(&AVRISP_LanguageString.Header.Size);
  169. break;
  170. case AVRISP_STRING_ID_Manufacturer:
  171. Address = &AVRISP_ManufacturerString;
  172. Size = pgm_read_byte(&AVRISP_ManufacturerString.Header.Size);
  173. break;
  174. case AVRISP_STRING_ID_Product:
  175. Address = &AVRISP_ProductString;
  176. Size = pgm_read_byte(&AVRISP_ProductString.Header.Size);
  177. break;
  178. case AVRISP_STRING_ID_Serial:
  179. Address = &AVRISP_SerialString;
  180. Size = AVRISP_SerialString.Header.Size;
  181. /* Update serial number to have a different serial based on the current endpoint address */
  182. ((uint16_t*)&AVRISP_SerialString.UnicodeString)[6] = cpu_to_le16('0' + (AVRISP_DATA_IN_EPADDR & ENDPOINT_EPNUM_MASK));
  183. *DescriptorMemorySpace = MEMSPACE_RAM;
  184. break;
  185. }
  186. break;
  187. }
  188. *DescriptorAddress = Address;
  189. return Size;
  190. }
  191. #if defined(RESET_TOGGLES_LIBUSB_COMPAT) || defined(__DOXYGEN__)
  192. /** Checks the state of the system status register MCUSR and indicates via a flag if
  193. * the current AVRISP driver compatibility mode needs to be reset.
  194. *
  195. * When the \c RESET_TOGGLES_LIBUSB_COMPAT compile time option is enabled, pulling
  196. * the reset line of the AVR low will toggle between Jungo and libUSB compatibility
  197. * modes. Other forms of reset (such as power on or watchdog) will not force a mode
  198. * change.
  199. */
  200. void CheckExternalReset(void)
  201. {
  202. /* If an external reset occurred, we need to change compatibility mode */
  203. AVRISP_NeedCompatibilitySwitch = (MCUSR == (1 << EXTRF));
  204. MCUSR = 0;
  205. }
  206. /** Updates the device descriptors so that the correct compatibility mode is used
  207. * when the \c RESET_TOGGLES_LIBUSB_COMPAT compile time option is enabled. This
  208. * configures the programmer for either Jungo or libUSB driver compatibility. Each
  209. * time the AVR is reset via pulling the reset line low the compatibility mode will
  210. * be toggled. The current mode is stored in EEPROM and preserved through power
  211. * cycles of the AVR.
  212. */
  213. void UpdateCurrentCompatibilityMode(void)
  214. {
  215. /* Load the current IN endpoint address stored in EEPROM */
  216. AVRISP_CurrDataINEndpointAddress = eeprom_read_byte(&AVRISP_CurrDataINEndpointAddress_EEPROM);
  217. /* Check if we need to switch compatibility modes */
  218. if (AVRISP_NeedCompatibilitySwitch)
  219. {
  220. /* Toggle between compatibility modes */
  221. AVRISP_CurrDataINEndpointAddress = (AVRISP_CurrDataINEndpointAddress == AVRISP_DATA_IN_EPADDR_LIBUSB) ?
  222. AVRISP_DATA_IN_EPADDR_JUNGO : AVRISP_DATA_IN_EPADDR_LIBUSB;
  223. /* Save the new mode into EEPROM */
  224. eeprom_update_byte(&AVRISP_CurrDataINEndpointAddress_EEPROM, AVRISP_CurrDataINEndpointAddress);
  225. }
  226. LEDs_SetAllLEDs(LEDS_NO_LEDS);
  227. /* Validate IN endpoint address and indicate current mode via LED flashes */
  228. switch (AVRISP_CurrDataINEndpointAddress)
  229. {
  230. default:
  231. /* Default to Jungo compatibility mode if saved EEPROM is invalid */
  232. AVRISP_CurrDataINEndpointAddress = AVRISP_DATA_IN_EPADDR_JUNGO;
  233. case AVRISP_DATA_IN_EPADDR_JUNGO:
  234. /* Two flashes for Jungo compatibility mode */
  235. for (uint8_t i = 0; i < 4; i++)
  236. {
  237. LEDs_ToggleLEDs(LEDS_ALL_LEDS);
  238. Delay_MS(100);
  239. }
  240. break;
  241. case AVRISP_DATA_IN_EPADDR_LIBUSB:
  242. /* Five flashes for libUSB compatibility mode */
  243. for (uint8_t i = 0; i < 10; i++)
  244. {
  245. LEDs_ToggleLEDs(LEDS_ALL_LEDS);
  246. Delay_MS(100);
  247. }
  248. break;
  249. }
  250. Delay_MS(500);
  251. }
  252. #endif