Oops - remove accidentally committed .lst files from the TemperatureDataLogger project.
[pub/USBasp.git] / Projects / Benito / Benito.c
index 712016b..fc87d63 100644 (file)
@@ -9,13 +9,13 @@
 /*\r
   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
 \r
 /*\r
   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
 \r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
 /** \file\r
  *\r
  *  Main source file for the Benito project. This file contains the main tasks of\r
 /** \file\r
  *\r
  *  Main source file for the Benito project. This file contains the main tasks of\r
- *  the demo and is responsible for the initial application hardware configuration.\r
+ *  the project and is responsible for the initial application hardware configuration.\r
  */\r
 \r
 #include "Benito.h"\r
 \r
  */\r
 \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
 \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
+/** 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
 \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
@@ -53,22 +56,20 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
        {\r
                .Config = \r
                        {\r
        {\r
                .Config = \r
                        {\r
-                               .ControlInterfaceNumber     = 0,\r
+                               .ControlInterfaceNumber         = 0,\r
 \r
 \r
-                               .DataINEndpointNumber       = CDC_TX_EPNUM,\r
-                               .DataINEndpointSize         = CDC_TXRX_EPSIZE,\r
+                               .DataINEndpointNumber           = CDC_TX_EPNUM,\r
+                               .DataINEndpointSize             = CDC_TXRX_EPSIZE,\r
+                               .DataINEndpointDoubleBank       = false,\r
 \r
 \r
-                               .DataOUTEndpointNumber      = CDC_RX_EPNUM,\r
-                               .DataOUTEndpointSize        = CDC_TXRX_EPSIZE,\r
+                               .DataOUTEndpointNumber          = CDC_RX_EPNUM,\r
+                               .DataOUTEndpointSize            = CDC_TXRX_EPSIZE,\r
+                               .DataOUTEndpointDoubleBank      = false,\r
 \r
 \r
-                               .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,\r
-                               .NotificationEndpointSize   = CDC_NOTIFICATION_EPSIZE,\r
+                               .NotificationEndpointNumber     = CDC_NOTIFICATION_EPNUM,\r
+                               .NotificationEndpointSize       = CDC_NOTIFICATION_EPSIZE,\r
+                               .NotificationEndpointDoubleBank = false,\r
                        },\r
                        },\r
-               \r
-               .State =\r
-                       {\r
-                               // Leave all state values to their defaults\r
-                       }\r
        };\r
 \r
 /** Main program entry point. This routine contains the overall program flow, including initial\r
        };\r
 \r
 /** Main program entry point. This routine contains the overall program flow, including initial\r
@@ -77,45 +78,52 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
 int main(void)\r
 {\r
        SetupHardware();\r
 int main(void)\r
 {\r
        SetupHardware();\r
-\r
-       LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
+       \r
+       Buffer_Initialize(&Tx_Buffer);\r
 \r
        for (;;)\r
        {\r
                /* Echo bytes from the host to the target via the hardware USART */\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))\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
                {\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
                }\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
                {\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
 \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 reset pulse period has elapsed, if so tristate the target reset line */\r
                }\r
                \r
                /* Check if the millisecond timer has elapsed */\r
                if (TIFR0 & (1 << OCF0A))\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
                        {\r
-                               AVR_RESET_LINE_PORT &= ~AVR_RESET_LINE_MASK;\r
-                               AVR_RESET_LINE_DDR  &= ~AVR_RESET_LINE_MASK;\r
+                               LEDs_TurnOffLEDs(LEDMASK_BUSY);\r
+                               AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK;\r
                        }\r
                        }\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
                        /* 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
                          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
                          LEDs_TurnOffLEDs(LEDMASK_RX);\r
 \r
                        /* Clear the millisecond timer CTC flag (cleared by writing logic one to the register) */\r
@@ -138,6 +146,7 @@ void SetupHardware(void)
        clock_prescale_set(clock_div_1);\r
 \r
        /* Hardware Initialization */\r
        clock_prescale_set(clock_div_1);\r
 \r
        /* Hardware Initialization */\r
+       Serial_Init(9600, false);\r
        LEDs_Init();\r
        USB_Init();\r
 \r
        LEDs_Init();\r
        USB_Init();\r
 \r
@@ -152,30 +161,33 @@ void SetupHardware(void)
 }\r
 \r
 /** Event handler for the library USB Connection event. */\r
 }\r
 \r
 /** Event handler for the library USB Connection event. */\r
-void EVENT_USB_Connect(void)\r
+void EVENT_USB_Device_Connect(void)\r
 {\r
 {\r
-       LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\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
 }\r
 \r
 /** Event handler for the library USB Disconnection event. */\r
-void EVENT_USB_Disconnect(void)\r
+void EVENT_USB_Device_Disconnect(void)\r
 {\r
 {\r
-       LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
+       PulseMSRemaining.PingPongLEDPulse = 0;\r
+       LEDs_SetAllLEDs(LEDS_NO_LEDS);\r
 }\r
 \r
 /** Event handler for the library USB Configuration Changed event. */\r
 }\r
 \r
 /** Event handler for the library USB Configuration Changed event. */\r
-void EVENT_USB_ConfigurationChanged(void)\r
+void EVENT_USB_Device_ConfigurationChanged(void)\r
 {\r
 {\r
-       LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
+       PulseMSRemaining.PingPongLEDPulse = 0;\r
+       LEDs_SetAllLEDs(LEDS_NO_LEDS);\r
 \r
        if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))\r
 \r
        if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))\r
-         LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+         LEDs_SetAllLEDs(LEDMASK_ERROR);\r
 }\r
 \r
 }\r
 \r
-/** Event handler for the library USB Unhandled Control Packet event. */\r
-void EVENT_USB_UnhandledControlPacket(void)\r
+/** Event handler for the library USB Unhandled Control Request event. */\r
+void EVENT_USB_Device_UnhandledControlRequest(void)\r
 {\r
 {\r
-       CDC_Device_ProcessControlPacket(&VirtualSerial_CDC_Interface);\r
+       CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);\r
 }\r
 \r
 /** Event handler for the CDC Class driver Line Encoding Changed event.\r
 }\r
 \r
 /** Event handler for the CDC Class driver Line Encoding Changed event.\r
@@ -212,12 +224,23 @@ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCI
                        break;\r
        }\r
        \r
                        break;\r
        }\r
        \r
-       UCSR1A = (1 << U2X1);   \r
-       UCSR1B = ((1 << TXEN1) | (1 << RXEN1));\r
+       UCSR1A = (1 << U2X1);\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
        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
 /** 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
@@ -227,9 +250,9 @@ void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const C
        /* Check if the DTR line has been asserted - if so, start the target AVR's reset pulse */\r
        if (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR)\r
        {\r
        /* Check if the DTR line has been asserted - if so, start the target AVR's reset pulse */\r
        if (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR)\r
        {\r
-               AVR_RESET_LINE_DDR  |= AVR_RESET_LINE_MASK;\r
-               AVR_RESET_LINE_PORT &= ~AVR_RESET_LINE_MASK;\r
-\r
-               ResetPulseMSRemaining = AVR_RESET_PULSE_MS;\r
+               LEDs_SetAllLEDs(LEDMASK_BUSY);\r
+       \r
+               AVR_RESET_LINE_DDR |= AVR_RESET_LINE_MASK;\r
+               PulseMSRemaining.ResetPulse = AVR_RESET_PULSE_MS;\r
        }\r
 }\r
        }\r
 }\r