From dfe50ff34970eda173cafbb3d636a5f7bedc87bd Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Thu, 15 Jul 2010 11:01:31 +0000
Subject: [PATCH] Added ability to set the serial baud rate via the user's
 terminal in the XPLAINBridge project.

---
 LUFA/ManPages/ChangeLog.txt              |  1 +
 Projects/Benito/Descriptors.c            |  2 +-
 Projects/USBtoSerial/Descriptors.c       |  2 +-
 Projects/XPLAINBridge/Lib/SoftUART.c     |  9 +++++----
 Projects/XPLAINBridge/Lib/SoftUART.h     | 12 +++++++++---
 Projects/XPLAINBridge/USARTDescriptors.c |  2 +-
 Projects/XPLAINBridge/XPLAINBridge.c     | 10 ++++++++++
 Projects/XPLAINBridge/XPLAINBridge.h     |  2 ++
 Projects/XPLAINBridge/XPLAINBridge.txt   | 15 ++++++++++-----
 9 files changed, 40 insertions(+), 15 deletions(-)

diff --git a/LUFA/ManPages/ChangeLog.txt b/LUFA/ManPages/ChangeLog.txt
index a6f3a954f..ffd1fcfbc 100644
--- a/LUFA/ManPages/ChangeLog.txt
+++ b/LUFA/ManPages/ChangeLog.txt
@@ -15,6 +15,7 @@
   *  - Added new SWAPENDIAN_16() and SWAPENDIAN_32() macros to Common.h for statically initialized variables at compile time
   *  - Added new Drivers/USB/LowLevel/Device.c file to house Device mode specific functions that are more complicated than simple macros
   *  - Added new AVRStudio 4 project files for all library demos, projects and bootloaders
+  *  - Added ability to set the serial baud rate via the user's terminal in the XPLAINBridge project
   *
   *  <b>Changed:</b>
   *  - The RingBuff library code has been replaced in the XPLAINBridge, Benito and USBtoSerial projects with an ultra lightweight
diff --git a/Projects/Benito/Descriptors.c b/Projects/Benito/Descriptors.c
index ef026254b..3ca9dd9f4 100644
--- a/Projects/Benito/Descriptors.c
+++ b/Projects/Benito/Descriptors.c
@@ -118,7 +118,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
 			.Data                   = {0x06}
 		},
 		
-	.CDC_Functional_Union= 
+	.CDC_Functional_Union = 
 		{
 			.Header                 = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24},
 			.SubType                = 0x06,
diff --git a/Projects/USBtoSerial/Descriptors.c b/Projects/USBtoSerial/Descriptors.c
index bb2ee1098..c60da313e 100644
--- a/Projects/USBtoSerial/Descriptors.c
+++ b/Projects/USBtoSerial/Descriptors.c
@@ -130,7 +130,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
 			.Data                   = {0x06}
 		},
 		
-	.CDC_Functional_Union= 
+	.CDC_Functional_Union = 
 		{
 			.Header                 = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24},
 			.SubType                = 0x06,
diff --git a/Projects/XPLAINBridge/Lib/SoftUART.c b/Projects/XPLAINBridge/Lib/SoftUART.c
index 4ec8dcdc8..8710722e4 100644
--- a/Projects/XPLAINBridge/Lib/SoftUART.c
+++ b/Projects/XPLAINBridge/Lib/SoftUART.c
@@ -63,12 +63,13 @@ void SoftUART_Init(void)
 	EICRA  = (1 << ISC01);
 	EIMSK  = (1 << INT0);
 
-	/* Set reception timer compare period and enable compare ISR */
-	OCR1A  = BIT_TIME;
+	/* Set the transmission and reception timer compare values for the default baud rate */
+	SoftUART_SetBaud(9600);
+
+	/* Setup reception timer compare ISR */
 	TIMSK1 = (1 << OCIE1A);
 
-	/* Set transmission timer compare period, enable compare ISR and start the timer */
-	OCR3A  = BIT_TIME;
+	/* Setup transmission timer compare ISR and start the timer */
 	TIMSK3 = (1 << OCIE3A);
 	TCCR3B = ((1 << CS30) | (1 << WGM32));
 }
diff --git a/Projects/XPLAINBridge/Lib/SoftUART.h b/Projects/XPLAINBridge/Lib/SoftUART.h
index a107d48a8..d0b03c04b 100644
--- a/Projects/XPLAINBridge/Lib/SoftUART.h
+++ b/Projects/XPLAINBridge/Lib/SoftUART.h
@@ -42,9 +42,6 @@
 		#include "LightweightRingBuff.h"
 
 	/* Macros: */
-		#define BAUD       9600
-		#define BIT_TIME   ((F_CPU / BAUD) - 1)
-
 		#define SRX        PD0
 		#define SRXPIN     PIND
 		#define SRXPORT    PORTD
