summaryrefslogtreecommitdiff
path: root/usb-dual-serial
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2015-06-30 01:56:20 +0200
committerChristian Pointner <equinox@spreadspace.org>2015-06-30 01:56:20 +0200
commit6136d891b10c01af0e0108c14a3c4031ea44e826 (patch)
treebac70d78361e0eef6d6a45d7456ba249ca5c3883 /usb-dual-serial
parentported usb-midi to new lufa (diff)
ported usb-dua-serial to new lufa
Diffstat (limited to 'usb-dual-serial')
-rw-r--r--usb-dual-serial/Makefile6
-rw-r--r--usb-dual-serial/usb-dual-serial.c111
2 files changed, 70 insertions, 47 deletions
diff --git a/usb-dual-serial/Makefile b/usb-dual-serial/Makefile
index a2ea03c..8dc0e29 100644
--- a/usb-dual-serial/Makefile
+++ b/usb-dual-serial/Makefile
@@ -29,15 +29,15 @@ SPREADAVR_PATH := ..
RESET_FUNC := $(SPREADAVR_PATH)/tools/reset_lufa_cdc
RESET_PARAM := 'r'
-LUFA_PATH := $(SPREADAVR_PATH)/contrib/LUFA-120219
+LUFA_PATH := $(SPREADAVR_PATH)/contrib/lufa-LUFA-140928
LUFA_OPTS = -D USB_DEVICE_ONLY
LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=8
LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1
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 USB_MANUFACTURER="L\"equinox\"" -D USB_MANUFACTURER_LEN=7
-LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-dual-serial example\"" -D USB_PRODUCT_LEN=35
+LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\""
+LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-dual-serial example\""
LUFA_COMPONENTS := USB USBCLASS SERIAL
diff --git a/usb-dual-serial/usb-dual-serial.c b/usb-dual-serial/usb-dual-serial.c
index 02b50be..d70095a 100644
--- a/usb-dual-serial/usb-dual-serial.c
+++ b/usb-dual-serial/usb-dual-serial.c
@@ -2,7 +2,7 @@
* spreadspace avr utils
*
*
- * Copyright (C) 2013-2014 Christian Pointner <equinox@spreadspace.org>
+ * Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org>
*
* This file is part of spreadspace avr utils.
*
@@ -31,7 +31,7 @@
/*
LUFA Library
- Copyright (C) Dean Camera, 2012.
+ Copyright (C) Dean Camera, 2014.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
@@ -50,19 +50,25 @@ USB_ClassInfo_CDC_Device_t VirtualSerial1_CDC_Interface =
{
.Config =
{
- .ControlInterfaceNumber = 0,
-
- .DataINEndpointNumber = CDC1_TX_EPNUM,
- .DataINEndpointSize = CDC_TXRX_EPSIZE,
- .DataINEndpointDoubleBank = false,
-
- .DataOUTEndpointNumber = CDC1_RX_EPNUM,
- .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
- .DataOUTEndpointDoubleBank = false,
-
- .NotificationEndpointNumber = CDC1_NOTIFICATION_EPNUM,
- .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
- .NotificationEndpointDoubleBank = false,
+ .ControlInterfaceNumber = INTERFACE_ID_CDC1_CCI,
+ .DataINEndpoint =
+ {
+ .Address = CDC1_TX_EPADDR,
+ .Size = CDC_TXRX_EPSIZE,
+ .Banks = 1,
+ },
+ .DataOUTEndpoint =
+ {
+ .Address = CDC1_RX_EPADDR,
+ .Size = CDC_TXRX_EPSIZE,
+ .Banks = 1,
+ },
+ .NotificationEndpoint =
+ {
+ .Address = CDC1_NOTIFICATION_EPADDR,
+ .Size = CDC_NOTIFICATION_EPSIZE,
+ .Banks = 1,
+ },
},
};
@@ -70,19 +76,25 @@ USB_ClassInfo_CDC_Device_t VirtualSerial2_CDC_Interface =
{
.Config =
{
- .ControlInterfaceNumber = 2,
-
- .DataINEndpointNumber = CDC2_TX_EPNUM,
- .DataINEndpointSize = CDC_TXRX_EPSIZE,
- .DataINEndpointDoubleBank = false,
-
- .DataOUTEndpointNumber = CDC2_RX_EPNUM,
- .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
- .DataOUTEndpointDoubleBank = false,
-
- .NotificationEndpointNumber = CDC2_NOTIFICATION_EPNUM,
- .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
- .NotificationEndpointDoubleBank = false,
+ .ControlInterfaceNumber = INTERFACE_ID_CDC2_CCI,
+ .DataINEndpoint =
+ {
+ .Address = CDC2_TX_EPADDR,
+ .Size = CDC_TXRX_EPSIZE,
+ .Banks = 1,
+ },
+ .DataOUTEndpoint =
+ {
+ .Address = CDC2_RX_EPADDR,
+ .Size = CDC_TXRX_EPSIZE,
+ .Banks = 1,
+ },
+ .NotificationEndpoint =
+ {
+ .Address = CDC2_NOTIFICATION_EPADDR,
+ .Size = CDC_NOTIFICATION_EPSIZE,
+ .Banks = 1,
+ },
},
};
@@ -131,6 +143,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;
@@ -143,13 +158,16 @@ 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);
}
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);
}
@@ -157,30 +175,36 @@ void usbserial_init(void)
{
RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data));
RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data));
- TCCR0B = (1 << CS02);
}
void usbserial_task(void)
{
- if (!(RingBuffer_IsFull(&USBtoUSART_Buffer))) {
- int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial1_CDC_Interface);
- if (!(ReceivedByte < 0))
- RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte);
- }
+ if (!(RingBuffer_IsFull(&USBtoUSART_Buffer))) {
+ int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial1_CDC_Interface);
+ if (!(ReceivedByte < 0))
+ RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte);
+ }
- uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
- if ((TIFR0 & (1 << TOV0)) || (BufferCount > (uint8_t)(sizeof(USARTtoUSB_Buffer_Data) * .75))) {
- TIFR0 |= (1 << TOV0);
- while (BufferCount--) {
- if (CDC_Device_SendByte(&VirtualSerial1_CDC_Interface, RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
- break;
+ uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
+ if (BufferCount) {
+ Endpoint_SelectEndpoint(VirtualSerial1_CDC_Interface.Config.DataINEndpoint.Address);
+ if (Endpoint_IsINReady()) {
+ uint8_t BytesToSend = MIN(BufferCount, (CDC_TXRX_EPSIZE - 1));
+ while (BytesToSend--) {
+ if (CDC_Device_SendByte(&VirtualSerial1_CDC_Interface,
+ RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError) {
+ break;
+ }
RingBuffer_Remove(&USARTtoUSB_Buffer);
}
}
+ }
+
+ if (Serial_IsSendReady() && !(RingBuffer_IsEmpty(&USBtoUSART_Buffer)))
+ Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer));
- if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer)))
- Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer));
+ CDC_Device_USBTask(&VirtualSerial1_CDC_Interface);
}
/* end LUFA CDC-ACM specific definitions*/
@@ -231,7 +255,6 @@ int main(void)
}
usbserial_task();
- CDC_Device_USBTask(&VirtualSerial1_CDC_Interface);
CDC_Device_USBTask(&VirtualSerial2_CDC_Interface);
USB_USBTask();
}