⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat16avr.c

📁 移植到mega32上面的fat16代码实现了
💻 C
📖 第 1 页 / 共 3 页
字号:
			{
				mmcWrite( FATSectorAddress_old , Buffer );
				FATSectorAddress_old = FATSectorAddress;
				mmcRead( FATSectorAddress , Buffer );
			}
		}
		while(*FATEntry != 0xFFFF);

		*FileSize = 0;
		*FATEntry = 0;

		// remember also to store FileSize in file information sector !!!!!
		fat_Init_File 		(	FATEntry,		// In/Ret:  FAT entry where data starts
								FileSize,    	// In: 		Filesize
								Sector,			// In: 		Sector which contains file size inforation
								SubSector,		// In: 		Return which of the 16 32byte long SubSectors contains the file size
								DataSector,		// Return:	Sector where data starts
								Buffer);		// In:		Select buffer to use
	}
}


//############################################################################
void fat_Init_File 		(	uint16_t *FATEntry,		// In/Ret:  FAT entry where data starts
							uint32_t *FileSize,    	// In: 		Filesize
							uint32_t *Sector,			// In: 		Sector which contains file size inforation
							uint8_t  *SubSector,		// In: 		Return which of the 16 32byte long SubSectors contains the file size
							uint32_t *DataSector,		// Return:	Sector where data starts
							uint8_t  *Buffer)			// In:		Select buffer to use  // not used
//############################################################################
{
	// if file is empty and if fat-entry is zero then search empty space in the FAT where data can start
	if( (*FileSize == 0) && (*FATEntry == 0)  )
	{
		// search next empty FAT-Entry(from zero on) where data can start
		fatWrite( FATEntry, DataSector , F16_BUFFER_TWO );
		// load file information sector in buffer
		mmcRead( *Sector , F16_BUFFER_ONE );
		// Save FAT-address where data of the current file starts into the file-name SubSector
		F16.direntries[*SubSector].deStartCluster = *FATEntry;
		// Save file size
		F16.direntries[*SubSector].deFileSize = 0;
		//  TODO Datum
		//F16.direntries[*SubSector].deMTime = xxx;   	// update time
		//F16.direntries[*SubSector].deMDate = yyy;		// update date
		// save file information
		mmcWrite( *Sector , F16_BUFFER_ONE );

		#ifdef DEBUG_FAT
			rprintfCRLF();
			rprintfProgStrM("First FAT allocated for new file : ");	rprintfNum( 10,6, FALSE,' ', *FATEntry );
			rprintfCRLF();
		#endif
	}
}


//####################################################################################
int8_t fatSearch_File (    char 	   	*FilePathName,		// In: 		File name to find
							uint16_t  	*FAT_Entry, 		// In/Ret:  FAT entry where data starts
							uint32_t  	*FileSize,    		// Return: 	Return filesize
							uint32_t  	*Sector,			// Return: 	Sector which contains file size inforation
							uint8_t   	*SubSector,   		// Return: 	Return which of the 16 32byte long SubSectors contains the file size
							uint32_t  	*DataSector,  		// Return:  Return sector where data starts
							uint8_t   	*Attributes,  		// Return: 	Return Attribute Dir, archive, hidden, system, volume ...
							uint8_t   	*Buffer)			// In: 		Buffer to use
//####################################################################################
{
	int8_t ret = FALSE;
	uint8_t  u = 0;
	uint8_t  a = 0;
	uint16_t i = 0;
	uint16_t k = 0;

	do
	{
		// fill buffer with 0x00
		memset( Name2FindBuffer, 0x00 , k );

		k=0;
		// search directory name in parameter "FilePathName" (i.e. My Directory/ )   0x2E = "."   0x2F = "/"
		while( ( FilePathName[i] != 0x2F ) && ( FilePathName[i] != 0x2E ) &&  ( i < NAMEBUFFERSIZE ) )
		{
			Name2FindBuffer[k++] = FilePathName[i++];
		}

		if(FilePathName[i] == 0x2E) // 0x2E = "."
		{
			// replace in string " "(0x20) by 0
			for(u=0; u<NAMEBUFFERSIZE;  u++)
			{
				if(Name2FindBuffer[u] == 0x20) Name2FindBuffer[u] = 0;
			}
			memcpy( NameBufferShort,  Name2FindBuffer, 8 );
			Name2FindBuffer[k++]  	= FilePathName[i++];  		// .
			Name2FindBuffer[k] 		= FilePathName[i++];		// t    i.e .txt
			NameBufferExt[0]		= Name2FindBuffer[k++];
			Name2FindBuffer[k]  	= FilePathName[i++];		// x
			NameBufferExt[1]		= Name2FindBuffer[k++];
			Name2FindBuffer[k]  	= FilePathName[i];			// t
			NameBufferExt[2]		= Name2FindBuffer[k];
		}

		if(i < NAMEBUFFERSIZE)
		{
			// search 100 fat tables
			do
			{
				ret = fatGetDirEntry( 	Name2FindBuffer,// In: 		String with name to find in sector
										FAT_Entry,  	// In/Ret:  FAT entry where data starts
										FileSize, 		// Return: 	Return filesize
										Sector, 		// Return: 	Sector which contains file size inforation
										SubSector, 		// Return: 	Return which of the 16 32byte long SubSectors contains the file size
										DataSector,  	// Return:  Return sector where data starts
										Attributes, 	// Return: 	Return Attribute Dir, archive, hidden, system, volume ...
										Buffer );		// In: 		Buffer to use
			}
			while( (a++<100)  &&  (ret != TRUE) );

			if(ret == FALSE) return( FALSE );

			// skip "/" in FilePathName
			i++;
		}
	}
	// search whole buffer for dir names
	while( i < NAMEBUFFERSIZE );


	return( ret ); // No file or dir found
}


//############################################################################
// Search a cluster for file name
// If "root dir entry" is selected (*FAT_Entry = READROOTDIR = 0) search all root dir entry secors. The length
// of this area is stored in RootDirectoryRegionSize
int8_t fatGetDirEntry (	char     *Name2Find,	// In: 		String with name to find in sector
							uint16_t *FAT_Entry,	// In/Ret:  FAT entry where data starts
							uint32_t *FileSize,    // Return: 	Return filesize
							uint32_t *Sector,		// Return: 	Sector which contains file size inforation
							uint8_t  *SubSector,   // Return: 	Return which of the 16 32byte long SubSectors contains the file size
							uint32_t *DataSector,  // Return:  Return sector where data starts
							uint8_t  *Attributes,  // Return: 	Return Attribute Dir, archive, hidden, system, volume ...
							uint8_t  *Buffer)		// In: 		Buffer to use
