From ca007f91f2aa959a738649d35becb54cb1efc8b8 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Thu, 29 Jul 2010 15:14:56 +0000
Subject: [PATCH] Fixed Benito and USBtoSerial projects not turning off the
 USART before reconfiguring it, which could cause incorrect operation to occur
 (thanks to Bob Paddock).

---
 LUFA/ManPages/ChangeLog.txt        |  2 ++
 Projects/Benito/Benito.c           | 14 +++++++++++---
 Projects/USBtoSerial/USBtoSerial.c | 13 ++++++++++---
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/LUFA/ManPages/ChangeLog.txt b/LUFA/ManPages/ChangeLog.txt
index 69a712cd0..1a7303b8c 100644
--- a/LUFA/ManPages/ChangeLog.txt
+++ b/LUFA/ManPages/ChangeLog.txt
@@ -60,6 +60,8 @@
   *  - Fixed HID device class driver still using PrevReportINBuffer for GetReport control requests even when it has been
   *    set to NULL by the user application (thanks to Axel Rohde)
   *  - Fixed MIDI_Device_SendEventPacket() not correctly waiting for the endpoint to become ready (thanks to Robin Green)
+  *  - Fixed Benito and USBtoSerial projects not turning off the USART before reconfiguring it, which could cause incorrect
+  *    operation to occur (thanks to Bob Paddock)
   *
   *  \section Sec_ChangeLog100513 Version 100513
   *  <b>New:</b>
diff --git a/Projects/Benito/Benito.c b/Projects/Benito/Benito.c
index 71b187bfb..834554b4f 100644
--- a/Projects/Benito/Benito.c
+++ b/Projects/Benito/Benito.c
@@ -235,10 +235,18 @@ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCI
 			break;
 	}
 	
-	UCSR1A = (1 << U2X1);
-	UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
-	UCSR1C = ConfigMask;	
+	/* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */
+	UCSR1A = 0;
+	UCSR1B = 0;
+	UCSR1C = 0;
+
+	/* Set the new baud rate before configuring the USART */
 	UBRR1  = SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
+	
+	/* Reconfigure the USART in double speed mode for a wider baud rate range at the expense of accuracy */
+	UCSR1A = (1 << U2X1);	
+	UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
+	UCSR1C = ConfigMask;
 }
 
 /** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer
diff --git a/Projects/USBtoSerial/USBtoSerial.c b/Projects/USBtoSerial/USBtoSerial.c
index 1cd6f5563..b59a3648f 100644
--- a/Projects/USBtoSerial/USBtoSerial.c
+++ b/Projects/USBtoSerial/USBtoSerial.c
@@ -120,7 +120,6 @@ void SetupHardware(void)
 	clock_prescale_set(clock_div_1);
 
 	/* Hardware Initialization */
-	Serial_Init(9600, false);
 	LEDs_Init();
 	USB_Init();
 
@@ -199,9 +198,17 @@ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCI
 			ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10));
 			break;
 	}
+
+	/* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */
+	UCSR1A = 0;
+	UCSR1B = 0;
+	UCSR1C = 0;
+
+	/* Set the new baud rate before configuring the USART */
+	UBRR1  = SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
 	
+	/* Reconfigure the USART in double speed mode for a wider baud rate range at the expense of accuracy */
 	UCSR1A = (1 << U2X1);	
 	UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
-	UCSR1C = ConfigMask;	
-	UBRR1  = SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
+	UCSR1C = ConfigMask;
 }
-- 
GitLab