FIL filtest;
extern u8 *fatbuf;
u8 buf_tmp[20];
int main(void)
{
u32 total,free;
u8 t=0;
u8 res=0;
UINT temp;
HAL_Init(); //初始化HAL库
Stm32_Clock_Init(RCC_PLL_MUL9); //设置时钟,72M
delay_init(72); //初始化延时函数
uart_init(115200); //初始化串口
LED_Init(); //初始化LED
SRAM_Init(); //初始化外部SRAM
my_mem_init(SRAMIN); //初始化内部内存池
exfuns_init(); //为fatfs相关变量申请内存
res=f_mount(fs[0],"0:",1); //挂载FLASH.
if(res==0X0D)//FLASH磁盘,FAT文件系统错误,重新格式化FLASH
{
printf("Flash Disk Formatting... !\r\n");
res=f_mkfs("0:",1,4096);//格式化FLASH,1,盘符;1,不需要引导区,8个扇区为1个簇
//如果norflash第一次使用,先f_mkfs,如果flash较小,则不创建分区表,mkfs的时候传入sfd参数即可
if(res==0)
{
f_setlabel((const TCHAR *)"0:ALIENTEK"); //设置Flash磁盘的名字为:ALIENTEK
printf("Flash Disk Format Finish !\n");
}else
{
printf("Flash Disk Format Error !\n");
}
delay_ms(1000);
}
memset(fatbuf, 'a', 512);
fatbuf[500] = 'D';
fatbuf[499] = 'D';
temp = f_open(&filtest,"0:/m5311.txt",FA_OPEN_ALWAYS|FA_WRITE|FA_READ);
printf("f_open temp= %d \r\n", temp);
f_write(&filtest, "Hello", 5, &temp);
printf("f_write1 temp= %d \r\n", temp);
f_write(&filtest, ",World!", 7, &temp);
printf("f_write2 temp= %d \r\n", temp);
f_write(&filtest, fatbuf, 500, &temp);
printf("f_write3 temp= %d \r\n", temp);
res = f_size(&filtest);
printf("f_size temp= %d \r\n", temp);
res = f_rewind(&filtest);
if (res == FR_OK)
{
printf("f_rewind FR_OK = %d \r\n", res);
}
else
{
printf("f_rewind failed res = %d \r\n", res);
}
res=f_read(&filtest,fatbuf,512,&temp);
printf("f_read temp= %d \r\n", temp);
if(res)
{
printf("Read Error:%d\r\n",res);
}
else
{
for(t=0;t<10;t++)
printf("%c",fatbuf[t]);
printf("%c",fatbuf[511]);
}
printf("\r\n");
res = f_close(&filtest);
printf("f_close = %d \r\n", res);
while(exf_getfree("0:",&total,&free)) //得到SD卡的总容量和剩余容量
{
printf("FATFS Error!\n");
delay_ms(200);
LED0=!LED0;//DS0闪烁
}
printf("FATFS OK!\r\n");
printf("Total Size:= %d KB \r\n", total);
printf("Free Size = %d KB \r\n", free);
printf("Total Size:= %d MB \r\n", total >> 10);
printf("Free Size = %d MB \r\n", free >> 10);
mf_scan_files("0:/");
printf("test end \r\n");
while(1)
{
t++;
delay_ms(200);
LED0=!LED0;
}
}
R0.12
//为磁盘注册工作区
//path:磁盘路径,比如"0:"、"1:"
//mt:0,不立即注册(稍后注册);1,立即注册
//返回值:执行结果
u8 mf_mount(u8* path,u8 mt)
{
return f_mount(fs[0],(const TCHAR*)path,mt);
}
//打开路径下的文件
//path:路径+文件名
//mode:打开模式
//返回值:执行结果
u8 mf_open(u8*path,u8 mode)
{
u8 res;
printf("mode=%#x\r\n",mode);
res=f_open(file,(const TCHAR*)path,mode);//打开文件夹
return res;
}
//关闭文件
//返回值:执行结果
u8 mf_close(void)
{
f_close(file);
return 0;
}
//读出数据
//len:读出的长度
//返回值:执行结果
u8 mf_read(u16 len)
{
u16 i,t;
u8 res=0;
u16 tlen=0;
printf("\r\nRead file data is:\r\n");
for(i=0;i<len/512;i++)
{
res=f_read(file,fatbuf,512,&br);
if(res)
{
printf("Read Error:%d\r\n",res);
break;
}else
{
tlen+=br;
for(t=0;t<br;t++)printf("%c",fatbuf[t]);
}
}
if(len%512)
{
res=f_read(file,fatbuf,len%512,&br);
if(res) //读数据出错了
{
printf("\r\nRead Error:%d\r\n",res);
}else
{
tlen+=br;
for(t=0;t<br;t++)printf("%c",fatbuf[t]);
}
}
if(tlen)printf("\r\nReaded data len:%d\r\n",tlen);//读到的数据长度
printf("Read data over\r\n");
return res;
}
//写入数据
//dat:数据缓存区
//len:写入长度
//返回值:执行结果
u8 mf_write(u8*dat,u16 len)
{
u8 res;
printf("\r\nBegin Write file...\r\n");
printf("Write data len:%d\r\n",len);
res=f_write(file,dat,len,&bw);
if(res)
{
printf("Write Error:%d\r\n",res);
}else printf("Writed data len:%d\r\n",bw);
printf("Write data over.\r\n");
return res;
}
//打开目录
//path:路径
//返回值:执行结果
u8 mf_opendir(u8* path)
{
return f_opendir(&dir,(const TCHAR*)path);
}
//关闭目录
//返回值:执行结果
u8 mf_closedir(void)
{
return f_closedir(&dir);
}
//打读取文件夹
//返回值:执行结果
u8 mf_readdir(void)
{
u8 res;
res=f_readdir(&dir,&fileinfo); //读取一个文件的信息
if(res!=FR_OK)return res; //出错了
printf("\r\n DIR info:\r\n");
printf("dir.dptr:%d\r\n",dir.dptr);
printf("dir.obj.id:%d\r\n",dir.obj.id);
printf("dir.obj.sclust:%d\r\n",dir.obj.sclust);
printf("dir.obj.objsize:%lld\r\n",dir.obj.objsize);
printf("dir.obj.c_ofs:%d\r\n",dir.obj.c_ofs);
printf("dir.clust:%d\r\n",dir.clust);
printf("dir.sect:%d\r\n",dir.sect);
printf("dir.blk_ofs:%d\r\n",dir.blk_ofs);
printf("\r\n");
printf("File Name is:%s\r\n",fileinfo.fname);
printf("File Size is:%lld\r\n",fileinfo.fsize);
printf("File data is:%d\r\n",fileinfo.fdate);
printf("File time is:%d\r\n",fileinfo.ftime);
printf("File Attr is:%d\r\n",fileinfo.fattrib);
printf("\r\n");
return 0;
}
//遍历文件
//path:路径
//返回值:执行结果
u8 mf_scan_files(u8 * path)
{
FRESULT res;
res = f_opendir(&dir,(const TCHAR*)path); //打开一个目录
if (res == FR_OK)
{
printf("\r\n");
while(1)
{
res = f_readdir(&dir, &fileinfo); //读取目录下的一个文件
if (res != FR_OK || fileinfo.fname[0] == 0) break; //错误了/到末尾了,退出
//if (fileinfo.fname[0] == '.') continue; //忽略上级目录
printf("%s/", path);//打印路径
printf("%s\r\n",fileinfo.fname);//打印文件名
}
}
return res;
}
//显示剩余容量
//drv:盘符
//返回值:剩余容量(字节)
u32 mf_showfree(u8 *drv)
{
FATFS *fs1;
u8 res;
u32 fre_clust=0, fre_sect=0, tot_sect=0;
//得到磁盘信息及空闲簇数量
res = f_getfree((const TCHAR*)drv,(DWORD*)&fre_clust, &fs1);
if(res==0)
{
tot_sect = (fs1->n_fatent - 2) * fs1->csize;//得到总扇区数
fre_sect = fre_clust * fs1->csize; //得到空闲扇区数
#if _MAX_SS!=512
tot_sect*=fs1->ssize/512;
fre_sect*=fs1->ssize/512;
#endif
if(tot_sect<20480)//总容量小于10M
{
/* Print free space in unit of KB (assuming 512 bytes/sector) */
printf("\r\n磁盘总容量:%d KB\r\n"
"可用空间:%d KB\r\n",
tot_sect>>1,fre_sect>>1);
}else
{
/* Print free space in unit of KB (assuming 512 bytes/sector) */
printf("\r\n磁盘总容量:%d MB\r\n"
"可用空间:%d MB\r\n",
tot_sect>>11,fre_sect>>11);
}
}
return fre_sect;
}
//文件读写指针偏移
//offset:相对首地址的偏移量
//返回值:执行结果.
u8 mf_lseek(u32 offset)
{
return f_lseek(file,offset);
}
//读取文件当前读写指针的位置.
//返回值:位置
u32 mf_tell(void)
{
return f_tell(file);
}
//读取文件大小
//返回值:文件大小
u32 mf_size(void)
{
return f_size(file);
}
//创建目录
//pname:目录路径+名字
//返回值:执行结果
u8 mf_mkdir(u8*pname)
{
return f_mkdir((const TCHAR *)pname);
}
//格式化
//path:磁盘路径,比如"0:"、"1:"
//mode:模式
//au:簇大小
//返回值:执行结果
u8 mf_fmkfs(u8* path,u8 mode,u16 au)
{
return f_mkfs((const TCHAR*)path,mode,au);//格式化,drv:盘符;mode:模式;au:簇大小
}
//删除文件/目录
//pname:文件/目录路径+名字
//返回值:执行结果
u8 mf_unlink(u8 *pname)
{
return f_unlink((const TCHAR *)pname);
}
//修改文件/目录名字(如果目录不同,还可以移动文件哦!)
//oldname:之前的名字
//newname:新名字
//返回值:执行结果
u8 mf_rename(u8 *oldname,u8* newname)
{
return f_rename((const TCHAR *)oldname,(const TCHAR *)newname);
}
//获取盘符(磁盘名字)
//path:磁盘路径,比如"0:"、"1:"
void mf_getlabel(u8 *path)
{
u8 buf[20];
u32 sn=0;
u8 res;
res=f_getlabel ((const TCHAR *)path,(TCHAR *)buf,(DWORD*)&sn);
if(res==FR_OK)
{
printf("\r\n磁盘%s 的盘符为:%s\r\n",path,buf);
printf("磁盘%s 的序列号:%X\r\n\r\n",path,sn);
}else printf("\r\n获取失败,错误码:%X\r\n",res);
}
//设置盘符(磁盘名字),最长11个字符!!,支持数字和大写字母组合以及汉字等
//path:磁盘号+名字,比如"0:ALIENTEK"、"1:OPENEDV"
void mf_setlabel(u8 *path)
{
u8 res;
res=f_setlabel ((const TCHAR *)path);
if(res==FR_OK)
{
printf("\r\n磁盘盘符设置成功:%s\r\n",path);
}else printf("\r\n磁盘盘符设置失败,错误码:%X\r\n",res);
}
//从文件里面读取一段字符串
//size:要读取的长度
void mf_gets(u16 size)
{
TCHAR* rbuf;
rbuf=f_gets((TCHAR*)fatbuf,size,file);
if(*rbuf==0)return ;//没有数据读到
else
{
printf("\r\nThe String Readed Is:%s\r\n",rbuf);
}
}
//需要_USE_STRFUNC>=1
//写一个字符到文件
//c:要写入的字符
//返回值:执行结果
u8 mf_putc(u8 c)
{
return f_putc((TCHAR)c,file);
}
//写字符串到文件
//c:要写入的字符串
//返回值:写入的字符串长度
u8 mf_puts(u8*c)
{
return f_puts((TCHAR*)c,file);
}
//得到磁盘剩余容量
//drv:磁盘编号("0:"/"1:")
//total:总容量 (单位KB)
//free:剩余容量 (单位KB)
//返回值:0,正常.其他,错误代码
u8 exf_getfree(u8 *drv,u32 *total,u32 *free)
{
FATFS *fs1;
u8 res;
u32 fre_clust=0, fre_sect=0, tot_sect=0;
//得到磁盘信息及空闲簇数量
res =(u32)f_getfree((const TCHAR*)drv, (DWORD*)&fre_clust, &fs1);
if(res==0)
{
tot_sect=(fs1->n_fatent-2)*fs1->csize; //得到总扇区数
fre_sect=fre_clust*fs1->csize; //得到空闲扇区数
#if _MAX_SS!=512 //扇区大小不是512字节,则转换为512字节
tot_sect*=fs1->ssize/512;
fre_sect*=fs1->ssize/512;
#endif
*total=tot_sect>>1; //单位为KB
*free=fre_sect>>1; //单位为KB
}
return res;
}
fatfs 应用测试程序
于 2022-09-03 20:42:04 首次发布