@@ -53,6 +50,15 @@
 		#define STXPORT    PORTD
 		#define STXDDR     DDRD
 
+	/* Inline Functions: */	
+		static inline void SoftUART_SetBaud(const uint32_t Baud)
+		{
+			uint16_t BitTime = ((F_CPU / Baud) - 1);
+		
+			OCR1A = BitTime;
+			OCR3A = BitTime;
+		}
+
 	/* Function Prototypes: */
 		void    SoftUART_Init(void);
 
diff --git a/Projects/XPLAINBridge/USARTDescriptors.c b/Projects/XPLAINBridge/USARTDescriptors.c
index 8acbacd2a..159f38609 100644
--- a/Projects/XPLAINBridge/USARTDescriptors.c
+++ b/Projects/XPLAINBridge/USARTDescriptors.c
@@ -130,7 +130,7 @@ USART_USB_Descriptor_Configuration_t PROGMEM USART_ConfigurationDescriptor =
 			.Data                   = {0x06}
 		},
 		
-	.CDC_Functional_Union= 
+	.CDC_Functional_Union = 
 		{
 			.Header                 = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24},
 			.SubType                = 0x06,
diff --git a/Projects/XPLAINBridge/XPLAINBridge.c b/Projects/XPLAINBridge/XPLAINBridge.c
index 3bbc0bc65..2e8e6a89e 100644
--- a/Projects/XPLAINBridge/XPLAINBridge.c
+++ b/Projects/XPLAINBridge/XPLAINBridge.c
@@ -225,6 +225,16 @@ void EVENT_USB_Device_Disconnect(void)
 	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
 }
 
+/** Event handler for the CDC Class driver Line Encoding Changed event.
+ *
+ *  \param[in] CDCInterfaceInfo  Pointer to the CDC class interface configuration structure being referenced
+ */
+void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
+{
+	/* Change the software UART's baud rate to match the new baud rate */
+	SoftUART_SetBaud(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
+}
+
 /** 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
  *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
diff --git a/Projects/XPLAINBridge/XPLAINBridge.h b/Projects/XPLAINBridge/XPLAINBridge.h
index 3d257e5ae..fcb177633 100644
--- a/Projects/XPLAINBridge/XPLAINBridge.h
+++ b/Projects/XPLAINBridge/XPLAINBridge.h
@@ -91,6 +91,8 @@
 		void EVENT_USB_Device_Connect(void);
 		void EVENT_USB_Device_Disconnect(void);		
 
+		void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo);
+
 		uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress);
 
 #endif
diff --git a/Projects/XPLAINBridge/XPLAINBridge.txt b/Projects/XPLAINBridge/XPLAINBridge.txt
index f22feca8e..3f4647e61 100644
--- a/Projects/XPLAINBridge/XPLAINBridge.txt
+++ b/Projects/XPLAINBridge/XPLAINBridge.txt
@@ -54,12 +54,17 @@
  *  will enumerate as a regular COM port on the host, which can then be opened and data exchanged between the XMEGA and Host as if
  *  the XMEGA was connected directly to the host's serial port. If the pin is pulled low by shorting it to GND (pin 10), the device
  *  will enumerate as an AVRISP-MKII to the host, so that the XMEGA can be reprogrammed by AVRStudio or other compatible software
- *  such as avrdude.
+ *  such as avrdude by connecting to the board as if it was an Atmel AVRISP-MKII programmer.
  *
- *  After running this project for the first time on a new computer, you will need to supply the .INF file located in this project
- *  project's directory as the device's driver when running under Windows. This will enable Windows to use its inbuilt CDC drivers,
- *  negating the need for custom drivers for the device. Other Operating Systems should automatically use their own inbuilt CDC-ACM
- *  drivers.
+ *  After running this project in serial bridge mode for the first time on a new computer, you will need to supply the .INF file
+ *  located in this project project's directory as the device's driver when running under Windows. This will enable Windows to use
+ *  its inbuilt CDC virtual serial drivers, negating the need for custom drivers for the device. Other Operating Systems should
+ *  automatically use their own inbuilt CDC-ACM drivers.
+ *
+ *  In serial bridge mode, the UART baud rate can be altered through the host terminal software to select a new baud rate - the default
+ *  baud is 9600. Note that parity, data bits and stop bits are fixed at none, eight and one respectively can cannot be altered. Changes
+ *  to the connection's parity, data bits or stop bits are ignored by the firmware. As the serial link between the controllers on the
+ *  XPLAIN is software emulated by the USB AVR, not all baud rates will work correctly.
  *
  *  This project relies on files from the LUFA AVRISP-MKII project for compilation.
  *
-- 
GitLab