Add explicit guards to all device mode tasks to ensure the device is connected and...
authorDean Camera <dean@fourwalledcubicle.com>
Tue, 21 Jul 2009 02:48:41 +0000 (02:48 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Tue, 21 Jul 2009 02:48:41 +0000 (02:48 +0000)
19 files changed:
Demos/Device/Incomplete/Sideshow/Sideshow.c
Demos/Device/LowLevel/AudioInput/AudioInput.c
Demos/Device/LowLevel/AudioOutput/AudioOutput.c
Demos/Device/LowLevel/CDC/CDC.c
Demos/Device/LowLevel/DualCDC/DualCDC.c
Demos/Device/LowLevel/GenericHID/GenericHID.c
Demos/Device/LowLevel/Joystick/Joystick.c
Demos/Device/LowLevel/Keyboard/Keyboard.c
Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c
Demos/Device/LowLevel/MIDI/MIDI.c
Demos/Device/LowLevel/Mouse/Mouse.c
Demos/Device/LowLevel/RNDISEthernet/RNDISEthernet.c
Demos/Device/LowLevel/USBtoSerial/USBtoSerial.c
Demos/Host/ClassDriver/CDCHost/CDCHost.c
Demos/Host/LowLevel/PrinterHost/PrinterHost.c
LUFA/Drivers/USB/Class/Device/CDC.c
LUFA/Drivers/USB/Class/Device/HID.c
LUFA/Drivers/USB/Class/Device/MassStorage.c
LUFA/Drivers/USB/Class/Device/RNDIS.c

index 41e36cc..cc8bd51 100644 (file)
@@ -141,17 +141,17 @@ void EVENT_USB_UnhandledControlPacket(void)
        \r
 void SideShow_Task(void)\r
 {\r
        \r
 void SideShow_Task(void)\r
 {\r
-       /* Check if the USB System is connected to a Host */\r
-       if (USB_IsConnected)\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+\r
+       /* Select the SideShow data out endpoint */\r
+       Endpoint_SelectEndpoint(SIDESHOW_OUT_EPNUM);\r
+       \r
+       /* Check to see if a new SideShow message has been received */\r
+       if (Endpoint_IsReadWriteAllowed())\r
        {\r
        {\r
-               /* Select the SideShow data out endpoint */\r
-               Endpoint_SelectEndpoint(SIDESHOW_OUT_EPNUM);\r
-               \r
-               /* Check to see if a new SideShow message has been received */\r
-               if (Endpoint_IsReadWriteAllowed())\r
-               {\r
-                       /* Process the received SideShow message */\r
-                       Sideshow_ProcessCommandPacket();\r
-               }\r
+               /* Process the received SideShow message */\r
+               Sideshow_ProcessCommandPacket();\r
        }\r
 }\r
        }\r
 }\r
index 50e7f4d..5656b05 100644 (file)
@@ -151,6 +151,10 @@ void EVENT_USB_UnhandledControlPacket(void)
 /** Task to manage the Audio interface, reading in ADC samples from the microphone, and them to the host. */\r
 void USB_Audio_Task(void)\r
 {\r
 /** Task to manage the Audio interface, reading in ADC samples from the microphone, and them to the host. */\r
 void USB_Audio_Task(void)\r
 {\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+\r
        /* Check to see if the streaming interface is selected, if not the host is not receiving audio */\r
        if (!(StreamingAudioInterfaceSelected))\r
          return;\r
        /* Check to see if the streaming interface is selected, if not the host is not receiving audio */\r
        if (!(StreamingAudioInterfaceSelected))\r
          return;\r
index 9661f03..6d22009 100644 (file)
@@ -180,6 +180,10 @@ void EVENT_USB_UnhandledControlPacket(void)
  */\r
 void USB_Audio_Task(void)\r
 {\r
  */\r
 void USB_Audio_Task(void)\r
 {\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+\r
        /* Check to see if the streaming interface is selected, if not the host is not receiving audio */\r
        if (!(StreamingAudioInterfaceSelected))\r
          return;       \r
        /* Check to see if the streaming interface is selected, if not the host is not receiving audio */\r
        if (!(StreamingAudioInterfaceSelected))\r
          return;       \r
index ac0623a..d8263b1 100644 (file)
@@ -254,6 +254,10 @@ void CDC_Task(void)
                        "Joystick Pressed\r\n",\r
                };\r
        \r
                        "Joystick Pressed\r\n",\r
                };\r
        \r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+         \r
 #if 0\r
        /* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232\r
         *       handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code:\r
 #if 0\r
        /* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232\r
         *       handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code:\r
index 1031068..862945e 100644 (file)
@@ -229,6 +229,10 @@ void CDC1_Task(void)
        uint8_t     JoyStatus_LCL   = Joystick_GetStatus();\r
        static bool ActionSent      = false;\r
 \r
        uint8_t     JoyStatus_LCL   = Joystick_GetStatus();\r
        static bool ActionSent      = false;\r
 \r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+\r
        char* JoystickStrings[] =\r
                {\r
                        "Joystick Up\r\n",\r
        char* JoystickStrings[] =\r
                {\r
                        "Joystick Up\r\n",\r
@@ -288,6 +292,10 @@ void CDC1_Task(void)
  */\r
 void CDC2_Task(void)\r
 {\r
  */\r
 void CDC2_Task(void)\r
 {\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+\r
        /* Select the Serial Rx Endpoint */\r
        Endpoint_SelectEndpoint(CDC2_RX_EPNUM);\r
        \r
        /* Select the Serial Rx Endpoint */\r
        Endpoint_SelectEndpoint(CDC2_RX_EPNUM);\r
        \r
index f467494..5c6d1ee 100644 (file)
@@ -202,47 +202,47 @@ void CreateGenericHIDReport(uint8_t* DataArray)
 \r
 void HID_Task(void)\r
 {\r
 \r
 void HID_Task(void)\r
 {\r
-       /* Check if the USB system is connected to a host */\r
-       if (USB_IsConnected)\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+\r
+       Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);\r
+       \r
+       /* Check to see if a packet has been sent from the host */\r
+       if (Endpoint_IsOUTReceived())\r
        {\r
        {\r
-               Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);\r
-               \r
-               /* Check to see if a packet has been sent from the host */\r
-               if (Endpoint_IsOUTReceived())\r
+               /* Check to see if the packet contains data */\r
+               if (Endpoint_IsReadWriteAllowed())\r
                {\r
                {\r
-                       /* Check to see if the packet contains data */\r
-                       if (Endpoint_IsReadWriteAllowed())\r
-                       {\r
-                               /* Create a temporary buffer to hold the read in report from the host */\r
-                               uint8_t GenericData[GENERIC_REPORT_SIZE];\r
-                               \r
-                               /* Read Generic Report Data */\r
-                               Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));\r
-                               \r
-                               /* Process Generic Report Data */\r
-                               ProcessGenericHIDReport(GenericData);\r
-                       }\r
+                       /* Create a temporary buffer to hold the read in report from the host */\r
+                       uint8_t GenericData[GENERIC_REPORT_SIZE];\r
+                       \r
+                       /* Read Generic Report Data */\r
+                       Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));\r
+                       \r
+                       /* Process Generic Report Data */\r
+                       ProcessGenericHIDReport(GenericData);\r
+               }\r
 \r
 \r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearOUT();\r
-               }       \r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearOUT();\r
+       }       \r
 \r
 \r
-               Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);\r
+       Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);\r
+       \r
+       /* Check to see if the host is ready to accept another packet */\r
+       if (Endpoint_IsINReady())\r
+       {\r
+               /* Create a temporary buffer to hold the report to send to the host */\r
+               uint8_t GenericData[GENERIC_REPORT_SIZE];\r
                \r
                \r
-               /* Check to see if the host is ready to accept another packet */\r
-               if (Endpoint_IsINReady())\r
-               {\r
-                       /* Create a temporary buffer to hold the report to send to the host */\r
-                       uint8_t GenericData[GENERIC_REPORT_SIZE];\r
-                       \r
-                       /* Create Generic Report Data */\r
-                       CreateGenericHIDReport(GenericData);\r
+               /* Create Generic Report Data */\r
+               CreateGenericHIDReport(GenericData);\r
 \r
 \r
-                       /* Write Generic Report Data */\r
-                       Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData));\r
+               /* Write Generic Report Data */\r
+               Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData));\r
 \r
 \r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearIN();\r
