AVRISPDescriptors.c 11.1 KB
Newer Older
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
17
18
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
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 "AVRISPDescriptors.h"
39

40
41
42
43
44
45
46
47
48
49
#if defined(RESET_TOGGLES_LIBUSB_COMPAT) || defined(__DOXYGEN__)
	static bool AVRISP_NeedCompatibilitySwitch ATTR_NO_INIT;

	/** Current AVRISP data IN endpoint address. */
	uint8_t AVRISP_CurrDataINEndpointAddress;
	
	/** Saved AVRISP data IN endpoint address in EEPROM. */
	uint8_t AVRISP_CurrDataINEndpointAddress_EEPROM EEMEM;
#endif

50
51
52
53
54
/** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
 *  device characteristics, including the supported USB version, control endpoint size and the
 *  number of device configurations. The descriptor is read out by the USB host when the enumeration
 *  process begins.
 */
55
const USB_Descriptor_Device_t PROGMEM AVRISP_DeviceDescriptor =
56
57
{
	.Header                 = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
58

59
	.USBSpecification       = VERSION_BCD(01.10),
60
61
62
	.Class                  = USB_CSCP_VendorSpecificClass,
	.SubClass               = USB_CSCP_NoDeviceSubclass,
	.Protocol               = USB_CSCP_NoDeviceProtocol,
63

64
	.Endpoint0Size          = FIXED_CONTROL_ENDPOINT_SIZE,
65

66
67
	.VendorID               = 0x03EB,
	.ProductID              = 0x2104,
68
	.ReleaseNumber          = VERSION_BCD(02.00),
69

70
71
72
	.ManufacturerStrIndex   = 0x01,
	.ProductStrIndex        = 0x02,
	.SerialNumStrIndex      = 0x03,
73

74
75
76
77
78
79
80
81
	.NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
};

/** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
 *  of the device in one of its supported configurations, including information about any device interfaces
 *  and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
 *  a configuration so that the host may correctly communicate with the USB device.
 */
82
AVRISP_USB_Descriptor_Configuration_t AVRISP_ConfigurationDescriptor =
83
{
84
	.Config =
85
86
87
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},

88
			.TotalConfigurationSize = sizeof(AVRISP_USB_Descriptor_Configuration_t),
89
			.TotalInterfaces        = 1,
90

91
92
			.ConfigurationNumber    = 1,
			.ConfigurationStrIndex  = NO_DESCRIPTOR,
93

94
			.ConfigAttributes       = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
95

96
97
98
			.MaxPowerConsumption    = USB_CONFIG_POWER_MA(100)
		},

99
	.AVRISP_Interface =
100
101
102
103
104
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},

			.InterfaceNumber        = 0,
			.AlternateSetting       = 0,
105

106
			.TotalEndpoints         = 2,
107

108
			.Class                  = USB_CSCP_VendorSpecificClass,
109
110
			.SubClass               = USB_CSCP_NoDeviceSubclass,
			.Protocol               = USB_CSCP_NoDeviceProtocol,
111

112
113
114
			.InterfaceStrIndex      = NO_DESCRIPTOR
		},

115
	.AVRISP_DataInEndpoint =
116
117
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
118

119
120
121
#if defined(RESET_TOGGLES_LIBUSB_COMPAT)
			.EndpointAddress        = 0,
#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
130
131
		},

	.AVRISP_DataOutEndpoint =
		{
			.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
141
142
143
		},
};

/** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
 *  the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
 *  via the language ID table available at USB.org what languages the device supports for its string descriptors.
 */
144
const USB_Descriptor_String_t PROGMEM AVRISP_LanguageString =
145
146
{
	.Header                 = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
147

148
149
150
151
152
153
154
	.UnicodeString          = {LANGUAGE_ID_ENG}
};

/** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
 *  form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
 *  Descriptor.
 */
155
const USB_Descriptor_String_t PROGMEM AVRISP_ManufacturerString =
156
{
157
	.Header                 = {.Size = USB_STRING_LEN(5), .Type = DTYPE_String},
158

159
	.UnicodeString          = L"ATMEL"
160
161
162
163
164
165
};

/** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
 *  and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
 *  Descriptor.
 */
166
const USB_Descriptor_String_t PROGMEM AVRISP_ProductString =
167
{
168
	.Header                 = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
169

170
	.UnicodeString          = L"AVRISP mkII"
171
172
173
174
175
};

/** Serial number string. This is a Unicode string containing the device's unique serial number, expressed as a
 *  series of uppercase hexadecimal digits.
 */
176
const USB_Descriptor_String_t PROGMEM AVRISP_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
184
185
186
187
188
};

/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
 *  documentation) by the application code so that the address and size of a requested descriptor can be given
 *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
 *  is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
 *  USB host.
 */
189
190
191
192
uint16_t AVRISP_GetDescriptor(const uint16_t wValue,
                              const uint8_t wIndex,
                              const void** const DescriptorAddress,
                              uint8_t* DescriptorMemorySpace)
193
194
195
196
{
	const uint8_t  DescriptorType   = (wValue >> 8);
	const uint8_t  DescriptorNumber = (wValue & 0xFF);

197
198
	const void* Address = NULL;
	uint16_t    Size    = NO_DESCRIPTOR;
199

200
201
	*DescriptorMemorySpace = MEMSPACE_FLASH;
	
202
203
	switch (DescriptorType)
	{
204
		case DTYPE_Device:
205
			Address = &AVRISP_DeviceDescriptor;
206
207
			Size    = sizeof(USB_Descriptor_Device_t);
			break;
208
		case DTYPE_Configuration:
209
210
			*DescriptorMemorySpace = MEMSPACE_RAM;
			#if defined(RESET_TOGGLES_LIBUSB_COMPAT)			
211
				AVRISP_ConfigurationDescriptor.AVRISP_DataInEndpoint.EndpointAddress = AVRISP_CurrDataINEndpointAddress;
212
213
			#endif

214
215
			Address = &AVRISP_ConfigurationDescriptor;
			Size    = sizeof(AVRISP_USB_Descriptor_Configuration_t);
216
			break;
217
		case DTYPE_String:
218
219
			switch (DescriptorNumber)
			{
220
				case 0x00:
221
222
					Address = &AVRISP_LanguageString;
					Size    = pgm_read_byte(&AVRISP_LanguageString.Header.Size);
223
					break;
224
				case 0x01:
225
226
					Address = &AVRISP_ManufacturerString;
					Size    = pgm_read_byte(&AVRISP_ManufacturerString.Header.Size);
227
					break;
228
				case 0x02:
229
230
					Address = &AVRISP_ProductString;
					Size    = pgm_read_byte(&AVRISP_ProductString.Header.Size);
231
232
					break;
				case 0x03:
233
234
					Address = &AVRISP_SerialString;
					Size    = pgm_read_byte(&AVRISP_SerialString.Header.Size);
235
					break;
236
			}
237

238
239
			break;
	}
240

241
242
243
	*DescriptorAddress = Address;
	return Size;
}
244

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#if defined(RESET_TOGGLES_LIBUSB_COMPAT) || defined(__DOXYGEN__)
/** Checks the state of the system status register MCUSR and indicates via a flag if
 *  the current AVRISP driver compatibility mode needs to be reset.
 *
 *  When the \c RESET_TOGGLES_LIBUSB_COMPAT compile time option is enabled, pulling
 *  the reset line of the AVR low will toggle between Jungo and libUSB compatibility
 *  modes. Other forms of reset (such as power on or watchdog) will not force a mode
 *  change.
 */
void CheckExternalReset(void)
{	
	/* If an external reset occured, we need to change compatibility mode */
	AVRISP_NeedCompatibilitySwitch = (MCUSR == (1 << EXTRF));

	MCUSR = 0;
}

/** Updates the device descriptors so that the correct compatibility mode is used
 *  when the \c RESET_TOGGLES_LIBUSB_COMPAT compile time option is enabled. This
 *  configures the programmer for either Jungo or libUSB driver compatibility. Each
 *  time the AVR is reset via pulling the reset line low the compatibility mode will
 *  be toggled. The current mode is stored in EEPROM and preserved through power
 *  cycles of the AVR.
 */
void UpdateCurrentCompatibilityMode(void)
{
	/* Load the current IN endpoint address stored in EEPROM */
	AVRISP_CurrDataINEndpointAddress = eeprom_read_byte(&AVRISP_CurrDataINEndpointAddress_EEPROM);
	
	/* Check if we need to switch compatibility modes */
	if (AVRISP_NeedCompatibilitySwitch)
	{
		/* Toggle between compatibility modes */
		AVRISP_CurrDataINEndpointAddress = (AVRISP_CurrDataINEndpointAddress == AVRISP_DATA_IN_EPADDR_LIBUSB) ?
				AVRISP_DATA_IN_EPADDR_JUNGO : AVRISP_DATA_IN_EPADDR_LIBUSB;

		/* Save the new mode into EEPROM */
		eeprom_update_byte(&AVRISP_CurrDataINEndpointAddress_EEPROM, AVRISP_CurrDataINEndpointAddress);
	}

	LEDs_SetAllLEDs(LEDS_NO_LEDS);

	/* Validate IN endpoint address and indicate current mode via LED flashes */
	switch (AVRISP_CurrDataINEndpointAddress)
	{
		default:
			/* Default to Jungo compatibility mode if saved EEPROM is invalid */
			AVRISP_CurrDataINEndpointAddress = AVRISP_DATA_IN_EPADDR_JUNGO;
		case AVRISP_DATA_IN_EPADDR_JUNGO:
			/* Two flashes for Jungo compatibility mode */
			for (uint8_t i = 0; i < 4; i++)
			{
				LEDs_ToggleLEDs(LEDS_ALL_LEDS);
				Delay_MS(100);
			}		
			break;
		case AVRISP_DATA_IN_EPADDR_LIBUSB:
			/* Five flashes for libUSB compatibility mode */
			for (uint8_t i = 0; i < 10; i++)
			{
				LEDs_ToggleLEDs(LEDS_ALL_LEDS);
				Delay_MS(100);
			}
			break;
	}
}
#endif