Add LUFA-111009-BETA tag.
[pub/USBasp.git] / Demos / Device / ClassDriver / MassStorageKeyboard / Lib / DataflashManager.c
index ce2ac81..d07e486 100644 (file)
@@ -1,21 +1,21 @@
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2010.
-              
+     Copyright (C) Dean Camera, 2011.
+
   dean [at] fourwalledcubicle [dot] com
-      www.fourwalledcubicle.com
+           www.lufa-lib.org
 */
 
 /*
-  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
-  Permission to use, copy, modify, distribute, and sell this 
+  Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
-  without fee, provided that the above copyright notice appear in 
+  without fee, provided that the above copyright notice appear in
   all copies and that both that the copyright notice and this
-  permission notice and warranty disclaimer appear in supporting 
-  documentation, and that the name of the author not be used in 
-  advertising or publicity pertaining to distribution of the 
+  permission notice and warranty disclaimer appear in supporting
+  documentation, and that the name of the author not be used in
+  advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.
 
   The author disclaim all warranties with regard to this
@@ -77,7 +77,7 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceIn
        while (TotalBlocks)
        {
                uint8_t BytesInBlockDiv16 = 0;
-               
+
                /* Write an endpoint packet sized data block to the Dataflash */
                while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
                {
@@ -86,7 +86,7 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceIn
                        {
                                /* Clear the current endpoint bank */
                                Endpoint_ClearOUT();
-                               
+
                                /* Wait until the host has sent another packet */
                                if (Endpoint_WaitUntilReady())
                                  return;
@@ -125,27 +125,27 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceIn
 
                                /* Send the Dataflash buffer write command */
                                Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
-                               Dataflash_SendAddressBytes(0, 0);                               
+                               Dataflash_SendAddressBytes(0, 0);
                        }
 
                        /* Write one 16-byte chunk of data to the Dataflash */
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       Dataflash_SendByte(Endpoint_Read_Byte());
-                       
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+                       Dataflash_SendByte(Endpoint_Read_8());
+
                        /* Increment the Dataflash page 16 byte block counter */
                        CurrDFPageByteDiv16++;
 
@@ -154,10 +154,10 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceIn
 
                        /* Check if the current command is being aborted by the host */
                        if (MSInterfaceInfo->State.IsMassStoreReset)
-                         return;                       
+                         return;
                }
-                       
-               /* Decrement the blocks remaining counter and reset the sub block counter */
+
+               /* Decrement the blocks remaining counter */
                TotalBlocks--;
        }
 
@@ -201,15 +201,15 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInf
        Dataflash_SendByte(0x00);
        Dataflash_SendByte(0x00);
        Dataflash_SendByte(0x00);
-       
+
        /* Wait until endpoint is ready before continuing */
        if (Endpoint_WaitUntilReady())
          return;
-       
+
        while (TotalBlocks)
        {
                uint8_t BytesInBlockDiv16 = 0;
-               
+
                /* Write an endpoint packet sized data block to the Dataflash */
                while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
                {
@@ -218,12 +218,12 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInf
                        {
                                /* Clear the endpoint bank to send its contents to the host */
                                Endpoint_ClearIN();
-                               
+
                                /* Wait until the endpoint is ready for more data */
                                if (Endpoint_WaitUntilReady())
                                  return;
                        }
-                       
+
                        /* Check if end of Dataflash page reached */
                        if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
                        {
@@ -233,7 +233,7 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInf
 
                                /* Select the next Dataflash chip based on the new Dataflash page index */
                                Dataflash_SelectChipFromPage(CurrDFPage);
-                               
+
                                /* Send the Dataflash main memory page read command */
                                Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
                                Dataflash_SendAddressBytes(CurrDFPage, 0);
@@ -241,29 +241,29 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInf
                                Dataflash_SendByte(0x00);
                                Dataflash_SendByte(0x00);
                                Dataflash_SendByte(0x00);
-                       }       
+                       }
 
                        /* Read one 16-byte chunk of data from the Dataflash */
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       Endpoint_Write_Byte(Dataflash_ReceiveByte());
-                       
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+                       Endpoint_Write_8(Dataflash_ReceiveByte());
+
                        /* Increment the Dataflash page 16 byte block counter */
                        CurrDFPageByteDiv16++;
-                       
+
                        /* Increment the block 16 byte block counter */
                        BytesInBlockDiv16++;
 
@@ -271,11 +271,11 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInf
                        if (MSInterfaceInfo->State.IsMassStoreReset)
                          return;
                }
-               
+
                /* Decrement the blocks remaining counter */
                TotalBlocks--;
        }
-       
+
        /* If the endpoint is full, send its contents to the host */
        if (!(Endpoint_IsReadWriteAllowed()))
          Endpoint_ClearIN();
@@ -285,7 +285,7 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInf
 }
 
 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board Dataflash IC(s), from
- *  the given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the
+ *  the given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the
  *  Dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the
  *  Dataflash.
  *
@@ -315,11 +315,11 @@ void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress,
        /* Send the Dataflash buffer write command */
        Dataflash_SendByte(DF_CMD_BUFF1WRITE);
        Dataflash_SendAddressBytes(0, CurrDFPageByte);
-       
+
        while (TotalBlocks)
        {
                uint8_t BytesInBlockDiv16 = 0;
-               
+
                /* Write an endpoint packet sized data block to the Dataflash */
                while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
                {
@@ -356,22 +356,22 @@ void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress,
 
                                /* Send the Dataflash buffer write command */
                                Dataflash_ToggleSelectedChipCS();
-                               Dataflash_SendByte(DF_CMD_BUFF1WRITE);
+                               Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
                                Dataflash_SendAddressBytes(0, 0);
                        }
-                       
+
                        /* Write one 16-byte chunk of data to the Dataflash */
                        for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
                          Dataflash_SendByte(*(BufferPtr++));
-                       
+
                        /* Increment the Dataflash page 16 byte block counter */
                        CurrDFPageByteDiv16++;
 
                        /* Increment the block 16 byte block counter */
-                       BytesInBlockDiv16++;            
+                       BytesInBlockDiv16++;
                }
-                       
-               /* Decrement the blocks remaining counter and reset the sub block counter */
+
+               /* Decrement the blocks remaining counter */
                TotalBlocks--;
        }
 
@@ -386,7 +386,7 @@ void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress,
 }
 
 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board Dataflash IC(s), into
- *  the preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash
+ *  the preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash
  *  and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read
  *  the files stored on the Dataflash.
  *
@@ -416,7 +416,7 @@ void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress,
        while (TotalBlocks)
        {
                uint8_t BytesInBlockDiv16 = 0;
-               
+
                /* Write an endpoint packet sized data block to the Dataflash */
                while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
                {
@@ -429,7 +429,7 @@ void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress,
 
                                /* Select the next Dataflash chip based on the new Dataflash page index */
                                Dataflash_SelectChipFromPage(CurrDFPage);
-                               
+
                                /* Send the Dataflash main memory page read command */
                                Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
                                Dataflash_SendAddressBytes(CurrDFPage, 0);
@@ -437,19 +437,19 @@ void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress,
                                Dataflash_SendByte(0x00);
                                Dataflash_SendByte(0x00);
                                Dataflash_SendByte(0x00);
-                       }       
+                       }
 
                        /* Read one 16-byte chunk of data from the Dataflash */
                        for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
                          *(BufferPtr++) = Dataflash_ReceiveByte();
-                       
+
                        /* Increment the Dataflash page 16 byte block counter */
                        CurrDFPageByteDiv16++;
-                       
+
                        /* Increment the block 16 byte block counter */
                        BytesInBlockDiv16++;
                }
-               
+
                /* Decrement the blocks remaining counter */
                TotalBlocks--;
        }
@@ -464,7 +464,7 @@ void DataflashManager_ResetDataflashProtections(void)
        /* Select first Dataflash chip, send the read status register command */
        Dataflash_SelectChip(DATAFLASH_CHIP1);
        Dataflash_SendByte(DF_CMD_GETSTATUS);
-       
+
        /* Check if sector protection is enabled */
        if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
        {
@@ -476,12 +476,12 @@ void DataflashManager_ResetDataflashProtections(void)
                Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
                Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
        }
-       
+
        /* Select second Dataflash chip (if present on selected board), send read status register command */
        #if (DATAFLASH_TOTALCHIPS == 2)
        Dataflash_SelectChip(DATAFLASH_CHIP2);
        Dataflash_SendByte(DF_CMD_GETSTATUS);
-       
+
        /* Check if sector protection is enabled */
        if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
        {
@@ -494,7 +494,7 @@ void DataflashManager_ResetDataflashProtections(void)
                Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
        }
        #endif
-       
+
        /* Deselect current Dataflash chip */
        Dataflash_DeselectChip();
 }
@@ -528,6 +528,7 @@ bool DataflashManager_CheckDataflashOperation(void)
        if (ReturnByte != DF_MANUFACTURER_ATMEL)
          return false;
        #endif
-       
+
        return true;
 }
+