Fixed MIDI_Host_Flush() not aborting early when the specified MIDI host interface...
[pub/USBasp.git] / Projects / Benito / Benito.c
index 834554b..a9afda4 100644 (file)
@@ -92,9 +92,10 @@ int main(void)
        for (;;)
        {
                /* Echo bytes from the host to the target via the hardware USART */
        for (;;)
        {
                /* Echo bytes from the host to the target via the hardware USART */
-               if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface) && (UCSR1A & (1 << UDRE1)))
+               int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
+               if (!(ReceivedByte < 0) && (UCSR1A & (1 << UDRE1)))
                {
                {
-                       UDR1 = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
+                       UDR1 = ReceivedByte;
 
                        LEDs_TurnOnLEDs(LEDMASK_TX);
                        PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;                       
 
                        LEDs_TurnOnLEDs(LEDMASK_TX);
                        PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;                       
@@ -126,13 +127,14 @@ int main(void)
                          LEDs_TurnOffLEDs(LEDMASK_RX);
 
                        /* Check if the receive buffer flush period has expired */
                          LEDs_TurnOffLEDs(LEDMASK_RX);
 
                        /* Check if the receive buffer flush period has expired */
-                       if (!(--FlushPeriodRemaining) || (Tx_Buffer.Count > 200))
+                       RingBuff_Count_t BufferCount = RingBuffer_GetCount(&Tx_Buffer);
+                       if (!(--FlushPeriodRemaining) || (BufferCount > 200))
                        {
                                /* Echo bytes from the target to the host via the virtual serial port */
                        {
                                /* Echo bytes from the target to the host via the virtual serial port */
-                               if (Tx_Buffer.Count)
+                               if (BufferCount)
                                {
                                {
-                                       while (Tx_Buffer.Count)
-                                         CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_AtomicRemove(&Tx_Buffer));
+                                       while (BufferCount--)
+                                         CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&Tx_Buffer));
 
                                        LEDs_TurnOnLEDs(LEDMASK_RX);
                                        PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
 
                                        LEDs_TurnOnLEDs(LEDMASK_RX);
                                        PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
@@ -188,11 +190,13 @@ void EVENT_USB_Device_Disconnect(void)
 /** Event handler for the library USB Configuration Changed event. */
 void EVENT_USB_Device_ConfigurationChanged(void)
 {
 /** Event handler for the library USB Configuration Changed event. */
 void EVENT_USB_Device_ConfigurationChanged(void)
 {
+       bool ConfigSuccess = true;
+
+       ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
+
        PulseMSRemaining.PingPongLEDPulse = 0;
        PulseMSRemaining.PingPongLEDPulse = 0;
-       LEDs_SetAllLEDs(LEDS_NO_LEDS);
 
 
-       if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))
-         LEDs_SetAllLEDs(LEDMASK_ERROR);
+       LEDs_SetAllLEDs(ConfigSuccess ? LEDS_NO_LEDS : LEDMASK_ERROR);
 }
 
 /** Event handler for the library USB Unhandled Control Request event. */
 }
 
 /** Event handler for the library USB Unhandled Control Request event. */
@@ -236,17 +240,17 @@ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCI
        }
        
        /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */
        }
        
        /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */
-       UCSR1A = 0;
        UCSR1B = 0;
        UCSR1B = 0;
+       UCSR1A = 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 */
        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 */
+       UCSR1C = ConfigMask;
        UCSR1A = (1 << U2X1);   
        UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
        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
 }
 
 /** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer