Added new USB_DeviceState variable to keep track of the current Device mode USB state.
[pub/USBasp.git] / Demos / Device / LowLevel / MassStorage / Lib / DataflashManager.c
index fdc91dd..2bd03c9 100644 (file)
@@ -68,8 +68,12 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo
        Dataflash_SendAddressBytes(0, CurrDFPageByte);\r
 \r
        /* Wait until endpoint is ready before continuing */\r
-       while (!(Endpoint_IsReadWriteAllowed()));\r
-\r
+       while (!(Endpoint_IsReadWriteAllowed()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;\r
+       }\r
+       \r
        while (TotalBlocks)\r
        {\r
                uint8_t BytesInBlockDiv16 = 0;\r
@@ -84,7 +88,11 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo
                                Endpoint_ClearOUT();\r
                                \r
                                /* Wait until the host has sent another packet */\r
-                               while (!(Endpoint_IsReadWriteAllowed()));\r
+                               while (!(Endpoint_IsReadWriteAllowed()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
                        }\r
 \r
                        /* Check if end of dataflash page reached */\r
@@ -195,7 +203,11 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc
        Dataflash_SendByte(0x00);\r
        \r
        /* Wait until endpoint is ready before continuing */\r
-       while (!(Endpoint_IsReadWriteAllowed()));\r
+       while (!(Endpoint_IsReadWriteAllowed()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;\r
+       }\r
        \r
        while (TotalBlocks)\r
        {\r
@@ -211,7 +223,11 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc
                                Endpoint_ClearIN();\r
                                \r
                                /* Wait until the endpoint is ready for more data */\r
-                               while (!(Endpoint_IsReadWriteAllowed()));\r
+                               while (!(Endpoint_IsReadWriteAllowed()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
                        }\r
                        \r
                        /* Check if end of dataflash page reached */\r