-               }\r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearIN();\r
        }\r
 }\r
        }\r
 }\r
index 42ae62c..e117475 100644 (file)
@@ -181,28 +181,28 @@ bool GetNextReport(USB_JoystickReport_Data_t* ReportData)
 /** Function to manage HID report generation and transmission to the host. */\r
 void HID_Task(void)\r
 {\r
 /** Function to manage HID report generation and transmission to the host. */\r
 void HID_Task(void)\r
 {\r
-       /* Check if the USB System is connected to a Host */\r
-       if (USB_IsConnected)\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+  \r
+       /* Select the Joystick Report Endpoint */\r
+       Endpoint_SelectEndpoint(JOYSTICK_EPNUM);\r
+\r
+       /* Check to see if the host is ready for another packet */\r
+       if (Endpoint_IsINReady())\r
        {\r
        {\r
-               /* Select the Joystick Report Endpoint */\r
-               Endpoint_SelectEndpoint(JOYSTICK_EPNUM);\r
-\r
-               /* Check to see if the host is ready for another packet */\r
-               if (Endpoint_IsINReady())\r
-               {\r
-                       USB_JoystickReport_Data_t JoystickReportData;\r
-                       \r
-                       /* Create the next HID report to send to the host */\r
-                       GetNextReport(&JoystickReportData);\r
+               USB_JoystickReport_Data_t JoystickReportData;\r
                \r
                \r
-                       /* Write Joystick Report Data */\r
-                       Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));\r
-\r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearIN();\r
-                       \r
-                       /* Clear the report data afterwards */\r
-                       memset(&JoystickReportData, 0, sizeof(JoystickReportData));\r
-               }\r
+               /* Create the next HID report to send to the host */\r
+               GetNextReport(&JoystickReportData);\r
+       \r
+               /* Write Joystick Report Data */\r
+               Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));\r
+\r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearIN();\r
+               \r
+               /* Clear the report data afterwards */\r
+               memset(&JoystickReportData, 0, sizeof(JoystickReportData));\r
        }\r
 }\r
        }\r
 }\r
index 2527148..9950484 100644 (file)
@@ -377,13 +377,13 @@ void ReceiveNextReport(void)
 /** Function to manage HID report generation and transmission to the host, when in report mode. */\r
 void HID_Task(void)\r
 {\r
 /** Function to manage HID report generation and transmission to the host, when in report mode. */\r
 void HID_Task(void)\r
 {\r
-       /* Check if the USB system is connected to a host */\r
-       if (USB_IsConnected)\r
-       {\r
-               /* Send the next keypress report to the host */\r
-               SendNextReport();\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+         \r
+       /* Send the next keypress report to the host */\r
+       SendNextReport();\r
                \r
                \r
-               /* Process the LED report sent from the host */\r
-               ReceiveNextReport();\r
-       }\r
+       /* Process the LED report sent from the host */\r
+       ReceiveNextReport();\r
 }\r
 }\r
index ddfe05a..c60f08f 100644 (file)
@@ -212,6 +212,10 @@ void Keyboard_HID_Task(void)
 {\r
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
 {\r
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+\r
        /* Check if board button is not pressed, if so mouse mode enabled */\r
        if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))\r
        {\r
        /* Check if board button is not pressed, if so mouse mode enabled */\r
        if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))\r
        {\r
@@ -228,51 +232,47 @@ void Keyboard_HID_Task(void)
                if (JoyStatus_LCL & JOY_PRESS)\r
                  KeyboardReportData.KeyCode[0] = 0x08; // E\r
        }\r
                if (JoyStatus_LCL & JOY_PRESS)\r
                  KeyboardReportData.KeyCode[0] = 0x08; // E\r
        }\r
-       \r
-       /* Check if the USB system is connected to a host and report protocol mode is enabled */\r
-       if (USB_IsConnected)\r
+\r
+       /* Select the Keyboard Report Endpoint */\r
+       Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);\r
+\r
+       /* Check if Keyboard Endpoint Ready for Read/Write */\r
+       if (Endpoint_IsReadWriteAllowed())\r
        {\r
        {\r
-               /* Select the Keyboard Report Endpoint */\r
-               Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);\r
-\r
-               /* Check if Keyboard Endpoint Ready for Read/Write */\r
-               if (Endpoint_IsReadWriteAllowed())\r
-               {\r
-                       /* Write Keyboard Report Data */\r
-                       Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));\r
-\r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearIN();\r
-\r
-                       /* Clear the report data afterwards */\r
-                       memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));\r
-               }\r
-\r
-               /* Select the Keyboard LED Report Endpoint */\r
-               Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);\r
-\r
-               /* Check if Keyboard LED Endpoint Ready for Read/Write */\r
-               if (Endpoint_IsReadWriteAllowed())\r
-               {               \r
-                       /* Read in the LED report from the host */\r
-                       uint8_t LEDStatus = Endpoint_Read_Byte();\r
-                       uint8_t LEDMask   = LEDS_LED2;\r
-                       \r
-                       if (LEDStatus & 0x01) // NUM Lock\r
-                         LEDMask |= LEDS_LED1;\r
-                       \r
-                       if (LEDStatus & 0x02) // CAPS Lock\r
-                         LEDMask |= LEDS_LED3;\r
+               /* Write Keyboard Report Data */\r
+               Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));\r
+\r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearIN();\r
+\r
+               /* Clear the report data afterwards */\r
+               memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));\r
+       }\r
+\r
+       /* Select the Keyboard LED Report Endpoint */\r
+       Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);\r
+\r
+       /* Check if Keyboard LED Endpoint Ready for Read/Write */\r
+       if (Endpoint_IsReadWriteAllowed())\r
+       {               \r
+               /* Read in the LED report from the host */\r
+               uint8_t LEDStatus = Endpoint_Read_Byte();\r
+               uint8_t LEDMask   = LEDS_LED2;\r
+               \r
+               if (LEDStatus & 0x01) // NUM Lock\r
+                 LEDMask |= LEDS_LED1;\r
+               \r
+               if (LEDStatus & 0x02) // CAPS Lock\r
+                 LEDMask |= LEDS_LED3;\r
 \r
 \r
-                       if (LEDStatus & 0x04) // SCROLL Lock\r
-                         LEDMask |= LEDS_LED4;\r
+               if (LEDStatus & 0x04) // SCROLL Lock\r
+                 LEDMask |= LEDS_LED4;\r
 \r
 \r
-                       /* Set the status LEDs to the current Keyboard LED status */\r
-                       LEDs_SetAllLEDs(LEDMask);\r
+               /* Set the status LEDs to the current Keyboard LED status */\r
+               LEDs_SetAllLEDs(LEDMask);\r
 \r
 \r
-                       /* Handshake the OUT Endpoint - clear endpoint and ready for next report */\r
-                       Endpoint_ClearOUT();\r
-               }\r
+               /* Handshake the OUT Endpoint - clear endpoint and ready for next report */\r
+               Endpoint_ClearOUT();\r
        }\r
 }\r
 \r
        }\r
 }\r
 \r
