📄 fat16avr2004.c
字号:
strncpy(str_ori, str_dest, 12);
if (str_ori[0] == '.') // if the file name is "." or ".."
{
d=1;
if (str_ori[1] == '.')
d=2;
for (;d<11; d++)
str_dest[d]=' '; // complete with spaces
str_dest[11]='\0';
return; // finish
}
for (d=0; d<8; d++) // for all the other file name
{
if (str_ori[o] == '.') // if we found the '.'
{
o++;
break; // break the loop
}
if (str_ori[o] == '\0') // if we find the file name end
break; // break the loop
str_dest[d]= str_ori[o++];
}
for (; d<8; d++) // complete the eight caracters file name with spaces
str_dest[d]= ' ';
if (str_ori[o]=='.') // eliminate the '.'
o++;
for (; d<11; d++) // copy the extension
{
if (str_ori[o] == '\0')
break;
str_dest[d]= str_ori[o++];
}
for (; d<11; d++) // complete the three caracters extension file name with spaces
str_dest[d]= ' ';
strupr(str_dest);
str_dest[11]='\0';
}
//*****************************************************************************
// Function: fatGetCurDirCluster
// Parameters: none
// Returns: The current dir cluster
//
// Description: Return the current directory cluster number
//*****************************************************************************
unsigned long fatGetCurDirCluster(void)
{
return currentDirCluster;
}
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatNextFreeDirEntry
// Parameters: start cluster
// Returns: On SUSCEFULL returns the next free dir entry found, NULL otherwise
//
// Description: Find a free dir entry in the directory started in cluster address
// If found a free dir entry, returns the address of this dir entry
// The SectorInCache variable will be filled with the sector address
// of the free dir entry found.
// If goes until the end of the clusters and don't found a free dir
// entry, returns NULL. Will be necessary alocate a new cluster
//*****************************************************************************
struct direntry *fatNextFreeDirEntry(unsigned long cluster)
{
unsigned int s,index=0;
unsigned long sector;
struct direntry *de;
do
{
sector = fatClustToSect(cluster);
for(s=0; s< SectorsPerCluster ; s++)
{
ataReadSectors(DRIVE0, sector+s, SectorBuffer, &SectorInCache);
de = (struct direntry *) SectorBuffer;
for (index=0; index<FAT_DIRENTRIES_PER_SECTOR; index++)
{
if((*de->deName == SLOT_DELETED) || (*de->deName == 0x00))
return(de);
de++;
}
}
cluster=fatNextCluster(cluster);
if (cluster == 0)
return NULL;
}while (cluster < NumClusters ); // end of FatDirentries
return NULL;
}
#endif
////////////////////
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatNextFreeCluster
// Parameters: start cluster
// Returns: On SUSCEFULL returns the next free cluster found, 0 otherwise
//
// Description: Returns the first free cluster found in Fat table.
// If goes until the end of the FAT table and don't found a free cluster.
// return 0.
//*****************************************************************************
unsigned long fatNextFreeCluster(unsigned long cluster)
{
unsigned int index;
unsigned long sector;
unsigned long *fat32Buffer;
unsigned int *fat16Buffer;
fat32Buffer= (unsigned long *)FatCache;
fat16Buffer= (unsigned int *)FatCache;
if (Fat32Enabled)
sector= (cluster>>7) + FirstFATSector;// 128 structures of 4 bytes each as a pointer to a cluster
else
sector= (cluster>>8) + FirstFATSector;// 256 structures of 2 bytes each as a pointer to a cluster
index=0;
do
{
ataReadSectors( DRIVE0, sector, FatCache, &FatInCache);
if (Fat32Enabled)
{
for (index=0; index < FAT32_STRUCTS_PER_SECTOR; index++) //Read all Cluster
{
if ((fat32Buffer[index]&FAT32_MASK) == 0x00000000)
return (index + ((sector - FirstFATSector)*FAT32_STRUCTS_PER_SECTOR));
}
}
else //FAT16
{
for (index=0; index < FAT16_STRUCTS_PER_SECTOR; index++) //Read all Cluster
{
if ((fat16Buffer[index]&FAT16_MASK) == 0x0000)
return (index + ((sector - FirstFATSector)*FAT16_STRUCTS_PER_SECTOR));
}
}
sector++;
}
while (sector < FirstFATSector+FatSectors); // end of FatDirentries
return 0;
}
#endif
////////////////////
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatWriteEOC
// Parameters: cluster
// Returns: none
//
// Description: Write in FAT in cluster position, the END OF CLUSTER mark
//*****************************************************************************
void fatWriteEOC(unsigned long cluster)
{
fatWrite(cluster, FAT32_MASK & CLUST_EOFE);
}
#endif
////////////////////
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatWrite
// Parameters: cluster, data to write
// Returns: none
//
// Description: Write the data in FAT in cluster position
//*****************************************************************************
void fatWrite(unsigned long cluster, unsigned long data)
{
unsigned int offset;
unsigned long *Fat32CacheLong, sector;
unsigned int *Fat16CacheInt;
// calculate offset of the our entry within that FAT sector
if(Fat32Enabled)
offset = cluster % FAT32_STRUCTS_PER_SECTOR;
else
offset = cluster % FAT16_STRUCTS_PER_SECTOR;
// read the FAT sector, with has information abou the cluster that we are interested in.
sector= fatTableClustToSect(cluster);
ataReadSectors( DRIVE0, sector, FatCache, &FatInCache);
// write the data to Fat Cache
if (Fat32Enabled)
{
Fat32CacheLong= (unsigned long *)FatCache;
Fat32CacheLong[offset]= (Fat32CacheLong[offset]&(~FAT32_MASK)) | (data&FAT32_MASK);
}
else
{
Fat16CacheInt= (unsigned int *)FatCache;
Fat16CacheInt[offset]= (Fat16CacheInt[offset]&(~FAT16_MASK)) | (data&FAT16_MASK);
}
// write Fat Cache in the HardDisk
ataWriteSectors( DRIVE0, sector, FatCache); // FAT1
ataWriteSectors( DRIVE0, sector+(FirstFAT2Sector-FirstFATSector), FatCache); // FAT2
}
#endif
////////////////////
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatTableClustToSect
// Parameters: cluster
// Returns: the FAT sector address with has cluster information
//
// Description: Calculate the FAT sector address with has cluster information
//*****************************************************************************
unsigned long fatTableClustToSect(unsigned long cluster)
{
unsigned long sector, fatOffset;
// get fat offset in bytes
if(Fat32Enabled)
fatOffset = cluster << 2;
else
fatOffset = cluster << 1;
// calculate the FAT sector that we're interested in
sector = FirstFATSector + (fatOffset / BYTES_PER_SECTOR);
return (sector);
}
#endif
////////////////////
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatLastCluster
// Parameters: cluster
// Returns: last cluster of the file started in cluster
//
// Description: Find the last cluster of a file started in "cluster" in fat table
//*****************************************************************************
unsigned long fatLastCluster(unsigned long cluster)
{
unsigned long lastCluster;
do
{
lastCluster=cluster;
cluster = fatNextCluster(lastCluster);
}
while (cluster != 0);
return lastCluster;
}
#endif
////////////////////
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatDirectoryIsEmpty
// Parameters: Start Directory Cluster
// Returns: TRUE if the directory is empty, FALSE otherwise
//
// Description: check to the existance of files in the directory started in DirCluster
//*****************************************************************************
unsigned char fatDirectoryIsEmpty(unsigned long DirCluster)
{
struct direntry *de;
unsigned long sector;
do
{
sector=fatClustToSect(DirCluster); //r ead directory clusters
for (unsigned long isector=0; isector < SectorsPerCluster ;isector++) // read one cluster
{
ataReadSectors( DRIVE0, sector + isector, SectorBuffer, &SectorInCache);
de= (struct direntry *)SectorBuffer;
for (int index=0; index<FAT_DIRENTRIES_PER_SECTOR; index++) // read one sector
{
if (*de->deName != '.' )
{
if ( *de->deName == SLOT_EMPTY )
return TRUE;
if ( *de->deName != SLOT_DELETED)
return FALSE;
}
// If goes until here it's because de->Name is equal '.' or is equal SLOT_DELETED
de++;
}
}
DirCluster=fatNextCluster(DirCluster);
}while ( DirCluster != 0 );
// if goes until here it's because the all file (directory) is filled with SLOT_DELETED
return TRUE;
}
#endif
////////////////////
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatRemoveAll
// Parameters: none
// Returns: none
//
// Description: Remove all files in currentDirCluster
//*****************************************************************************
void fatRemoveAll(void)
{
unsigned long offset=0;
struct direntry *de = 0; // avoid compiler warning by initializing
int index;
for (offset=0; ; offset++)
{
de = (struct direntry *) fatDir(currentDirCluster, offset);
if (de == NULL)
return;
for (index=0; index<FAT_DIRENTRIES_PER_SECTOR; index++)
{
if (*de->deName == SLOT_EMPTY)
return; // there is no more direntries
if (( de->deAttributes & ATTR_VOLUME ) != ATTR_VOLUME)
if ((*de->deName != SLOT_DELETED) && (*de->deName != '.'))
fatRemove(de->deName);
de++;
} // end of sector
} // end of cluster
}
#endif
////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -