Update Studio Integration DLL, to include package logging.
[pub/USBasp.git] / Projects / USBtoSerial / USBtoSerial.c
index 5fd7192..266592b 100644 (file)
@@ -1,13 +1,13 @@
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2012.
+     Copyright (C) Dean Camera, 2015.
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
-  Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2015  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
@@ -56,7 +56,7 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
        {
                .Config =
                        {
-                               .ControlInterfaceNumber         = 0,
+                               .ControlInterfaceNumber         = INTERFACE_ID_CDC_CCI,
                                .DataINEndpoint                 =
                                        {
                                                .Address                = CDC_TX_EPADDR,
@@ -99,24 +99,20 @@ 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)
                {
                        Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address);
-                       
+
                        /* 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())
                        {
-                               /* Clear flush timer expiry flag */
-                               TIFR0 |= (1 << TOV0);
-                       
                                /* 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));
@@ -137,8 +133,8 @@ int main(void)
                        }
                }
 
-               /* 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);
@@ -149,19 +145,18 @@ int main(void)
 /** Configures the board hardware and chip peripherals for the demo's functionality. */
 void SetupHardware(void)
 {
+#if (ARCH == ARCH_AVR8)
        /* Disable watchdog if enabled by bootloader/fuses */
        MCUSR &= ~(1 << WDRF);
        wdt_disable();
 
        /* Disable clock division */
        clock_prescale_set(clock_div_1);
+#endif
 
        /* Hardware Initialization */
        LEDs_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. */
@@ -199,7 +194,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);
 }
 
@@ -237,6 +232,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;
@@ -249,5 +247,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);
 }