Commit 60fd0ff4 authored by Dean Camera's avatar Dean Camera
Browse files

Changed XPLAINBridge project to be both a USB to USART bridge and a PDI...

Changed XPLAINBridge project to be both a USB to USART bridge and a PDI programmer, based on the state of a mode select pin.
parent 27461546
......@@ -181,8 +181,8 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
.InterfaceNumber = 0x01,
.AlternateSetting = 0x00,
.InterfaceNumber = 1,
.AlternateSetting = 0,
.TotalEndpoints = 1,
......
This diff is collapsed.
......@@ -5,7 +5,7 @@
www.fourwalledcubicle.com
*/
#include "TWI.h"
#include "../TWI.h"
bool TWI_StartTransmission(uint8_t SlaveAddress)
{
......
......@@ -42,8 +42,6 @@
int main(void)
{
SetupHardware();
V2Params_LoadNonVolatileParamValues();
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
......@@ -70,18 +68,7 @@ void SetupHardware(void)
/* Hardware Initialization */
LEDs_Init();
USB_Init();
#if defined(ADC)
/* Initialize the ADC converter for VTARGET level detection on supported AVR models */
ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128);
ADC_SetupChannel(VTARGET_ADC_CHANNEL);
ADC_StartReading(VTARGET_ADC_CHANNEL | ADC_RIGHT_ADJUSTED | ADC_REFERENCE_AVCC);
#endif
/* Millisecond timer initialization for managing the command timeout counter */
OCR0A = ((F_CPU / 64) / 1000);
TCCR0A = (1 << WGM01);
TCCR0B = ((1 << CS01) | (1 << CS00));
V2Protocol_Init();
}
/** Event handler for the library USB Connection event. */
......
......@@ -54,7 +54,7 @@ void ISPProtocol_EnterISPMode(void)
uint8_t EnterProgBytes[4];
} Enter_ISP_Params;
Endpoint_Read_Stream_LE(&Enter_ISP_Params, sizeof(Enter_ISP_Params));
Endpoint_Read_Stream_LE(&Enter_ISP_Params, sizeof(Enter_ISP_Params), NO_STREAM_CALLBACK);
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
......@@ -107,7 +107,7 @@ void ISPProtocol_LeaveISPMode(void)
uint8_t PostDelayMS;
} Leave_ISP_Params;
Endpoint_Read_Stream_LE(&Leave_ISP_Params, sizeof(Leave_ISP_Params));
Endpoint_Read_Stream_LE(&Leave_ISP_Params, sizeof(Leave_ISP_Params), NO_STREAM_CALLBACK);
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
......@@ -142,7 +142,7 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
} Write_Memory_Params; // whole page and ACK the packet as fast as possible to prevent it from aborting
Endpoint_Read_Stream_LE(&Write_Memory_Params, (sizeof(Write_Memory_Params) -
sizeof(Write_Memory_Params.ProgData)));
sizeof(Write_Memory_Params.ProgData)), NO_STREAM_CALLBACK);
Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite);
......@@ -158,7 +158,7 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
return;
}
Endpoint_Read_Stream_LE(&Write_Memory_Params.ProgData, Write_Memory_Params.BytesToWrite);
Endpoint_Read_Stream_LE(&Write_Memory_Params.ProgData, Write_Memory_Params.BytesToWrite, NO_STREAM_CALLBACK);
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
......@@ -287,7 +287,7 @@ void ISPProtocol_ReadMemory(uint8_t V2Command)
uint8_t ReadMemoryCommand;
} Read_Memory_Params;
Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params));
Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params), NO_STREAM_CALLBACK);
Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead);
Endpoint_ClearOUT();
......@@ -358,7 +358,7 @@ void ISPProtocol_ChipErase(void)
uint8_t EraseCommandBytes[4];
} Erase_Chip_Params;
Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params));
Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params), NO_STREAM_CALLBACK);
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
......@@ -393,7 +393,7 @@ void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command)
uint8_t ReadCommandBytes[4];
} Read_FuseLockSigOSCCAL_Params;
Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params));
Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params), NO_STREAM_CALLBACK);
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
......@@ -423,7 +423,7 @@ void ISPProtocol_WriteFuseLock(uint8_t V2Command)
uint8_t WriteCommandBytes[4];
} Write_FuseLockSig_Params;
Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params));
Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params), NO_STREAM_CALLBACK);
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
......@@ -449,8 +449,8 @@ void ISPProtocol_SPIMulti(void)
uint8_t TxData[255];
} SPI_Multi_Params;
Endpoint_Read_Stream_LE(&SPI_Multi_Params, sizeof(SPI_Multi_Params) - sizeof(SPI_Multi_Params.TxData));
Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes);
Endpoint_Read_Stream_LE(&SPI_Multi_Params, (sizeof(SPI_Multi_Params) - sizeof(SPI_Multi_Params.TxData)), NO_STREAM_CALLBACK);
Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes, NO_STREAM_CALLBACK);
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
......
......@@ -50,6 +50,24 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK)
TimeoutMSRemaining--;
}
/** Initializes the hardware and software associated with the V2 protocol command handling. */
void V2Protocol_Init(void)
{
#if defined(ADC)
/* Initialize the ADC converter for VTARGET level detection on supported AVR models */
ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128);
ADC_SetupChannel(VTARGET_ADC_CHANNEL);
ADC_StartReading(VTARGET_ADC_CHANNEL | ADC_RIGHT_ADJUSTED | ADC_REFERENCE_AVCC);
#endif
/* Millisecond timer initialization for managing the command timeout counter */
OCR0A = ((F_CPU / 64) / 1000);
TCCR0A = (1 << WGM01);
TCCR0B = ((1 << CS01) | (1 << CS00));
V2Params_LoadNonVolatileParamValues();
}
/** Master V2 Protocol packet handler, for received V2 Protocol packets from a connected host.
* This routine decodes the issued command and passes off the handling of the command to the
* appropriate function.
......@@ -160,7 +178,7 @@ static void V2Protocol_SignOn(void)
Endpoint_Write_Byte(CMD_SIGN_ON);
Endpoint_Write_Byte(STATUS_CMD_OK);
Endpoint_Write_Byte(sizeof(PROGRAMMER_ID) - 1);
Endpoint_Write_Stream_LE(PROGRAMMER_ID, (sizeof(PROGRAMMER_ID) - 1));
Endpoint_Write_Stream_LE(PROGRAMMER_ID, (sizeof(PROGRAMMER_ID) - 1), NO_STREAM_CALLBACK);
Endpoint_ClearIN();
}
......@@ -222,7 +240,7 @@ static void V2Protocol_GetSetParam(const uint8_t V2Command)
*/
static void V2Protocol_LoadAddress(void)
{
Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress));
Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress), NO_STREAM_CALLBACK);
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
......
......@@ -70,6 +70,7 @@
extern bool MustSetAddress;
/* Function Prototypes: */
void V2Protocol_Init(void);
void V2Protocol_ProcessCommand(void);
#if defined(INCLUDE_FROM_V2PROTOCOL_C)
......
......@@ -62,7 +62,7 @@ void XPROGProtocol_SetMode(void)
uint8_t Protocol;
} SetMode_XPROG_Params;
Endpoint_Read_Stream_LE(&SetMode_XPROG_Params, sizeof(SetMode_XPROG_Params));
Endpoint_Read_Stream_LE(&SetMode_XPROG_Params, sizeof(SetMode_XPROG_Params), NO_STREAM_CALLBACK);
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
......@@ -202,7 +202,7 @@ static void XPROGProtocol_Erase(void)
uint32_t Address;
} Erase_XPROG_Params;
Endpoint_Read_Stream_LE(&Erase_XPROG_Params, sizeof(Erase_XPROG_Params));
Endpoint_Read_Stream_LE(&Erase_XPROG_Params, sizeof(Erase_XPROG_Params), NO_STREAM_CALLBACK);
Erase_XPROG_Params.Address = SwapEndian_32(Erase_XPROG_Params.Address);
Endpoint_ClearOUT();
......@@ -262,10 +262,10 @@ static void XPROGProtocol_WriteMemory(void)
} WriteMemory_XPROG_Params;
Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params, (sizeof(WriteMemory_XPROG_Params) -
sizeof(WriteMemory_XPROG_Params).ProgData));
sizeof(WriteMemory_XPROG_Params).ProgData), NO_STREAM_CALLBACK);
WriteMemory_XPROG_Params.Address = SwapEndian_32(WriteMemory_XPROG_Params.Address);
WriteMemory_XPROG_Params.Length = SwapEndian_16(WriteMemory_XPROG_Params.Length);
Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length);
Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length, NO_STREAM_CALLBACK);
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
......@@ -351,7 +351,7 @@ static void XPROGProtocol_ReadMemory(void)
uint16_t Length;
} ReadMemory_XPROG_Params;
Endpoint_Read_Stream_LE(&ReadMemory_XPROG_Params, sizeof(ReadMemory_XPROG_Params));
Endpoint_Read_Stream_LE(&ReadMemory_XPROG_Params, sizeof(ReadMemory_XPROG_Params), NO_STREAM_CALLBACK);
ReadMemory_XPROG_Params.Address = SwapEndian_32(ReadMemory_XPROG_Params.Address);
ReadMemory_XPROG_Params.Length = SwapEndian_16(ReadMemory_XPROG_Params.Length);
......@@ -380,7 +380,7 @@ static void XPROGProtocol_ReadMemory(void)
Endpoint_Write_Byte(ReturnStatus);
if (ReturnStatus == XPRG_ERR_OK)
Endpoint_Write_Stream_LE(ReadBuffer, ReadMemory_XPROG_Params.Length);
Endpoint_Write_Stream_LE(ReadBuffer, ReadMemory_XPROG_Params.Length, NO_STREAM_CALLBACK);
Endpoint_ClearIN();
}
......@@ -397,7 +397,7 @@ static void XPROGProtocol_ReadCRC(void)
uint8_t CRCType;
} ReadCRC_XPROG_Params;
Endpoint_Read_Stream_LE(&ReadCRC_XPROG_Params, sizeof(ReadCRC_XPROG_Params));
Endpoint_Read_Stream_LE(&ReadCRC_XPROG_Params, sizeof(ReadCRC_XPROG_Params), NO_STREAM_CALLBACK);
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
......
......@@ -119,7 +119,6 @@ LUFA_PATH = ../../
LUFA_OPTS = -D USB_DEVICE_ONLY
LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=16
LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1
LUFA_OPTS += -D NO_STREAM_CALLBACKS
LUFA_OPTS += -D USE_FLASH_DESCRIPTORS
LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"
......
......@@ -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;
}
......
......@@ -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
......@@ -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);
}
......@@ -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);