Updated Benito project -- added hardware interrupt and software buffering for serial...
[pub/lufa.git] / Projects / Benito / Benito.c
index 90f8e0b..2ee24da 100644 (file)
 \r
 #include "Benito.h"\r
 \r
-/** Counter for the number of milliseconds remaining for the target /RESET pulse being generated. */\r
-volatile uint8_t ResetPulseMSRemaining = 0;\r
+/** Circular buffer to hold data from the serial port before it is sent to the host. */\r
+RingBuff_t Tx_Buffer;\r
 \r
-/** Counter for the number of milliseconds remaining for the TX activity LED pulse being generated. */\r
-volatile uint8_t TxPulseMSRemaining    = 0;\r
-\r
-/** Counter for the number of milliseconds remaining for the RX activity LED pulse being generated. */\r
-volatile uint8_t RxPulseMSRemaining    = 0;\r
-\r
-/** Counter for the number of milliseconds remaining for the enumeration LED ping-pong being generated. */\r
-volatile uint8_t PingPongMSRemaining   = 0;\r
+/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */\r
+volatile struct\r
+{\r
+       uint8_t ResetPulse; /**< Milliseconds remaining for target /RESET pulse */\r
+       uint8_t TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */\r
+       uint8_t RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */\r
+       uint8_t PingPongLEDPulse; /**< Milliseconds remaining for enumeration Tx/Rx ping-pong LED pulse */\r
+} PulseMSRemaining;\r
 \r
 /** LUFA CDC Class driver interface configuration and state information. This structure is\r
  *  passed to all CDC Class driver functions, so that multiple instances of the same class\r
@@ -75,50 +75,52 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
 int main(void)\r
 {\r
        SetupHardware();\r
+       \r
+       Buffer_Initialize(&Tx_Buffer);\r
 \r
        for (;;)\r
        {\r
                /* Echo bytes from the host to the target via the hardware USART */\r
-               if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface) > 0)\r
+               while (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface) > 0)\r
                {\r
                        Serial_TxByte(CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));\r
 \r
                        LEDs_TurnOnLEDs(LEDMASK_TX);\r
-                       TxPulseMSRemaining = TX_RX_LED_PULSE_MS;                        \r
+                       PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;                       \r
                }\r
                \r
                /* Echo bytes from the target to the host via the virtual serial port */\r
-               if (Serial_IsCharReceived())\r
+               while (Tx_Buffer.Elements > 0)\r
                {\r
-                       CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Serial_RxByte());\r
+                       CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&Tx_Buffer));\r
 \r
                        LEDs_TurnOnLEDs(LEDMASK_RX);\r
-                       RxPulseMSRemaining = TX_RX_LED_PULSE_MS;\r
+                       PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;\r
                }\r
                \r
                /* Check if the millisecond timer has elapsed */\r
                if (TIFR0 & (1 << OCF0A))\r
                {\r
-                       /* Check if the LEDs should be ping-ponging (during enumeration) */\r
-                       if (PingPongMSRemaining && !(--PingPongMSRemaining))\r
-                       {\r
-                               LEDs_ToggleLEDs(LEDMASK_TX | LEDMASK_RX);\r
-                               PingPongMSRemaining = PING_PONG_LED_PULSE_MS;\r
-                       }\r
-               \r
                        /* Check if the reset pulse period has elapsed, if so tristate the target reset line */\r
-                       if (ResetPulseMSRemaining && !(--ResetPulseMSRemaining))\r
+                       if (PulseMSRemaining.ResetPulse && !(--PulseMSRemaining.ResetPulse))\r
                        {\r
                                LEDs_TurnOffLEDs(LEDMASK_BUSY);\r
                                AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK;\r
                        }\r
                        \r
+                       /* Check if the LEDs should be ping-ponging (during enumeration) */\r
+                       if (PulseMSRemaining.PingPongLEDPulse && !(--PulseMSRemaining.PingPongLEDPulse))\r
+                       {\r
+                               LEDs_ToggleLEDs(LEDMASK_TX | LEDMASK_RX);\r
+                               PulseMSRemaining.PingPongLEDPulse = PING_PONG_LED_PULSE_MS;\r
+                       }\r
+               \r
                        /* Turn off TX LED(s) once the TX pulse period has elapsed */\r
-                       if (TxPulseMSRemaining && !(--TxPulseMSRemaining))\r
+                       if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse))\r
                          LEDs_TurnOffLEDs(LEDMASK_TX);\r
 \r
                        /* Turn off RX LED(s) once the RX pulse period has elapsed */\r
-                       if (RxPulseMSRemaining && !(--RxPulseMSRemaining))\r
+                       if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse))\r
                          LEDs_TurnOffLEDs(LEDMASK_RX);\r
 \r
                        /* Clear the millisecond timer CTC flag (cleared by writing logic one to the register) */\r
@@ -158,21 +160,21 @@ void SetupHardware(void)
 /** Event handler for the library USB Connection event. */\r
 void EVENT_USB_Device_Connect(void)\r
 {\r
-       PingPongMSRemaining = PING_PONG_LED_PULSE_MS;\r
+       PulseMSRemaining.PingPongLEDPulse = PING_PONG_LED_PULSE_MS;\r
        LEDs_SetAllLEDs(LEDMASK_TX);\r
 }\r
 \r
 /** Event handler for the library USB Disconnection event. */\r
 void EVENT_USB_Device_Disconnect(void)\r
 {\r
-       PingPongMSRemaining = 0;\r
+       PulseMSRemaining.PingPongLEDPulse = 0;\r
        LEDs_SetAllLEDs(LEDS_NO_LEDS);\r
 }\r
 \r
 /** Event handler for the library USB Configuration Changed event. */\r
 void EVENT_USB_Device_ConfigurationChanged(void)\r
 {\r
-       PingPongMSRemaining = 0;\r
+       PulseMSRemaining.PingPongLEDPulse = 0;\r
        LEDs_SetAllLEDs(LEDS_NO_LEDS);\r
 \r
        if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))\r
@@ -220,11 +222,22 @@ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCI
        }\r
        \r
        UCSR1A = (1 << U2X1);\r
-       UCSR1B = ((1 << TXEN1) | (1 << RXEN1));\r
+       UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));\r
        UCSR1C = ConfigMask;    \r
        UBRR1  = SERIAL_2X_UBBRVAL((uint16_t)CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);\r
 }\r
 \r
+/** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer\r
+ *  for later transmission to the host.\r
+ */\r
+ISR(USART1_RX_vect, ISR_BLOCK)\r
+{\r
+       uint8_t ReceivedByte = UDR1;\r
+\r
+       if (USB_DeviceState == DEVICE_STATE_Configured)\r
+         Buffer_StoreElement(&Tx_Buffer, ReceivedByte);\r
+}\r
+\r
 /** Event handler for the CDC Class driver Host-to-Device Line Encoding Changed event.\r
  *\r
  *  \param[in] CDCInterfaceInfo  Pointer to the CDC class interface configuration structure being referenced\r
@@ -236,7 +249,7 @@ void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const C
        {\r
                LEDs_SetAllLEDs(LEDMASK_BUSY);\r
        \r
-               AVR_RESET_LINE_DDR    |= AVR_RESET_LINE_MASK;\r
-               ResetPulseMSRemaining  = AVR_RESET_PULSE_MS;\r
+               AVR_RESET_LINE_DDR |= AVR_RESET_LINE_MASK;\r
+               PulseMSRemaining.ResetPulse = AVR_RESET_PULSE_MS;\r
        }\r
 }\r