summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2015-06-29 21:40:01 +0200
committerChristian Pointner <equinox@spreadspace.org>2015-06-29 21:40:01 +0200
commit4a8fc8b532de721018eb4a337cc161bcdc6a9064 (patch)
treeec1f462904da2da5c41403ed33da05f859491313
parentstarted porting to newer lufa version (diff)
ported usb-serial to new lufa version
-rw-r--r--lib/lufa-descriptor-usbserial.c77
-rw-r--r--lib/lufa-descriptor-usbserial.h39
-rw-r--r--usb-serial/Makefile8
-rw-r--r--usb-serial/usb-serial.c92
4 files changed, 113 insertions, 103 deletions
diff --git a/lib/lufa-descriptor-usbserial.c b/lib/lufa-descriptor-usbserial.c
index cb83679..d407428 100644
--- a/lib/lufa-descriptor-usbserial.c
+++ b/lib/lufa-descriptor-usbserial.c
@@ -1,13 +1,13 @@
/*
LUFA Library
- Copyright (C) Dean Camera, 2012.
+ Copyright (C) Dean Camera, 2014.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
- Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
+ Copyright 2014 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
@@ -37,17 +37,6 @@
#include "lufa-descriptor-usbserial.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
@@ -58,7 +47,7 @@ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
{
.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
- .USBSpecification = VERSION_BCD(01.10),
+ .USBSpecification = VERSION_BCD(1,1,0),
.Class = CDC_CSCP_CDCClass,
.SubClass = CDC_CSCP_NoSpecificSubclass,
.Protocol = CDC_CSCP_NoSpecificProtocol,
@@ -67,10 +56,10 @@ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
.VendorID = 0x03EB,
.ProductID = 0x204B,
- .ReleaseNumber = VERSION_BCD(00.01),
+ .ReleaseNumber = VERSION_BCD(0,0,1),
- .ManufacturerStrIndex = 0x01,
- .ProductStrIndex = 0x02,
+ .ManufacturerStrIndex = STRING_ID_Manufacturer,
+ .ProductStrIndex = STRING_ID_Product,
.SerialNumStrIndex = USE_INTERNAL_SERIAL,
.NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
@@ -102,7 +91,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
- .InterfaceNumber = 0,
+ .InterfaceNumber = INTERFACE_ID_CDC_CCI,
.AlternateSetting = 0,
.TotalEndpoints = 1,
@@ -119,7 +108,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = DTYPE_CSInterface},
.Subtype = CDC_DSUBTYPE_CSInterface_Header,
- .CDCSpecification = VERSION_BCD(01.10),
+ .CDCSpecification = VERSION_BCD(1,1,0),
},
.CDC_Functional_ACM =
@@ -135,15 +124,15 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = DTYPE_CSInterface},
.Subtype = CDC_DSUBTYPE_CSInterface_Union,
- .MasterInterfaceNumber = 0,
- .SlaveInterfaceNumber = 1,
+ .MasterInterfaceNumber = INTERFACE_ID_CDC_CCI,
+ .SlaveInterfaceNumber = INTERFACE_ID_CDC_DCI,
},
.CDC_NotificationEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
- .EndpointAddress = (ENDPOINT_DIR_IN | CDC_NOTIFICATION_EPNUM),
+ .EndpointAddress = CDC_NOTIFICATION_EPADDR,
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_NOTIFICATION_EPSIZE,
.PollingIntervalMS = 0xFF
@@ -153,7 +142,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
- .InterfaceNumber = 1,
+ .InterfaceNumber = INTERFACE_ID_CDC_DCI,
.AlternateSetting = 0,
.TotalEndpoints = 2,
@@ -169,20 +158,20 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
- .EndpointAddress = (ENDPOINT_DIR_OUT | CDC_RX_EPNUM),
+ .EndpointAddress = CDC_RX_EPADDR,
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_TXRX_EPSIZE,
- .PollingIntervalMS = 0x01
+ .PollingIntervalMS = 0x05
},
.CDC_DataInEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
- .EndpointAddress = (ENDPOINT_DIR_IN | CDC_TX_EPNUM),
+ .EndpointAddress = CDC_TX_EPADDR,
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_TXRX_EPSIZE,
- .PollingIntervalMS = 0x01
+ .PollingIntervalMS = 0x05
}
};
@@ -190,43 +179,27 @@ const 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.
*/
-const USB_Descriptor_String_t PROGMEM LanguageString =
-{
- .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
-
- .UnicodeString = {LANGUAGE_ID_ENG}
-};
-
+const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(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.
*/
-const USB_Descriptor_String_t PROGMEM ManufacturerString =
-{
#ifndef USB_MANUFACTURER
- .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
- .UnicodeString = L"Dean Camera"
+const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera");
#else
- .Header = {.Size = USB_STRING_LEN(USB_MANUFACTURER_LEN), .Type = DTYPE_String},
- .UnicodeString = USB_MANUFACTURER
+const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(USB_MANUFACTURER);
#endif
-};
/** 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.
*/
-const USB_Descriptor_String_t PROGMEM ProductString =
-{
#ifndef USB_PRODUCT
- .Header = {.Size = USB_STRING_LEN(23), .Type = DTYPE_String},
- .UnicodeString = L"LUFA USB-RS232 Adapter"
+const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA USB-RS232 Adapter");
#else
- .Header = {.Size = USB_STRING_LEN(USB_PRODUCT_LEN), .Type = DTYPE_String},
- .UnicodeString = USB_PRODUCT
+const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(USB_PRODUCT);
#endif
-};
/** 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
@@ -257,15 +230,15 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
case DTYPE_String:
switch (DescriptorNumber)
{
- case 0x00:
+ case STRING_ID_Language:
Address = &LanguageString;
Size = pgm_read_byte(&LanguageString.Header.Size);
break;
- case 0x01:
+ case STRING_ID_Manufacturer:
Address = &ManufacturerString;
Size = pgm_read_byte(&ManufacturerString.Header.Size);
break;
- case 0x02:
+ case STRING_ID_Product:
Address = &ProductString;
Size = pgm_read_byte(&ProductString.Header.Size);
break;
diff --git a/lib/lufa-descriptor-usbserial.h b/lib/lufa-descriptor-usbserial.h
index 3bb021a..2c9ecaa 100644
--- a/lib/lufa-descriptor-usbserial.h
+++ b/lib/lufa-descriptor-usbserial.h
@@ -1,13 +1,13 @@
/*
LUFA Library
- Copyright (C) Dean Camera, 2012.
+ Copyright (C) Dean Camera, 2014.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
- Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
+ Copyright 2014 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
@@ -42,14 +42,14 @@
#include <LUFA/Drivers/USB/USB.h>
/* Macros: */
- /** Endpoint number of the CDC device-to-host notification IN endpoint. */
- #define CDC_NOTIFICATION_EPNUM 2
+ /** Endpoint address of the CDC device-to-host notification IN endpoint. */
+ #define CDC_NOTIFICATION_EPADDR (ENDPOINT_DIR_IN | 2)
- /** Endpoint number of the CDC device-to-host data IN endpoint. */
- #define CDC_TX_EPNUM 3
+ /** Endpoint address of the CDC device-to-host data IN endpoint. */
+ #define CDC_TX_EPADDR (ENDPOINT_DIR_IN | 3)
- /** Endpoint number of the CDC host-to-device data OUT endpoint. */
- #define CDC_RX_EPNUM 4
+ /** Endpoint address of the CDC host-to-device data OUT endpoint. */
+ #define CDC_RX_EPADDR (ENDPOINT_DIR_OUT | 4)
/** Size in bytes of the CDC device-to-host notification IN endpoint. */
#define CDC_NOTIFICATION_EPSIZE 8
@@ -79,6 +79,27 @@
USB_Descriptor_Endpoint_t CDC_DataInEndpoint;
} USB_Descriptor_Configuration_t;
+ /** Enum for the device interface descriptor IDs within the device. Each interface descriptor
+ * should have a unique ID index associated with it, which can be used to refer to the
+ * interface from other descriptors.
+ */
+ enum InterfaceDescriptors_t
+ {
+ INTERFACE_ID_CDC_CCI = 0, /**< CDC CCI interface descriptor ID */
+ INTERFACE_ID_CDC_DCI = 1, /**< CDC DCI interface descriptor ID */
+ };
+
+ /** Enum for the device string descriptor IDs within the device. Each string descriptor should
+ * have a unique ID index associated with it, which can be used to refer to the string from
+ * other descriptors.
+ */
+ enum StringDescriptors_t
+ {
+ STRING_ID_Language = 0, /**< Supported Languages string descriptor ID (must be zero) */
+ STRING_ID_Manufacturer = 1, /**< Manufacturer string ID */
+ STRING_ID_Product = 2, /**< Product string ID */
+ };
+
/* Function Prototypes: */
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
const uint8_t wIndex,
diff --git a/usb-serial/Makefile b/usb-serial/Makefile
index 093a2c9..ba0fb9a 100644
--- a/usb-serial/Makefile
+++ b/usb-serial/Makefile
@@ -21,13 +21,13 @@
##
NAME := usb-serial
-BOARD_TYPE := slowpandongle2
+BOARD_TYPE := teensy2
OBJ := $(NAME).o
LIBS := util led lufa-descriptor-usbserial
EXTERNAL_LIBS := lufa
SPREADAVR_PATH := ..
-LUFA_PATH := $(SPREADAVR_PATH)/contrib/LUFA-120219
+LUFA_PATH := $(SPREADAVR_PATH)/contrib/lufa-LUFA-140928
LUFA_OPTS = -D USB_DEVICE_ONLY
LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0
LUFA_OPTS += -D ORDERED_EP_CONFIG
@@ -37,8 +37,8 @@ LUFA_OPTS += -D USE_FLASH_DESCRIPTORS
LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"
LUFA_OPTS += -D INTERRUPT_CONTROL_ENDPOINT
-LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\"" -D USB_MANUFACTURER_LEN=7
-LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-serial example\"" -D USB_PRODUCT_LEN=30
+LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\""
+LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-serial example\""
LUFA_COMPONENTS := USB USBCLASS SERIAL
diff --git a/usb-serial/usb-serial.c b/usb-serial/usb-serial.c
index f333675..6cfa30e 100644
--- a/usb-serial/usb-serial.c
+++ b/usb-serial/usb-serial.c
@@ -26,14 +26,14 @@
/*
LUFA Library
- Copyright (C) Dean Camera, 2012.
+ Copyright (C) Dean Camera, 2014.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
- Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
+ Copyright 2014 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
@@ -44,7 +44,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
@@ -71,10 +71,10 @@
#include "util.h"
#include "led.h"
-#include <LUFA/Version.h>
#include <LUFA/Drivers/Peripheral/Serial.h>
#include <LUFA/Drivers/Misc/RingBuffer.h>
#include <LUFA/Drivers/USB/USB.h>
+#include <LUFA/Platform/Platform.h>
/* Function Prototypes: */
void SetupHardware(void);
@@ -106,19 +106,25 @@ 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,
+ },
},
};
@@ -133,7 +139,7 @@ int main(void)
RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data));
RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data));
- sei();
+ GlobalInterruptEnable();
for (;;)
{
@@ -142,35 +148,42 @@ int main(void)
{
int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
- /* Read bytes from the USB OUT endpoint into the USART transmit buffer */
+ /* Store received byte into the USART transmit buffer */
if (!(ReceivedByte < 0))
RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte);
}
- /* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */
uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
- if ((TIFR0 & (1 << TOV0)) || (BufferCount > (uint8_t)(sizeof(USARTtoUSB_Buffer_Data) * .75)))
+ if (BufferCount)
{
- /* Clear flush timer expiry flag */
- TIFR0 |= (1 << TOV0);
+ Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address);
- /* Read bytes from the USART receive buffer into the USB IN endpoint */
- while (BufferCount--)
+ /* 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())
{
- /* 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)
+ /* 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--)
{
- break;
+ /* 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);
}
-
- /* 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 (!(RingBuffer_IsEmpty(&USBtoUSART_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);
@@ -188,9 +201,6 @@ void SetupHardware(void)
cpu_init();
led_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. */
@@ -227,7 +237,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);
}
@@ -265,6 +275,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;
@@ -277,5 +290,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);
}