State information for class drivers is now zeroed out during enumeration (both in...
[pub/USBasp.git] / Demos / Device / LowLevel / MassStorage / Lib / DataflashManager.c
index 6dc5495..2bd03c9 100644 (file)
@@ -53,22 +53,27 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo
        uint8_t  CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);\r
        bool     UsingSecondBuffer   = false;\r
 \r
        uint8_t  CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);\r
        bool     UsingSecondBuffer   = false;\r
 \r
-       /* Copy selected dataflash's current page contents to the dataflash buffer */\r
+       /* Select the correct starting Dataflash IC for the block requested */\r
        Dataflash_SelectChipFromPage(CurrDFPage);\r
        Dataflash_SelectChipFromPage(CurrDFPage);\r
+\r
 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)\r
 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)\r
+       /* Copy selected dataflash's current page contents to the dataflash buffer */\r
        Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);\r
        Dataflash_SendAddressBytes(CurrDFPage, 0);\r
        Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);\r
        Dataflash_SendAddressBytes(CurrDFPage, 0);\r
-#endif\r
        Dataflash_WaitWhileBusy();\r
        Dataflash_WaitWhileBusy();\r
+#endif\r
 \r
        /* Send the dataflash buffer write command */\r
 \r
        /* Send the dataflash buffer write command */\r
-       Dataflash_ToggleSelectedChipCS();\r
        Dataflash_SendByte(DF_CMD_BUFF1WRITE);\r
        Dataflash_SendAddressBytes(0, CurrDFPageByte);\r
 \r
        /* Wait until endpoint is ready before continuing */\r
        Dataflash_SendByte(DF_CMD_BUFF1WRITE);\r
        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
        while (TotalBlocks)\r
        {\r
                uint8_t BytesInBlockDiv16 = 0;\r
@@ -83,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
                                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
                        }\r
 \r
                        /* Check if end of dataflash page reached */\r
@@ -182,8 +191,10 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc
        uint16_t CurrDFPageByte      = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);\r
        uint8_t  CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);\r
 \r
        uint16_t CurrDFPageByte      = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);\r
        uint8_t  CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);\r
 \r
-       /* Send the dataflash main memory page read command */\r
+       /* Select the correct starting Dataflash IC for the block requested */\r
        Dataflash_SelectChipFromPage(CurrDFPage);\r
        Dataflash_SelectChipFromPage(CurrDFPage);\r
+\r
+       /* Send the dataflash main memory page read command */\r
        Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);\r
        Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);\r
        Dataflash_SendByte(0x00);\r
        Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);\r
        Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);\r
        Dataflash_SendByte(0x00);\r
@@ -192,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
        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
        \r
        while (TotalBlocks)\r
        {\r
@@ -208,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
                                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
                        }\r
                        \r
                        /* Check if end of dataflash page reached */\r
@@ -287,16 +306,17 @@ void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress, uint16_t Tota
        uint8_t  CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);\r
        bool     UsingSecondBuffer   = false;\r
 \r
        uint8_t  CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);\r
        bool     UsingSecondBuffer   = false;\r
 \r
-       /* Copy selected dataflash's current page contents to the dataflash buffer */\r
+       /* Select the correct starting Dataflash IC for the block requested */\r
        Dataflash_SelectChipFromPage(CurrDFPage);\r
        Dataflash_SelectChipFromPage(CurrDFPage);\r
+\r
 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)\r
 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)\r
+       /* Copy selected dataflash's current page contents to the dataflash buffer */\r
        Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);\r
        Dataflash_SendAddressBytes(CurrDFPage, 0);\r
        Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);\r
        Dataflash_SendAddressBytes(CurrDFPage, 0);\r
-#endif\r
        Dataflash_WaitWhileBusy();\r
        Dataflash_WaitWhileBusy();\r
+#endif\r
 \r
        /* Send the dataflash buffer write command */\r
 \r
        /* Send the dataflash buffer write command */\r
-       Dataflash_ToggleSelectedChipCS();\r
        Dataflash_SendByte(DF_CMD_BUFF1WRITE);\r
        Dataflash_SendAddressBytes(0, CurrDFPageByte);\r
 \r
        Dataflash_SendByte(DF_CMD_BUFF1WRITE);\r
        Dataflash_SendAddressBytes(0, CurrDFPageByte);\r
 \r
@@ -384,8 +404,10 @@ void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress, uint16_t Total
        uint16_t CurrDFPageByte      = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);\r
        uint8_t  CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);\r
 \r
        uint16_t CurrDFPageByte      = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);\r
        uint8_t  CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);\r
 \r
-       /* Send the dataflash main memory page read command */\r
+       /* Select the correct starting Dataflash IC for the block requested */\r
        Dataflash_SelectChipFromPage(CurrDFPage);\r
        Dataflash_SelectChipFromPage(CurrDFPage);\r
+\r
+       /* Send the dataflash main memory page read command */\r
        Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);\r
        Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);\r
        Dataflash_SendByte(0x00);\r
        Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);\r
        Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);\r
        Dataflash_SendByte(0x00);\r