Fix up the incomplete Webserver project so that it integrates with the uIP stack...
[pub/USBasp.git] / Demos / Device / LowLevel / MassStorage / Lib / DataflashManager.c
index fdc91dd..4bfe757 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
@@ -68,8 +68,9 @@ 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
+       if (Endpoint_WaitUntilReady())\r
+         return;\r
+       \r
        while (TotalBlocks)\r
        {\r
                uint8_t BytesInBlockDiv16 = 0;\r
@@ -84,7 +85,8 @@ 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
+                               if (Endpoint_WaitUntilReady())\r
+                                 return;\r
                        }\r
 \r
                        /* Check if end of dataflash page reached */\r
@@ -195,7 +197,8 @@ 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
+       if (Endpoint_WaitUntilReady())\r
+         return;\r
        \r
        while (TotalBlocks)\r
        {\r
@@ -211,7 +214,8 @@ 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
+                               if (Endpoint_WaitUntilReady())\r
+                                 return;\r
                        }\r
                        \r
                        /* Check if end of dataflash page reached */\r
@@ -484,3 +488,36 @@ void DataflashManager_ResetDataflashProtections(void)
        /* Deselect current dataflash chip */\r
        Dataflash_DeselectChip();\r
 }\r
+\r
+/** Performs a simple test on the attached Dataflash IC(s) to ensure that they are working.\r
+ *\r
+ *  \return Boolean true if all media chips are working, false otherwise\r
+ */\r
+bool DataflashManager_CheckDataflashOperation(void)\r
+{\r
+       uint8_t ReturnByte;\r
+\r
+       /* Test first Dataflash IC is present and responding to commands */\r
+       Dataflash_SelectChip(DATAFLASH_CHIP1);\r
+       Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO);\r
+       ReturnByte = Dataflash_ReceiveByte();\r
+       Dataflash_DeselectChip();\r
+\r
+       /* If returned data is invalid, fail the command */\r
+       if (ReturnByte != DF_MANUFACTURER_ATMEL)\r
+         return false;\r
+\r
+       #if (DATAFLASH_TOTALCHIPS == 2)\r
+       /* Test second Dataflash IC is present and responding to commands */\r
+       Dataflash_SelectChip(DATAFLASH_CHIP2);\r
+       Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO);\r
+       ReturnByte = Dataflash_ReceiveByte();\r
+       Dataflash_DeselectChip();\r
+\r
+       /* If returned data is invalid, fail the command */\r
+       if (ReturnByte != DF_MANUFACTURER_ATMEL)\r
+         return false;\r
+       #endif\r
+       \r
+       return true;\r
+}\r