写了一个fat16文件系统的格式化程序,但是写到闪存上后,总是识别不出来是个文件系统。
使用K9F2G08X0闪存,256M,2048块,每块64页,每页2048+64字节
请高手帮忙看看这个程序错误在哪里?
#ifndef __FAT_h__
#define __FAT_h__
#define Start_Head 0x0000 //起始磁头;
#define Start_Sector 0x0000 //起始扇区;
#define Start_Cylinder 0x0000 //起始柱面;
#define End_Head 0x0000 //结束磁头;
#define End_Sector 0x0000 //结束扇区;
#define End_Cylinder 0x0000 //结束柱面;
#define Fs_Type 0x0004 //文件系统类型,04为fat16
#define LBA_Begin_Address 0x0000003F //PBR的绝对起始扇区;
// #define Total_Sectors 2000 //文件系统管理的总扇区数;
#define Sector_Size 0x0800 //K9中每个Sector为2048;
#define Cluster_Sector 0x0040 //一个cluster含有64个Sector;
#define Reserve_Sector 0x0001 //保留扇区数,从DBR到FAT扇区数
#define FAT_Num 0x0002 //FAT个数
#define FAT_Sector 0x0002 //FAT表占用的Sector数;
#define ROOT_Entry 2048 //根目录项数;
#define DIR_Size 32 //目录项分配32个字节
#define Total_Sectors 0x0000 //分区总扇区数,小于32M时使用
#define BigTotal_Sectors 0x020000 //分区总扇区数,大于32M时使用
#define Hidden_Sectors 62 //隐藏扇区数, MBR在里面;
void Fat_Format ();
#endif
void Fat_Format ()
{
unsigned long int Init_Row = 0x00000000;
unsigned int Init_Col = 0x0000;
unsigned int buffer[512]; //1024个字节的缓冲区,61基本数据单元为16位,写的时候取低8位;
unsigned int zero_value[1] = {0x0000}; //写入0x0000值时用到的数组;
unsigned int reserved_clusters[4] = {0x00F8,0x00FF,0x00FF,0x00FF}; //FAT表头两个cluster number为保留区域 4字节;
unsigned int end_value[1] = {0xFFFF}; //文件结束标志;
int i, j, k;
// -- MASTER BOOT RECORD -- // 446 + 64 + 2 = 512 个字节;
K9_SeqWrite_Start(Init_Row, Init_Col); //起始地址; 准备开始往缓冲区里面写数据;
for(i = 0; i < 446; i++)//启动代码, 没有操作系统, 故全部设为0x00;
{
buffer = 0x0000;
}
//-- First Partition entry //占16个字节
buffer[446] = 0x0080; //启动标志
buffer[447] = Start_Head; //起始磁头
buffer[448] = Start_Sector; //起始扇区
buffer[449] = Start_Cylinder; //起始柱面(一般为0x00)
buffer[450] = Fs_Type; //文件类型为FAT16;
buffer[451] = End_Head; //结束磁头
buffer[452] = End_Sector; //结束扇区
buffer[453] = End_Cylinder; //结束柱面
buffer[454] = (LBA_Begin_Address & 0x000000FF); //取出低8位放在457的低8位 //LBA Begin占四个字节,
buffer[455] = (LBA_Begin_Address & 0x0000FF00)>>8; //取出8-15位放在456的低8位
buffer[456] = (LBA_Begin_Address & 0x00FF0000)>>16; //取次高8位放在455的低8位
buffer[457] = (LBA_Begin_Address & 0xFF000000)>>24; // 取出高8位放在454的低8位
//高位放后面
buffer[458] = (Total_Sectors & 0x000000FF); //文件系统管理的总扇区数
buffer[459] = (Total_Sectors & 0x0000FF00)>>8;
buffer[460] = (Total_Sectors & 0x00FF0000)>>16;
buffer[461] = (Total_Sectors & 0xFF000000)>>24;
// Unused Partition Tables //剩下的未用的三个分区表; 共48个字节;
for(j = 1; j <= 48; j++)
{
buffer[461 + j] = 0x0000;
}
//MBR结束标志;
buffer[510] = 0x0055;
buffer[511] = 0x00AA;
K9_Write_Data(buffer, 512);
//因为K9中一个Sector占2048个字节, 所以先把MBR所在Sector中除MBR外的空间初始化为0x00;
for(i = (Sector_Size - 512) ; i >= 1; i --)
{
K9_Write_Data(zero_value, 1);
}
add_log("MBR");
K9_SeqWrite_Execute(); //写满第一页
// -- Hidden Sectors --中其他的Sectors;
for( j=0; j
{
Init_Row++;
K9_SeqWrite_Start( Init_Row, Init_Col);
for( i=0; i
{
K9_Write_Data(zero_value, 1);
}
K9_SeqWrite_Execute(); //写满一页
}
// -- PARTITION BOOT RECORD--放在第0块最后一页 -- //占用512个字节;
//---------------------PBR的关键信息---------------------------//
buffer[0] = 0x00EB; //boot
buffer[1] = 0x003C;
buffer[2] = 0x0090;
buffer[3] = 0x0000; //OEM name
buffer[4] = 0x0000;
buffer[5] = 0x0000;
buffer[6] = 0x0000;
buffer[7] = 0x0000;
buffer[8] = 0x0000;
buffer[9] = 0x0000;
buffer[10] = 0x0000;
buffer[11] = Sector_Size & 0x00FF; //每扇区字节数
buffer[12] = (Sector_Size & 0xFF00)>>8;
buffer[13] = Cluster_Sector; //每族扇区数
buffer[14] = Reserve_Sector & 0x00FF; //保留扇区数
buffer[15] = (Reserve_Sector & 0xFF00)>>8;
buffer[16] = FAT_Num; //FAT个数
buffer[17] = ROOT_Entry & 0x00FF; //根目录项数 两个字节
buffer[18] = (ROOT_Entry & 0xFF00)>>8;
buffer[19] = Total_Sectors & 0x00FF; //分区总扇区数
buffer[20] = (Total_Sectors & 0xFF00) >>8;
buffer[21] = 0x00F8; //分区介质标志
buffer[22] = FAT_Sector & 0x00FF; //每个FAT占的扇区数
buffer[23] = (FAT_Sector & 0xFF00)>>8;
buffer[24] = 0x0000; //每道扇区数
buffer[25] = 0x0000;
buffer[26] = 0x0000; //磁头数
buffer[27] = 0x0000;
buffer[28] = Hidden_Sectors & 0x000000FF; //隐藏扇区数,占4个字节
buffer[29] = (Hidden_Sectors & 0x0000FF00)>>8;
buffer[30] = (Hidden_Sectors & 0x00FF0000)>>16;
buffer[31] = (Hidden_Sectors & 0xFF000000)>>24;
buffer[32] = BigTotal_Sectors & 0x000000FF; //分区总扇区数 大于32M时使用
buffer[33] = (BigTotal_Sectors & 0x0000FF00)>>8;
buffer[34] = (BigTotal_Sectors & 0x00FF0000)>>16;
buffer[35] = (BigTotal_Sectors & 0xFF000000)>>24;
buffer[36] = 0x0080;
buffer[37] = 0x0000; //保留
buffer[38] = 0x0029; //扩展引导标志
buffer[39] = 0x0000; //盘序列号 占4个字节
buffer[40] = 0x0000;
buffer[41] = 0x0000;
buffer[42] = 0x0000;
for( k=1; k<=19; k++) // BS_VolLab & BS_FilSysType
{
buffer[42+k] = 0x0000;
}
for (j = 1; j <= 448; j++) // Boot Code
{
buffer[62 + j - 1] = 0x0000;
}
buffer[510] = 0x0055; // 结束标志
buffer[511] = 0x00AA;
Init_Row++;
K9_SeqWrite_Start( Init_Row, Init_Col); //第0块第63页
K9_Write_Data(buffer, 512);
for(i = (Sector_Size - 512) ; i >= 1; i --)
{
K9_Write_Data(zero_value, 1);
}
add_log("PBR");
K9_SeqWrite_Execute(); //将K9页缓冲区中的数据实际写入K9的物理存储区中
//----------------FAT表1 放在第1块第0、1页--------------------//
Init_Row++;
K9_SeqWrite_Start( Init_Row, Init_Col);
// -- FAT 1 --//占用 2048 * 2B = 4K(2个Sectors)
K9_Write_Data(reserved_clusters, 4);//Reserved Clusters #0&1,4个字节
// K9_Write_Data(end_value, 1);
//Free clusters in first FAT sector from Cluster #2
for( i=(Sector_Size - 4); i>=1; i--)
{
K9_Write_Data(zero_value, 1);
}
K9_SeqWrite_Execute();
//Free culsters in other FAT sectors
for( j = FAT_Sector - 1; j >= 1; j--)//FAT_Sector为FAT表占用的Sector数目;
{
Init_Row++;
K9_SeqWrite_Start( Init_Row, Init_Col);
for( i = Sector_Size; i >= 1; i--)
{
K9_Write_Data(zero_value, 1);
}
K9_SeqWrite_Execute();
}
// -- FAT 2 --占用 2048 * 2B = 4K(2个Sectors)
//----------------FAT表2 放在第1块第2、3页,紧跟着FAT1--------------------//
Init_Row++;
K9_SeqWrite_Start( Init_Row, Init_Col);
K9_Write_Data(reserved_clusters, 4);
//Free clusters in first FAT sector from Cluster #2
for( i = (Sector_Size - 4); i >= 1; i--)
{
K9_Write_Data(zero_value, 1);
}
K9_SeqWrite_Execute();
//Free culsters in other FAT sectors
for( j = FAT_Sector - 1; j >= 1; j--)//FAT_Sector为FAT表占用的Sector数目;
{
Init_Row++;
K9_SeqWrite_Start( Init_Row, Init_Col);
for( i = Sector_Size; i >= 1; i--)
{
K9_Write_Data(zero_value, 1);
}
K9_SeqWrite_Execute();
}
// -- ROOT DIRECTORY ENTRIES --//为根目录区, 紧接着两个FAT表
for( j=0; j<32; j++) //占2048 * 32B = 64K(32个Sectors)
{
Init_Row++;
K9_SeqWrite_Start( Init_Row, Init_Col);
for( k=0; k<64; k++)
{
for( i=0; i
{
K9_Write_Data(zero_value, 1);
}
}
K9_SeqWrite_Execute(); //写满一页
}
}