Added new RESET_TOGGLES_LIBUSB_COMPAT compile time option to the AVRISP-MKII clone...
[pub/lufa.git] / Projects / AVRISP-MKII / Descriptors.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * USB Device Descriptors, for library use when in USB device mode. Descriptors are special
34 * computer-readable structures which the host requests upon device enumeration, to determine
35 * the device's capabilities and functions.
36 */
37
38 #include "Descriptors.h"
39
40 #if defined(RESET_TOGGLES_LIBUSB_COMPAT) || defined(__DOXYGEN__)
41 static bool AVRISP_NeedCompatibilitySwitch ATTR_NO_INIT;
42
43 /** Current AVRISP data IN endpoint address. */
44 uint8_t AVRISP_CurrDataINEndpointAddress;
45
46 /** Saved AVRISP data IN endpoint address in EEPROM. */
47 uint8_t AVRISP_CurrDataINEndpointAddress_EEPROM EEMEM;
48 #endif
49
50 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
51 * device characteristics, including the supported USB version, control endpoint size and the
52 * number of device configurations. The descriptor is read out by the USB host when the enumeration
53 * process begins.
54 */
55 const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
56 {
57 .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
58
59 .USBSpecification = VERSION_BCD(01.10),
60 .Class = USB_CSCP_VendorSpecificClass,
61 .SubClass = USB_CSCP_NoDeviceSubclass,
62 .Protocol = USB_CSCP_NoDeviceProtocol,
63
64 .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
65
66 .VendorID = 0x03EB,
67 .ProductID = 0x2104,
68 .ReleaseNumber = VERSION_BCD(02.00),
69
70 .ManufacturerStrIndex = 0x01,
71 .ProductStrIndex = 0x02,
72 .SerialNumStrIndex = 0x03,
73
74 .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
75 };
76
77 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
78 * of the device in one of its supported configurations, including information about any device interfaces
79 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
80 * a configuration so that the host may correctly communicate with the USB device.
81 */
82 USB_Descriptor_Configuration_t ConfigurationDescriptor =
83 {
84 .Config =
85 {
86 .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
87
88 .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
89 .TotalInterfaces = 1,
90
91 .ConfigurationNumber = 1,
92 .ConfigurationStrIndex = NO_DESCRIPTOR,
93
94 .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
95
96 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
97 },
98
99 .AVRISP_Interface =
100 {
101 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
102
103 .InterfaceNumber = 0,
104 .AlternateSetting = 0,
105
106 .TotalEndpoints = 2,
107
108 .Class = USB_CSCP_VendorSpecificClass,
109 .SubClass = USB_CSCP_NoDeviceSubclass,
110 .Protocol = USB_CSCP_NoDeviceProtocol,
111
112 .InterfaceStrIndex = NO_DESCRIPTOR
113 },
114
115 .AVRISP_DataInEndpoint =
116 {
117 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
118
119 #if defined(RESET_TOGGLES_LIBUSB_COMPAT)
120 .EndpointAddress = 0,
121 #else
122 .EndpointAddress = AVRISP_DATA_IN_EPADDR,
123 #endif
124 .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
125 .EndpointSize = AVRISP_DATA_EPSIZE,
126 .PollingIntervalMS = 0x0A
127 },
128
129 .AVRISP_DataOutEndpoint =
130 {
131 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
132
133 .EndpointAddress = AVRISP_DATA_OUT_EPADDR,
134 .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
135 .EndpointSize = AVRISP_DATA_EPSIZE,
136 .PollingIntervalMS = 0x0A
137 },
138 };
139
140 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
141 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
142 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
143 */
144 const USB_Descriptor_String_t PROGMEM LanguageString =
145 {
146 .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
147
148 .UnicodeString = {LANGUAGE_ID_ENG}
149 };
150
151 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
152 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
153 * Descriptor.
154 */
155 const USB_Descriptor_String_t PROGMEM ManufacturerString =
156 {
157 .Header = {.Size = USB_STRING_LEN(5), .Type = DTYPE_String},
158
159 .UnicodeString = L"ATMEL"
160 };
161
162 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
163 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
164 * Descriptor.
165 */
166 const USB_Descriptor_String_t PROGMEM ProductString =
167 {
168 .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
169
170 .UnicodeString = L"AVRISP mkII"
171 };
172
173 /** Serial number string. This is a Unicode string containing the device's unique serial number, expressed as a
174 * series of uppercase hexadecimal digits.
175 */
176 const USB_Descriptor_String_t PROGMEM SerialString =
177 {
178 .Header = {.Size = USB_STRING_LEN(13), .Type = DTYPE_String},
179
180 .UnicodeString = L"000200012345\0" // Note: Real AVRISP-MKII has the embedded NUL byte, bug in firmware?
181 };
182
183 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
184 * documentation) by the application code so that the address and size of a requested descriptor can be given
185 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
186 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
187 * USB host.
188 */
189 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
190 const uint8_t wIndex,
191 const void** const DescriptorAddress,
192 uint8_t* DescriptorMemorySpace)
193 {
194 const uint8_t DescriptorType = (wValue >> 8);
195 const uint8_t DescriptorNumber = (wValue & 0xFF);
196
197 const void* Address = NULL;
198 uint16_t Size = NO_DESCRIPTOR;
199
200 *DescriptorMemorySpace = MEMSPACE_FLASH;
201
202 switch (DescriptorType)
203 {
204 case DTYPE_Device:
205 Address = &DeviceDescriptor;
206 Size = sizeof(USB_Descriptor_Device_t);
207 break;
208 case DTYPE_Configuration:
209 *DescriptorMemorySpace = MEMSPACE_RAM;
210 #if defined(RESET_TOGGLES_LIBUSB_COMPAT)
211 ConfigurationDescriptor.AVRISP_DataInEndpoint.EndpointAddress = AVRISP_CurrDataINEndpointAddress;
212 #endif
213
214 Address = &ConfigurationDescriptor;
215 Size = sizeof(USB_Descriptor_Configuration_t);
216 break;
217 case DTYPE_String:
218 switch (DescriptorNumber)
219 {
220 case 0x00:
221 Address = &LanguageString;
222 Size = pgm_read_byte(&LanguageString.Header.Size);
223 break;
224 case 0x01:
225 Address = &ManufacturerString;
226 Size = pgm_read_byte(&ManufacturerString.Header.Size);
227 break;
228 case 0x02:
229 Address = &ProductString;
230 Size = pgm_read_byte(&ProductString.Header.Size);
231 break;
232 case 0x03:
233 Address = &SerialString;
234 Size = pgm_read_byte(&SerialString.Header.Size);
235 break;
236 }
237
238 break;
239 }
240
241 *DescriptorAddress = Address;
242 return Size;
243 }
244
245 #if defined(RESET_TOGGLES_LIBUSB_COMPAT) || defined(__DOXYGEN__)
246 /** Checks the state of the system status register MCUSR and indicates via a flag if
247 * the current AVRISP driver compatibility mode needs to be reset.
248 *
249 * When the \c RESET_TOGGLES_LIBUSB_COMPAT compile time option is enabled, pulling
250 * the reset line of the AVR low will toggle between Jungo and libUSB compatibility
251 * modes. Other forms of reset (such as power on or watchdog) will not force a mode
252 * change.
253 */
254 void CheckExternalReset(void)
255 {
256 /* If an external reset occured, we need to change compatibility mode */
257 AVRISP_NeedCompatibilitySwitch = (MCUSR == (1 << EXTRF));
258
259 MCUSR = 0;
260 }
261
262 /** Updates the device descriptors so that the correct compatibility mode is used
263 * when the \c RESET_TOGGLES_LIBUSB_COMPAT compile time option is enabled. This
264 * configures the programmer for either Jungo or libUSB driver compatibility. Each
265 * time the AVR is reset via pulling the reset line low the compatibility mode will
266 * be toggled. The current mode is stored in EEPROM and preserved through power
267 * cycles of the AVR.
268 */
269 void UpdateCurrentCompatibilityMode(void)
270 {
271 /* Load the current IN endpoint address stored in EEPROM */
272 AVRISP_CurrDataINEndpointAddress = eeprom_read_byte(&AVRISP_CurrDataINEndpointAddress_EEPROM);
273
274 /* Check if we need to switch compatibility modes */
275 if (AVRISP_NeedCompatibilitySwitch)
276 {
277 /* Toggle between compatibility modes */
278 AVRISP_CurrDataINEndpointAddress = (AVRISP_CurrDataINEndpointAddress == AVRISP_DATA_IN_EPADDR_LIBUSB) ?
279 AVRISP_DATA_IN_EPADDR_JUNGO : AVRISP_DATA_IN_EPADDR_LIBUSB;
280
281 /* Save the new mode into EEPROM */
282 eeprom_update_byte(&AVRISP_CurrDataINEndpointAddress_EEPROM, AVRISP_CurrDataINEndpointAddress);
283 }
284
285 LEDs_SetAllLEDs(LEDS_NO_LEDS);
286
287 /* Validate IN endpoint address and indicate current mode via LED flashes */
288 switch (AVRISP_CurrDataINEndpointAddress)
289 {
290 default:
291 /* Default to Jungo compatibility mode if saved EEPROM is invalid */
292 AVRISP_CurrDataINEndpointAddress = AVRISP_DATA_IN_EPADDR_JUNGO;
293 case AVRISP_DATA_IN_EPADDR_JUNGO:
294 /* Two flashes for Jungo compatibility mode */
295 for (uint8_t i = 0; i < 4; i++)
296 {
297 LEDs_ToggleLEDs(LEDS_ALL_LEDS);
298 Delay_MS(100);
299 }
300 break;
301 case AVRISP_DATA_IN_EPADDR_LIBUSB:
302 /* Five flashes for libUSB compatibility mode */
303 for (uint8_t i = 0; i < 10; i++)
304 {
305 LEDs_ToggleLEDs(LEDS_ALL_LEDS);
306 Delay_MS(100);
307 }
308 break;
309 }
310 }
311 #endif