📄 fat16.c
字号:
{ // search the next 16 rootentries in this sector of the roordirectory.
rootentry=0;
mmc_read_sector((unsigned long)(RootDirectory + sector_offset),&FileBuffer[0]); // Read the Rootdirectory.
DirectoryEntry = (struct DirEntry *)&FileBuffer[0];
while((rootentry<16) && (!retvalue))
{
if(DirectoryEntry[rootentry].attribute == 0) // empty directory entry found
{
for(i=0;i<11;i++) DirectoryEntry[rootentry].name[i] = fname[i]; // Kopie the filename and the file extension to the directoryentry.
DirectoryEntry[rootentry].attribute = 0x20; // Set the fileattribute to archive to reserve the directoryentry.
DirectoryEntry[rootentry].startcluster = cluster; // copy the location of the first datacluster to the directoryentry.
DirectoryEntry[rootentry].size = 0; // the new createted file has no content yet.
fpoint->directory_sector = (unsigned long) (RootDirectory + sector_offset);
fpoint->directory_index = (unsigned char) rootentry;
retvalue = 1;
// printf("Cluster: %u \n\r",cluster);
mmc_write_sector((unsigned long)(RootDirectory + sector_offset),&FileBuffer[0]);
}
rootentry++;
cnt_enries_searched++;
}
if(!retvalue) // file not found in this sector so take next sector.
{
rootentry = 0;
sector_offset++;
}
}
while((cnt_enries_searched<PossibleRootEntries) && (!retvalue));
return(retvalue); // return 1 if file has been created otherwise return 0.
}
//________________________________________________________________________________________________________________________________________
// Funtion: unsigned int FindNextFreeCluster(void)
//
// Description: This function looks in the FAT to find the next free datacluster
//
//________________________________________________________________________________________________________________________________________
unsigned int FindNextFreeCluster(File *file)
{
unsigned long fat_pointer = 0; // Pointer to the first sector of the FAT.
unsigned long ul_tmp = 0; // temporary variable used to calculate a sectornumber.
unsigned int fat_sector_offset = 0; // index to a sector within the FAT.
unsigned int fat_entry = 0; // index to an fatentry within the actual sector (256 fatentries are possible within one sector).
unsigned int free_cluster = 0; // a pointer to the first sector of the next free cluster.
#if USE_MMC
fat_pointer = (unsigned long) FileAllocationTable; // start searching for empty cluster at the beginning of the fat.
// printf("FindNextFreeCluster:\n\r"); // if the end of the fat is not reached yet and no free cluster has been found
while((fat_sector_offset < SectorsPerFat) && (!free_cluster))
{
ul_tmp = (unsigned long) ((unsigned long)fat_pointer + (unsigned long)fat_sector_offset);
mmc_read_sector((unsigned long)ul_tmp,&FileBuffer[0]); // read next sector of FAT.
file->sector_in_buffer = ul_tmp; // remember the number of the sector in FileBuffer.
Fat = (struct FatEntry *)&FileBuffer[0];
for(fat_entry=0;fat_entry<256;fat_entry++) // look for an free cluster at all entries in this sector of the fat.
{
if(Fat[fat_entry].next_cluster == 0x0000) // empty cluster found!!
{
Fat[fat_entry].next_cluster = 0xffff; // mark this fat-entry as used and save it to the device.
// printf("sect: %lu\n\r", file->sector_in_buffer);
mmc_write_sector((unsigned long)file->sector_in_buffer,&FileBuffer[0]);
free_cluster = fat_entry; // the relative position of the free cluster found in this sector of the FAT.
free_cluster += (fat_sector_offset << 8); // calculate the absolute position of the free cluster in the FAT;
fat_entry = 256; // terminate the search for a free cluster in this sector.
}
}
fat_sector_offset++;
}
#endif
return(free_cluster);
}
//________________________________________________________________________________________________________________________________________
// Funtion: unsigned int GetFatClusterIndex(File *file);
//
// Description: This function returns the clusterindex of the cluster specified by file->cluster_pointer of the specified file.
//
//________________________________________________________________________________________________________________________________________
unsigned int GetFatClusterOffset(File *file)
{
unsigned long fat_sector_offset = 0;
fat_sector_offset = ((file->cluster_pointer) - (FirstDataCluster)); // Calculate index of actual cluster in FAT.
fat_sector_offset /= SectorsPerCluster;
fat_sector_offset += 2; // In Fat16 clusterpositions have an offset of two.
return((unsigned int)fat_sector_offset);
}
//________________________________________________________________________________________________________________________________________
// Funtion: unsigned int GetFatSectorIndex(File *file);
//
// Description: This function returns the sectorindex of the cluster specified by file->cluster_pointer of the specified file.
//
//________________________________________________________________________________________________________________________________________
unsigned int GetFatSectorIndex(File *file)
{
unsigned int fat_pointer = 0;
fat_pointer = GetFatClusterOffset(file);
fat_pointer = fat_pointer % 0x100; // Calculate the clusterposition in the fat
return(fat_pointer);
}
//________________________________________________________________________________________________________________________________________
// Funtion: unsigned int AppendCluster(File *file);
//
// Description: This function finds the next free datacluster on the disk and appends it to the specified file.
//
//________________________________________________________________________________________________________________________________________
/*
unsigned int AppendCluster(File *file)
{
unsigned int free_cluster = 0;
unsigned long fat_pointer = 0;
#if USE_MMC
free_cluster = FindNextFreeCluster(file); // the next free cluster found on the disk.
fat_pointer = FileAllocationTable; // Set Pointer to the beginnig of the FAT.
fat_pointer += (unsigned long)((unsigned long)GetFatClusterOffset(file) >> 8); // find the sector in the FAT with 256 entries per sector.
Fat = (struct FatEntry *)&FileBuffer[0];
Fat[GetFatSectorIndex(file)].next_cluster = free_cluster; // append the free cluster to the end of the file in the FAT.
mmc_write_sector((unsigned long)fat_pointer,&FileBuffer[0]); // save the modified sector to the FAT.
fat_pointer = (unsigned long)free_cluster;
fat_pointer -= 2;
fat_pointer *= SectorsPerCluster;
fat_pointer += FirstDataCluster;
file->cluster_pointer = fat_pointer; // continue wrtiting to the file in the new and free datacluster.
#endif
return(1);
}
*/
static unsigned short AppendCluster(File *file)
{
unsigned short free_cluster = 0;
unsigned int fat_pointer = 0;
#if USE_MMC
// the next free cluster found on the disk.
// This free cluster will be marked as FFFF signalling that it is now the end of the file
free_cluster = FindNextFreeCluster(file);
// Set Pointer to the beginnig of the FAT.
fat_pointer = FileAllocationTable;
// find the correct sector in the FAT with 256 entries per sector.
fat_pointer += GetFatClusterOffset(file) >> 8;
// Read the sector we are now \"jumping\" out off..
mmc_read_sector(fat_pointer,&FileBuffer[0]);
Fat = (struct FatEntry *)&FileBuffer[0];
// append the free cluster to the end of the file in the FAT.
Fat[GetFatSectorIndex(file)].next_cluster = free_cluster;
// save the modified sector to the FAT.
mmc_write_sector(fat_pointer,&FileBuffer[0]);
fat_pointer = (unsigned int)free_cluster;
fat_pointer -= 2;
fat_pointer *= SectorsPerCluster;
fat_pointer += FirstDataCluster;
// continue writing to the file in the new and free datacluster.
file->cluster_pointer = fat_pointer;
#endif
return(1);
}
//________________________________________________________________________________________________________________________________________
// Funtion: void FindEndOfFile(void)
//
// Description: This function looks for the end of the specified file to appand data.
//
//________________________________________________________________________________________________________________________________________
unsigned int FindEndOfFile(File *file)
{
unsigned long filesize;
unsigned long temp;
#if USE_MMC
filesize = file->filesize; // the size of the open file.
while(filesize)
{
filesize--; // decrement the number of available
if(file->byte_index < 511) // continue reading from this sector until the end of the sector is reached.
{
file->byte_index++;
}
else
{
file->byte_index=0; // reading at the beginning of new sector.
file->sector_index++; // continue reading in next sector
if(file->sector_index >= SectorsPerCluster) // When end of cluster is reached, the next datacluster has to be searched in the FAT.
{
if(filesize)
{
file->sector_index = 0; // start reading new cluster at first sector of the cluster.
GetNextCluster(file); // Sets the clusterpointer of the file to the next datacluster.
}
/*else
{
AppendCluster(file); // append a new and free cluster at the end of the file.
}*/
}
}
}
if(file->byte_index)
{ temp = (unsigned long)((unsigned long)file->cluster_pointer + (unsigned long)file->sector_index);
mmc_read_sector((unsigned long)temp,&FileBuffer[0]); // read FileBuffer
}
#endif
return(1);
}
//________________________________________________________________________________________________________________________________________
// Funtion: FilePutchar(File *file);
//
// Description: This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries.
//
//________________________________________________________________________________________________________________________________________
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -