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

📄 fat16.c

📁 基于MCU的U盘源码
💻 C
字号:
/////////////////////////////////////////////////////////////////
//
// Copyright (c) 2004 
// All rights reserved
// 
// Author	:	frank_wang     frank_wang@263.net
// Created	:	JUN 23th 2004
// Modified	:
// Revision	:	ARM7+D12,
// Usage	:	 USB DISK ,SCSI-2
/////////////////////////////////////////////////////////////////

//#include "def.h"

#include "Fat16.h"
#include "Flash.h"
//数据结构与全局变量定义

U8 BPB_Data[512]=
{
0xeb,0x3c,0x90,//	Offset_U8_BS_jmpBoot		0
'F','r','a','n','k','w','x','j',// Offset_U8_BS_OEMName		3
0x00,0x02,// Offset_U16_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.//
64,// 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.                                //
0x1,0x00,// Offset_U16_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.             //
0xb0,0x03,// Offset_U16_BPB_RootEntCnt	17	//Is only useful for FAT12 and FAT16 volumes.                 //
//										//For FAT32 volumes,this field must be set to 0.              //
//通过调整文件项为03b0,从而使数据扇区刚好从Block2Sector0开始,但
//文件分区表中根目录区并为标记为已占用。数据实际从Block4Sector0开始
0xea,0x7f,// Offset_U16_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).                                     //
0xf8,// Offset_U8_BPB_Media			21	//0xF8 is the standard value for fixed(non-removable) media.  //
										//For removable media, 0xF0 is frequently used.               //
0x02,0x00,// Offset_U16_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.                                //
0x00,0x00,// Offset_U16_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.                         //
0x00,0x00,// Offset_U16_BPB_NumHeads		26	//Number of heads for interrupt 0x13. This field is relevant  //
										//as discussed earlier for BPB_SecPerTrk.                     //
0x01,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
0x06,//is partion used
0x00,//end head
0x00,//partion end 
0x00,//end cylinder
0x00,0x00,0x00,0x00,
0x00,0x80,0x00,0x00,//fist partion

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_U16_Signature		510	//0x55AA           

};

/*
//from 	e Carry
0xeb,0x3c,0x90,0x4d,0x53,0x44,0x4f,0x53,0x35,0x2e,0x30,0x00,0x02,0x08,0x01,0x00,
0x02,0x00,0x02,0x00,0x7d,0xf8,0x0c,0x00,0x3f,0x00,0xff,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x29,0xfa,0x9f,0x92,0x90,0x4e,0x4f,0x20,0x4e,0x41,
0x4d,0x45,0x20,0x20,0x20,0x20,0x46,0x41,0x54,0x31,0x36,0x20,0x20,0x20,

//executable code
0x33,0xc9,
0x8e,0xd1,0xbc,0xf0,0x7b,0x8e,0xd9,0xb8,0x00,0x20,0x8e,0xc0,0xfc,0xbd,0x00,0x7c,
0x38,0x4e,0x24,0x7d,0x24,0x8b,0xc1,0x99,0xe8,0x3c,0x01,0x72,0x1c,0x83,0xeb,0x3a,
0x66,0xa1,0x1c,0x7c,0x26,0x66,0x3b,0x07,0x26,0x8a,0x57,0xfc,0x75,0x06,0x80,0xca,
0x02,0x88,0x56,0x02,0x80,0xc3,0x10,0x73,0xeb,0x33,0xc9,0x8a,0x46,0x10,0x98,0xf7,
0x66,0x16,0x03,0x46,0x1c,0x13,0x56,0x1e,0x03,0x46,0x0e,0x13,0xd1,0x8b,0x76,0x11,
0x60,0x89,0x46,0xfc,0x89,0x56,0xfe,0xb8,0x20,0x00,0xf7,0xe6,0x8b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,0x02,0x0e,0x75,0x04,0xb4,0x42,
0x8b,0xf4,0x8a,0x56,0x24,0xcd,0x13,0x61,0x61,0x72,0x0b,0x40,0x75,0x01,0x42,0x03,
0x5e,0x0b,0x49,0x75,0x06,0xf8,0xc3,0x41,0xbb,0x00,0x00,0x60,0x66,0x6a,0x00,0xeb,
0xb0,0x4e,0x54,0x4c,0x44,0x52,0x20,0x20,0x20,0x20,0x20,0x20,0x0d,0x0a,0x4e,0x54,
0x4c,0x44,0x52,0x20,0x69,0x73,0x20,0x6d,0x69,0x73,0x73,0x69,0x6e,0x67,0xff,0x0d,
0x0a,0x44,0x69,0x73,0x6b,0x20,0x65,0x72,0x72,0x6f,0x72,0xff,0x0d,0x0a,0x50,0x72,
0x65,0x73,0x73,0x20,0x61,0x6e,0x79,0x20,0x6b,0x65,0x79,0x20,0x74,0x6f,0x20,0x72,
0x65,0x73,0x74,0x61,0x72,0x74,0x0d,0x0a,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xac,0xbf,0xcc,

*/
void WriteMBR2Flash(void)
{
	if(Check_Flash_Id()==KM29U128_ID){
	WritePage(Root_Cluster, BPB_Sector, BPB_Data);
	}
	else Uart_Printf("\nKM29U128 is Not Found!");
}

void CreatFAT16(void)
{
	U16 i;
	static U8 FAT[528];
	for(i=0;i<528;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;
	
	if(Check_Flash_Id()==KM29U128_ID){
		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
		}
	else Uart_Printf("\nKM29U128 is Not Found!");
}

void CreatDirectoryEntry(void)
{
	static U8 Directory_Enties[528];
	U32 i;
	for(i=0;i<528;i++)  Directory_Enties[i]= 0x00;

	if(Check_Flash_Id()==KM29U128_ID){
		for(i=0;i<0x35;i++) {
			WritePage(Root_Cluster,Directory_Sector+i,Directory_Enties);
			Delay(100);
			}
		}	
	else Uart_Printf("\nKM29U128 is Not Found!");
	
}


//函数实现
void Format_Fat16Media(void)
{	
	Flash_Reset();
	Erase_Cluster(Root_Cluster);
	WriteMBR2Flash();
	CreatFAT16();
	CreatDirectoryEntry();
	}

void Write2Flash(unsigned int block,unsigned int StSector,unsigned int EnSector,unsigned char *ClusterBuf)

{
	unsigned int i;
	for(i=0;i<StSector;i++) 
		{ReadPage(block,i,ClusterBuf+i*528);	//前前面的扇区读出
		}
	for(i=EnSector+1;i<32;i++) 
		{ReadPage(block,i,ClusterBuf+i*528);	//前前面的扇区读出
		}
	Erase_Cluster(block);
	for(i=0;i<32;i++) 
		{
//		Uart_Printf("W:%x",ClusterBuf[i*528]);
		WritePage(block,i,ClusterBuf+i*528);	//前前面的扇区读出
//		ReadPage(block,i,BPB_Data);
//		Uart_Printf("R:%x",BPB_Data[0]);
		
		}
}


⌨️ 快捷键说明

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