Added new USB_DeviceState variable to keep track of the current Device mode USB state.
[pub/lufa.git] / Bootloaders / CDC / BootloaderCDC.c
index d875b84..cbcfd7e 100644 (file)
@@ -165,9 +165,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                \r
                                Endpoint_ClearIN();\r
                                \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsOUTReceived()));\r
-                               Endpoint_ClearOUT();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
@@ -176,16 +174,18 @@ void EVENT_USB_UnhandledControlPacket(void)
                        {\r
                                Endpoint_ClearSETUP();\r
 \r
-                               while (!(Endpoint_IsOUTReceived()));\r
-\r
+                               while (!(Endpoint_IsOUTReceived()))\r
+                               {                               \r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
+                       \r
                                for (uint8_t i = 0; i < sizeof(LineCoding); i++)\r
                                  *(LineCodingData++) = Endpoint_Read_Byte();\r
 \r
                                Endpoint_ClearOUT();\r
 \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
        \r
                        break;\r
@@ -194,9 +194,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                        {\r
                                Endpoint_ClearSETUP();\r
                                \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
        \r
                        break;\r
@@ -333,7 +331,12 @@ static uint8_t FetchNextCommandByte(void)
        while (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearOUT();\r
-               while (!(Endpoint_IsOUTReceived()));\r
+\r
+               while (!(Endpoint_IsOUTReceived()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return 0;\r
+               }\r
        }\r
        \r
        /* Fetch the next byte from the OUT endpoint */\r
@@ -354,7 +357,12 @@ static void WriteNextResponseByte(const uint8_t Response)
        if (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearIN();\r
-               while (!(Endpoint_IsINReady()));\r
+               \r
+               while (!(Endpoint_IsINReady()))\r
+               {                               \r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
+               }\r
        }\r
        \r
        /* Write the next byte to the OUT endpoint */\r
@@ -563,12 +571,21 @@ void CDC_Task(void)
                /* If a full endpoint's worth of data was sent, we need to send an empty packet afterwards to signal end of transfer */\r
                if (IsEndpointFull)\r
                {\r
-                       while (!(Endpoint_IsINReady()));\r
+                       while (!(Endpoint_IsINReady()))\r
+                       {                               \r
+                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                 return;\r
+                       }\r
+\r
                        Endpoint_ClearIN();\r
                }\r
 \r
                /* Wait until the data has been sent to the host */\r
-               while (!(Endpoint_IsINReady()));\r
+               while (!(Endpoint_IsINReady()))\r
+               {                               \r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
+               }\r
                \r
                /* Select the OUT endpoint */\r
                Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r