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

📄 fattester.c

📁 stm32 红外万年历
💻 C
字号:
#include "fattester.h"	 
#include "mmc_sd.h"
#include "usmart.h"
#include "usart.h"
#include "exfuns.h"
#include "malloc.h"

#include "ff.h"


//挂载磁盘	   
u8 mf_mount(u8 drv)
{		   
	return f_mount(drv,&fs[drv]); 
}
//打开路径下的文件
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;
}
u8 mf_read(u16 len)
{
	u16 i,t;
	u8 res;
	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("Read Error:%d\r\n",res);   
		}else
		{
			tlen+=br;
			for(t=0;t<br;t++)printf("%c",fatbuf[t]); 
		}	 
	}
	if(tlen)printf("Readed data len:%d\r\n",tlen);//读到的数据长度
	printf("Read data over\r\n");	 
	return res;
}
//写入数据
u8 mf_write(u8*dat,u16 len)
{			    
	u8 res;	   					   

	printf("\r\nWriting file data.\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;
}

//打开文件夹
u8 mf_opendir(u8* path)
{
	return f_opendir(&dir,(const TCHAR*)path);	
}
//打读取文件夹
u8 mf_readdir(void)
{
	u8 res;
	char *fn;			 
#if _USE_LFN
 	fileinfo.lfsize = _MAX_LFN * 2 + 1;
	fileinfo.lfname = mymalloc(SRAMIN,fileinfo.lfsize);
#endif		  
	res=f_readdir(&dir,&fileinfo);//读取一个文件的信息
	if(res!=FR_OK||fileinfo.fname[0]==0)
	{
		myfree(SRAMIN,fileinfo.lfname);
		return res;//读完了.
	}
#if _USE_LFN
	fn=*fileinfo.lfname ? fileinfo.lfname : fileinfo.fname;
#else
	fn=fileinfo.fname;;
#endif	
	printf("\r\n DIR info:\r\n");

	printf("dir.id:%d\r\n",dir.id);
	printf("dir.index:%d\r\n",dir.index);
	printf("dir.sclust:%d\r\n",dir.sclust);
	printf("dir.clust:%d\r\n",dir.clust);
	printf("dir.sect:%d\r\n",dir.sect);	  

	printf("\r\n");
	printf("File Name is:%s\r\n",fn);
	printf("File Size is:%d\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");
	myfree(SRAMIN,fileinfo.lfname);
	return 0;
}			 

 //遍历文件
u8 mf_scan_files(u8 * path)
{
	FRESULT res;	  
    char *fn;   /* This function is assuming non-Unicode cfg. */
#if _USE_LFN
 	fileinfo.lfsize = _MAX_LFN * 2 + 1;
	fileinfo.lfname = mymalloc(SRAMIN,fileinfo.lfsize);
#endif		  

    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;             //忽略上级目录
#if _USE_LFN
        	fn = *fileinfo.lfname ? fileinfo.lfname : fileinfo.fname;
#else							   
        	fn = fileinfo.fname;
#endif	                                              /* It is a file. */
			printf("%s/", path);//打印路径	
			printf("%s\r\n",  fn);//打印文件名	
		} 
    }	  
	myfree(SRAMIN,fileinfo.lfname);
    return res;	  
}
//显示剩余容量
u32 mf_showfree(u8 *drv)
{
	FATFS *fs1;
	u8 res;
    u32 fre_clust, fre_sect, tot_sect;
    //得到磁盘信息及空闲簇数量
    res = f_getfree((const TCHAR*)drv, &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);
}
//读取文件当前读写指针的位置.
//返回值:位置
u8 mf_tell(void)
{
	return f_tell(&file);
}
//读取文件大小
//返回值:文件大小
u32 mf_size(void)
{
	return f_size(&file);
} 
//创建目录
u8 mf_mkdir(u8*name)
{
	return f_mkdir((const TCHAR *)name);
}
//删除文件/目录
u8 mf_unlink(u8 *name)
{
	return  f_unlink((const TCHAR *)name);
}
//修改文件/目录名字(如果目录不同,还可以移动文件哦!)
//oldname:之前的名字
//newname:新名字
u8 mf_rename(u8 *oldname,u8* newname)
{
	return  f_rename((const TCHAR *)oldname,(const TCHAR *)newname);
}

//从文件里面读取一段字符串
void mf_gets(u16 size)
{
	u16 i;
	TCHAR* rbuf;
	rbuf=f_gets((TCHAR*)fatbuf,size,&file);
	if(*rbuf==0)return  ;//没有数据读到
	else
	{
		printf("\r\nGet String Is:\r\n");
		for(i=0;i<size;i++)
		{
			printf("%02x ",fatbuf[i]);
		}
		printf("\r\n");
	}			    	
}
//需要_USE_STRFUNC>=1
//写一个字符到文件
u8 mf_putc(u8 c)
{
	return f_putc((TCHAR)c,&file);
}
//写字符串到文件
u8 mf_puts(u8*c)
{
	return f_puts((TCHAR*)c,&file);
}
//将nsrc文件,copy到ndst.
u8 mf_copy(u8 *nsrc,u8 *ndst)
{
	u8 res;
	res=f_open(&file,(const TCHAR*)nsrc,FA_READ|FA_OPEN_EXISTING);
 	res=f_open(&ftemp,(const TCHAR*)ndst,FA_WRITE|FA_CREATE_ALWAYS);
 	while(res==0)
	{
		res=f_read(&file,fatbuf,sizeof(fatbuf),&br);//源头读出512字节
		if(res||br==0)break;
		res=f_write(&ftemp,fatbuf,br,&bw);//写入目的文件
		if(res||bw<br)break;       
	}
    f_close(&file);
    f_close(&ftemp);
	return res;
} 












⌨️ 快捷键说明

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