#include "fattester.h" #include "sdio_sdcard.h" #include "usmart.h" #include "usart.h" #include "exfuns.h" #include "malloc.h" #include "ff.h" #include "string.h" ////////////////////////////////////////////////////////////////////////////////// //本程序只供学习使用,未经作者许可,不得用于其它任何用途 //ALIENTEK STM32开发板 //FATFS 测试代码 //正点原子@ALIENTEK //技术论坛:www.openedv.com //创建日期:2016/1/7 //版本:V1.0 //版权所有,盗版必究。 //Copyright(C) 广州市星翼电子科技有限公司 2014-2024 //All rights reserved ////////////////////////////////////////////////////////////////////////////////// //为磁盘注册工作区 //path:磁盘路径,比如"0:"、"1:" //mt:0,不立即注册(稍后注册);1,立即注册 //返回值:执行结果 u8 mf_mount(u8* path,u8 mt) { return f_mount(fs[1],(const TCHAR*)path,mt); } //打开路径下的文件 //path:路径+文件名 //mode:打开模式 //返回值:执行结果 u8 mf_open(u8*path,u8 mode) { u8 res; 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;in_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); }