📄 file.c
字号:
#include <string.h>
#include "..\inc\option.h"
#include "..\inc\def.h"
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\flash.h"
#include "..\inc\download.h"
#include "..\inc\usb.h"
#include "..\inc\file.h"
/*Download and format is OK!*/
/***************************************************************/
/* FAT表说明 */
/*(1)KM29U128T 共有1024个Block/Cluster,1Block=32Page */
/* 或 1Cluster=32Sector */
/*(2)1Page/Sector=512B,1Block/Cluster=16KB */
/* 因此共有空间1024*16KB=16MB */
/*(3)RootDir信息占用Block0,用户数据存放于Block1~1023 */
/* 可用空间共为1023*32=32736Sector=15.98MB */
/*(4)FAT信息存放在每个Block的首页(Page)的独立区,该区共有16Bytes*/
/* 这16Bytes的含义如下: */
/* Offset[0]->该Block的使用情况 */
/* 0xFF :unused Block */
/* 0x00 :bad Block */
/* 0x01~0xFE:used Block */
/* Offset[ 1~ 7]->保留字节(对其中的数据无任何要求) */
/* Offset[ 8~11]->Pre Block(MSB) */
/* 若当前Block是FirstBlock,则该值为其本身 */
/* Offset[12~15]->Next Block(MSB) */
/* 若当前块为LastBlock,则该值为0xFFFF */
/***************************************************************/
/* Root Directory 记录格式 */
/* Offset[ 0~10]->file name */
/* Offset[11~25]->file attribute */
/* Offset[26~27]->file starting cluster(LSB) */
/* Offset[28~31]->file size in byte(LSB) */
/*暂存Root Directory数据,每条记录为32Byte,因此共可有512条记录*/
U8 root_buf[512][32];
U32 pre_block,current_block;
U8 Block_1Page_Buf[528]; //Block首页Buffer
U8 Page_Buf[528];
/*void disp_root(void)
{
U32 s1,s2;
for(s1=0;s1<16;s1++)
{
Uart_Printf("************************************\n");
for(s2=0;s2<16;s2++)
{
Uart_Printf("%2xh ",root_buf[s1][s2]);
}
Uart_Printf("\n");
for(s2=16;s2<32;s2++)
{
Uart_Printf("%2xh ",root_buf[s1][s2]);
}
Uart_Printf("\n");
}
Uart_Printf("++++++++++++++++++++++++++++++++++++\n");
Uart_Printf("++++++++++++++++++++++++++++++++++++\n");
}*/
/*************************************************/
/*寻找介于StartBlock与EndBlock之间的第一个BlankBlock*/
/*************************************************/
U32 seek_blank_block(U32 StartBlock)
{
U32 Block_Number;
Block_Number=StartBlock;
ReadPage(Block_Number,0,Block_1Page_Buf);
while(Block_1Page_Buf[512]!=UNUSED_MARK)
ReadPage(++Block_Number,0,Block_1Page_Buf);
return(Block_Number);
}
/******************************************************/
/*将Root Directory数据读入root_buf[],并在root_buf中修改之*/
/******************************************************/
/*调用方式:*/
void creat_file(U8 *file_information)
{
U32 page_num,Pointer,i;
for(page_num=0;page_num<32;page_num++)
{
/*将Root information读到root_buf中*/
ReadPage(Root_Cluster,page_num,Page_Buf);
for(Pointer=0;Pointer<512;Pointer++)
{
root_buf[page_num*16+Pointer/32][Pointer%32]=Page_Buf[Pointer];
}
}
/*在root_buf寻找空表项*/
Pointer=0;
while(root_buf[Pointer][0]!=0x00)
{
Pointer++;
}
/*copy the file name and file size*/
for(i=0;i<32;i++)
root_buf[Pointer][i]= *file_information++;
current_block=seek_blank_block(Start_Cluster);
pre_block=current_block;
root_buf[Pointer][26]=(U8)(current_block%256); //store LSB
root_buf[Pointer][27]=(U8)(current_block/256); //store MSB
// disp_root();
}
/********************************************************/
/*将root_buf中的数据写回Root Directory所处的Block(即Block0)*/
/*********************************************************/
void close_file(void)
{
U32 page_num,Pointer;
/*Erase Root_information Cluster*/
Erase_Cluster(0);
/*将root_buf中的数据逐页写入*/
for(page_num=0;page_num<32;page_num++)
{
for(Pointer=0;Pointer<512;Pointer++)
{
Page_Buf[Pointer]=root_buf[page_num*16+Pointer/32][Pointer%32];
}
/*独立区以0xFF填充*/
for(Pointer=512;Pointer<(512+16);Pointer++)
{
Page_Buf[Pointer]=0xFF;
}
WritePage(Root_Cluster,page_num,&(Page_Buf[0]));
}
}
/*******************/
/*将FlashDisk格式化*/
/******************/
#if 0
void format_disk(void)
{
U32 Block_Number,page_num,Pointer;
/*擦除数据区*/
for(Block_Number=Start_Cluster;Block_Number<=End_Cluster;Block_Number++)
{
if(Erase_Cluster(Block_Number)==FAIL)
{
/*Mark this Block with BAD flag*/
Page_Buf[512]=0x00;
WritePage(Block_Number,0,&Page_Buf[0]);
}
}
//将root_buf中全部写0x00//
for(page_num=0;page_num<32;page_num++)
{
for(Pointer=0;Pointer<512;Pointer++)
{
Page_Buf[Pointer]=0;
}
//独立区以0xFF填充//
for(Pointer=512;Pointer<(512+16);Pointer++)
{
Page_Buf[Pointer]=0xFF;
}
WritePage(Root_Cluster,page_num,&Page_Buf[0]);
}
}
#endif
/******************************/
/*将rx_buf中的数据写到1个Block中*/
/******************************/
void write_file(U8 endflag,U8 *block_buf)
{
U32 next_block,page_num,Pointer;
//Uart_Printf("\nWrite data,Current block is:%d",current_block);
if(endflag) //若为文件的最后一个Block,则LastBlock=0xFFFF
next_block=LAST_BLOCK;
else
next_block=seek_blank_block(current_block+1);
//write USED mark//
Page_Buf[512+0]=0x01;
//write pre_block//
Page_Buf[512+8]=0x00;
Page_Buf[512+9]=0x00;
Page_Buf[512+10]=pre_block/256;
Page_Buf[512+11]=pre_block%256;
//write next_block//
Page_Buf[512+12]=0x00;
Page_Buf[512+13]=0x00;
Page_Buf[512+14]=next_block/256;
Page_Buf[512+15]=next_block%256;
for(page_num=0;page_num<32;page_num++)
{
for(Pointer=0;Pointer<512;Pointer++)
{
Page_Buf[Pointer]=*block_buf;
*block_buf=0xff;
block_buf++;
}
WritePage(current_block,page_num,Page_Buf);
}
pre_block=current_block;
current_block=next_block;
}
void read_file(U8 *block_buf)
{
U32 page_num,Pointer;
//Uart_Printf("\nRead data,Current block is:%d",current_block);
for(page_num=0;page_num<32;page_num++)
{
ReadPage(current_block,page_num,Page_Buf);
for(Pointer=0;Pointer<512;Pointer++)
{
*block_buf=Page_Buf[Pointer];
block_buf++;
}
}
current_block=Page_Buf[512+14]*256+Page_Buf[512+15];
}
U32 comp_filename(U8 *s1,U8 *s2)
{
U32 diff_num=0;
U32 name_pointer;
for(name_pointer=0;name_pointer<11;name_pointer++)
{
if(s1[name_pointer]!=s2[name_pointer])
diff_num++;
}
return (diff_num);
}
//返回值为该文件在Root information区中的位置,即第几条记录//
U32 find_file(U8 *file_name)
{
U32 page_num,Pointer;
//read the root information//
for(page_num=0;page_num<32;page_num++)
{
//将Root information读到root_buf中//
ReadPage(Root_Cluster,page_num,Page_Buf);
for(Pointer=0;Pointer<512;Pointer++)
{
root_buf[page_num*16+Pointer/32][Pointer%32]=Page_Buf[Pointer];
}
}
for(Pointer=0;Pointer<512;Pointer++)
{
if(comp_filename(&root_buf[Pointer][0],file_name)==0)
break;
}
return(Pointer);
}
#if 0
void file_rename(U8 *old_new_filename)
{
U32 Pointer,i;
//Pointer=find_file(old_new_filename);
//for(i=0;i<11;i++)
// root_buf[Pointer][i]=old_new_filename[11+i];
//close_file();
}
void del_file(U8 *filename)
{
U32 root_location,i;
// get the location of this file via look for its file name in Root Directory //
root_location=find_file(filename);
//get the file first cluster//
current_block=root_buf[root_location][27]*256+root_buf[root_location][26];
//flush this file's recoder in root information//
for(i=0;i<32;i++)
root_buf[root_location][i]=0;
while(1)
{
ReadPage(current_block,0,Page_Buf);
Erase_Cluster(current_block);
//get the next block number//
current_block=Page_Buf[512+14]*256+Page_Buf[512+15];
if(current_block==65535)
break;
}
close_file();
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -