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

📄 fat16avr2004.c

📁 移植到mega32上面的fat16代码实现了
💻 C
📖 第 1 页 / 共 4 页
字号:
	int index, isetor;
	char Name[12];

	strncpy(Name, shortName,12);
	Name[11]='\0';
	fatNormalize(Name); // adjust the name to the FAT format

	cluster= currentDirCluster;	// start the search in the current cluster
	do
	{
		for (isetor=0; isetor< SectorsPerCluster; isetor++)		// clusters
		{
			// read dir data
			sector = fatClustToSect(cluster) + (unsigned long)isetor;
			ataReadSectors( DRIVE0, sector, SectorBuffer, &SectorInCache);
			de = (struct direntry *) SectorBuffer;

			for (index=1; index<=FAT_DIRENTRIES_PER_SECTOR; index++)
			{
				if (*de->deName == 0x00)
				{
					return NULL;		// there is no more direntries
				}
				if((*de->deName != SLOT_DELETED) && (de->deAttributes != ATTR_LONG_FILENAME))
				{
					if (strncmp(de->deName, Name, 11) == 0)
					{
						memcpy(rde, de, DIRENTRY_SIZE);
						return(de);
					}
				}
				de++;
			}	// end of sector
		}	// end of cluster
		cluster= fatNextCluster(cluster);
	}while  (sector < FirstFATSector+FatSectors); // end of FatDirentries

	return NULL;
}


//*****************************************************************************
// Function: fatGetVolLavel
// Parameters: none
// Returns: the FAT Volume Name
//
// Description: Return the FAT Volume Name read in fatInit
//*****************************************************************************
char* fatGetVolLabel(void)
{
	return VolLabel;
}


//*****************************************************************************
// Function: fatCddir
// Parameters: path
// Returns: On SUSCEFULL returns TRUE, otherwise returns FALSE
//
// Description: Change the current directory. Only one level path
//              ex: cd \test
//              cd \test\test2  IS INVALID, use cd \test cd\test2 instead
//*****************************************************************************
unsigned char fatCddir(char *path)
{
	struct direntry de;

	// if the path doesn't exist return FALSE
	if (fatGetFileInfo(&de, path) == NULL)
		return FALSE;

	// if the path is a directory
	if ((de.deAttributes & ATTR_DIRECTORY) == ATTR_DIRECTORY)
	{
		// change the current dir cluster to the path information
		currentDirCluster= (de.deStartCluster) + ((unsigned long)de.deHighClust<<16);
		if (currentDirCluster == 0)
			currentDirCluster = 2;
		return TRUE;
	}
	return FALSE;
}


////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatMkdir
// Parameters: path
// Returns: TRUE if the path was created, otherwise FALSE
//
// Description: Create a new directory on the current directory
//*****************************************************************************
unsigned char fatMkdir(char *path)
{
	struct direntry *de, rde;
	unsigned long freeCluster, newDirEntrySector, lastCluster;
	unsigned int i, date, time;
	char string[12];
	TTime t;

	// if the file already exist
	if (fatGetFileInfo(&rde, path) != NULL)
	{
		return FALSE;
	}

	// Find a free direntry, returns a pointer to the direntry struct inside SectorBuffer,
	de=fatNextFreeDirEntry(currentDirCluster);

	// Saves the sector address of this new DirEntry to write it back to the ATA dispositive
	newDirEntrySector= SectorInCache;


	// if goes until the end of the cluster and don't find a free direntry
	// needs to create a new direntry in a new cluster to put this new directory.
	if (de == NULL)
	{
		freeCluster=fatNextFreeCluster(0); 	// find a new free cluster
		if (freeCluster == 0)				// is the disk is full
			return (FALSE);					//		return ERROR

		lastCluster= fatLastCluster(currentDirCluster);	// find the last cluster of the current dir
		fatWrite(lastCluster, freeCluster);				// creates a connection between the last cluster of the current directory and the new cluster created
		fatWriteEOC(freeCluster);						// mark the new cluster with an END OF CLUSTER mark
		memset (SectorBuffer, '\0', BYTES_PER_SECTOR); 	// fill the SectorBuffer with ZEROS to informate to the FAT that don't have more direntries after this one that we are creating
		de= (struct direntry *)SectorBuffer;			// de points to the first address in the SectorBuffer
		newDirEntrySector= fatClustToSect(freeCluster); // Calculate the start sector address of this new cluster
		SectorInCache= newDirEntrySector;				// the sector in memory is this new direntry allocated
		for (unsigned int i=1; i< SectorsPerCluster; i++)	// write zeros in all the new cluster allocated
			ataWriteSectors( DRIVE0, newDirEntrySector+i, SectorBuffer);
	}

	// in this point we have a direntry in SectorBuffer, and de point to the address of the informations about the new directory


	// find a FAT free cluster to write the directory content
	freeCluster=fatNextFreeCluster(0);

	// fill the direntry
	strcpy(string, path);
	string[11]='\0';
	fatNormalize(string);

	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

	memcpy(de->deName, string, 11);		// name of the directory
	de->deAttributes= ATTR_DIRECTORY;
	de->deLowerCase=0;   	 			// lowercase Names
	de->deCHundredth= 0x00;	 			// hundredth of seconds in CTime

	de->deCTime[0]=(unsigned char)time&0x00FF;
	de->deCTime[1]=(unsigned char)((time&0xFF00)>>8);
	de->deCDate[0]=(unsigned char)date&0x00FF;
	de->deCDate[1]=(unsigned char)((date&0xFF00)>>8);
	de->deADate[0]=de->deCDate[0];     	// access date
	de->deADate[1]=de->deCDate[1];     	// access date
	de->deHighClust= (freeCluster>>16); // high bytes of cluster number
	de->deMTime[0]=de->deCTime[0];     	// last update time
	de->deMTime[1]=de->deCTime[1];     	// last update time
	de->deMDate[0]=de->deCDate[0];     	// last update date
	de->deMDate[1]=de->deCDate[1];     	// last update date
	de->deStartCluster= freeCluster; 	// starting cluster of file
	de->deFileSize=0;  					// size of file in bytes, is a directory, so the size is 0


	// write SectorBuffer filled with the new directory information
	ataWriteSectors( DRIVE0, newDirEntrySector, SectorBuffer);


	// create the files "." and ".."
	de= (struct direntry *)SectorBuffer;
	strncpy(de->deName, ".          ", 11);
	de->deAttributes= ATTR_DIRECTORY;
	de->deLowerCase=0;   				// lowercase Names
	de->deCHundredth= 0x00;	 			// hundredth of seconds in CTime
	de->deCTime[0]=(unsigned char)time&0x00FF;
	de->deCTime[1]=(unsigned char)((time&0xFF00)>>8);
	de->deCDate[0]=(unsigned char)date&0x00FF;
	de->deCDate[1]=(unsigned char)((date&0xFF00)>>8);	de->deADate[0]=de->deCDate[0];     	// access date
	de->deADate[1]=de->deCDate[1];     	// access date
	de->deHighClust= (freeCluster>>16); // high bytes of cluster number
	de->deMTime[0]=de->deCTime[0];     	// last update time
	de->deMTime[1]=de->deCTime[1];     	// last update time
	de->deMDate[0]=de->deCDate[0];     	// last update date
	de->deMDate[1]=de->deCDate[1];     	// last update date
	de->deStartCluster= freeCluster; // starting cluster of file
	de->deFileSize=0;  					// size of file in bytes

	de++;
	strncpy(de->deName, "..         ", 11);
	de->deAttributes= ATTR_DIRECTORY;
	de->deLowerCase=0;   				// lowercase Names
	de->deCHundredth= 0x00;	 			// hundredth of seconds in CTime
	de->deCTime[0]=(unsigned char)time&0x00FF;
	de->deCTime[1]=(unsigned char)((time&0xFF00)>>8);
	de->deCDate[0]=(unsigned char)date&0x00FF;
	de->deCDate[1]=(unsigned char)((date&0xFF00)>>8);
	de->deADate[0]=de->deCDate[0];     	// access date
	de->deADate[1]=de->deCDate[1];     	// access date
	de->deHighClust= (currentDirCluster>>16); // high bytes of cluster number
	de->deMTime[0]=de->deCTime[0];     	// last update time
	de->deMTime[1]=de->deCTime[1];     	// last update time
	de->deMDate[0]=de->deCDate[0];     	// last update date
	de->deMDate[1]=de->deCDate[1];     	// last update date
	de->deStartCluster= currentDirCluster; // starting cluster of file
	de->deFileSize=0;  					// size of file in bytes
	de++;

	// fill the rest with zeros
	for (i= (unsigned int)(((unsigned char *)de) - SectorBuffer); i < 512; i++)
		SectorBuffer[i]= 0;

	// write the content of the new directory created, including "." and ".." files
	ataWriteSectors( DRIVE0, fatClustToSect(freeCluster), SectorBuffer);


	// fill the entire sector buffer with zeros
	memset (SectorBuffer, '\0', BYTES_PER_SECTOR);

	// write all the others sectors of this new cluster.
	for (i=1; i< SectorsPerCluster; i++)
		ataWriteSectors( DRIVE0, fatClustToSect(freeCluster)+i, SectorBuffer);

	// actualize the sector address in cache
	SectorInCache= fatClustToSect(freeCluster)+SectorsPerCluster-1;


	// mark the cluster with the content off the new directory created with an END OF CLUSTER mark
	fatWriteEOC(freeCluster);
	return TRUE;
}
#endif
////////////////////



////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatFcreate
// Parameters: File short name
// Returns: A TFILE struct filled with the created file information, or NULL
//          if an error has ocurred
//
// Description: Create a file and open it in the current directory
//*****************************************************************************
TFILE* fatFcreate(char *shortName)
{
	struct direntry *de, rde;
	unsigned long freeCluster, newDirEntrySector, lastCluster;
	unsigned int date, time;
	char string[12];
	TTime t;

	// if the file already exist
	if (fatGetFileInfo(&rde, shortName) != NULL)
		return NULL;


	// Find a free direntry, returns a pointer to the direntry struct inside SectorBuffer,
	de=fatNextFreeDirEntry(currentDirCluster);

	// Saves the sector address of this new DirEntry to write it back to the ATA dispositive
	newDirEntrySector= SectorInCache;


	// if goes until the end of the cluster and don't find a free direntry
	// needs to create a new direntry in a new cluster to put this new file.
	if (de == NULL)
	{
		freeCluster=fatNextFreeCluster(0);
		if (freeCluster == 0)	// if disk is full
			return (FALSE);		// returns ERROR


		lastCluster= fatLastCluster(currentDirCluster);	// find the last cluster of the current dir
		fatWrite(lastCluster, freeCluster);				// creates a connection between the last cluster of the current directory and the new cluster created
		fatWriteEOC(freeCluster);						// mark the new cluster with an END OF CLUSTER mark
		memset (SectorBuffer, '\0', BYTES_PER_SECTOR); 	// fill the SectorBuffer with ZEROS to informate to the FAT that don't have more direntries after this one that we are creating
		de= (struct direntry *)SectorBuffer;			// de points to the first address in the SectorBuffer
		newDirEntrySector= fatClustToSect(freeCluster); // Calculate the start sector address of this new cluster
		SectorInCache= newDirEntrySector;				// the sector in memory is this new direntry allocated
	}

	// find a free fat cluster to put the content of this new file
	freeCluster=fatNextFreeCluster(0);

	// fill with file data information
	strcpy(string, shortName);
	string[11]='\0';
	fatNormalize(string);


	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

	memcpy(de->deName, string, 11);
	de->deAttributes= ATTR_ARCHIVE;
	de->deLowerCase=0;   				// lowercase Names
	de->deCHundredth= 0x00;				// hundredth of seconds in CTime

	de->deCTime[0]=(unsigned char)time&0x00FF;
	de->deCTime[1]=(unsigned char)((time&0xFF00)>>8);
	de->deCDate[0]=(unsigned char)date&0x00FF;
	de->deCDate[1]=(unsigned char)((date&0xFF00)>>8);
	de->deADate[0]=de->deCDate[0];     	// access date
	de->deADate[1]=de->deCDate[1];     	// access date
	de->deHighClust= (freeCluster>>16); // high bytes of cluster number
	de->deMTime[0]=de->deCTime[0];     	// last update time
	de->deMTime[1]=de->deCTime[1];     	// last update time
	de->deMDate[0]=de->deCDate[0];     	// last update date
	de->deMDate[1]=de->deCDate[1];     	// last update date
	de->deStartCluster= freeCluster; 	// starting cluster of file
	de->deFileSize=0;  					// size of file in bytes

	// write direntry with the file information
	ataWriteSectors( DRIVE0, newDirEntrySector, SectorBuffer);

	// mark the cluster with the content off the new directory created with an END OF CLUSTER mark
	fatWriteEOC(freeCluster);

	// open the file to read and write
	return(fatFopen(shortName));
}
#endif
////////////////////


////////////////////
#ifndef ATA_READ_ONLY
//*****************************************************************************
// Function: fatRename
// Parameters: old name, new name
// Returns: TRUE if the name was chanched, otherwise FALSE
//
// Description: Change the name of a directory or file
//*****************************************************************************
unsigned char fatRename(char *oldShortName, char *newShortName)
{
	struct direntry *de, rde;
	char string[12];

	// if the new file name already exist
	de=fatGetFileInfo(&rde, newShortName);
	if (de != NULL)
		return FALSE;

	// if the file don't exist
	de=fatGetFileInfo(&rde, oldShortName);
	if (de == NULL)
		return FALSE;


⌨️ 快捷键说明

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