summaryrefslogtreecommitdiff
path: root/usb-serial/usb-serial.c
diff options
context:
space:
mode:
Diffstat (limited to 'usb-serial/usb-serial.c')
-rw-r--r--usb-serial/usb-serial.c92
1 files changed, 54 insertions, 38 deletions
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);
}