-
- /* Read bytes from the UART receive buffer into the USB IN endpoint */
- if (UARTtoUSB_Buffer.In != UARTtoUSB_Buffer.Out)
- {
- CDC_Device_SendByte(&VirtualSerial_CDC_Interface, *UARTtoUSB_Buffer.Out);
- if (++UARTtoUSB_Buffer.Out == &UARTtoUSB_Buffer.Buffer[128])
- UARTtoUSB_Buffer.Out = UARTtoUSB_Buffer.Buffer;
- }
-
- /* Load bytes from the UART transmit buffer into the UART */
- if ((USBtoUART_Buffer.In != USBtoUART_Buffer.Out) && SoftUART_IsReady())
- {
- SoftUART_TxByte(*USBtoUART_Buffer.Out);
- if (++USBtoUART_Buffer.Out == &USBtoUART_Buffer.Buffer[128])
- USBtoUART_Buffer.Out = USBtoUART_Buffer.Buffer;
- }
-
- /* Load bytes from the UART into the UART receive buffer */
- if (SoftUART_IsReceived())
+
+ /* Check if the UART receive buffer flush timer has expired or buffer is nearly full */
+ uint16_t BufferCount = RingBuffer_GetCount(&UARTtoUSB_Buffer);
+ if ((TIFR0 & (1 << TOV0)) || (BufferCount > 200))