From 0f0ed6b45fd77c0f8d2761fa28e992e2b1053f09 Mon Sep 17 00:00:00 2001
From: "David A. Mellis" <d.mellis@arduino.cc>
Date: Sat, 4 Dec 2010 13:50:27 -0500
Subject: Minor bug fixes to the 8U2 USB-to-serial firmware.

---
 firmwares/arduino-usbserial/Arduino-usbserial.c | 33 ++++++++++++-------------
 firmwares/arduino-usbserial/Descriptors.c       |  4 +--
 2 files changed, 18 insertions(+), 19 deletions(-)

(limited to 'firmwares')

diff --git a/firmwares/arduino-usbserial/Arduino-usbserial.c b/firmwares/arduino-usbserial/Arduino-usbserial.c
index a2bdfb1..4de73c8 100755
--- a/firmwares/arduino-usbserial/Arduino-usbserial.c
+++ b/firmwares/arduino-usbserial/Arduino-usbserial.c
@@ -88,10 +88,15 @@ int main(void)
 
 	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);		
+		/* Only try to read in bytes from the CDC interface if the transmit buffer is not full */
+		if (!(RingBuffer_IsFull(&USBtoUSART_Buffer)))
+		{
+			int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
+
+			/* Read bytes from the USB OUT endpoint 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 */
 		RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
@@ -153,10 +158,7 @@ void SetupHardware(void)
 /** Event handler for the library USB Configuration Changed event. */
 void EVENT_USB_Device_ConfigurationChanged(void)
 {
-
-
-	if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface))) 
-		;
+	CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
 }
 
 /** Event handler for the library USB Unhandled Control Request event. */
@@ -205,12 +207,13 @@ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCI
 	UCSR1C = 0;
 
 	/* Special case 57600 baud for compatibility with the ATmega328 bootloader. */	
-	UCSR1A = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600) ? 0 : (1 << U2X1);
-	UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
-	UCSR1C = ConfigMask;    
 	UBRR1  = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600)
 			 ? SERIAL_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)
 			 : SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);	
+
+	UCSR1C = ConfigMask;
+	UCSR1A = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600) ? 0 : (1 << U2X1);
+	UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
 }
 
 /** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer
@@ -233,11 +236,7 @@ void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const C
 	bool CurrentDTRState = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR);
 
 	if (CurrentDTRState)
-	{
-		AVR_RESET_LINE_PORT &= ~AVR_RESET_LINE_MASK;
-	}
+	  AVR_RESET_LINE_PORT &= ~AVR_RESET_LINE_MASK;
 	else
-	{
-		AVR_RESET_LINE_PORT |= AVR_RESET_LINE_MASK;
-	}
+	  AVR_RESET_LINE_PORT |= AVR_RESET_LINE_MASK;
 }
diff --git a/firmwares/arduino-usbserial/Descriptors.c b/firmwares/arduino-usbserial/Descriptors.c
index e4ad956..025250e 100755
--- a/firmwares/arduino-usbserial/Descriptors.c
+++ b/firmwares/arduino-usbserial/Descriptors.c
@@ -172,7 +172,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
 			.EndpointAddress        = (ENDPOINT_DESCRIPTOR_DIR_OUT | CDC_RX_EPNUM),
 			.Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
 			.EndpointSize           = CDC_TXRX_EPSIZE,
-			.PollingIntervalMS      = 0x00
+			.PollingIntervalMS      = 0x01
 		},
 		
 	.CDC_DataInEndpoint = 
@@ -182,7 +182,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
 			.EndpointAddress        = (ENDPOINT_DESCRIPTOR_DIR_IN | CDC_TX_EPNUM),
 			.Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
 			.EndpointSize           = CDC_TXRX_EPSIZE,
-			.PollingIntervalMS      = 0x00
+			.PollingIntervalMS      = 0x01
 		}
 };
 
-- 
cgit v1.2.3-18-g5258