From 60fd0ff418566d85224f9841037ba01b77acc0d8 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Tue, 19 Jan 2010 05:27:58 +0000 Subject: Changed XPLAINBridge project to be both a USB to USART bridge and a PDI programmer, based on the state of a mode select pin. --- Projects/XPLAINBridge/Descriptors.c | 275 ------------------------------- Projects/XPLAINBridge/Descriptors.h | 84 ---------- Projects/XPLAINBridge/USARTDescriptors.c | 269 ++++++++++++++++++++++++++++++ Projects/XPLAINBridge/USARTDescriptors.h | 83 ++++++++++ Projects/XPLAINBridge/XPLAINBridge.c | 128 ++++++++++---- Projects/XPLAINBridge/XPLAINBridge.h | 27 ++- Projects/XPLAINBridge/makefile | 26 ++- 7 files changed, 490 insertions(+), 402 deletions(-) delete mode 100644 Projects/XPLAINBridge/Descriptors.c delete mode 100644 Projects/XPLAINBridge/Descriptors.h create mode 100644 Projects/XPLAINBridge/USARTDescriptors.c create mode 100644 Projects/XPLAINBridge/USARTDescriptors.h (limited to 'Projects/XPLAINBridge') diff --git a/Projects/XPLAINBridge/Descriptors.c b/Projects/XPLAINBridge/Descriptors.c deleted file mode 100644 index 87746991e..000000000 --- a/Projects/XPLAINBridge/Descriptors.c +++ /dev/null @@ -1,275 +0,0 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2010. - - dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com -*/ - -/* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that the copyright notice and this - 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 - 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 - * - * USB Device Descriptors, for library use when in USB device mode. Descriptors are special - * computer-readable structures which the host requests upon device enumeration, to determine - * the device's capabilities and functions. - */ - -#include "Descriptors.h" - -/* On some devices, there is a factory set internal serial number which can be automatically sent to the host as - * the device's serial number when the Device Descriptor's .SerialNumStrIndex entry is set to USE_INTERNAL_SERIAL. - * This allows the host to track a device across insertions on different ports, allowing them to retain allocated - * resources like COM port numbers and drivers. On demos using this feature, give a warning on unsupported devices - * so that the user can supply their own serial number descriptor instead or remove the USE_INTERNAL_SERIAL value - * from the Device Descriptor (forcing the host to generate a serial number for each device from the VID, PID and - * port location). - */ -#if (USE_INTERNAL_SERIAL == NO_DESCRIPTOR) - #warning USE_INTERNAL_SERIAL is not available on this AVR - please manually construct a device serial descriptor. -#endif - -/** 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. - */ -USB_Descriptor_Device_t PROGMEM DeviceDescriptor = -{ - .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device}, - - .USBSpecification = VERSION_BCD(01.10), - .Class = 0x02, - .SubClass = 0x00, - .Protocol = 0x00, - - .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, - - .VendorID = 0x03EB, - .ProductID = 0x204B, - .ReleaseNumber = 0x0000, - - .ManufacturerStrIndex = 0x01, - .ProductStrIndex = 0x02, - .SerialNumStrIndex = USE_INTERNAL_SERIAL, - - .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. - */ -USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = -{ - .Config = - { - .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, - - .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), - .TotalInterfaces = 2, - - .ConfigurationNumber = 1, - .ConfigurationStrIndex = NO_DESCRIPTOR, - - .ConfigAttributes = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELFPOWERED), - - .MaxPowerConsumption = USB_CONFIG_POWER_MA(100) - }, - - .CCI_Interface = - { - .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, - - .InterfaceNumber = 0, - .AlternateSetting = 0, - - .TotalEndpoints = 1, - - .Class = 0x02, - .SubClass = 0x02, - .Protocol = 0x01, - - .InterfaceStrIndex = NO_DESCRIPTOR - }, - - .CDC_Functional_IntHeader = - { - .Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24}, - .SubType = 0x00, - - .Data = {0x01, 0x10} - }, - - .CDC_Functional_CallManagement = - { - .Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24}, - .SubType = 0x01, - - .Data = {0x03, 0x01} - }, - - .CDC_Functional_AbstractControlManagement = - { - .Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(1)), .Type = 0x24}, - .SubType = 0x02, - - .Data = {0x06} - }, - - .CDC_Functional_Union= - { - .Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24}, - .SubType = 0x06, - - .Data = {0x00, 0x01} - }, - - .ManagementEndpoint = - { - .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, - - .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | CDC_NOTIFICATION_EPNUM), - .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = CDC_NOTIFICATION_EPSIZE, - .PollingIntervalMS = 0xFF - }, - - .DCI_Interface = - { - .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, - - .InterfaceNumber = 1, - .AlternateSetting = 0, - - .TotalEndpoints = 2, - - .Class = 0x0A, - .SubClass = 0x00, - .Protocol = 0x00, - - .InterfaceStrIndex = NO_DESCRIPTOR - }, - - .DataOutEndpoint = - { - .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, - - .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_OUT | CDC_RX_EPNUM), - .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = CDC_TXRX_EPSIZE, - .PollingIntervalMS = 0x00 - }, - - .DataInEndpoint = - { - .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, - - .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | CDC_TX_EPNUM), - .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = CDC_TXRX_EPSIZE, - .PollingIntervalMS = 0x00 - } -}; - -/** 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. - */ -USB_Descriptor_String_t PROGMEM LanguageString = -{ - .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String}, - - .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. - */ -USB_Descriptor_String_t PROGMEM ManufacturerString = -{ - .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String}, - - .UnicodeString = L"Dean Camera" -}; - -/** 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. - */ -USB_Descriptor_String_t PROGMEM ProductString = -{ - .Header = {.Size = USB_STRING_LEN(18), .Type = DTYPE_String}, - - .UnicodeString = L"LUFA XPLAIN Bridge" -}; - -/** 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. - */ -uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress) -{ - const uint8_t DescriptorType = (wValue >> 8); - const uint8_t DescriptorNumber = (wValue & 0xFF); - - void* Address = NULL; - uint16_t Size = NO_DESCRIPTOR; - - switch (DescriptorType) - { - case DTYPE_Device: - Address = (void*)&DeviceDescriptor; - Size = sizeof(USB_Descriptor_Device_t); - break; - case DTYPE_Configuration: - Address = (void*)&ConfigurationDescriptor; - Size = sizeof(USB_Descriptor_Configuration_t); - break; - case DTYPE_String: - switch (DescriptorNumber) - { - case 0x00: - Address = (void*)&LanguageString; - Size = pgm_read_byte(&LanguageString.Header.Size); - break; - case 0x01: - Address = (void*)&ManufacturerString; - Size = pgm_read_byte(&ManufacturerString.Header.Size); - break; - case 0x02: - Address = (void*)&ProductString; - Size = pgm_read_byte(&ProductString.Header.Size); - break; - } - - break; - } - - *DescriptorAddress = Address; - return Size; -} diff --git a/Projects/XPLAINBridge/Descriptors.h b/Projects/XPLAINBridge/Descriptors.h deleted file mode 100644 index a5461dd74..000000000 --- a/Projects/XPLAINBridge/Descriptors.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2010. - - dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com -*/ - -/* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that the copyright notice and this - 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 - 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 - * - * Header file for Descriptors.c. - */ - -#ifndef _DESCRIPTORS_H_ -#define _DESCRIPTORS_H_ - - /* Includes: */ - #include - - #include - #include - - /* Macros: */ - /** Endpoint number of the CDC device-to-host notification IN endpoint. */ - #define CDC_NOTIFICATION_EPNUM 2 - - /** Endpoint number of the CDC device-to-host data IN endpoint. */ - #define CDC_TX_EPNUM 3 - - /** Endpoint number of the CDC host-to-device data OUT endpoint. */ - #define CDC_RX_EPNUM 4 - - /** Size in bytes of the CDC device-to-host notification IN endpoint. */ - #define CDC_NOTIFICATION_EPSIZE 8 - - /** Size in bytes of the CDC data IN and OUT endpoints. */ - #define CDC_TXRX_EPSIZE 16 - - /* Type Defines: */ - /** Type define for the device configuration descriptor structure. This must be defined in the - * application code, as the configuration descriptor contains several sub-descriptors which - * vary between devices, and which describe the device's usage to the host. - */ - typedef struct - { - USB_Descriptor_Configuration_Header_t Config; - USB_Descriptor_Interface_t CCI_Interface; - CDC_FUNCTIONAL_DESCRIPTOR(2) CDC_Functional_IntHeader; - CDC_FUNCTIONAL_DESCRIPTOR(2) CDC_Functional_CallManagement; - CDC_FUNCTIONAL_DESCRIPTOR(1) CDC_Functional_AbstractControlManagement; - CDC_FUNCTIONAL_DESCRIPTOR(2) CDC_Functional_Union; - USB_Descriptor_Endpoint_t ManagementEndpoint; - USB_Descriptor_Interface_t DCI_Interface; - USB_Descriptor_Endpoint_t DataOutEndpoint; - USB_Descriptor_Endpoint_t DataInEndpoint; - } USB_Descriptor_Configuration_t; - - /* Function Prototypes: */ - uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress) - ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(3); - -#endif diff --git a/Projects/XPLAINBridge/USARTDescriptors.c b/Projects/XPLAINBridge/USARTDescriptors.c new file mode 100644 index 000000000..d8587f2b2 --- /dev/null +++ b/Projects/XPLAINBridge/USARTDescriptors.c @@ -0,0 +1,269 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +/* + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + 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 + 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 + * + * USB Device Descriptors, for library use when in USB device mode. Descriptors are special + * computer-readable structures which the host requests upon device enumeration, to determine + * the device's capabilities and functions. + */ + +#include "USARTDescriptors.h" + +/* On some devices, there is a factory set internal serial number which can be automatically sent to the host as + * the device's serial number when the Device Descriptor's .SerialNumStrIndex entry is set to USE_INTERNAL_SERIAL. + * This allows the host to track a device across insertions on different ports, allowing them to retain allocated + * resources like COM port numbers and drivers. On demos using this feature, give a warning on unsupported devices + * so that the user can supply their own serial number descriptor instead or remove the USE_INTERNAL_SERIAL value + * from the Device Descriptor (forcing the host to generate a serial number for each device from the VID, PID and + * port location). + */ +#if (USE_INTERNAL_SERIAL == NO_DESCRIPTOR) + #warning USE_INTERNAL_SERIAL is not available on this AVR - please manually construct a device serial descriptor. +#endif + +/** 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. + */ +USB_Descriptor_Device_t PROGMEM USART_DeviceDescriptor = +{ + .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device}, + + .USBSpecification = VERSION_BCD(01.10), + .Class = 0x02, + .SubClass = 0x00, + .Protocol = 0x00, + + .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, + + .VendorID = 0x03EB, + .ProductID = 0x204B, + .ReleaseNumber = 0x0000, + + .ManufacturerStrIndex = 0x01, + .ProductStrIndex = 0x02, + .SerialNumStrIndex = USE_INTERNAL_SERIAL, + + .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. + */ +USART_USB_Descriptor_Configuration_t PROGMEM USART_ConfigurationDescriptor = +{ + .Config = + { + .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, + + .TotalConfigurationSize = sizeof(USART_USB_Descriptor_Configuration_t), + .TotalInterfaces = 2, + + .ConfigurationNumber = 1, + .ConfigurationStrIndex = NO_DESCRIPTOR, + + .ConfigAttributes = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELFPOWERED), + + .MaxPowerConsumption = USB_CONFIG_POWER_MA(100) + }, + + .CCI_Interface = + { + .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + + .InterfaceNumber = 0, + .AlternateSetting = 0, + + .TotalEndpoints = 1, + + .Class = 0x02, + .SubClass = 0x02, + .Protocol = 0x01, + + .InterfaceStrIndex = NO_DESCRIPTOR + }, + + .CDC_Functional_IntHeader = + { + .Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24}, + .SubType = 0x00, + + .Data = {0x01, 0x10} + }, + + .CDC_Functional_CallManagement = + { + .Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24}, + .SubType = 0x01, + + .Data = {0x03, 0x01} + }, + + .CDC_Functional_AbstractControlManagement = + { + .Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(1)), .Type = 0x24}, + .SubType = 0x02, + + .Data = {0x06} + }, + + .CDC_Functional_Union= + { + .Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24}, + .SubType = 0x06, + + .Data = {0x00, 0x01} + }, + + .ManagementEndpoint = + { + .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + + .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | CDC_NOTIFICATION_EPNUM), + .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = CDC_NOTIFICATION_EPSIZE, + .PollingIntervalMS = 0xFF + }, + + .DCI_Interface = + { + .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + + .InterfaceNumber = 1, + .AlternateSetting = 0, + + .TotalEndpoints = 2, + + .Class = 0x0A, + .SubClass = 0x00, + .Protocol = 0x00, + + .InterfaceStrIndex = NO_DESCRIPTOR + }, + + .DataOutEndpoint = + { + .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + + .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_OUT | CDC_RX_EPNUM), + .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = CDC_TXRX_EPSIZE, + .PollingIntervalMS = 0x00 + }, + + .DataInEndpoint = + { + .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + + .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | CDC_TX_EPNUM), + .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = CDC_TXRX_EPSIZE, + .PollingIntervalMS = 0x00 + } +}; + +/** 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. + */ +USB_Descriptor_String_t PROGMEM USART_LanguageString = +{ + .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String}, + + .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. + */ +USB_Descriptor_String_t PROGMEM USART_ManufacturerString = +{ + .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String}, + + .UnicodeString = L"Dean Camera" +}; + +/** 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. + */ +USB_Descriptor_String_t PROGMEM USART_ProductString = +{ + .Header = {.Size = USB_STRING_LEN(18), .Type = DTYPE_String}, + + .UnicodeString = L"LUFA XPLAIN Bridge" +}; + +uint16_t USART_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress) +{ + const uint8_t DescriptorType = (wValue >> 8); + const uint8_t DescriptorNumber = (wValue & 0xFF); + + void* Address = NULL; + uint16_t Size = NO_DESCRIPTOR; + + switch (DescriptorType) + { + case DTYPE_Device: + Address = (void*)&USART_DeviceDescriptor; + Size = sizeof(USB_Descriptor_Device_t); + break; + case DTYPE_Configuration: + Address = (void*)&USART_ConfigurationDescriptor; + Size = sizeof(USART_USB_Descriptor_Configuration_t); + break; + case DTYPE_String: + switch (DescriptorNumber) + { + case 0x00: + Address = (void*)&USART_LanguageString; + Size = pgm_read_byte(&USART_LanguageString.Header.Size); + break; + case 0x01: + Address = (void*)&USART_ManufacturerString; + Size = pgm_read_byte(&USART_ManufacturerString.Header.Size); + break; + case 0x02: + Address = (void*)&USART_ProductString; + Size = pgm_read_byte(&USART_ProductString.Header.Size); + break; + } + + break; + } + + *DescriptorAddress = Address; + return Size; +} diff --git a/Projects/XPLAINBridge/USARTDescriptors.h b/Projects/XPLAINBridge/USARTDescriptors.h new file mode 100644 index 000000000..7b5a39112 --- /dev/null +++ b/Projects/XPLAINBridge/USARTDescriptors.h @@ -0,0 +1,83 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +/* + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + 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 + 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 + * + * Header file for USARTDescriptors.c. + */ + +#ifndef _USART_DESCRIPTORS_H_ +#define _USART_DESCRIPTORS_H_ + + /* Includes: */ + #include + + #include + #include + + /* Macros: */ + /** Endpoint number of the CDC device-to-host notification IN endpoint. */ + #define CDC_NOTIFICATION_EPNUM 2 + + /** Endpoint number of the CDC device-to-host data IN endpoint. */ + #define CDC_TX_EPNUM 3 + + /** Endpoint number of the CDC host-to-device data OUT endpoint. */ + #define CDC_RX_EPNUM 4 + + /** Size in bytes of the CDC device-to-host notification IN endpoint. */ + #define CDC_NOTIFICATION_EPSIZE 8 + + /** Size in bytes of the CDC data IN and OUT endpoints. */ + #define CDC_TXRX_EPSIZE 16 + + /* Type Defines: */ + /** Type define for the device configuration descriptor structure. This must be defined in the + * application code, as the configuration descriptor contains several sub-descriptors which + * vary between devices, and which describe the device's usage to the host. + */ + typedef struct + { + USB_Descriptor_Configuration_Header_t Config; + USB_Descriptor_Interface_t CCI_Interface; + CDC_FUNCTIONAL_DESCRIPTOR(2) CDC_Functional_IntHeader; + CDC_FUNCTIONAL_DESCRIPTOR(2) CDC_Functional_CallManagement; + CDC_FUNCTIONAL_DESCRIPTOR(1) CDC_Functional_AbstractControlManagement; + CDC_FUNCTIONAL_DESCRIPTOR(2) CDC_Functional_Union; + USB_Descriptor_Endpoint_t ManagementEndpoint; + USB_Descriptor_Interface_t DCI_Interface; + USB_Descriptor_Endpoint_t DataOutEndpoint; + USB_Descriptor_Endpoint_t DataInEndpoint; + } USART_USB_Descriptor_Configuration_t; + + /* Function Prototypes: */ + uint16_t USART_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress); + +#endif diff --git a/Projects/XPLAINBridge/XPLAINBridge.c b/Projects/XPLAINBridge/XPLAINBridge.c index 37509a981..2de545f95 100644 --- a/Projects/XPLAINBridge/XPLAINBridge.c +++ b/Projects/XPLAINBridge/XPLAINBridge.c @@ -36,11 +36,8 @@ #include "XPLAINBridge.h" -/** Circular buffer to hold data from the host before it is sent to the device via the serial port. */ -RingBuff_t USBtoUART_Buffer; - -/** Circular buffer to hold data from the serial port before it is sent to the host. */ -RingBuff_t UARTtoUSB_Buffer; +/* Current firmware mode, making the device behave as either a programmer or a USART bridge */ +bool CurrentFirmwareMode = MODE_PDI_PROGRAMMER; /** LUFA CDC Class driver interface configuration and state information. This structure is * passed to all CDC Class driver functions, so that multiple instances of the same class @@ -65,6 +62,13 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = .NotificationEndpointDoubleBank = false, }, }; + +/** Circular buffer to hold data from the host before it is sent to the device via the serial port. */ +RingBuff_t USBtoUART_Buffer; + +/** Circular buffer to hold data from the serial port before it is sent to the host. */ +RingBuff_t UARTtoUSB_Buffer; + /** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. @@ -78,32 +82,60 @@ int main(void) for (;;) { - /* Read bytes from the USB OUT endpoint into the UART transmit buffer */ - for (uint8_t DataBytesRem = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--) + if (USB_DeviceState == DEVICE_STATE_Configured) { - if (!(BUFF_STATICSIZE - USBtoUART_Buffer.Elements)) - break; - - Buffer_StoreElement(&USBtoUART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface)); + if (CurrentFirmwareMode == MODE_USART_BRIDGE) + USARTBridge_Task(); + else + AVRISP_Task(); } - /* Read bytes from the UART receive buffer into the USB IN endpoint */ - if (UARTtoUSB_Buffer.Elements) - CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&UARTtoUSB_Buffer)); - - /* Load bytes from the UART transmit buffer into the UART */ - if ((USBtoUART_Buffer.Elements) && SoftUART_IsReady()) - SoftUART_TxByte(Buffer_GetElement(&USBtoUART_Buffer)); - - /* Load bytes from the UART into the UART receive buffer */ - if(SoftUART_IsReceived()) - Buffer_StoreElement(&UARTtoUSB_Buffer, SoftUART_RxByte()); - - CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } } +void AVRISP_Task(void) +{ + Endpoint_SelectEndpoint(AVRISP_DATA_EPNUM); + + /* Check to see if a V2 Protocol command has been received */ + if (Endpoint_IsOUTReceived()) + { + LEDs_SetAllLEDs(LEDMASK_BUSY); + + /* Pass off processing of the V2 Protocol command to the V2 Protocol handler */ + V2Protocol_ProcessCommand(); + + LEDs_SetAllLEDs(LEDMASK_USB_READY); + } +} + +void USARTBridge_Task(void) +{ + /* Read bytes from the USB OUT endpoint into the UART transmit buffer */ + for (uint8_t DataBytesRem = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--) + { + if (!(BUFF_STATICSIZE - USBtoUART_Buffer.Elements)) + break; + + Buffer_StoreElement(&USBtoUART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface)); + } + + /* Read bytes from the UART receive buffer into the USB IN endpoint */ + if (UARTtoUSB_Buffer.Elements) + CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&UARTtoUSB_Buffer)); + + /* Load bytes from the UART transmit buffer into the UART */ + if ((USBtoUART_Buffer.Elements) && SoftUART_IsReady()) + SoftUART_TxByte(Buffer_GetElement(&USBtoUART_Buffer)); + + /* Load bytes from the UART into the UART receive buffer */ + if(SoftUART_IsReceived()) + Buffer_StoreElement(&UARTtoUSB_Buffer, SoftUART_RxByte()); + + CDC_Device_USBTask(&VirtualSerial_CDC_Interface); +} + /** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { @@ -118,21 +150,59 @@ void SetupHardware(void) SoftUART_Init(); LEDs_Init(); USB_Init(); + V2Protocol_Init(); - PORTD |= (1 << 5); // PD5 is connected to the XMEGA /RESET, enable pullup + /* Disable JTAG debugging */ + MCUCR |= (1 << JTD); + MCUCR |= (1 << JTD); + + /* Enable pullup on the JTAG TDI pin so we can use it to select the mode */ + PORTF |= (1 << 7); + _delay_ms(10); + + /* Select the firmware mode based on the JTD pin's value */ + CurrentFirmwareMode = (PINF & (1 << 7)) ? MODE_USART_BRIDGE : MODE_PDI_PROGRAMMER; } /** Event handler for the library USB Configuration Changed event. */ void EVENT_USB_Device_ConfigurationChanged(void) { - LEDs_SetAllLEDs(LEDS_LED1); + bool EndpointConfigSuccess; + + if (CurrentFirmwareMode == MODE_USART_BRIDGE) + { + EndpointConfigSuccess = CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface); + } + else + { + EndpointConfigSuccess = Endpoint_ConfigureEndpoint(AVRISP_DATA_EPNUM, EP_TYPE_BULK, + ENDPOINT_DIR_OUT, AVRISP_DATA_EPSIZE, + ENDPOINT_BANK_SINGLE); + } - if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface))) - LEDs_SetAllLEDs(LEDS_NO_LEDS); + if (EndpointConfigSuccess) + LEDs_SetAllLEDs(LEDMASK_USB_READY); + else + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); } /** Event handler for the library USB Unhandled Control Request event. */ void EVENT_USB_Device_UnhandledControlRequest(void) { - CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); + if (CurrentFirmwareMode == MODE_USART_BRIDGE) + CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); +} + +/** 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. + */ +uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress) +{ + if (CurrentFirmwareMode == MODE_USART_BRIDGE) + return USART_GetDescriptor(wValue, wIndex, DescriptorAddress); + else + return AVRISP_GetDescriptor(wValue, wIndex, DescriptorAddress); } diff --git a/Projects/XPLAINBridge/XPLAINBridge.h b/Projects/XPLAINBridge/XPLAINBridge.h index 6b8d2a5f3..543a03e72 100644 --- a/Projects/XPLAINBridge/XPLAINBridge.h +++ b/Projects/XPLAINBridge/XPLAINBridge.h @@ -41,10 +41,12 @@ #include #include - #include "Descriptors.h" + #include "AVRISPDescriptors.h" + #include "USARTDescriptors.h" #include "Lib/RingBuff.h" - #include "Lib/SoftUART.h" + #include "Lib/SoftUART.h" + #include #include #include @@ -53,23 +55,36 @@ /* Macros: */ /** LED mask for the library LED driver, to indicate that the USB interface is not ready. */ - #define LEDMASK_USB_NOTREADY LEDS_LED1 + #define LEDMASK_USB_NOTREADY LEDS_LED1 /** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */ - #define LEDMASK_USB_ENUMERATING (LEDS_LED2 | LEDS_LED3) + #define LEDMASK_USB_ENUMERATING LEDS_LED1 /** LED mask for the library LED driver, to indicate that the USB interface is ready. */ - #define LEDMASK_USB_READY (LEDS_LED2 | LEDS_LED4) + #define LEDMASK_USB_READY LEDS_NO_LEDS /** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */ - #define LEDMASK_USB_ERROR (LEDS_LED1 | LEDS_LED3) + #define LEDMASK_USB_ERROR LEDS_LED1 + + /** LED mask for the library LED driver, to indicate that the USB interface is busy. */ + #define LEDMASK_BUSY LEDS_LED1 + + #define MODE_USART_BRIDGE false + #define MODE_PDI_PROGRAMMER true + + /* External Variables: */ + extern bool CurrentFirmwareMode; /* Function Prototypes: */ void SetupHardware(void); + void AVRISP_Task(void); + void USARTBridge_Task(void); void EVENT_USB_Device_ConfigurationChanged(void); void EVENT_USB_Device_UnhandledControlRequest(void); void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo); + uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress); + #endif diff --git a/Projects/XPLAINBridge/makefile b/Projects/XPLAINBridge/makefile index fc7244ff4..831d53bbd 100644 --- a/Projects/XPLAINBridge/makefile +++ b/Projects/XPLAINBridge/makefile @@ -114,6 +114,8 @@ OBJDIR = . # Path to the LUFA library LUFA_PATH = ../../ +# Path to the LUFA AVRISP-MKII project +AVRISP_PATH = ../AVRISP-MKII/ # LUFA library compile-time options LUFA_OPTS = -D USB_DEVICE_ONLY @@ -125,9 +127,18 @@ LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENAB # List C source files here. (C dependencies are automatically generated.) SRC = $(TARGET).c \ - Descriptors.c \ + AVRISPDescriptors.c \ + USARTDescriptors.c \ Lib/RingBuff.c \ Lib/SoftUART.c \ + $(AVRISP_PATH)/Lib/V2Protocol.c \ + $(AVRISP_PATH)/Lib/V2ProtocolParams.c \ + $(AVRISP_PATH)/Lib/ISP/ISPProtocol.c \ + $(AVRISP_PATH)/Lib/ISP/ISPTarget.c \ + $(AVRISP_PATH)/Lib/XPROG/XPROGProtocol.c \ + $(AVRISP_PATH)/Lib/XPROG/XPROGTarget.c \ + $(AVRISP_PATH)/Lib/XPROG/XMEGANVM.c \ + $(AVRISP_PATH)/Lib/XPROG/TINYNVM.c \ $(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/DevChapter9.c \ $(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Endpoint.c \ $(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Host.c \ @@ -173,7 +184,7 @@ DEBUG = dwarf-2 # Each directory must be seperated by a space. # Use forward slashes for directory separators. # For a directory that has spaces, enclose it in quotes. -EXTRAINCDIRS = $(LUFA_PATH)/ +EXTRAINCDIRS = $(LUFA_PATH)/ $(AVRISP_PATH)/ # Compiler flag to set the C Standard level. @@ -186,12 +197,11 @@ CSTANDARD = -std=gnu99 # Place -D or -U options here for C sources CDEFS = -DF_CPU=$(F_CPU)UL -DF_CLOCK=$(F_CLOCK)UL -DBOARD=BOARD_$(BOARD) $(LUFA_OPTS) -CDEFS += -DAVR_RESET_LINE_PORT="PORTD" -CDEFS += -DAVR_RESET_LINE_DDR="DDRD" -CDEFS += -DAVR_RESET_LINE_MASK="(1 << 4)" -CDEFS += -DAVR_RESET_PULSE_MS=10 -CDEFS += -DTX_RX_LED_PULSE_MS=30 -CDEFS += -DPING_PONG_LED_PULSE_MS=100 +CDEFS += -DAUX_LINE_PORT=PORTB +CDEFS += -DAUX_LINE_PIN=PINB +CDEFS += -DAUX_LINE_DDR=DDRB +CDEFS += -DAUX_LINE_MASK="(1 << 4)" +CDEFS += -DVTARGET_ADC_CHANNEL=2 # Place -D or -U options here for ASM sources ADEFS = -DF_CPU=$(F_CPU) -- cgit v1.2.3