Added new USB_DeviceState variable to keep track of the current Device mode USB state.
[pub/USBasp.git] / Bootloaders / DFU / BootloaderDFU.c
index 2086a91..f9c8195 100644 (file)
@@ -177,7 +177,11 @@ void EVENT_USB_UnhandledControlPacket(void)
                        /* If the request has a data stage, load it into the command struct */\r
                        if (SentCommand.DataSize)\r
                        {\r
-                               while (!(Endpoint_IsOUTReceived()));\r
+                               while (!(Endpoint_IsOUTReceived()))\r
+                               {                               \r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
 \r
                                /* First byte of the data stage is the DNLOAD request's command */\r
                                SentCommand.Command = Endpoint_Read_Byte();\r
@@ -235,7 +239,12 @@ void EVENT_USB_UnhandledControlPacket(void)
                                                        if (!(Endpoint_BytesInEndpoint()))\r
                                                        {\r
                                                                Endpoint_ClearOUT();\r
-                                                               while (!(Endpoint_IsOUTReceived()));\r
+\r
+                                                               while (!(Endpoint_IsOUTReceived()))\r
+                                                               {                               \r
+                                                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                                                         return;\r
+                                                               }\r
                                                        }\r
 \r
                                                        /* Write the next word into the current flash page */\r
@@ -279,7 +288,12 @@ void EVENT_USB_UnhandledControlPacket(void)
                                                        if (!(Endpoint_BytesInEndpoint()))\r
                                                        {\r
                                                                Endpoint_ClearOUT();\r
-                                                               while (!(Endpoint_IsOUTReceived()));\r
+\r
+                                                               while (!(Endpoint_IsOUTReceived()))\r
+                                                               {                               \r
+                                                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                                                         return;\r
+                                                               }\r
                                                        }\r
 \r
                                                        /* Read the byte from the USB interface and write to to the EEPROM */\r
@@ -297,16 +311,18 @@ void EVENT_USB_UnhandledControlPacket(void)
 \r
                        Endpoint_ClearOUT();\r
 \r
-                       /* Acknowledge status stage */\r
-                       while (!(Endpoint_IsINReady()));\r
-                       Endpoint_ClearIN();\r
-                               \r
+                       Endpoint_ClearStatusStage();\r
+\r
                        break;\r
                case DFU_UPLOAD:\r
                        Endpoint_ClearSETUP();\r
 \r
-                       while (!(Endpoint_IsINReady()));\r
-\r
+                       while (!(Endpoint_IsINReady()))\r
+                       {                               \r
+                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                 return;\r
+                       }\r
+                                                       \r
                        if (DFU_State != dfuUPLOAD_IDLE)\r
                        {\r
                                if ((DFU_State == dfuERROR) && IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01))       // Blank Check\r
@@ -343,7 +359,12 @@ void EVENT_USB_UnhandledControlPacket(void)
                                                if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)\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
                                                /* Read the flash word and send it via USB to the host */\r
@@ -368,7 +389,12 @@ void EVENT_USB_UnhandledControlPacket(void)
                                                if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)\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
                                                /* Read the EEPROM byte and send it via USB to the host */\r
@@ -385,10 +411,7 @@ void EVENT_USB_UnhandledControlPacket(void)
 \r
                        Endpoint_ClearIN();\r
 \r
-                       /* Acknowledge status stage */\r
-                       while (!(Endpoint_IsOUTReceived()));\r
-                       Endpoint_ClearOUT();\r
-\r
+                       Endpoint_ClearStatusStage();\r
                        break;\r
                case DFU_GETSTATUS:\r
                        Endpoint_ClearSETUP();\r
@@ -408,10 +431,7 @@ void EVENT_USB_UnhandledControlPacket(void)
 \r
                        Endpoint_ClearIN();\r
                        \r
-                       /* Acknowledge status stage */\r
-                       while (!(Endpoint_IsOUTReceived()));\r
-                       Endpoint_ClearOUT();\r
-       \r
+                       Endpoint_ClearStatusStage();\r
                        break;          \r
                case DFU_CLRSTATUS:\r
                        Endpoint_ClearSETUP();\r
@@ -419,10 +439,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                        /* Reset the status value variable to the default OK status */\r
                        DFU_Status = OK;\r
 \r
-                       /* Acknowledge status stage */\r
-                       while (!(Endpoint_IsINReady()));\r
-                       Endpoint_ClearIN();\r
-                       \r
+                       Endpoint_ClearStatusStage();\r
                        break;\r
                case DFU_GETSTATE:\r
                        Endpoint_ClearSETUP();\r
@@ -432,21 +449,15 @@ void EVENT_USB_UnhandledControlPacket(void)
                \r
                        Endpoint_ClearIN();\r
                        \r
-                       /* Acknowledge status stage */\r
-                       while (!(Endpoint_IsOUTReceived()));\r
-                       Endpoint_ClearOUT();\r
-\r
+                       Endpoint_ClearStatusStage();\r
                        break;\r
                case DFU_ABORT:\r
                        Endpoint_ClearSETUP();\r
                        \r
                        /* Reset the current state variable to the default idle state */\r
                        DFU_State = dfuIDLE;\r
-                       \r
-                       /* Acknowledge status stage */\r
-                       while (!(Endpoint_IsINReady()));\r
-                       Endpoint_ClearIN();\r
 \r
+                       Endpoint_ClearStatusStage();\r
                        break;\r
        }\r
 }\r
@@ -465,7 +476,11 @@ static void DiscardFillerBytes(uint8_t NumberOfBytes)
                        Endpoint_ClearOUT();\r
 \r
                        /* Wait until next data packet received */\r
-                       while (!(Endpoint_IsOUTReceived()));\r
+                       while (!(Endpoint_IsOUTReceived()))\r
+                       {                               \r
+                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                 return;\r
+                       }\r
                }\r
                else\r
                {\r