diff --git a/lufa/Config/LUFAConfig.h b/lufa/Config/LUFAConfig.h index 0ec4635..f7d7e62 100644 --- a/lufa/Config/LUFAConfig.h +++ b/lufa/Config/LUFAConfig.h @@ -71,7 +71,7 @@ // #define USE_EEPROM_DESCRIPTORS // #define NO_INTERNAL_SERIAL #define FIXED_CONTROL_ENDPOINT_SIZE 8 -// #define DEVICE_STATE_AS_GPIOR {Insert Value Here} + #define DEVICE_STATE_AS_GPIOR 0 #define FIXED_NUM_CONFIGURATIONS 1 // #define CONTROL_ONLY_DEVICE #define INTERRUPT_CONTROL_ENDPOINT @@ -79,45 +79,12 @@ // #define NO_DEVICE_SELF_POWER /* USB Host Mode Driver Related Tokens: */ -// #define HOST_STATE_AS_GPIOR {Insert Value Here} +// #define HOST_STATE_AS_GPIOR 0 // #define USB_HOST_TIMEOUT_MS {Insert Value Here} // #define HOST_DEVICE_SETTLE_DELAY_MS {Insert Value Here} // #define NO_AUTO_VBUS_MANAGEMENT // #define INVERTED_VBUS_ENABLE_LINE - #elif (ARCH == ARCH_XMEGA) - - /* Non-USB Related Configuration Tokens: */ -// #define DISABLE_TERMINAL_CODES - - /* USB Class Driver Related Tokens: */ -// #define HID_HOST_BOOT_PROTOCOL_ONLY -// #define HID_STATETABLE_STACK_DEPTH {Insert Value Here} -// #define HID_USAGE_STACK_DEPTH {Insert Value Here} -// #define HID_MAX_COLLECTIONS {Insert Value Here} -// #define HID_MAX_REPORTITEMS {Insert Value Here} -// #define HID_MAX_REPORT_IDS {Insert Value Here} -// #define NO_CLASS_DRIVER_AUTOFLUSH - - /* General USB Driver Related Tokens: */ - #define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_RC32MCLKSRC | USB_OPT_BUSEVENT_PRIHIGH) -// #define USB_STREAM_TIMEOUT_MS {Insert Value Here} -// #define NO_LIMITED_CONTROLLER_CONNECT -// #define NO_SOF_EVENTS - - /* USB Device Mode Driver Related Tokens: */ -// #define USE_RAM_DESCRIPTORS - #define USE_FLASH_DESCRIPTORS -// #define USE_EEPROM_DESCRIPTORS -// #define NO_INTERNAL_SERIAL - #define FIXED_CONTROL_ENDPOINT_SIZE 8 -// #define DEVICE_STATE_AS_GPIOR {Insert Value Here} - #define FIXED_NUM_CONFIGURATIONS 1 -// #define CONTROL_ONLY_DEVICE - #define MAX_ENDPOINT_INDEX 4 -// #define NO_DEVICE_REMOTE_WAKEUP -// #define NO_DEVICE_SELF_POWER - #else #error Unsupported architecture for this LUFA configuration file. diff --git a/lufa/Descriptors.c b/lufa/Descriptors.c index 5ec042c..9827037 100644 --- a/lufa/Descriptors.c +++ b/lufa/Descriptors.c @@ -55,7 +55,7 @@ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor = .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, .VendorID = 0x03EB, - .ProductID = 0x2044, + .ProductID = 0x204B, .ReleaseNumber = VERSION_BCD(0,0,1), .ManufacturerStrIndex = STRING_ID_Manufacturer, @@ -191,7 +191,7 @@ const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device * Descriptor. */ -const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA CDC Demo"); +const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA USB-RS232 Adapter"); /** 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 diff --git a/lufa/Descriptors.h b/lufa/Descriptors.h index 5b4bf2a..b19682a 100644 --- a/lufa/Descriptors.h +++ b/lufa/Descriptors.h @@ -66,7 +66,7 @@ { USB_Descriptor_Configuration_Header_t Config; - // CDC Control Interface + // CDC Command Interface USB_Descriptor_Interface_t CDC_CCI_Interface; USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header; USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM; diff --git a/lufa/LUFA USBtoSerial.inf b/lufa/LUFA USBtoSerial.inf new file mode 100644 index 0000000..b00fab6 --- /dev/null +++ b/lufa/LUFA USBtoSerial.inf @@ -0,0 +1,66 @@ +;************************************************************ +; Windows USB CDC ACM Setup File +; Copyright (c) 2000 Microsoft Corporation +;************************************************************ + +[DefaultInstall] +CopyINF="LUFA USBtoSerial.inf" + +[Version] +Signature="$Windows NT$" +Class=Ports +ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318} +Provider=%MFGNAME% +DriverVer=7/1/2012,10.0.0.0 + +[Manufacturer] +%MFGNAME%=DeviceList, NTx86, NTamd64, NTia64 + +[SourceDisksNames] + +[SourceDisksFiles] + +[DestinationDirs] +DefaultDestDir=12 + +[DriverInstall] +Include=mdmcpq.inf +CopyFiles=FakeModemCopyFileSection +AddReg=DriverInstall.AddReg + +[DriverInstall.Services] +Include=mdmcpq.inf +AddService=usbser, 0x00000002, LowerFilter_Service_Inst + +[DriverInstall.AddReg] +HKR,,EnumPropPages32,,"msports.dll,SerialPortPropPageProvider" + +;------------------------------------------------------------------------------ +; Vendor and Product ID Definitions +;------------------------------------------------------------------------------ +; When developing your USB device, the VID and PID used in the PC side +; application program and the firmware on the microcontroller must match. +; Modify the below line to use your VID and PID. Use the format as shown below. +; Note: One INF file can be used for multiple devices with different VID and PIDs. +; For each supported device, append ",USB\VID_xxxx&PID_yyyy" to the end of the line. +;------------------------------------------------------------------------------ +[DeviceList] +%DESCRIPTION%=DriverInstall, USB\VID_03EB&PID_204B + +[DeviceList.NTx86] +%DESCRIPTION%=DriverInstall, USB\VID_03EB&PID_204B + +[DeviceList.NTamd64] +%DESCRIPTION%=DriverInstall, USB\VID_03EB&PID_204B + +[DeviceList.NTia64] +%DESCRIPTION%=DriverInstall, USB\VID_03EB&PID_204B + +;------------------------------------------------------------------------------ +; String Definitions +;------------------------------------------------------------------------------ +;Modify these strings to customize your device +;------------------------------------------------------------------------------ +[Strings] +MFGNAME="http://www.lufa-lib.org" +DESCRIPTION="LUFA USB to Serial" \ No newline at end of file diff --git a/lufa/USB_gadget.c b/lufa/USB_gadget.c index 0e6ffe1..a4db054 100644 --- a/lufa/USB_gadget.c +++ b/lufa/USB_gadget.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2010. + Copyright (C) Dean Camera, 2017. dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com + www.lufa-lib.org */ /* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2017 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 @@ -18,7 +18,7 @@ advertising or publicity pertaining to distribution of the software without specific, written prior permission. - The author disclaim all warranties with regard to this + The author disclaims 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 @@ -31,16 +31,22 @@ /** \file * * Main source file for the USBtoSerial project. This file contains the main tasks of - * the demo and is responsible for the initial application hardware configuration. + * the project and is responsible for the initial application hardware configuration. */ -#include "USBtoSerial.h" +#include "USB_gadget.h" /** Circular buffer to hold data from the host before it is sent to the device via the serial port. */ -RingBuff_t USBtoUSART_Buffer; +static RingBuffer_t USBtoUSART_Buffer; + +/** Underlying data buffer for \ref USBtoUSART_Buffer, where the stored bytes are located. */ +static uint8_t USBtoUSART_Buffer_Data[128]; /** Circular buffer to hold data from the serial port before it is sent to the host. */ -RingBuff_t USARTtoUSB_Buffer; +static RingBuffer_t USARTtoUSB_Buffer; + +/** Underlying data buffer for \ref USARTtoUSB_Buffer, where the stored bytes are located. */ +static uint8_t USARTtoUSB_Buffer_Data[128]; /** 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 @@ -50,22 +56,29 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = { .Config = { - .ControlInterfaceNumber = 0, - - .DataINEndpointNumber = CDC_TX_EPNUM, - .DataINEndpointSize = CDC_TXRX_EPSIZE, - .DataINEndpointDoubleBank = false, - - .DataOUTEndpointNumber = CDC_RX_EPNUM, - .DataOUTEndpointSize = CDC_TXRX_EPSIZE, - .DataOUTEndpointDoubleBank = false, - - .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, - .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, - .NotificationEndpointDoubleBank = false, + .ControlInterfaceNumber = INTERFACE_ID_CDC_CCI, + .DataINEndpoint = + { + .Address = CDC_TX_EPADDR, + .Size = CDC_TXRX_EPSIZE, + .Banks = 1, + }, + .DataOUTEndpoint = + { + .Address = CDC_RX_EPADDR, + .Size = CDC_TXRX_EPSIZE, + .Banks = 1, + }, + .NotificationEndpoint = + { + .Address = CDC_NOTIFICATION_EPADDR, + .Size = CDC_NOTIFICATION_EPSIZE, + .Banks = 1, + }, }, }; + /** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ @@ -73,33 +86,56 @@ int main(void) { SetupHardware(); - RingBuffer_InitBuffer(&USBtoUSART_Buffer); - RingBuffer_InitBuffer(&USARTtoUSB_Buffer); + RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data)); + RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data)); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); - sei(); + GlobalInterruptEnable(); for (;;) { - /* Read bytes from the USB OUT endpoint into the USART transmit buffer */ - int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); - if (!(ReceivedByte < 0) && !(RingBuffer_IsFull(&USBtoUSART_Buffer))) - RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte); - - /* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */ - RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); - if ((TIFR0 & (1 << TOV0)) || (BufferCount > 200)) + /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */ + if (!(RingBuffer_IsFull(&USBtoUSART_Buffer))) { - TIFR0 |= (1 << TOV0); + int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); - /* Read bytes from the USART receive buffer into the USB IN endpoint */ - while (BufferCount--) - CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&USARTtoUSB_Buffer)); + /* Store received byte into the USART transmit buffer */ + if (!(ReceivedByte < 0)) + RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte); } - /* Load the next byte from the USART transmit buffer into the USART */ - if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) - Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer)); + uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); + if (BufferCount) + { + Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address); + + /* Check if a packet is already enqueued to the host - if so, we shouldn't try to send more data + * until it completes as there is a chance nothing is listening and a lengthy timeout could occur */ + if (Endpoint_IsINReady()) + { + /* Never send more than one bank size less one byte to the host at a time, so that we don't block + * while a Zero Length Packet (ZLP) to terminate the transfer is sent if the host isn't listening */ + uint8_t BytesToSend = MIN(BufferCount, (CDC_TXRX_EPSIZE - 1)); + + /* Read bytes from the USART receive buffer into the USB IN endpoint */ + while (BytesToSend--) + { + /* Try to send the next byte of data to the host, abort if there is an error without dequeuing */ + if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, + RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError) + { + break; + } + + /* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */ + RingBuffer_Remove(&USARTtoUSB_Buffer); + } + } + } + + /* Load the next byte from the USART transmit buffer into the USART if transmit buffer space is available */ + if (Serial_IsSendReady() && !(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) + Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer)); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); @@ -109,19 +145,18 @@ int main(void) /** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { +#if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); +#endif /* Hardware Initialization */ LEDs_Init(); USB_Init(); - - /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */ - TCCR0B = (1 << CS02); } /** Event handler for the library USB Connection event. */ @@ -146,8 +181,8 @@ void EVENT_USB_Device_ConfigurationChanged(void) LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); } -/** Event handler for the library USB Unhandled Control Request event. */ -void EVENT_USB_Device_UnhandledControlRequest(void) +/** Event handler for the library USB Control Request reception event. */ +void EVENT_USB_Device_ControlRequest(void) { CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); } @@ -159,7 +194,7 @@ ISR(USART1_RX_vect, ISR_BLOCK) { uint8_t ReceivedByte = UDR1; - if (USB_DeviceState == DEVICE_STATE_Configured) + if ((USB_DeviceState == DEVICE_STATE_Configured) && !(RingBuffer_IsFull(&USARTtoUSB_Buffer))) RingBuffer_Insert(&USARTtoUSB_Buffer, ReceivedByte); } @@ -197,6 +232,9 @@ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCI break; } + /* Keep the TX line held high (idle) while the USART is reconfigured */ + PORTD |= (1 << 3); + /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */ UCSR1B = 0; UCSR1A = 0; @@ -209,4 +247,8 @@ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCI UCSR1C = ConfigMask; UCSR1A = (1 << U2X1); UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); + + /* Release the TX line after the USART has been reconfigured */ + PORTD &= ~(1 << 3); } + diff --git a/lufa/USB_gadget.h b/lufa/USB_gadget.h index 3b567d3..ed6880b 100644 --- a/lufa/USB_gadget.h +++ b/lufa/USB_gadget.h @@ -30,24 +30,23 @@ /** \file * - * Header file for VirtualSerial.c. + * Header file for USBtoSerial.c. */ -#ifndef _VIRTUALSERIAL_H_ -#define _VIRTUALSERIAL_H_ +#ifndef _USB_SERIAL_H_ +#define _USB_SERIAL_H_ /* Includes: */ #include #include - #include #include - #include - #include + #include #include "Descriptors.h" - //#include - //#include + #include + #include + #include #include #include @@ -66,12 +65,13 @@ /* Function Prototypes: */ void SetupHardware(void); - void CheckJoystickMovement(void); void EVENT_USB_Device_Connect(void); void EVENT_USB_Device_Disconnect(void); void EVENT_USB_Device_ConfigurationChanged(void); void EVENT_USB_Device_ControlRequest(void); + void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo); + #endif diff --git a/lufa/USBtoSerial.txt b/lufa/USBtoSerial.txt new file mode 100644 index 0000000..0620f93 --- /dev/null +++ b/lufa/USBtoSerial.txt @@ -0,0 +1,78 @@ +/** \file + * + * This file contains special DoxyGen information for the generation of the main page and other special + * documentation pages. It is not a project source file. + */ + +/** \mainpage USB to Serial Converter Project + * + * \section Sec_Compat Project Compatibility: + * + * The following list indicates what microcontrollers are compatible with this project. + * + * \li Series 7 USB AVRs (AT90USBxxx7) + * \li Series 6 USB AVRs (AT90USBxxx6) + * \li Series 4 USB AVRs (ATMEGAxxU4) + * \li Series 2 USB AVRs (AT90USBxx2, ATMEGAxxU2) + * + * \section Sec_Info USB Information: + * + * The following table gives a rundown of the USB utilization of this project. + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
USB Mode:Device
USB Class:Communications Device Class (CDC)
USB Subclass:Abstract Control Model (ACM)
Relevant Standards:USBIF CDC Class Standard
Supported USB Speeds:Full Speed Mode
+ * + * \section Sec_Description Project Description: + * + * USB to Serial bridge project. This project allows a USB AVR to serve + * as a USB to USART bridge between a USB host and a device lacking a + * USB port. When programmed into a USB AVR, the AVR will enumerate as a + * virtual COM port. + * + * The AVR's hardware USART's settings will change to mirror as closely as + * possible the serial settings set on the host. However, due to hardware + * limitations, some options may not be supported (baud rates with unacceptable + * error rates at the AVR's clock speed, data lengths other than 6, 7 or 8 bits, + * 1.5 stop bits, parity other than none, even or odd). + * + * After running this project for the first time on a new computer, + * you will need to supply the .INF file located in this project + * project's directory as the device's driver when running under + * Windows. This will enable Windows to use its inbuilt CDC drivers, + * negating the need for custom drivers for the device. Other + * Operating Systems should automatically use their own inbuilt + * CDC-ACM drivers. + * + * \section Sec_Options Project Options + * + * The following defines can be found in this project, which can control the project behaviour when defined, or changed in value. + * + * + * + * + * + *
+ * None + *
+ */ + diff --git a/lufa/asf.xml b/lufa/asf.xml index 4839b7c..5afcafe 100644 --- a/lufa/asf.xml +++ b/lufa/asf.xml @@ -1,31 +1,19 @@ - - + + - + - - + + - - - - - - - - - - - - - + - Virtual Serial device demo, implementing a virtual serial channel between the host PC and the device. This demo uses the user-friendly USB Class Driver APIs to provide a simple, abstracted interface into the USB stack. + USB to Serial USART converter project. @@ -41,12 +29,12 @@ - - + + - + - + @@ -54,9 +42,10 @@ + - + diff --git a/lufa/doxyfile b/lufa/doxyfile index aa21df5..4f79dc6 100644 --- a/lufa/doxyfile +++ b/lufa/doxyfile @@ -32,7 +32,7 @@ DOXYFILE_ENCODING = UTF-8 # title of most generated pages and in a few other places. # The default value is: My Project. -PROJECT_NAME = "LUFA Library - Virtual Serial Device Demo" +PROJECT_NAME = "LUFA Library - USB to Serial Device Project" # The PROJECT_NUMBER tag can be used to enter a project or revision number. This # could be handy for archiving the generated documentation or if some version diff --git a/lufa/makefile b/lufa/makefile index f0f7325..316e36b 100644 --- a/lufa/makefile +++ b/lufa/makefile @@ -17,9 +17,9 @@ BOARD = USBKEY F_CPU = 8000000 F_USB = $(F_CPU) OPTIMIZATION = s -TARGET = VirtualSerial +TARGET = USB_gadget SRC = $(TARGET).c Descriptors.c $(LUFA_SRC_USB) $(LUFA_SRC_USBCLASS) -LUFA_PATH = ../../../../LUFA +LUFA_PATH = ../../LUFA CC_FLAGS = -DUSE_LUFA_CONFIG_HEADER -IConfig/ LD_FLAGS = -- libgit2 0.21.2