@@ -283,6 +283,10 @@ void Mouse_HID_Task(void)
 {\r
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
 {\r
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+\r
        /* Check if board button is pressed, if so mouse mode enabled */\r
        if (Buttons_GetStatus() & BUTTONS_BUTTON1)\r
        {\r
        /* Check if board button is pressed, if so mouse mode enabled */\r
        if (Buttons_GetStatus() & BUTTONS_BUTTON1)\r
        {\r
@@ -300,23 +304,19 @@ void Mouse_HID_Task(void)
                  MouseReportData.Button  = (1 << 0);\r
        }\r
 \r
                  MouseReportData.Button  = (1 << 0);\r
        }\r
 \r
-       /* Check if the USB system is connected to a host and report protocol mode is enabled */\r
-       if (USB_IsConnected)\r
-       {\r
-               /* Select the Mouse Report Endpoint */\r
-               Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);\r
+       /* Select the Mouse Report Endpoint */\r
+       Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);\r
 \r
 \r
-               /* Check if Mouse Endpoint Ready for Read/Write */\r
-               if (Endpoint_IsReadWriteAllowed())\r
-               {\r
-                       /* Write Mouse Report Data */\r
-                       Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));\r
+       /* Check if Mouse Endpoint Ready for Read/Write */\r
+       if (Endpoint_IsReadWriteAllowed())\r
+       {\r
+               /* Write Mouse Report Data */\r
+               Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));\r
 \r
 \r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearIN();\r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearIN();\r
 \r
 \r
-                       /* Clear the report data afterwards */\r
-                       memset(&MouseReportData, 0, sizeof(MouseReportData));\r
-               }\r
+               /* Clear the report data afterwards */\r
+               memset(&MouseReportData, 0, sizeof(MouseReportData));\r
        }\r
 }\r
        }\r
 }\r
index 84d3778..954cbcb 100644 (file)
@@ -116,16 +116,17 @@ void MIDI_Task(void)
 {\r
        static uint8_t PrevJoystickStatus;\r
 \r
 {\r
        static uint8_t PrevJoystickStatus;\r
 \r
-       /* Select the MIDI IN stream */\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+\r
        Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);\r
 \r
        Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);\r
 \r
-       /* Check if endpoint is ready to be written to */\r
        if (Endpoint_IsINReady())\r
        {\r
                uint8_t MIDICommand = 0;\r
                uint8_t MIDIPitch;\r
        \r
        if (Endpoint_IsINReady())\r
        {\r
                uint8_t MIDICommand = 0;\r
                uint8_t MIDIPitch;\r
        \r
-               /* Get current joystick mask, XOR with previous to detect joystick changes */\r
                uint8_t JoystickStatus  = Joystick_GetStatus();\r
                uint8_t JoystickChanges = (JoystickStatus ^ PrevJoystickStatus);\r
                \r
                uint8_t JoystickStatus  = Joystick_GetStatus();\r
                uint8_t JoystickChanges = (JoystickStatus ^ PrevJoystickStatus);\r
                \r
index 60eb7fa..febb364 100644 (file)
@@ -313,10 +313,10 @@ void SendNextReport(void)
 /** Task to manage HID report generation and transmission to the host, when in report mode. */\r
 void Mouse_Task(void)\r
 {\r
 /** Task to manage HID report generation and transmission to the host, when in report mode. */\r
 void Mouse_Task(void)\r
 {\r
-       /* Check if the USB system is connected to a host */\r
-       if (USB_IsConnected)\r
-       {\r
-               /* Send the next mouse report to the host */\r
-               SendNextReport();\r
-       }\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+         \r
+       /* Send the next mouse report to the host */\r
+       SendNextReport();\r
 }\r
 }\r
index d77d25b..1c0e030 100644 (file)
@@ -284,6 +284,10 @@ void Ethernet_Task(void)
           outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single\r
           Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */\r
 \r
           outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single\r
           Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */\r
 \r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+\r
        /* Check if a frame has been written to the IN frame buffer */\r
        if (FrameIN.FrameInBuffer)\r
        {\r
        /* Check if a frame has been written to the IN frame buffer */\r
        if (FrameIN.FrameInBuffer)\r
        {\r
index c48c33c..9160dd1 100644 (file)
@@ -204,89 +204,90 @@ void EVENT_USB_UnhandledControlPacket(void)
 /** Task to manage CDC data transmission and reception to and from the host, from and to the physical USART. */\r
 void CDC_Task(void)\r
 {\r
 /** Task to manage CDC data transmission and reception to and from the host, from and to the physical USART. */\r
 void CDC_Task(void)\r
 {\r
-       if (USB_IsConnected)\r
-       {\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+         \r
 #if 0\r
 #if 0\r
-               /* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232\r
-                                handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code:\r
-               */\r
+       /* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232\r
+                        handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code:\r
+       */\r
 \r
 \r
-               USB_Notification_Header_t Notification = (USB_Notification_Header_t)\r
-                       {\r
-                               .NotificationType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),\r
-                               .Notification     = NOTIF_SerialState,\r
-                               .wValue           = 0,\r
-                               .wIndex           = 0,\r
-                               .wLength          = sizeof(uint16_t),\r
-                       };\r
-                       \r
-               uint16_t LineStateMask;\r
-               \r
-               // Set LineStateMask here to a mask of CONTROL_LINE_IN_* masks to set the input handshake line states to send to the host\r
+       USB_Notification_Header_t Notification = (USB_Notification_Header_t)\r
+               {\r
+                       .NotificationType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),\r
+                       .Notification     = NOTIF_SerialState,\r
+                       .wValue           = 0,\r
+                       .wIndex           = 0,\r
+                       .wLength          = sizeof(uint16_t),\r
+               };\r
                \r
                \r
-               Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM);\r
-               Endpoint_Write_Stream_LE(&Notification, sizeof(Notification));\r
-               Endpoint_Write_Stream_LE(&LineStateMask, sizeof(LineStateMask));\r
-               Endpoint_ClearIN();\r
+       uint16_t LineStateMask;\r
+       \r
+       // Set LineStateMask here to a mask of CONTROL_LINE_IN_* masks to set the input handshake line states to send to the host\r
+       \r
+       Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM);\r
+       Endpoint_Write_Stream_LE(&Notification, sizeof(Notification));\r
+       Endpoint_Write_Stream_LE(&LineStateMask, sizeof(LineStateMask));\r
+       Endpoint_ClearIN();\r
 #endif\r
 \r
 #endif\r
 \r
-               /* Select the Serial Rx Endpoint */\r
-               Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r
-               \r
-               /* Check to see if a packet has been received from the host */\r
-               if (Endpoint_IsOUTReceived())\r
+       /* Select the Serial Rx Endpoint */\r
+       Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r
+       \r
+       /* Check to see if a packet has been received from the host */\r
+       if (Endpoint_IsOUTReceived())\r
+       {\r
+               /* Read the bytes in from the endpoint into the buffer while space is available */\r
+               while (Endpoint_BytesInEndpoint() && (Rx_Buffer.Elements != BUFF_STATICSIZE))\r
                {\r
                {\r
-                       /* Read the bytes in from the endpoint into the buffer while space is available */\r
-                       while (Endpoint_BytesInEndpoint() && (Rx_Buffer.Elements != BUFF_STATICSIZE))\r
-                       {\r
-                               /* Store each character from the endpoint */\r
-                               Buffer_StoreElement(&Rx_Buffer, Endpoint_Read_Byte());\r
-                       }\r
-                       \r
-                       /* Check to see if all bytes in the current packet have been read */\r
-                       if (!(Endpoint_BytesInEndpoint()))\r
-                       {\r
-                               /* Clear the endpoint buffer */\r
-                               Endpoint_ClearOUT();\r
-                       }\r
+                       /* Store each character from the endpoint */\r
+                       Buffer_StoreElement(&Rx_Buffer, Endpoint_Read_Byte());\r
                }\r
                \r
                }\r
                \r
-               /* Check if Rx buffer contains data - if so, send it */\r
-               if (Rx_Buffer.Elements)\r
-                 Serial_TxByte(Buffer_GetElement(&Rx_Buffer));\r
+               /* Check to see if all bytes in the current packet have been read */\r
+               if (!(Endpoint_BytesInEndpoint()))\r
+               {\r
+                       /* Clear the endpoint buffer */\r
+                       Endpoint_ClearOUT();\r
+               }\r
+       }\r
+       \r
+       /* Check if Rx buffer contains data - if so, send it */\r
+       if (Rx_Buffer.Elements)\r
+         Serial_TxByte(Buffer_GetElement(&Rx_Buffer));\r
+\r
+       /* Select the Serial Tx Endpoint */\r
+       Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
 \r
 \r
-               /* Select the Serial Tx Endpoint */\r
-               Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
+       /* Check if the Tx buffer contains anything to be sent to the host */\r
+       if (Tx_Buffer.Elements)\r
+       {\r
+               /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
+               while (!(Endpoint_IsReadWriteAllowed()));\r
+               \r
+               /* Write the bytes from the buffer to the endpoint while space is available */\r
+               while (Tx_Buffer.Elements && Endpoint_IsReadWriteAllowed())\r
+               {\r
+                       /* Write each byte retreived from the buffer to the endpoint */\r
+                       Endpoint_Write_Byte(Buffer_GetElement(&Tx_Buffer));\r
+               }\r
+               \r
+               /* Remember if the packet to send completely fills the endpoint */\r
+               bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);\r
+               \r
+               /* Send the data */\r
+               Endpoint_ClearIN();\r
 \r
 \r
-               /* Check if the Tx buffer contains anything to be sent to the host */\r
-               if (Tx_Buffer.Elements)\r
+               /* If no more data to send and the last packet filled the endpoint, send an empty packet to release\r
+                * the buffer on the receiver (otherwise all data will be cached until a non-full packet is received) */\r
+               if (IsFull && !(Tx_Buffer.Elements))\r
                {\r
                        /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
                        while (!(Endpoint_IsReadWriteAllowed()));\r
                {\r
                        /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
                        while (!(Endpoint_IsReadWriteAllowed()));\r
-                       \r
-                       /* Write the bytes from the buffer to the endpoint while space is available */\r
-                       while (Tx_Buffer.Elements && Endpoint_IsReadWriteAllowed())\r
-                       {\r
-                               /* Write each byte retreived from the buffer to the endpoint */\r
-                               Endpoint_Write_Byte(Buffer_GetElement(&Tx_Buffer));\r
-                       }\r
-                       \r
-                       /* Remember if the packet to send completely fills the endpoint */\r
-                       bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);\r
-                       \r
-                       /* Send the data */\r
-                       Endpoint_ClearIN();\r
 \r
 \r
-                       /* If no more data to send and the last packet filled the endpoint, send an empty packet to release\r
-                        * the buffer on the receiver (otherwise all data will be cached until a non-full packet is received) */\r
-                       if (IsFull && !(Tx_Buffer.Elements))\r
-                       {\r
-                               /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
-                               while (!(Endpoint_IsReadWriteAllowed()));\r
-\r
-                               /* Send an empty packet to terminate the transfer */\r
-                               Endpoint_ClearIN();\r
-                       }\r
+                       /* Send an empty packet to terminate the transfer */\r
+                       Endpoint_ClearIN();\r
                }\r
        }\r
 }\r
                }\r
        }\r
 }\r
index 1edb352..0c331bc 100644 (file)
@@ -62,7 +62,7 @@ int main(void)
 {\r
        SetupHardware();\r
 \r
 {\r
        SetupHardware();\r
 \r
-       puts_P(PSTR(ESC_FG_CYAN "CDC Host Demo running.\r\n"));\r
+       puts_P(PSTR(ESC_FG_CYAN "CDC Host Demo running.\r\n" ESC_FG_WHITE));\r
 \r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 \r
 \r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 \r
@@ -72,8 +72,11 @@ int main(void)
                {\r
                        case HOST_STATE_Addressed:\r
                                if (!(CDC_Host_ConfigurePipes(&VirtualSerial_CDC_Interface)))\r
                {\r
                        case HOST_STATE_Addressed:\r
                                if (!(CDC_Host_ConfigurePipes(&VirtualSerial_CDC_Interface)))\r
-                                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
-                                 \r
+                               {\r
+                                       LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+                                       USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
+                               }\r
+                               \r
                                USB_HostState = HOST_STATE_Configured;\r
                                break;\r
                        case HOST_STATE_Configured:\r
                                USB_HostState = HOST_STATE_Configured;\r
                                break;\r
                        case HOST_STATE_Configured:\r
@@ -135,8 +138,8 @@ void EVENT_USB_HostError(const uint8_t ErrorCode)
 {\r
        USB_ShutDown();\r
 \r
 {\r
        USB_ShutDown();\r
 \r
-       puts_P(PSTR(ESC_FG_RED "Host Mode Error\r\n" ESC_FG_WHITE));\r
-       printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);\r
+       puts_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"));\r
+       printf_P(PSTR(" -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);\r
 \r
        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
        for(;;);\r
 \r
        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
        for(;;);\r
index 5623dc7..3b145a6 100644 (file)
@@ -226,7 +226,7 @@ void USB_Printer_Host(void)
                \r
                        Printer_Data_t TestPageData =\r
                                {\r
                \r
                        Printer_Data_t TestPageData =\r
                                {\r
-                                       "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X",\r
+                                       "\033%-12345X\033E" "LUFA PCL Test Page" "\033E\033%-12345X",\r
                                        (sizeof(TestPageData.Data) - 1)\r
                                };\r
                \r
                                        (sizeof(TestPageData.Data) - 1)\r
                                };\r
                \r
index 07c0514..12b5593 100644 (file)
@@ -114,7 +114,7 @@ bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)
 \r
 void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)\r
 {\r
 \r
 void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)\r
 {\r
-       if (!(USB_IsConnected))\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
          return;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
          return;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
index 2ac0dd3..4a6295a 100644 (file)
@@ -152,7 +152,7 @@ bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfac
                \r
 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)\r
 {\r
                \r
 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)\r
 {\r
-       if (!(USB_IsConnected))\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
          return;\r
 \r
        Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);\r
          return;\r
 \r
        Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);\r
index 9d73989..ceb4b96 100644 (file)
@@ -96,7 +96,7 @@ bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceIn
 \r
 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)\r
 {\r
 \r
 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)\r
 {\r
-       if (!(USB_IsConnected))\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
          return;\r
 \r
        Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);\r
          return;\r
 \r
        Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);\r
index 1b2da6f..0d35ee6 100644 (file)
@@ -138,7 +138,7 @@ bool RNDIS_Device_ConfigureEndpoints(USB_ClassInfo_RNDIS_Device_t* const RNDISIn
 \r
 void RNDIS_Device_USBTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)\r
 {\r
 \r
 void RNDIS_Device_USBTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)\r
 {\r
-       if (!(USB_IsConnected))\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
          return;\r
 \r
        RNDIS_Message_Header_t* MessageHeader = (RNDIS_Message_Header_t*)&RNDISInterfaceInfo->State.RNDISMessageBuffer;\r
          return;\r
 \r
        RNDIS_Message_Header_t* MessageHeader = (RNDIS_Message_Header_t*)&RNDISInterfaceInfo->State.RNDISMessageBuffer;\r