//############################################################################
{
	int16_t i = 0;
	int16_t NoSecorToSerach;
	uint8_t nr_LFN_entries;
	uint8_t order = 0;
	uint8_t k = 0;
	uint8_t p = 0;
	uint8_t m = 0;
	uint8_t q = 0;
	uint8_t u = 0;


	// if READROOTDIR then search root directory region ( 496... ) // READROOTDIR = 0
	// *FAT_Entry == 0 is treated as root directory region start address
	if(*FAT_Entry == READROOTDIR){
		// RootDirectoryRegionStart --> RootDirectoryRegionSize [sectors]
		*Sector = RootDirectoryRegionStart;
		NoSecorToSerach = RootDirectoryRegionSize; 	// search RootDirectoryRegionSize entries (32)
	//	#ifdef DEBUG_FAT
	//		rprintfProgStrM("----- Searching in root dir entry -----");	rprintfCRLF();
	//	#endif
	}
	else{
		fatLoad( FAT_Entry, Sector, F16_BUFFER_TWO );
		NoSecorToSerach = SectorsPerCluster;     	// search SectorsPerCluster entries (4)
	}

	do
	{
		// search all sectors in selected cluster
		// Read data from cluster
		mmcRead( *Sector + q, Buffer );

		i=0;m=0;p=0;
		do
		{
			// fill buffer with 0x00
			memset( NameBuffer, 0x00 , NAMEBUFFERSIZE );

			// If not a deleted entry (0xE5)
			if( F16.direntries[i].deName[0] != SLOT_DELETED )
			{
				switch( F16.direntries[i].deAttributes )
				{
					// If long filename entry found (0x0F) ----------------------------------------------------------------------------
					case ATTR_LONG_FILENAME: 							// deleted if 7th bit is set in .weCnt?
					{
						// We have a long name entry
						// Max (16) 20(10100b) "subSectors" in a Sector(512 byte).
						// Each Subsector contains max 13 UTF-16 characters in 26 bytes.
						nr_LFN_entries = (uint8_t)( F16.longdirentries[i].weCnt & 0x1F ) ;

						for( m=0; m < nr_LFN_entries; m++ ){
							if((i+m) == 16){				// if LFN goes over the length of a sector, load next sector
								// if searching in the root dir entry, no information in fat table is available
								// the root dir entry lays in a specified area (see Boot Sector ~> BIOS Parameter Block)
								if( (q == SectorsPerCluster-1 ) && (*FAT_Entry != READROOTDIR) ){
									// if q=3 (4 sectors per cluster) then load next fat and...
									fatLoad( FAT_Entry , Sector, F16_BUFFER_TWO );
									//... reset sector counter to zero
									q=0;
								}
								// load new secor
								mmcRead( ++q + *Sector , Buffer);
								i-=16;						// start to read the next sector from zero position: (i+m)=0
							}
							p=0;
							order = (nr_LFN_entries - m - 1) * 13;	// load SubSector-data(each one 32Byte) in correct order in NameBuffer;
							for(k=0;k<5;k++)	NameBuffer[order + p++] = F16.longdirentries[i+m].wePart1[k*2]; 	// copy first part
							for(k=0;k<6;k++)	NameBuffer[order + p++] = F16.longdirentries[i+m].wePart2[k*2]; 	// second part
							for(k=0;k<2;k++)	NameBuffer[order + p++] = F16.longdirentries[i+m].wePart3[k*2]; 	// and third part
						}
						if((i+m) == 16){	// if LFN goes over the length of a sector, load next sector
							// if searching in the root dir entry, no information in fat table is available
							// the root dir entry lays in a specified area (see Boot Sector ~> BIOS Parameter Block)
							if( (q == SectorsPerCluster-1) && (*FAT_Entry != READROOTDIR) ){
								// if q=3 (4 sectors per cluster) then load next fat and...
								fatLoad( FAT_Entry , Sector, F16_BUFFER_TWO );
								//... reset sector counter to zero
								q=0;
							}
							// load new secor
							mmcRead( ++q + *Sector , Buffer);
							i-=16;				// start to read the next sector from zero position: (i+m)=0
						}

						// replace empty data(0xFF) in file name by zeros
						// this is a necessary requirement to be able to use the string compare function
						for(u=0; u< NAMEBUFFERSIZE;  u++)
						{
							if(NameBuffer[u] == 0xFF) NameBuffer[u] = 0;
						}

						if(!strncmp( NameBuffer, Name2Find , 32 ) ){
							*FAT_Entry 	= F16.direntries[i+m].deStartCluster;
							*FileSize  	= F16.direntries[i+m].deFileSize;
							*Attributes = F16.direntries[i+m].deAttributes;
							*DataSector	= DataRegionStart + ( ( *FAT_Entry - 2 ) * SectorsPerCluster ) ;
							*SubSector 	= i+m;
							*Sector    += q;

							#ifdef DEBUG_FAT
								PrintDOSLine(	NameBuffer,
												F16.direntries[i+m].deCDate,
												F16.direntries[i+m].deCTime,
												F16.direntries[i+m].deFileSize,
												F16.direntries[i+m].deAttributes);
							#endif

							return (TRUE);
						}

						i=i+m;	// update current position in cluster
						break;
					}
					// if short filename entry found ----------------------------------------------------------------------------------
					case ATTR_DIRECTORY:
					{
						if( F16.direntries[i].deName[0] != SLOT_DNt_Entry )  	// if dir name beginns with '.'
						{
							// copy data in NameBuffer
							for(k=0;k<8;k++)   NameBuffer[k] = F16.direntries[i].deName[k];

							// replace "space"(0x20) in file name by zeros
							// this is a necessary requirement to be able to use the string compare function
							for( u=0; u<8;  u++ )
							{
								if( NameBuffer[u] == 0x20 ) NameBuffer[u] = 0;
							}

							// if name match with input dir get dir information from buffer
							if( !strncmp( NameBuffer, Name2Find , 32 ) ){

								*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;
					}
					// if data entry found --------------------------------------------------------------------------------------------
					case ATTR_ARCHIVE:
					{
						// copy data in NameBuffer
						for(k=0;k<8;k++)   NameBuffer[k] = F16.direntries[i].deName[k];
						NameBuffer[8] = '.';
						for(k=0;k<3;k++)  NameBuffer[k+9]  = F16.direntries[i].deExtension[k];

						// replace zeros in file name "space"(0x20)
						// this is a necessary requirement to be able to use the string compare function
						for(u=0; u<8;  u++)
						{
							if( NameBufferShort[u] == 0x00 ) NameBufferShort[u] = 0x20;
						}
						for(u=0; u<3;  u++)
						{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -