diff options
Diffstat (limited to 'Projects/XPLAINBridge')
-rw-r--r-- | Projects/XPLAINBridge/USARTDescriptors.c (renamed from Projects/XPLAINBridge/Descriptors.c) | 40 | ||||
-rw-r--r-- | Projects/XPLAINBridge/USARTDescriptors.h (renamed from Projects/XPLAINBridge/Descriptors.h) | 15 | ||||
-rw-r--r-- | Projects/XPLAINBridge/XPLAINBridge.c | 128 | ||||
-rw-r--r-- | Projects/XPLAINBridge/XPLAINBridge.h | 27 | ||||
-rw-r--r-- | Projects/XPLAINBridge/makefile | 26 |
5 files changed, 162 insertions, 74 deletions
diff --git a/Projects/XPLAINBridge/Descriptors.c b/Projects/XPLAINBridge/USARTDescriptors.c index 87746991e..d8587f2b2 100644 --- a/Projects/XPLAINBridge/Descriptors.c +++ b/Projects/XPLAINBridge/USARTDescriptors.c @@ -35,7 +35,7 @@ * the device's capabilities and functions.
*/
-#include "Descriptors.h"
+#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.
@@ -54,7 +54,7 @@ * number of device configurations. The descriptor is read out by the USB host when the enumeration
* process begins.
*/
-USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
+USB_Descriptor_Device_t PROGMEM USART_DeviceDescriptor =
{
.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
@@ -81,13 +81,13 @@ USB_Descriptor_Device_t PROGMEM DeviceDescriptor = * 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 =
+USART_USB_Descriptor_Configuration_t PROGMEM USART_ConfigurationDescriptor =
{
.Config =
{
.Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
- .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
+ .TotalConfigurationSize = sizeof(USART_USB_Descriptor_Configuration_t),
.TotalInterfaces = 2,
.ConfigurationNumber = 1,
@@ -197,7 +197,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = * 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 =
+USB_Descriptor_String_t PROGMEM USART_LanguageString =
{
.Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
@@ -208,7 +208,7 @@ USB_Descriptor_String_t PROGMEM LanguageString = * 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 =
+USB_Descriptor_String_t PROGMEM USART_ManufacturerString =
{
.Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
@@ -219,20 +219,14 @@ USB_Descriptor_String_t PROGMEM ManufacturerString = * 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 =
+USB_Descriptor_String_t PROGMEM USART_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)
+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);
@@ -243,27 +237,27 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, switch (DescriptorType)
{
case DTYPE_Device:
- Address = (void*)&DeviceDescriptor;
+ Address = (void*)&USART_DeviceDescriptor;
Size = sizeof(USB_Descriptor_Device_t);
break;
case DTYPE_Configuration:
- Address = (void*)&ConfigurationDescriptor;
- Size = sizeof(USB_Descriptor_Configuration_t);
+ Address = (void*)&USART_ConfigurationDescriptor;
+ Size = sizeof(USART_USB_Descriptor_Configuration_t);
break;
case DTYPE_String:
switch (DescriptorNumber)
{
case 0x00:
- Address = (void*)&LanguageString;
- Size = pgm_read_byte(&LanguageString.Header.Size);
+ Address = (void*)&USART_LanguageString;
+ Size = pgm_read_byte(&USART_LanguageString.Header.Size);
break;
case 0x01:
- Address = (void*)&ManufacturerString;
- Size = pgm_read_byte(&ManufacturerString.Header.Size);
+ Address = (void*)&USART_ManufacturerString;
+ Size = pgm_read_byte(&USART_ManufacturerString.Header.Size);
break;
case 0x02:
- Address = (void*)&ProductString;
- Size = pgm_read_byte(&ProductString.Header.Size);
+ Address = (void*)&USART_ProductString;
+ Size = pgm_read_byte(&USART_ProductString.Header.Size);
break;
}
diff --git a/Projects/XPLAINBridge/Descriptors.h b/Projects/XPLAINBridge/USARTDescriptors.h index a5461dd74..7b5a39112 100644 --- a/Projects/XPLAINBridge/Descriptors.h +++ b/Projects/XPLAINBridge/USARTDescriptors.h @@ -30,11 +30,11 @@ /** \file
*
- * Header file for Descriptors.c.
+ * Header file for USARTDescriptors.c.
*/
-#ifndef _DESCRIPTORS_H_
-#define _DESCRIPTORS_H_
+#ifndef _USART_DESCRIPTORS_H_
+#define _USART_DESCRIPTORS_H_
/* Includes: */
#include <avr/pgmspace.h>
@@ -56,8 +56,8 @@ #define CDC_NOTIFICATION_EPSIZE 8
/** Size in bytes of the CDC data IN and OUT endpoints. */
- #define CDC_TXRX_EPSIZE 16
-
+ #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
@@ -75,10 +75,9 @@ USB_Descriptor_Interface_t DCI_Interface;
USB_Descriptor_Endpoint_t DataOutEndpoint;
USB_Descriptor_Endpoint_t DataInEndpoint;
- } USB_Descriptor_Configuration_t;
+ } USART_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);
+ 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 <avr/wdt.h>
#include <avr/power.h>
- #include "Descriptors.h"
+ #include "AVRISPDescriptors.h"
+ #include "USARTDescriptors.h"
#include "Lib/RingBuff.h"
- #include "Lib/SoftUART.h"
+ #include "Lib/SoftUART.h"
+ #include <Lib/V2Protocol.h>
#include <LUFA/Version.h>
#include <LUFA/Drivers/Board/LEDs.h>
@@ -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)
|