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

📄 fat16.c

📁 在s3c44b0下利用SD卡进行WAV文件(FAT16格式)播放的源程序
💻 C
📖 第 1 页 / 共 4 页
字号:
	{
		for(; i< SectorsPerCluster; i++)
		{
			Cache = GetSectorData(fp->CurrentSectorNum);
			if(Cache == NULL)
				return 0;

			Cache += fp->SectorOffset;
			max_copy_bytes_in_sector = (BytesPerSector - fp->SectorOffset) > (bytes - read_bytes) ? (bytes - read_bytes) : (BytesPerSector- fp->SectorOffset);
			memcpy(ReadBuffer1[i], Cache, max_copy_bytes_in_sector);

			read_bytes += max_copy_bytes_in_sector;
			fp->SectorOffset += max_copy_bytes_in_sector;
			fp->offset += max_copy_bytes_in_sector;
			buffer = (char*)buffer + max_copy_bytes_in_sector;

			if(fp->SectorOffset == BytesPerSector)
			{
				if(i == SectorsPerCluster -1)
				{
					Cluster = GetNextClusterNum(Cluster);
					if(Cluster != 0xffff)
						fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
				}
				else
					fp->CurrentSectorNum ++;

				fp->SectorOffset = 0;
			}

			if(read_bytes == bytes)
			{
				return bytes;
			}
		}
	}
	
	while(Cluster != 0xffff)	//读取数据簇,直到0xffff
	{
		for(i=0; i< SectorsPerCluster; i++)
		{
			Cache = GetSectorData(fp->CurrentSectorNum);//取得当前扇区数据
			if(Cache == NULL)
				return 0;

			Cache += fp->SectorOffset;//加上要储存数据的零头(在最后有用),一个扇区512字节内的一个偏移量,一般为0
			
			//计算最大数据拷贝字节数,在最后计算出要存放的零头   
			//如果 每扇区需要存放的字节数(一般为512) > 还没有存放的字节总数    那就等于 还没有存放的字节总数
			//如果 每扇区需要存放的字节数(一般为512) < 还没有存放的字节总数    那就等于 每扇区需要存放的字节数(一般为512)
			max_copy_bytes_in_sector = (BytesPerSector- fp->SectorOffset) > (bytes - read_bytes) ? (bytes - read_bytes) : (BytesPerSector- fp->SectorOffset);
			memcpy(ReadBuffer1[j*8+i], Cache, max_copy_bytes_in_sector);//存放到缓存
			
			read_bytes += max_copy_bytes_in_sector;//计算已读取存放的字节总数
			
			//计算偏移量,如果之前存放的是一整扇区512字节的话,接下去会被清0
			//SectorOffset一般在数据最后才有意义,因为要读取的数据不一定都是扇区的整数倍
			//SectorOffset就是最后的零头
			fp->SectorOffset += max_copy_bytes_in_sector;
			
			fp->offset += max_copy_bytes_in_sector;
			buffer = (char*)buffer + max_copy_bytes_in_sector;

			if(fp->SectorOffset == BytesPerSector)//如果之前存放的是一整扇区的话,说明后面还有数据
			{				
				if(i == SectorsPerCluster -1) //如果是一簇中的最后一个扇区
				{
					Cluster = GetNextClusterNum(Cluster); //取得下一个簇号
					if(Cluster != 0xffff)	//如果不是结束标志,则计算当前扇区数
						fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
						
					j++;//将数据缓存往后挪8个扇区,j*8
					if(j == 10) j=0;
				}
				else
					fp->CurrentSectorNum ++; //当前扇区加1

				fp->SectorOffset = 0; //如果之前存放的是一整扇区512字节的话,SectorOffset清0
			}

			if(read_bytes == bytes)
			{
				return bytes;
			}
		}		
	}

	return 0;
}


/********************************************************************************************************************
** 函数名称: unsigned int fat_read1
** 功能描述: 从SD/MMC卡中读取一个文件,按簇读取(实际是单块读取)	
** 输   入: int handle		: 句柄		
			 void* buffer   : 接收缓冲区	
** 输    出: 0:   成功    >0:  错误码			
*********************************************************************************************************************/
unsigned int fat_read1(int handle, void* buffer)
{
	BYTE* Cache;
	_FILE *fp;
	WORD Cluster;
	int i,j=0;
	int k=0;

	if(handle <0 || handle >= sizeof(handles)/sizeof(_FILE))
		return 0;

	fp = &handles[handle];	//装入文件句柄(由之前fat_open带回的x),handles为指向_FILE的数组

	Cluster = SectorNum2ClusterNum(fp->CurrentSectorNum);//根据当前扇区算出数据起始簇

	i = (fp->CurrentSectorNum - FirstDataSector) % SectorsPerCluster;//计算是否有不是一整簇的扇区

	if(i != 0)	//读取不满一簇的数据扇区
	{
		for(; i< SectorsPerCluster; i++)
		{
			Cache = GetSectorData(fp->CurrentSectorNum);
			if(Cache == NULL)
				return 0;

			memcpy(ReadBuffer1[j*8+i], Cache, 512);

			if(i == SectorsPerCluster -1)
			{
				Cluster = GetNextClusterNum(Cluster);
				if(Cluster != 0xffff)
					fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
			}
			else
			fp->CurrentSectorNum ++;
		}
	}
	
	while(Cluster != 0xffff)	//读取数据簇,直到0xffff
	{
		for(i=0; i< SectorsPerCluster; i++)
		{
			Cache = GetSectorData(fp->CurrentSectorNum);//取得当前扇区数据
			if(Cache == NULL)
				return 0;
				
			memcpy(ReadBuffer1[j*8+i], Cache, 512);//存放到缓存
			//if(k==0) memcpy(ReadBuffer2[0], Cache, 512);//保存第一扇区内容
			k++;
			
			if(i == SectorsPerCluster -1) //如果是一簇中的最后一个扇区
			{
				Cluster = GetNextClusterNum(Cluster); //取得下一个簇号
				if(Cluster != 0xffff)	//如果不是结束标志,则计算当前扇区数
					fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
				else
				{
					m=i;
					n=j;
				}
						
				j++;//将数据缓存往后挪8个扇区,j*8
				if(j == 10) j=0;
			}
			else
			fp->CurrentSectorNum ++; //当前扇区加1
		}		
	}
	return 0;
}


/********************************************************************************************************************
** 函数名称: unsigned int fat_read2
** 功能描述: 从SD/MMC卡中读取一个文件,按簇读取(实际是多块--以簇为单位读取)	
** 输   入: int handle		: 句柄		
			 void* buffer   : 接收缓冲区	
** 输    出: 0:   成功    >0:  错误码			
*********************************************************************************************************************/

unsigned int fat_read2(int handle)
{
	BYTE* Cache;
	_FILE *fp;
	WORD Cluster;
	int i,j=0;
	int k=0,buf1_busy=0,buf2_busy=0;
	unsigned long int aa1=0,aa2=0;

	
	if(handle <0 || handle >= sizeof(handles)/sizeof(_FILE))
		return 0;

	fp = &handles[handle];	//装入文件句柄(由之前fat_open带回的x),handles为指向_FILE的数组

	Cluster = SectorNum2ClusterNum(fp->CurrentSectorNum);//根据当前扇区算出数据起始簇

	i = (fp->CurrentSectorNum - FirstDataSector) % SectorsPerCluster;//计算是否有不是一整簇的扇区

	if(i != 0)	//读取不满一簇的数据扇区
	{
		for(; i< SectorsPerCluster; i++)
		{
			Cache = GetSectorData(fp->CurrentSectorNum);
			if(Cache == NULL)
				return 0;

			memcpy(ReadBuffer1[j*8+i], Cache, 512);

			if(i == SectorsPerCluster -1)
			{
				Cluster = GetNextClusterNum(Cluster);
				if(Cluster != 0xffff)
					fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
			}
			else
			fp->CurrentSectorNum ++;
		}
	}
	
	aa1 = (1<<30)+(1<<28)+(int)( (unsigned char *)ReadBuffer1 + 0x30 );
	aa2 = (1<<30)+(1<<28)+(int)( (unsigned char *)ReadBuffer2 + 0x30 );
	
	while(Cluster != 0xffff)	//读取数据簇,直到0xffff
	{
		//for(i=0; i<50; i++)
		//{
			SD_ReadMultiBlock1(Cluster, ReadBuffer1+j);	//读取一整簇的数据
			Cluster = GetNextClusterNum(Cluster); //取得下一个簇号
    		//rBDISRC0 = aa1;
    		j=j+8;
		//}
		
    	//rIISCON |=0x1;

		
		//SD_ReadMultiBlock1(Cluster, ReadBuffer2);	//读取一整簇的数据
		//Cluster = GetNextClusterNum(Cluster); //取得下一个簇号
    	//rBDISRC0 = aa2;


	}
	return 0;
}


unsigned int fat_write(int handle, const char* buffer, unsigned int bytes)
{
	BYTE* Cache;
	unsigned int write_bytes =0;
	unsigned int max_write_bytes_in_sector;
	_FILE *fp;
	WORD Cluster;
	WORD PrevCluster;
	int i;

	if(handle <0 || handle >= sizeof(handles)/sizeof(_FILE))
		return 0;

	fp = &handles[handle];

	Cluster = SectorNum2ClusterNum(fp->CurrentSectorNum);
	PrevCluster = Cluster;

	i = (fp->CurrentSectorNum - FirstDataSector) % SectorsPerCluster;

	if(i != 0)
	{
		for(; i< SectorsPerCluster; i++)
		{
			Cache = GetSectorData(fp->CurrentSectorNum);
			if(Cache == NULL)
				return 0;

			Cache += fp->SectorOffset;
			max_write_bytes_in_sector = (BytesPerSector- fp->SectorOffset) > (bytes - write_bytes) ? (bytes - write_bytes) : (BytesPerSector - fp->SectorOffset);
			memcpy(Cache, buffer, max_write_bytes_in_sector);
			Flush();

			write_bytes += max_write_bytes_in_sector;
			fp->SectorOffset += max_write_bytes_in_sector;
			fp->offset += max_write_bytes_in_sector;
			buffer = (char*)buffer + max_write_bytes_in_sector;
			fp->dir.deFileSize +=  max_write_bytes_in_sector;

			if(fp->SectorOffset == BytesPerSector)
			{
				if(i == SectorsPerCluster -1)
				{
					PrevCluster = Cluster;
					Cluster = GetNextClusterNum(Cluster);
					if(Cluster != 0xffff)
						fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
					else
					{
						Cluster = AllocCluster(PrevCluster);
						if(Cluster == 0xffff)
							return 0;
						
						fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
					}
				}
				else
					fp->CurrentSectorNum ++;

				fp->SectorOffset = 0;
			}

			if(write_bytes == bytes)
			{
				return bytes;
			}
		}
	}

	for(;;)
	{
		for(i=0; i< SectorsPerCluster; i++)
		{
			Cache = GetSectorData(fp->CurrentSectorNum);
			if(Cache == NULL)
				return 0;

			Cache += fp->SectorOffset;
			max_write_bytes_in_sector = (BytesPerSector - fp->SectorOffset) > (bytes - write_bytes) ? (bytes - write_bytes) : (BytesPerSector - fp->SectorOffset);
			memcpy(Cache, buffer, max_write_bytes_in_sector);
			Flush();

			write_bytes += max_write_bytes_in_sector;
			fp->SectorOffset += max_write_bytes_in_sector;
			fp->offset += max_write_bytes_in_sector;
			buffer = (char*)buffer + max_write_bytes_in_sector;
			fp->dir.deFileSize +=  max_write_bytes_in_sector;

			if(fp->SectorOffset == BytesPerSector)
			{
				if(i == SectorsPerCluster -1)
				{
					PrevCluster = Cluster;
					Cluster = GetNextClusterNum(Cluster);
					if(Cluster != 0xffff)
						fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
					else
					{
						Cluster = AllocCluster(PrevCluster);
						if(Cluster == 0xffff)
							return 0;
						
						fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
					}
				}
				else
					fp->CurrentSectorNum ++;

				fp->SectorOffset = 0;
			}

			if(write_bytes == bytes)
			{
				return bytes;
			}
		}
	}

	// we can not reach here.
	return 0;
}

int fat_remove( const char *filename)
{
	DWORD SectorNum;
	_FILE file;

	//locate
	SectorNum = fat_locate(filename, &file);
	if(SectorNum == 0xffffffff)
		return 4;

	// is it a dir ?
	if(file.dir.deAttributes & ATTR_DIRECTORY)
		return 6;

	if(DeleteDir(&file) != 0)
		return 5;

	FreeCluster(file.dir.deStartCluster);

	return 0;
}

int fat_get_stat( const char *filename, _STAT * stat)
{
	DWORD SectorNum;
	_FILE file;

	//locate
	SectorNum = fat_locate(filename, &file);
	if(SectorNum == 0xffffffff)
		return 1;

	stat->Attr = file.dir.deAttributes;
	stat->CrtDate = file.dir.CrtDate;
	stat->CrtTime = file.dir.CrtTime;
	stat->CrtTimeTenth = file.dir.CrtTimeTenth;
	stat->FileSize = file.dir.deFileSize;
	stat->LstAccDate = file.dir.LstAccDate;
	stat->WrtDate = file.dir.WrtDate;
	stat->WrtTime = file.dir.WrtTime;

	return 0;
}

int fat_set_stat( const char *filename, _STAT * stat)
{
	DWORD SectorNum;
	_FILE file;
	BYTE* Cache;
	DIRENTRY *dir;

	//locate
	SectorNum = fat_locate(filename, &file);
	if(SectorNum == 0xffffffff)
		return 1;

	file.dir.deAttributes = stat->Attr;
	file.dir.CrtDate = stat->CrtDate;
	file.dir.CrtTime = stat->CrtTime;
	file.dir.CrtTimeTenth = stat->CrtTimeTenth;
	file.dir.deFileSize = stat->FileSize;
	file.dir.LstAccDate = stat->LstAccDate;
	file.dir.WrtDate = stat->WrtDate;
	file.dir.WrtTime = stat->WrtTime;

	Cache = GetSectorData(file.DirSectorNum);
	if(Cache == NULL)
		return 2;

	dir = (DIRENTRY *)Cache;
	dir += file.DirIndex;

	memcpy((BYTE *)dir, (BYTE *)&file.dir, sizeof(DIRENTRY));
	Flush();

	return 0;
}


int fat_rename( const char *oldname, const char *newname )
{
	DIRENTRY dir;
	char path[512];
	char newpath[512];
	char name[11];
	char new_name[11];
	char *p;
	DWORD ParentDirSectorNum;
	_FILE old_file;

	//
	//check oldname file
	//

	// is path format correct ?
	p = get_valid_format(oldname);
	if(p == NULL)
		return -2;

	//if exist this file ?
	if(fat_locate(oldname, &old_file) == 0xffffffff)
		return -3;

	//separate path into parent and name
	strncpy(name, &p[strlen(p)-11], 11);

	strcpy(path, oldname);
	p = strrchr(path, '\\');
	*p = '\0';


	//
	//check newname file
	//

	if(strchr(newname, '\\') != NULL)
		return -2;

	sprintf(newpath, "%s\\%s", path, newname);

	// is path format correct ?
	p = get_valid_format(newpath);
	if(p == NULL)
		return -2;

	//if exist this file ?
	if(fat_locate(newpath, NULL) != 0xffffffff)
		return -3;

	//separate path into parent and name
	strncpy(new_name, &p[strlen(p)-11], 11);



	//locate parent path
	ParentDirSectorNum = fat_locate(path, NULL);
	if(ParentDirSectorNum == 0xffffffff)
		return -4;

	//fill dir attributes
	memcpy((BYTE *)&dir,(BYTE *)(&old_file.dir), sizeof(DIRENTRY));
	memcpy((BYTE *)dir.deName, new_name, 11);

	//alloc one dir
	if(AllocDir(ParentDirSectorNum, &dir, NULL) != 0)
		return -5;

	//delete old one
	if(DeleteDir(&old_file) != 0)
		return -6;

	return 0;
}

⌨️ 快捷键说明

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