FATFS

(一),什么是文件管理系统

答:数据在PC上是以文件的形式储存在磁盘中的,这些数据的形式一般为ASCII码或二进制形式。简单点说就是:管理磁盘上的文件的方法的代码!

如:我们写到SD卡上面的数据管理一下,更科学的方法来管理

http://elm-chan.org/fsw/ff/00index_e.html,官网介绍 Resources下面是源码

(二),我们在移植时主要是那些函数?

答:Device Control Interface(硬件接口函数)

  • disk_status - Get device status                                            
  • disk_initialize - Initialize device                                                   初始化函数                              
  • disk_read - Read sector(s)                                                          读
  • disk_write - Write sector(s)                                                         写
  • disk_ioctl - Control device dependent features
  • get_fattime - Get current time

(三),例程:

1,我们用的是0.09版本的

2,CC936.c中文字体库

①新建工作区间

	/* Register work area for each volume (Always succeeds regardless of disk status) */
	f_mount(0,&fs);  //(文件管理系统)注册一个工作区间,工作空间命名为0。

 那么工作区间的命名范围是什么呢?

答:

FRESULT f_mount (
	BYTE vol,		/* Logical drive number to be mounted/unmounted */
	FATFS *fs		/* Pointer to new file system object (NULL for unmount)*/
)
{
	FATFS *rfs;


	if (vol >= _VOLUMES)		/* Check if the drive number is valid */
		return FR_INVALID_DRIVE;

 vol就是f_mount的第一个形参(命名),当他的值大于等于 _VOLUMES,就会返回错误

#define _VOLUMES	1  //can define 9
/* Number of volumes (logical drives) to be used. */

 

Parameters(官网)

Drive
Logical drive number (0-9) to register/unregister the work area.
FileSystemObject
Pointer to the work area (file system object) to be registered.

 

 

②然后再工作区间里面新建文件

/* function disk_initialize() has been called in f_open */
	
/* Create new file on the drive 0 */
res = f_open(&fnew, "0:newfile.txt", FA_CREATE_ALWAYS | FA_WRITE );

 OPEN的属性:

1.怎么打开呢?

FA_CREATE_ALWAYS 总是以创建总是新建的形式打开这个文件

FA_WRITE:此文件只能写

2.看下open函数

FRESULT f_open (
  FIL* FileObject,       /* Pointer to the blank file object structure */   文件指针,把打开的文件关联到这个指针连
  const TCHAR* FileName, /* Pointer to the file neme */        文件名字
  BYTE ModeFlags         /* Mode flags */               属性     
);  

属性:

ValueDescription
FA_READ Specifies read access to the object. Data can be read from the file. Combine with FA_WRITE for read-write access.
FA_WRITE Specifies write access to the object. Data can be written to the file. Combine with FA_READ for read-write access.
FA_OPEN_EXISTING Opens the file. The function fails if the file is not existing. (Default)
FA_OPEN_ALWAYS Opens the file if it is existing. If not, a new file is created. To append data to the file, use f_lseek function after file open in this method.
FA_CREATE_NEW Creates a new file. The function fails with FR_EXIST if the file is existing.
FA_CREATE_ALWAYS Creates a new file. If the file is existing, it is truncated and overwritten.

可以第一次打开用:FA_CREATE_NEW,之后打开用:FA_OPEN_EXISTING(已存在)就可以了

或者:总是打开,总是关闭

当用到:FA_CREATE_ALWAYS时,我们要将  #define _FS_READONLY 0 /* 0:Read/Write or 1:Read only */    打开

3.对于返回值(Return Values):我们只测试:FR_OK(是否成功)

③往该文件内写数据

if ( res == FR_OK )
{
	res = f_write(&fnew, textFileBuffer, sizeof(textFileBuffer), &bw);
	f_close(&fnew);      写完之后,把该文件关掉
}

 ④关掉之后再打开它

res = f_open(&fnew, "0:newfile.txt", FA_OPEN_EXISTING | FA_READ); 	// FA_READ:此时打开定义可读

 ⑤读取数据

res = f_read(&fnew, buffer, sizeof(buffer), &br); 

 ⑥打印之后关闭

printf("\r\n %s ", buffer);
		
/* Close open files */
f_close(&fnew);

⑦在文件系统中注册掉刚才开辟的“0”工作区间

/* Unregister work area prior to discard it */
f_mount(0, NULL);

 (四)文件管理系统与SD卡的关联!

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。