📄 fat16avr2004.c
字号:
strcpy(string, newShortName);
string[11]='\0';
fatNormalize(string);
memcpy(de->deName, string, 11);
ataWriteSectors( DRIVE0, SectorInCache, SectorBuffer);
return TRUE;
}
#endif
////////////////////
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatRemove
// Parameters: File or Directory short name
// Returns: TRUE if the file was removed, otherwise FALSE
//
// Description: remove a file or directory
//*****************************************************************************
unsigned char fatRemove(char *shortName)
{
struct direntry *de, rde;
unsigned long CurrentDirCluster, NextCluster, fileSector;
de=fatGetFileInfo(&rde, shortName); // get file information
if (de == NULL) // if the file doesn't exist
return FALSE; // return ERROR
fileSector=SectorInCache; // save the File sector address
// calculate the cluster address of this file
CurrentDirCluster = ((unsigned long)de->deHighClust << 16) + de->deStartCluster;
// if the file is a directory
if ( de->deAttributes == ATTR_DIRECTORY )
{
if (!fatDirectoryIsEmpty(CurrentDirCluster)) // if there are files in the directory
return FALSE; // return ERROR
}
// Read the direntry information about the file
ataReadSectors( DRIVE0, fileSector, SectorBuffer, &SectorInCache);
// mark the file name as deleted
*de->deName = SLOT_DELETED;
// write the direntry back to the ATA dispositive
ataWriteSectors( DRIVE0, SectorInCache, SectorBuffer);
// erase fat table
do{
NextCluster=fatNextCluster(CurrentDirCluster);
fatWrite(CurrentDirCluster,CLUST_FREE); // free the current dir cluster
CurrentDirCluster=NextCluster;
}while( NextCluster != 0 );
//file erased
return TRUE;
}
#endif
////////////////////
//*****************************************************************************
// Function: fatFopen
// Parameters: File or Directory short name
// Returns: A TFILE struct filled with the opened file information, or NULL
// if an error has ocurred
//
// Description: Open a file to read and write. The File struct is filled and
// the Sector Buffer in memory is filled with the first sector
// of the file opened
//*****************************************************************************
TFILE* fatFopen(char *shortName)
{
// if the file don't exist
if (fatGetFileInfo(&File.de, shortName) == NULL)
return NULL;
File.currentSector = fatClustToSect(((unsigned long)File.de.deHighClust << 16) + File.de.deStartCluster);
File.buffer=SectorBuffer;
File.bytePointer=0;
File.sectorHasChanged=FALSE;
ataReadSectors( DRIVE0, File.currentSector, File.buffer, &SectorInCache); // read the first file sector
return (&File);
}
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatFclose
// Parameters: TFILE struct of the file to be closed
// Returns: TRUE if the file was corrected closed, and FALSE otherwise
//
// Description: write current file to the FAT file system
// refresh the file size
//*****************************************************************************
unsigned char fatFclose(TFILE *fp)
{
struct direntry *de, rde;
unsigned long fileSize;
unsigned int date, time;
TTime t;
fileSize= fp->de.deFileSize;
fp->bytePointer=0;
if (fatFflush(fp))
{
if ((de=fatGetFileInfo(&rde, fp->de.deName)) == NULL) // if the file don't exist
return FALSE;
de->deFileSize= fileSize; // refresh the file size
fatGetCurTime(&t);
time= (unsigned int)((t.hour)<<DT_HOURS_SHIFT) + ((t.minutes)<<DT_MINUTES_SHIFT) + ((t.seconds)>>DT_2SECONDS_SHIFT); // create time
date= (unsigned int)((t.year-1980)<<DD_YEAR_SHIFT) + (t.month<<DD_MONTH_SHIFT) + t.day; // create date
de->deMTime[0]=(unsigned char)time&0x00FF; // last update time
de->deMTime[1]=(unsigned char)((time&0xFF00)>>8); // last update time
de->deMDate[0]=(unsigned char)date&0x00FF; // last update date
de->deMDate[1]=(unsigned char)((date&0xFF00)>>8); // last update date
de->deADate[0]=de->deMDate[0]; // access date
de->deADate[1]=de->deMDate[1]; // access date
ataWriteSectors( DRIVE0, SectorInCache, SectorBuffer);
ataReadSectors( DRIVE0, FSInfo, SectorBuffer, &SectorInCache); // Read FSInfo
((struct fsinfo *)SectorBuffer)->fsinfree[0]=0xFF; // change to Operation System don't to try to correct this field and the FAT
((struct fsinfo *)SectorBuffer)->fsinfree[1]=0xFF;
((struct fsinfo *)SectorBuffer)->fsinfree[2]=0xFF;
((struct fsinfo *)SectorBuffer)->fsinfree[3]=0xFF;
((struct fsinfo *)SectorBuffer)->fsinxtfree[0]=0xFF;
((struct fsinfo *)SectorBuffer)->fsinxtfree[1]=0xFF;
((struct fsinfo *)SectorBuffer)->fsinxtfree[2]=0xFF;
((struct fsinfo *)SectorBuffer)->fsinxtfree[3]=0xFF;
ataWriteSectors( DRIVE0, SectorInCache, SectorBuffer);
return(TRUE);
}
return (FALSE);
}
#endif
////////////////////
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatFflush
// Parameters: TFILE struct of the file to be flushed
// Returns: TRUE if the file was corrected flushed, and FALSE otherwise
//
// Description: write current sector file to hard disk if necessary
//*****************************************************************************
unsigned char fatFflush(TFILE *fp)
{
if (fp->sectorHasChanged)
{
ataWriteSectors( DRIVE0, fp->currentSector, fp->buffer);
fp->sectorHasChanged=FALSE;
}
return TRUE;
}
#endif
////////////////////
//*****************************************************************************
// Function: fatFseek
// Parameters: TFILE struct of the file opened, offSet, seek mode
// Returns: On SUSCEFULL returns TRUE, and FALSE otherwise
//
// Description: find a byte position in the file and load the corresponded sector in buffer
// Modes: SEEK_CUR: from the current position of the file pointer;
// SEEK_SET: from the beggining of the file
// SEEK_END: from the end of file to back.
//*****************************************************************************
unsigned int fatFseek(TFILE *fp, unsigned long offSet, unsigned char mode)
{
unsigned long numClusters, curCluster, numSector;
// calculate the new byte pointer
switch (mode)
{
case SEEK_END: fp->bytePointer= fp->de.deFileSize - offSet; break;
case SEEK_SET: fp->bytePointer= offSet; break;
case SEEK_CUR: fp->bytePointer= fp->bytePointer + offSet; break;
default: return FALSE;
}
// calculate the current file cluster
curCluster=((unsigned long)fp->de.deHighClust<<16)+(fp->de.deStartCluster);
numClusters=fp->bytePointer/(BYTES_PER_SECTOR*SectorsPerCluster);
numSector=(fp->bytePointer/BYTES_PER_SECTOR)%SectorsPerCluster;
// calculate the cluster address of the new byte pointer
while (numClusters > 0)
{
curCluster=fatNextCluster(curCluster);
numClusters--;
}
// calculate the Sector address of the new byte pointer
fp->currentSector=fatClustToSect(curCluster) + numSector;
// read that sector
ataReadSectors( DRIVE0, fp->currentSector, fp->buffer, &SectorInCache);
return TRUE;
}
//*****************************************************************************
// Function: fatFgetc
// Parameters: TFILE struct of the file opened
// Returns: On SUSCEFULL returns the next character from file, and 0 otherwise
//
// Description: Get the next character from file, and actualize the byte pointer
//*****************************************************************************
char fatFgetc(TFILE *fp)
{
unsigned int bufferPointer;
if (fatFeof(fp)) // if is the end of file
return 0;
bufferPointer= fp->bytePointer & 0x001FF; // equal (fp->bytePointer % 512)
fp->bytePointer++;
if ((fp->bytePointer == 1) || (bufferPointer != 0))
{
return(fp->buffer[bufferPointer]);
}
else
{
// Next Sector is in current Cluster
if (fatSectToClust(fp->currentSector) == fatSectToClust(fp->currentSector+1))
{
fp->currentSector++;
}
else // Next Sector is in next Cluster
{
unsigned long cluster;
cluster= fatNextCluster(fatSectToClust(fp->currentSector));
fp->currentSector= fatClustToSect(cluster);
}
ataReadSectors( DRIVE0, fp->currentSector, fp->buffer, &SectorInCache);
return(fp->buffer[bufferPointer]);
}
}
////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatFputc
// Parameters: TFILE struct of the file opened, character to be writed
// Returns: On SUSCEFULL returns TRUE, and FALSE otherwise
//
// Description: Write a character to the file. Return false if the disk is full
//*****************************************************************************
unsigned char fatFputc(TFILE *fp, char c)
{
unsigned int bufferPointer;
unsigned long freeCluster;
bufferPointer= fp->bytePointer & 0x001FF; // equal (fp->bytePointer % 512)
fp->bytePointer++;
if ((fp->bytePointer != 1) && (bufferPointer == 0)) // if we need to allocate a new sector to the file
{
ataWriteSectors(DRIVE0, fp->currentSector, fp->buffer);
if(fp->de.deFileSize>fp->bytePointer) // sector already exist
{
// Next Sector is in current Cluster
if (fatSectToClust(fp->currentSector) == fatSectToClust(fp->currentSector+1))
{
fp->currentSector++;
}
else // Next Sector is in next Cluster
{
unsigned long cluster;
cluster= fatNextCluster(fatSectToClust(fp->currentSector));
fp->currentSector= fatClustToSect(cluster);
}
ataReadSectors( DRIVE0, fp->currentSector, fp->buffer, &SectorInCache);
}
else // sector isn′t used or the cluster isn′t allocated
{
if (fatSectToClust(fp->currentSector) == fatSectToClust(fp->currentSector+1))//the next sector is in current cluster
{
fp->currentSector++;
}
else
{
freeCluster=fatNextFreeCluster(0);
if (freeCluster == 0) // if disk is full
return (FALSE);
fatWrite(fatSectToClust(fp->currentSector), freeCluster);
fatWriteEOC(freeCluster);
fp->currentSector=fatClustToSect(freeCluster);
}
}
}
fp->buffer[bufferPointer]=c;
fp->sectorHasChanged=TRUE;
if(fp->bytePointer>fp->de.deFileSize)
fp->de.deFileSize=fp->bytePointer;
return (TRUE);
}
#endif
////////////////////
//*****************************************************************************
// Function: fatFeof
// Parameters: TFILE struct of the file opened
// Returns: On END OF FILE returns TRUE, and FALSE otherwise
//
// Description: return TRUE if the byte pointer points to the end of the file
//*****************************************************************************
unsigned char fatFeof(TFILE *fp)
{
if ((fp->bytePointer) >= (fp->de.deFileSize))
return TRUE;
return FALSE;
}
//*****************************************************************************
// Function: fatNormalize
// Parameters: file name to be normalized
// Returns: the normalized file name
//
// Description: normalize the file name. Insert spaces betwen the name and the extension.
// The file name will be 11 caracters
//*****************************************************************************
void fatNormalize(char *str_dest)
{
unsigned char o=0, d=0;
char str_ori[12];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -