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.
312 lines
11 KiB
312 lines
11 KiB
15 years ago
|
/*
|
||
|
LUFA Library
|
||
13 years ago
|
Copyright (C) Dean Camera, 2012.
|
||
14 years ago
|
|
||
15 years ago
|
dean [at] fourwalledcubicle [dot] com
|
||
14 years ago
|
www.lufa-lib.org
|
||
15 years ago
|
*/
|
||
|
|
||
|
/*
|
||
13 years ago
|
Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
|
||
15 years ago
|
|
||
14 years ago
|
Permission to use, copy, modify, distribute, and sell this
|
||
15 years ago
|
software and its documentation for any purpose is hereby granted
|
||
14 years ago
|
without fee, provided that the above copyright notice appear in
|
||
15 years ago
|
all copies and that both that the copyright notice and this
|
||
14 years ago
|
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
|
||
15 years ago
|
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
|
||
|
*
|
||
14 years ago
|
* USB Device Descriptors, for library use when in USB device mode. Descriptors are special
|
||
15 years ago
|
* computer-readable structures which the host requests upon device enumeration, to determine
|
||
14 years ago
|
* the device's capabilities and functions.
|
||
15 years ago
|
*/
|
||
|
|
||
13 years ago
|
#include "AVRISPDescriptors.h"
|
||
15 years ago
|
|
||
13 years ago
|
#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
|
||
|
|
||
15 years ago
|
/** 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.
|
||
|
*/
|
||
13 years ago
|
const USB_Descriptor_Device_t PROGMEM AVRISP_DeviceDescriptor =
|
||
15 years ago
|
{
|
||
|
.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
|
||
14 years ago
|
|
||
15 years ago
|
.USBSpecification = VERSION_BCD(01.10),
|
||
14 years ago
|
.Class = USB_CSCP_VendorSpecificClass,
|
||
|
.SubClass = USB_CSCP_NoDeviceSubclass,
|
||
|
.Protocol = USB_CSCP_NoDeviceProtocol,
|
||
14 years ago
|
|
||
15 years ago
|
.Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
|
||
14 years ago
|
|
||
14 years ago
|
.VendorID = 0x03EB,
|
||
|
.ProductID = 0x2104,
|
||
15 years ago
|
.ReleaseNumber = VERSION_BCD(02.00),
|
||
14 years ago
|
|
||
15 years ago
|
.ManufacturerStrIndex = 0x01,
|
||
|
.ProductStrIndex = 0x02,
|
||
|
.SerialNumStrIndex = 0x03,
|
||
14 years ago
|
|
||
15 years ago
|
.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.
|
||
|
*/
|
||
13 years ago
|
AVRISP_USB_Descriptor_Configuration_t AVRISP_ConfigurationDescriptor =
|
||
15 years ago
|
{
|
||
14 years ago
|
.Config =
|
||
15 years ago
|
{
|
||
|
.Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
|
||
|
|
||
13 years ago
|
.TotalConfigurationSize = sizeof(AVRISP_USB_Descriptor_Configuration_t),
|
||
15 years ago
|
.TotalInterfaces = 1,
|
||
14 years ago
|
|
||
15 years ago
|
.ConfigurationNumber = 1,
|
||
|
.ConfigurationStrIndex = NO_DESCRIPTOR,
|
||
14 years ago
|
|
||
13 years ago
|
.ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
|
||
14 years ago
|
|
||
15 years ago
|
.MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
|
||
|
},
|
||
|
|
||
14 years ago
|
.AVRISP_Interface =
|
||
15 years ago
|
{
|
||
|
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
|
||
|
|
||
|
.InterfaceNumber = 0,
|
||
|
.AlternateSetting = 0,
|
||
14 years ago
|
|
||
15 years ago
|
.TotalEndpoints = 2,
|
||
14 years ago
|
|
||
14 years ago
|
.Class = USB_CSCP_VendorSpecificClass,
|
||
13 years ago
|
.SubClass = USB_CSCP_NoDeviceSubclass,
|
||
|
.Protocol = USB_CSCP_NoDeviceProtocol,
|
||
14 years ago
|
|
||
15 years ago
|
.InterfaceStrIndex = NO_DESCRIPTOR
|
||
|
},
|
||
|
|
||
14 years ago
|
.AVRISP_DataInEndpoint =
|
||
15 years ago
|
{
|
||
|
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
|
||
14 years ago
|
|
||
13 years ago
|
#if defined(RESET_TOGGLES_LIBUSB_COMPAT)
|
||
|
.EndpointAddress = 0,
|
||
|
#else
|
||
13 years ago
|
.EndpointAddress = AVRISP_DATA_IN_EPADDR,
|
||
13 years ago
|
#endif
|
||
15 years ago
|
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
|
||
14 years ago
|
.EndpointSize = AVRISP_DATA_EPSIZE,
|
||
14 years ago
|
.PollingIntervalMS = 0x0A
|
||
15 years ago
|
},
|
||
|
|
||
|
.AVRISP_DataOutEndpoint =
|
||
|
{
|
||
|
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
|
||
14 years ago
|
|
||
13 years ago
|
.EndpointAddress = AVRISP_DATA_OUT_EPADDR,
|
||
15 years ago
|
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
|
||
14 years ago
|
.EndpointSize = AVRISP_DATA_EPSIZE,
|
||
14 years ago
|
.PollingIntervalMS = 0x0A
|
||
15 years ago
|
},
|
||
|
};
|
||
|
|
||
|
/** 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.
|
||
|
*/
|
||
13 years ago
|
const USB_Descriptor_String_t PROGMEM AVRISP_LanguageString =
|
||
15 years ago
|
{
|
||
|
.Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
|
||
14 years ago
|
|
||
15 years ago
|
.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.
|
||
|
*/
|
||
13 years ago
|
const USB_Descriptor_String_t PROGMEM AVRISP_ManufacturerString =
|
||
15 years ago
|
{
|
||
13 years ago
|
.Header = {.Size = USB_STRING_LEN(5), .Type = DTYPE_String},
|
||
14 years ago
|
|
||
13 years ago
|
.UnicodeString = L"ATMEL"
|
||
15 years ago
|
};
|
||
|
|
||
|
/** 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.
|
||
|
*/
|
||
13 years ago
|
const USB_Descriptor_String_t PROGMEM AVRISP_ProductString =
|
||
15 years ago
|
{
|
||
13 years ago
|
.Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
|
||
14 years ago
|
|
||
13 years ago
|
.UnicodeString = L"AVRISP mkII"
|
||
15 years ago
|
};
|
||
|
|
||
|
/** Serial number string. This is a Unicode string containing the device's unique serial number, expressed as a
|
||
|
* series of uppercase hexadecimal digits.
|
||
|
*/
|
||
13 years ago
|
const USB_Descriptor_String_t PROGMEM AVRISP_SerialString =
|
||
15 years ago
|
{
|
||
|
.Header = {.Size = USB_STRING_LEN(13), .Type = DTYPE_String},
|
||
13 years ago
|
|
||
|
.UnicodeString = L"000200012345\0" // Note: Real AVRISP-MKII has the embedded NUL byte, bug in firmware?
|
||
15 years ago
|
};
|
||
|
|
||
|
/** 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.
|
||
|
*/
|
||
13 years ago
|
uint16_t AVRISP_GetDescriptor(const uint16_t wValue,
|
||
|
const uint8_t wIndex,
|
||
|
const void** const DescriptorAddress,
|
||
|
uint8_t* DescriptorMemorySpace)
|
||
15 years ago
|
{
|
||
|
const uint8_t DescriptorType = (wValue >> 8);
|
||
|
const uint8_t DescriptorNumber = (wValue & 0xFF);
|
||
|
|
||
15 years ago
|
const void* Address = NULL;
|
||
|
uint16_t Size = NO_DESCRIPTOR;
|
||
14 years ago
|
|
||
13 years ago
|
*DescriptorMemorySpace = MEMSPACE_FLASH;
|
||
|
|
||
15 years ago
|
switch (DescriptorType)
|
||
|
{
|
||
14 years ago
|
case DTYPE_Device:
|
||
13 years ago
|
Address = &AVRISP_DeviceDescriptor;
|
||
15 years ago
|
Size = sizeof(USB_Descriptor_Device_t);
|
||
|
break;
|
||
14 years ago
|
case DTYPE_Configuration:
|
||
13 years ago
|
*DescriptorMemorySpace = MEMSPACE_RAM;
|
||
|
#if defined(RESET_TOGGLES_LIBUSB_COMPAT)
|
||
13 years ago
|
AVRISP_ConfigurationDescriptor.AVRISP_DataInEndpoint.EndpointAddress = AVRISP_CurrDataINEndpointAddress;
|
||
13 years ago
|
#endif
|
||
|
|
||
13 years ago
|
Address = &AVRISP_ConfigurationDescriptor;
|
||
|
Size = sizeof(AVRISP_USB_Descriptor_Configuration_t);
|
||
15 years ago
|
break;
|
||
14 years ago
|
case DTYPE_String:
|
||
15 years ago
|
switch (DescriptorNumber)
|
||
|
{
|
||
14 years ago
|
case 0x00:
|
||
13 years ago
|
Address = &AVRISP_LanguageString;
|
||
|
Size = pgm_read_byte(&AVRISP_LanguageString.Header.Size);
|
||
15 years ago
|
break;
|
||
14 years ago
|
case 0x01:
|
||
13 years ago
|
Address = &AVRISP_ManufacturerString;
|
||
|
Size = pgm_read_byte(&AVRISP_ManufacturerString.Header.Size);
|
||
15 years ago
|
break;
|
||
14 years ago
|
case 0x02:
|
||
13 years ago
|
Address = &AVRISP_ProductString;
|
||
|
Size = pgm_read_byte(&AVRISP_ProductString.Header.Size);
|
||
15 years ago
|
break;
|
||
|
case 0x03:
|
||
13 years ago
|
Address = &AVRISP_SerialString;
|
||
|
Size = pgm_read_byte(&AVRISP_SerialString.Header.Size);
|
||
14 years ago
|
break;
|
||
15 years ago
|
}
|
||
14 years ago
|
|
||
15 years ago
|
break;
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
*DescriptorAddress = Address;
|
||
|
return Size;
|
||
|
}
|
||
14 years ago
|
|
||
13 years ago
|
#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
|