📄 fat16mega128.c
字号:
if( NameBufferExt[u] == 0x00 ) NameBufferExt[u] = 0x20;
}
strupr(NameBufferShort); // convert string to upper case
strupr(NameBufferExt); // convert string to upper case
// if name and extension match with input file get file information from buffer
if ( (!strncmp( NameBufferShort, F16.direntries[i].deName, 8 ) ) &&
(!strncmp( NameBufferExt, F16.direntries[i].deExtension, 3 ) )
)
{
*FAT_Entry = F16.direntries[i].deStartCluster;
*FileSize = F16.direntries[i].deFileSize;
*Attributes = F16.direntries[i].deAttributes;
*DataSector = DataRegionStart + ( ( *FAT_Entry - 2 ) * SectorsPerCluster ) ;
*SubSector = i;
*Sector += q;
#ifdef DEBUG_FAT
PrintDOSLine( NameBuffer,
F16.direntries[i].deCDate,
F16.direntries[i].deCTime,
F16.direntries[i].deFileSize,
F16.direntries[i].deAttributes);
#endif
return (TRUE);
}
break;
}
default:
break;
}
}
i++;
}while( (F16.direntries[i].deAttributes != ATTR_NORMAL) && (i != 16) ); // 0 in de->deName[0] if no more entries
q++;
}while( q < NoSecorToSerach );
// save actual sector address
*Sector += q;
// if no file found start next xearch from fat entry 1 (*FAT_Entry == 0 would again read all root dir entries)
*FAT_Entry = 1;
// file or dir not found
return (FALSE);
}
//############################################################################
void fatLoad ( uint16_t *FATEntry, // In: Angabe Start cluster , Rückgabe des n?chsten FAT- Eintrags auf dem das aktuelle FAT-Eintrag zeigt
uint32_t *Adr_FirstSectorOfCluster,// Return: Rückgabe Adresse des ersten Sectors des gew?hlten neuen Clusters
uint8_t *Buffer) // In: Buffer to use
//############################################################################
{
uint16_t FATSectorAddress; // FAT Sector Adresse
// calculate sector that has to be read
FATSectorAddress = ((*FATEntry*2) / BytePerSector) + FATRegionStart; // ((8*2)/512) + 6 = 6
// if sought sector is a new sector then load this sector
if (FATSectorAddress != FATSectorAddress_old){
FATSectorAddress_old = FATSectorAddress;
mmcRead( FATSectorAddress , Buffer );
}
// first sector address of actual cluster
*Adr_FirstSectorOfCluster = DataRegionStart + ( ( *FATEntry - 2 ) * SectorsPerCluster ) ;
// next FAT number of of this file
*FATEntry = FAT_Buf2[*FATEntry];
}
//############################################################################
void fatRead_File ( uint16_t *FATEntry, // In:
uint32_t *Sector_Address, // In:
uint32_t *FileSize, // In:
uint8_t *Buffer) // In: Buffer to use
//############################################################################
{
*Sector_Address = 0;
uint8_t c;
uint16_t NextCluster_save = *FATEntry;
if( *FileSize != 0)
{
#ifdef DEBUG_FAT
rprintfProgStrM("\r\n----- Read file content-----\r\n");
#endif
while(*FATEntry != 0xFFFF)
{
#ifdef DEBUG_FAT
rprintfProgStrM("Current cluster: "); rprintfNum(10,6, FALSE,' ', *FATEntry );
rprintfProgStrM(" Current sector: ");rprintfNum(10,10, FALSE,' ', *Sector_Address ); rprintfCRLF();
#endif
fatLoad( FATEntry , Sector_Address, F16_BUFFER_TWO ); // F16_BUFFER_TWO
#ifdef DEBUG_FAT
rprintfProgStrM("Next cluster : ");rprintfNum(10,6, FALSE,' ', *FATEntry );
rprintfProgStrM(" Next sector : ");rprintfNum(10,10, FALSE,' ', *Sector_Address );rprintfCRLF();
#endif
c=0;
while(c < SectorsPerCluster)
{
mmcRead( *Sector_Address + c , Buffer);
#ifdef DEBUG_FAT
// print buffer contents
rprintfCRLF();
rprintfProgStrM("Sector : "); rprintfNum( 10,6, FALSE,' ', *Sector_Address+c ); rprintfCRLF();
rprintfProgStrM("Next FAT : "); rprintfNum( 10,6, FALSE,' ', *FATEntry ); rprintfCRLF();
debugPrintHexTable(0x200, Buffer);
#endif
c++;
}
}
}
#ifdef DEBUG_FAT
else{
rprintfProgStrM("\r\n----- File is empty -----\r\n");
}
#endif
*FATEntry = NextCluster_save; // restore cluster number
}
//############################################################################
void FAT16_Write_Data ( uint32_t Data ) // In: Data to be stored on MMC
//############################################################################
{
fatWrite_Data ( &Data,
&glbl_FAT_Entry,
&glbl_DataSector,
&glbl_FileSize,
F16_BUFFER_ONE );
}
//############################################################################
void fatWrite_Data ( uint32_t *data, // In:
uint16_t *FATEntry, // In/Ret:
uint32_t *DataSector, // In/Ret:
uint32_t *FileSize, // In/Ret:
uint8_t *Buffer) // In: Buffer to use
//############################################################################
{
F16.FATData[ (*FileSize/4) % 128 ] = *data; // write data in buffer
(*FileSize) += 4; // file size counter (+4 byte because 32 bit data )
if( ((*FileSize/4) % 128) == 0 ) // SectorsPerCluster = 4
{
#ifdef DEBUG_FAT
rprintfProgStrM("--FileSize : "); rprintfNum( 10,6, FALSE,' ', *FileSize ); rprintfCRLF();
rprintfProgStrM("--DataSector : "); rprintfNum( 10,6, FALSE,' ', *DataSector ); rprintfCRLF();
#endif
mmcWrite( (*DataSector)++ , Buffer );
if( *DataSector % SectorsPerCluster == 0 )
{
#ifdef DEBUG_FAT
rprintfProgStrM("Actual Cluster : "); rprintfNum(10,10, FALSE,' ', *FATEntry ); rprintfCRLF();
rprintfProgStrM("Data Sector : "); rprintfNum(10,10, FALSE,' ', *DataSector ); rprintfCRLF();
#endif
fatWrite( FATEntry, DataSector, F16_BUFFER_TWO );
#ifdef DEBUG_FAT
rprintfProgStrM("Next Cluster : "); rprintfNum(10,10, FALSE,' ', *FATEntry ); rprintfCRLF();
rprintfProgStrM("Sector Address : "); rprintfNum(10,10, FALSE,' ', *DataSector ); rprintfCRLF();
#endif
}
}
}
//############################################################################
void FAT16_Finish_Data( void ) //
//############################################################################
{
// save last sector of current file
mmcWrite( glbl_DataSector, F16_BUFFER_ONE );
// save file size in file information sector
mmcRead( glbl_Sector , F16_BUFFER_ONE );
F16.direntries[glbl_SubSector].deFileSize = glbl_FileSize;
// TODO Datum update
//F16.direntries[glbl_SubSector].deMTime = xxx; // last update time
//F16.direntries[glbl_SubSector].deMDate = yyy; // last update date
mmcWrite( glbl_Sector , F16_BUFFER_ONE );
// reload last sector of current file
mmcRead( glbl_DataSector , F16_BUFFER_ONE );
#ifdef DEBUG_FAT
rprintfProgStrM("glbl_DataSector : "); rprintfNum( 10,6, FALSE,' ', glbl_DataSector ); rprintfCRLF();
rprintfProgStrM("glbl_Sector : "); rprintfNum( 10,6, FALSE,' ', glbl_Sector ); rprintfCRLF();
rprintfProgStrM("glbl_FileSize : "); rprintfNum( 10,6, FALSE,' ', glbl_FileSize ); rprintfCRLF();
rprintfProgStrM("\r\n----- File finished -----\r\n\r\n");
#endif
}
//############################################################################
void fatWrite( uint16_t *FATEntry, // In/Ret: Angabe Start cluster , Rückgabe des n?chsten FAT- Eintrags auf dem das aktuelle FAT-Eintrag zeigt
uint32_t *Adr_FirstSectorOfCluster,// Return: Rückgabe Addresse des ersten Sectors des gew?hlten Clusters
uint8_t *Buffer) // In: Buffer to use
//############################################################################
{
uint16_t FATSectorAddress; // FAT Sector Addresse
uint16_t FATEntry_start = *FATEntry;
uint16_t FATSectorAddress_start;
// Berechnung des Sectors der gelesen werden mu?
FATSectorAddress = ((*FATEntry*2) / BytePerSector) + FATRegionStart; // ((8*2)/512) + 6 = 6 //((800*2)/512) + 6 = 9
FATSectorAddress_start = FATSectorAddress;
// Wenn gesuchter FAT-Eintag im neuer FAT-Sector Bereich dann neuen Sector laden
if (FATSectorAddress != FATSectorAddress_old)
{
FATSectorAddress_old = FATSectorAddress;
mmcRead( FATSectorAddress , Buffer );
}
// Find next empty cluster
while( FAT_Buf2[(*FATEntry) % 256 ] != 0x0000 )
{
(*FATEntry)++;
if( *FATEntry % 256 == 0 ){
mmcRead( FATSectorAddress++ , Buffer );
// TODO return "no space" if end of fat table reached
}
}
// If empty FAT-Entry found, mark this field as end of document(0xFFFF)
FAT_Buf2[(*FATEntry) % 256] = 0xFFFF;
// If file is empty, do not overwrite the previous fat address
// change instead the fat address in the Directory Entry Structure (32byte long) -> Starting cluster
if(glbl_FileSize != 0x0000){
// If no new Fat-Sector has been loaded during search for empty cluster (0x0000)...
if (FATSectorAddress == FATSectorAddress_old)
{
#ifdef DEBUG_FAT
rprintfProgStrM("FAT Table Entry : "); rprintfNum(10,10, FALSE,' ', *FATEntry ); rprintfCRLF();
#endif
// ...overwrite the old cluster address with the address of the new cluster
FAT_Buf2[FATEntry_start % 256] = *FATEntry;
}
// Save the entry on data medium
mmcWrite( FATSectorAddress , Buffer );
//
if(FATSectorAddress != FATSectorAddress_old)
{
FATSectorAddress_old = FATSectorAddress;
// If we use a cluster that isn't the cluster in which we started to search for an empty place
// then read the startig sector, to actualize the postion of the 0xFFFF in the old FAT-entry.
mmcRead( FATSectorAddress_start , Buffer );
// Overwrite the old cluster address with the address of the new cluster
FAT_Buf2[FATEntry_start % 256] = *FATEntry;
// Save the entry on data medium
mmcWrite( FATSectorAddress_start , Buffer );
}
}
else{
// Save the entry on data medium
mmcWrite( FATSectorAddress , Buffer );
}
// Return first sector-address of new cluster
*Adr_FirstSectorOfCluster = DataRegionStart + ( ( *FATEntry - 2 ) * SectorsPerCluster ) ;
}
#ifdef DEBUG_FAT
//############################################################################
// Creates DOS like output: 08.07.2005 15:53 <DIR> My New Directory
// Creates DOS like output: 08.07.2005 15:54 541 Textdokument.txt
void PrintDOSLine ( char *DirNameAddress, // In:
uint16_t CreateDate, // In:
uint16_t CreateTime, // In:
uint32_t File_Size, // In:
uint8_t Dir_Or_Data) // In: Buffer to use
//############################################################################
{
rprintfNum(10,2, FALSE,'0',( CreateDate & DD_DAY_MASK ) >> DD_DAY_SHIFT ); // Date
rprintfProgStrM(".");
rprintfNum(10,2, FALSE,'0',( CreateDate & DD_MONTH_MASK ) >> DD_MONTH_SHIFT ); // Month
rprintfNum(10,5, FALSE,'.',( (CreateDate & DD_YEAR_MASK) >> DD_YEAR_SHIFT ) + 1980); // Year
rprintfProgStrM(" ");
rprintfNum(10,2, FALSE,'0',( CreateTime & DT_HOURS_MASK ) >> DT_HOURS_SHIFT ); // Hours
rprintfProgStrM(":");
rprintfNum(10,2, FALSE,'0',( CreateTime & DT_MINUTES_MASK ) >> DT_MINUTES_SHIFT ); // Min
if(Dir_Or_Data == ATTR_DIRECTORY) {
rprintfProgStrM(" <DIR> ");
}
if(Dir_Or_Data == ATTR_ARCHIVE){
rprintfProgStrM(" ");
rprintfNum(10,10, FALSE,' ', File_Size );
rprintfProgStrM(" ");
}
rprintfStrLen(DirNameAddress, 0, 32 ); // Dir or file name
rprintfCRLF(); // New line
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -