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

📄 fat16.c

📁 usblab usb程序开发实验 一个实例
💻 C
字号:
#include "hardware.h"

UCHAR	xdata	Page_Buf[512];

#define Root_Cluster	(0)				//MBR所在的簇号
#define Data_Cluster	(1)				//数据真正开始的扇区

#define BPB_Sector	(0)
#define Hid_Sector	(68)
#define Fat_Sector	(BPB_Sector+Hid_Sector)
#define Fat_Size		(254)

#define UNUSED_MARK		(0xFF)
#define BAD_MARK		(0x00)
#define LAST_BLOCK		(0xFFFF)
#define FILE_NO_FOUND	(512)
//#define BLOCK_SIZE		(32*512)


code unsigned char BPB_Data[512] = {
			0xeb, 0x3c, 0x90,//	Offset_U8_BS_jmpBoot		0
			'g', 'u', 'o', 'b', 'i', 'n', 'u', 'p',// Offset_U8_BS_OEMName		3
			0x00, 0x02,// Offset_short int_BPB_BytsPerSec	11	//Count of bytes per sector. This value may take on only the  //
//512									//following values: 512, 1024, 2048 or 4096. If maximum       //
										//compatibility is desired, only the value 512 should be used.//
			1,// Offset_U8_BPB_SecPerClus	13	//Number of sectors per allocation unit. This value must be a //
										//power of 2 that is greater than 0. The legal values are 1,  //
										//	2, 4, 8, 16, 32, 64,and 128.                                //
			68, 0x00,// Offset_short int_BPB_RsvdSecCnt	14	//Number of reserved sectors in the Reserved region of the    //
										//volume starting at the first sector of the volume.For FAT32 //
										//volumes, this value is typically 32.                        //
			0x02,// Offset_U8_BPB_NumFATs		16	//The count of FAT data structures on the volume.             //
			0x00, 0x02,// Offset_short int_BPB_RootEntCnt	17	//Is only useful for FAT12 and FAT16 volumes.                 //
//										//For FAT32 volumes,this field must be set to 0.              //
//通过调整文件项为03b0,从而使数据扇区刚好从Block2Sector0开始,但
//文件分区表中根目录区并为标记为已占用。数据实际从Block4Sector0开始
			0xe0,	0xff,// Offset_short int_BPB_TotSec16		19	//For FAT12 and FAT16 volumes, this field contains the sector //
										//count, and BPB_TotSec32 is 0 if the total sector count fits //
										//(is less than 0x10000).                                     //
			0xf0,// Offset_U8_BPB_Media			21	//0xF8 is the standard value for fixed(non-removable) media.  //
										//For removable media, 0xF0 is frequently used.               //
			0xfe, 0x00,// Offset_short int_BPB_FATSz16		22	//This field is FAT12/FAT16 16bit count of sectors occupied by//
										//	1 FAT.On FAT32 volumes this field must be 0, and BPB_FATSz32//
										//contains the FAT size count.                                //
			0x3f, 0x00,// Offset_short int_BPB_SecPerTrk	24	//Sectors per track for interrupt 0x13. This field is only    //
										//relevant for media that have a geometry (volume is broken   //
										//down into tracks by multiple heads and cylinders) and are   //
										//visible on interrupt 0x13.This field contains the           //
										//"sectors per track" geometry value.                         //
			0xff, 0x00,// Offset_short int_BPB_NumHeads		26	//Number of heads for interrupt 0x13. This field is relevant  //
										//as discussed earlier for BPB_SecPerTrk.                     //
			0, 0x00, 0x00, 0x00,// Offset_U32_BPB_HiddSec		28	//Count of hidden sectors preceding the partition that        //
										//contains this FAT volume.                                   //
			0x00, 0x00, 0x00, 0x00,// Offset_U32_BPB_TotSec32		32	//This field is the new 32-bit total count of sectors on the  //
										//volume. This count includes the count of all sectors in all //
										//four regions of the volume. This field can be 0; if it is 0,//
										//then BPB_TotSec16 must be non-zero. For FAT32 volumes, this //
										//field must be non-zero. For FAT12/FAT16 volumes, this field //
										//contains the sector count if BPB_TotSec16 is 0 (count is    //
										//greater than or equal to 0x10000).//
			0x00,// Offset_U8_BS_DrvNum		36	//This field is the FAT32 32-bit count of sectors occupied by //
	//根据该值得到FAT的大小//				//ONE FAT. BPB_FATSz16 must be 0.                             //
	
			0x00,// Offset_U8_BS_Reserved		37	//    Bits 0-3 -- Zero-based number of active FAT.Only valid  //
										//                if mirroring is disabled.                   //
										//    Bits 4-6 -- Reserved.                                   //
										//    Bit 7    -- 0 means the FAT is mirrored at runtime into //
										//                  all FATs.                                 //
										//             -- 1 means only one FAT is active;it is the one//
										//                  referenced in bits 0-3.                   //
										//    Bits 8-15 -- Reserved.                                  //
			0x29,// Offset_U8_BS_BootSig		38	//High byte is major revision number.                         //
										//Low byte is minor revision number.                          //
			'N', 'A', 'M', 'E',// Offset_U32_BS_VolID		39	//This is set to the cluster number of the first cluster of   //
	//根据该值得到ROOT的地址//				//the root directory, usually 2 but not required to be 2.     //
	
			'N', 'O', ' ', 'N', 'A', 'M', 'E', ' ', ' ', ' ', ' ',// Offset_U2048_BS_VolLab		43	//Sector number of FSINFO structure in the reserved area of   //
										//the FAT32 volume.Usually 1.                                 //
			'F', 'A', 'T', '1', '6', ' ', ' ', ' ',// Offset_U256_BS_FilSysType	54	//If non-zero, indicates the sector number in the reserved    //
										//area of the volume of a copy of the boot record.            //
//Executable Code
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0,0 ,0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	

			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0,

			0x00,//active partion
			0x00,//head
			0x00,//partion begin
			0x00,//cylinder
			0x00,//0x06,//is partion used
			0x00,//end head
			0x00,//partion end 
			0x00,//end cylinder
			0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00,//fist partion0x80

			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0,//second partion
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0,//third partion
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0,//forth partion

			0x55, 0xAA	// Offset_short int_Signature		510	//0x55AA           

			};

unsigned char Flash_Reset(void)
{
	unsigned int	i;

	Flash_EN();
	Flash_Com();
	Flash_Write(0xFF);
	Flash_WaitBusy();

	Flash_Com();
	Flash_Write(0x90);
	Flash_Add();
	Flash_Write(0x00);
	Flash_Dat();
	P2=0xFF;
	Flash_Read(((UCHAR*)&i)[0]);
	Flash_Read(((UCHAR*)&i)[1]);
	Flash_DIS();

	if(i==0xAD75)
		return	1;
	else
		return	0;
}


/********************************************************/
/*功能:擦除FLASH的1Block(对应文件系统为1Cluster)          */
/*     每一个簇为16KB,因此KM29U128共有1024个簇            */
/*输入:unsigned int cluster/block(需要擦除的Block Number)*/
/*返回:OK或FAIL                                         */
/********************************************************/

unsigned char Erase_Block(unsigned int Block) 
{
	unsigned int blockPage = (Block << 5);
	UCHAR	i;

	Flash_EN();
	Flash_Com();
	Flash_Write(0x60);
	Flash_Add();
	Flash_Write(((UCHAR*)&blockPage)[1]);
	Flash_Write(((UCHAR*)&blockPage)[0]);
	Flash_Com();
	Flash_Write(0xD0);

	Flash_Dat();
	Flash_WaitBusy();

   	Flash_Com();
	Flash_Write(0x70);
	Flash_Dat();
	P2=0xFF;
	Flash_Read(i);
	Flash_DIS();
	if(i&0x01)
		return	0;
	else
		return	1;	
}

/*****************************************************************/
/*功能:读取FLASH的某个Block中的1page数据                           */
/*     在文件系统中,有如下对应关系:Block=Cluster                   */
/*                               Page =Sector                    */
/*输入:unsigned int block,page,unsigned char *pPage(存放数据的地址)*/
/*****************************************************************/

void ReadPage(unsigned int block, unsigned int page, unsigned char *pPage)
{
	unsigned int i ;
	unsigned int blockPage = (block << 5) + page;

	Flash_EN();
	Flash_Com();
	Flash_Write(0x00);
	Flash_Add();
	Flash_Write(0);
	Flash_Write(((UCHAR*)&blockPage)[1]);
	Flash_Write(((UCHAR*)&blockPage)[0]);
	Flash_Dat();
	Flash_WaitBusy();

	P2=0xFF;
	for(i=0;i<512;i++)
		Flash_Read(*pPage++);

	Flash_DIS();
}

/*****************************************************************/
/*功能:向FLASH的某个Block中的1page写入数据                         */
/*     在文件系统中,有如下对应关系:Block=Cluster                   */
/*                               Page =Sector                    */
/*输入:unsigned int block,page,unsigned char *pPage(存放数据的地址)*/
/*输出:0:Fail;1:OK                                               */
/*****************************************************************/

int WritePage(unsigned int block, unsigned int page, unsigned char *pPage)  
{
	unsigned char j;
	unsigned int i ;
	unsigned int blockPage = (block << 5) + page;


	Flash_EN();
	Flash_Com();
	Flash_Write(0x80);
	Flash_Add();
	Flash_Write(0);
	Flash_Write(((UCHAR*)&blockPage)[1]);
	Flash_Write(((UCHAR*)&blockPage)[0]);
	Flash_Dat();

	for(i=0;i<512;i++)
		Flash_Write(*pPage++);

	Flash_Com();
	Flash_Write(0x10);
	Flash_WaitBusy();

	Flash_Write(0x70);
	Flash_Dat();
	P2=0xFF;
	Flash_Read(j);
	Flash_DIS();
	
	if (j& 0x1)	//		
	{
		return 0;
	}
	else
	{
		return 1;
	}
}


void WriteMBR2Flash(void)
{
	WritePage(Root_Cluster, BPB_Sector, BPB_Data);
}

void CreatFAT12(void)
{
	short int i;
	unsigned char *FAT = Page_Buf;
	for (i=0; i<512; i++) 
	{
		FAT[i] = 0x00;
	}
	//disk type lower byte is equal to Offset_U8_BPB_Media
	FAT[0] = 0xf8;
	FAT[1] = 0xff;//system occupied the first cluster
	FAT[2] = 0xff;
	FAT[3] = 0x7f;
	
		WritePage(Root_Cluster, Fat_Sector, FAT);	//Sector 1 of Fat16
		WritePage(Root_Cluster, Fat_Sector + 2, FAT);	//Sector 1 of Fat16
		FAT[0] = FAT[1] = FAT[2] = FAT[3] = 0x00;
		WritePage(Root_Cluster, Fat_Sector + 1, FAT);	//Sector 2 of Fat16
		WritePage(Root_Cluster, Fat_Sector + 3, FAT);	//Sector 2 of Fat16
}

void CreatFAT16(void)
{
	short int i;
	unsigned char *FAT = Page_Buf;
	for (i=0; i<512; i++) 
	{
		FAT[i] = 0x00;
	}
	//disk type lower byte is equal to Offset_U8_BPB_Media
	FAT[0] = 0xf8;
	FAT[1] = 0xff;//system occupied the first cluster
	FAT[2] = 0xff;
	FAT[3] = 0xff;
	
	WritePage(Root_Cluster, Fat_Sector, FAT);	//Sector 1 of Fat16
	WritePage(Root_Cluster, Fat_Sector + Fat_Size, FAT);	//Sector 1 of Fat16
	FAT[0] = FAT[1] = FAT[2] = FAT[3] = 0x00;
		for(i=1;i<Fat_Size;i++)
			{
		//WritePage(Root_Cluster+i/32,  i%32, FAT);	//Sector 2 of Fat16
		WritePage(Root_Cluster, Fat_Sector+i, FAT);
		//WritePage(, Fat_Sector + 3, FAT);	//Sector 2 of Fat16
			}
		for(i=1;i<Fat_Size;i++)
			{
		WritePage(Root_Cluster,   Fat_Sector + Fat_Size+i, FAT);	//Sector 2 of Fat16
		//WritePage(Root_Cluster, Fat_Sector + 3, FAT);	//Sector 2 of Fat16
			}
}
void CreatDirectoryEntry(void)
{
	long int i;
	unsigned char *Directory_Enties = Page_Buf;

	for (i=0; i<512; i++)  
	{
		Directory_Enties[i] = 0x00;
	}

	for (i=513; i<2048; i++) 
	{
		WritePage(Root_Cluster + i / 32,  i % 32, Directory_Enties);
	}
	
}

void CreatDirectoryEntry16(void)
{
	unsigned long  i;
	unsigned char *Directory_Enties = Page_Buf;

	for (i=0; i<512; i++)  
	{
		Directory_Enties[i] = 0x00;
	}

	for (i=0; i<512; i++) 
	{
			WritePage(0, Fat_Sector+Fat_Size+Fat_Size+i, Directory_Enties);

	}	
}

//函数实现
void Format_Fat12Media(void)
{	
	WriteMBR2Flash();
	printf("\nCreat BPB Succeed");
	CreatFAT12();
	printf("\nCreat FAT12 Succeed");
	CreatDirectoryEntry();
    	printf("\nCreat Directory Entry Succeed");

    	printf("\nFlash have been Fomarted");
}
//函数实现
void Format_Fat16Media(void)
{	
	WriteMBR2Flash();
	printf("\nCreat BPB Succeed");
	
	CreatFAT16();
	printf("\nCreat FAT16 Succeed");
	
	CreatDirectoryEntry16();
    	printf("\nCreat Directory Entry Succeed");

    	printf("\nFlash have been Fomarted");
}

void Format_Flash(void)
{
	unsigned int i;

	for(i=0;i<1000;i++)
		if(Erase_Block(i)==0)
			{
			printf("\nErase NG!");
			return;
			}
	printf("\n32M Flash Format Over");
		
}

⌨️ 快捷键说明

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