Fixed Low Speed USB devices broken when using the library HID Class driver (thanks...
[pub/USBasp.git] / Bootloaders / MassStorage / Lib / VirtualFAT.c
index 0bcda06..e0541f4 100644 (file)
@@ -72,16 +72,34 @@ static const FATBootBlock_t BootBlock =
        };
 
 /** FAT 8.3 style directory entry, for the virtual FLASH contents file. */
        };
 
 /** FAT 8.3 style directory entry, for the virtual FLASH contents file. */
-static FATDirectoryEntry_t FirmwareFileEntry =
+static FATDirectoryEntry_t FirmwareFileEntries[2] =
        {
        {
-               .Filename               = "FIRMWARE",
-               .Extension              = "BIN",
-               .Attributes             = 0,
-               .Reserved               = {0},
-               .CreationTime           = FAT_TIME(1, 1, 0),
-               .CreationDate           = FAT_DATE(14, 2, 1989),
-               .StartingCluster        = 2,
-               .FileSizeBytes          = FIRMWARE_FILE_SIZE_BYTES,
+               /* Root volume label entry; disk label is contained in the Filename and
+                * Extension fields (concatenated) with a special attribute flag - other
+                * fields are ignored. Should be the same as the label in the boot block.
+                */
+               {
+                       .Filename               = "LUFA BOO",
+                       .Extension              = "T  ",
+                       .Attributes             = (1 << 3),
+                       .Reserved               = {0},
+                       .CreationTime           = 0,
+                       .CreationDate           = 0,
+                       .StartingCluster        = 0,
+                       .FileSizeBytes          = 0,
+               },
+
+               /* File entry for the virtual Firmware image. */
+               {
+                       .Filename               = "FIRMWARE",
+                       .Extension              = "BIN",
+                       .Attributes             = 0,
+                       .Reserved               = {0},
+                       .CreationTime           = FAT_TIME(1, 1, 0),
+                       .CreationDate           = FAT_DATE(14, 2, 1989),
+                       .StartingCluster        = 2,
+                       .FileSizeBytes          = FIRMWARE_FILE_SIZE_BYTES,
+               },
        };
 
 
        };
 
 
@@ -101,8 +119,8 @@ static void UpdateFAT12ClusterEntry(uint8_t* const FATTable,
                                     const uint16_t ChainEntry)
 {
        /* Calculate the starting offset of the cluster entry in the FAT12 table */
                                     const uint16_t ChainEntry)
 {
        /* Calculate the starting offset of the cluster entry in the FAT12 table */
-       uint8_t FATOffset   =   (Index * 3) / 2;
-       bool    UpperNibble = (((Index * 3) % 2) != 0);
+       uint8_t FATOffset   = (Index + (Index >> 1));
+       bool    UpperNibble = ((Index & 1) != 0);
 
        /* Check if the start of the entry is at an upper nibble of the byte, fill
         * out FAT12 entry as required */
 
        /* Check if the start of the entry is at an upper nibble of the byte, fill
         * out FAT12 entry as required */
@@ -133,26 +151,28 @@ static void WriteVirtualBlock(const uint16_t BlockNumber)
 
        if ((BlockNumber >= 4) && (BlockNumber < (4 + FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES))))
        {
 
        if ((BlockNumber >= 4) && (BlockNumber < (4 + FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES))))
        {
+               #if (FLASHEND > 0xFFFF)
                uint32_t WriteFlashAddress = (uint32_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
                uint32_t WriteFlashAddress = (uint32_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
+               #else
+               uint16_t WriteFlashAddress = (uint16_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
+               #endif
 
                for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i += 2)
                {
                        if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
                        {
                                /* Erase the given FLASH page, ready to be programmed */
 
                for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i += 2)
                {
                        if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
                        {
                                /* Erase the given FLASH page, ready to be programmed */
-                               boot_page_erase(WriteFlashAddress);
-                               boot_spm_busy_wait();
+                               BootloaderAPI_ErasePage(WriteFlashAddress);
                        }
 
                        /* Write the next data word to the FLASH page */
                        }
 
                        /* Write the next data word to the FLASH page */
-                       boot_page_fill(WriteFlashAddress, (BlockBuffer[i + 1] << 8) | BlockBuffer[i]);
+                       BootloaderAPI_FillWord(WriteFlashAddress, (BlockBuffer[i + 1] << 8) | BlockBuffer[i]);
                        WriteFlashAddress += 2;
 
                        if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
                        {
                                /* Write the filled FLASH page to memory */
                        WriteFlashAddress += 2;
 
                        if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
                        {
                                /* Write the filled FLASH page to memory */
-                               boot_page_write(WriteFlashAddress - SPM_PAGESIZE);
-                               boot_spm_busy_wait();
+                               BootloaderAPI_WritePage(WriteFlashAddress - SPM_PAGESIZE);
                        }
                }
        }
                        }
                }
        }
@@ -195,16 +215,23 @@ static void ReadVirtualBlock(const uint16_t BlockNumber)
                        break;
 
                case 3: /* Block 3: Root file entries */
                        break;
 
                case 3: /* Block 3: Root file entries */
-                       memcpy(BlockBuffer, &FirmwareFileEntry, sizeof(FATDirectoryEntry_t));
+                       memcpy(BlockBuffer, FirmwareFileEntries, sizeof(FirmwareFileEntries));
                        break;
 
                default: /* Blocks 4 onwards: Data allocation section */
                        if ((BlockNumber >= 4) && (BlockNumber < (4 + FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES))))
                        {
                        break;
 
                default: /* Blocks 4 onwards: Data allocation section */
                        if ((BlockNumber >= 4) && (BlockNumber < (4 + FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES))))
                        {
+                               #if (FLASHEND > 0xFFFF)
                                uint32_t ReadFlashAddress = (uint32_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
 
                                for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i++)
                                  BlockBuffer[i] = pgm_read_byte_far(ReadFlashAddress++);
                                uint32_t ReadFlashAddress = (uint32_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
 
                                for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i++)
                                  BlockBuffer[i] = pgm_read_byte_far(ReadFlashAddress++);
+                               #else
+                               uint16_t ReadFlashAddress = (uint16_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
+
+                               for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i++)
+                                 BlockBuffer[i] = pgm_read_byte(ReadFlashAddress++);
+                               #endif
                        }
 
                        break;
                        }
 
                        break;
@@ -218,12 +245,10 @@ static void ReadVirtualBlock(const uint16_t BlockNumber)
 /** Writes a number of blocks to the virtual FAT file system, from the host
  *  PC via the USB Mass Storage interface.
  *
 /** Writes a number of blocks to the virtual FAT file system, from the host
  *  PC via the USB Mass Storage interface.
  *
- *  \param[in] MSInterfaceInfo  Pointer to a structure containing a Mass Storage Class configuration and state
- *  \param[in] BlockAddress  Data block starting address for the write sequence
- *  \param[in] TotalBlocks   Number of blocks of data to write
+ *  \param[in] BlockAddress     Data block starting address for the write sequence
+ *  \param[in] TotalBlocks      Number of blocks of data to write
  */
  */
-void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
-                            const uint32_t BlockAddress,
+void VirtualFAT_WriteBlocks(const uint16_t BlockAddress,
                             uint16_t TotalBlocks)
 {
        uint16_t CurrentBlock = (uint16_t)BlockAddress;
                             uint16_t TotalBlocks)
 {
        uint16_t CurrentBlock = (uint16_t)BlockAddress;
@@ -237,12 +262,10 @@ void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
 /** Reads a number of blocks from the virtual FAT file system, and sends them
  *  to the host PC via the USB Mass Storage interface.
  *
 /** Reads a number of blocks from the virtual FAT file system, and sends them
  *  to the host PC via the USB Mass Storage interface.
  *
- *  \param[in] MSInterfaceInfo  Pointer to a structure containing a Mass Storage Class configuration and state
- *  \param[in] BlockAddress  Data block starting address for the read sequence
- *  \param[in] TotalBlocks   Number of blocks of data to read
+ *  \param[in] BlockAddress     Data block starting address for the read sequence
+ *  \param[in] TotalBlocks      Number of blocks of data to read
  */
  */
-void VirtualFAT_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
-                           const uint32_t BlockAddress,
+void VirtualFAT_ReadBlocks(const uint16_t BlockAddress,
                            uint16_t TotalBlocks)
 {
        uint16_t CurrentBlock = (uint16_t)BlockAddress;
                            uint16_t TotalBlocks)
 {
        uint16_t CurrentBlock = (uint16_t)BlockAddress;