专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

uC/FS详解(开发文档)

作者:佚名   来源:本站原创   点击数:  更新时间:2014年08月18日   【字体:

1: uC/FS简介

   µC/FS is a FAT file system which can be used on any media, for which you can 
provide basic hardware access functions. . µC/FS is a high performance library that has been optimized for speed, versatility and memory footprint.
   文件配置表(英文:File Allocation Table,首字母缩略字:FAT)是一种由微软发明的并带有部分专

件系统。FAT文件系统考虑当时计算机效能有限,所以未被复杂化,因而被几乎所有个人计算机的操作系统支持。这特性使它成为理想的软盘和记忆卡文件系统,也适合用作不同操作系统中的数据交流。但FAT有一个严重的缺点:当文件被删除并且在同一位置被写入新数据,他们的片段通常是分散的,减慢了读写速度。磁盘碎片重整是一种解决方法,但必须经常重组来保持FAT文件系统的效率。 FAT文件系统遵行已用了多年的软件方法来进行规范。它在1977年由比尔·盖茨和马斯·麦当劳为了管理磁盘而发明。

2: uC/FS特点
µC/FS is written in ANSI C and can be used on virtually any CPU. Some 
features of µC/FS are: 
·  MS-DOS/MS-Windows compatible FAT12 and FAT16 support. 
·  Multiple device driver support. You can use different device drivers with µC/FS, which allows           you to access different types of hardware with the file system at the same time. 【在该文件系统中可同时支持多个设备驱动】
·  Multiple media support. A device driver does allow you to access different medias at the same            time. 
·  OS support. µC/FS can easily be integrated into any OS. In that way you can make file operations        in a multithreaded environment. 
·  ANSI C stdio.h like API for user applications. An application using standard C I/O library can             easily be ported to use µC/FS. 
·  Very simple device driver structure. µC/FS device drivers need only very basic functions for                 reading and writing blocks. Therefore it is very simple to support your custom hardware. 
·  Generic device driver for SmartMedia cards, which can easily be used with any kind of card                  reader hardware. 
·  Generic device driver for MultitMedia & SD cards using SPI mode, which can be easily integrated.
3: uC/FS结构
   µC/FS 由不同的分层构成。
 
上面的图是FATFS,但与UC/FS的是一样的

   1:API层【类似于linux的VFS层】
      The API layer is the interface between µC/FS and the user application. It does contain a library of ANSI C oriented file functions, such as  FS_FOpen, FS_FWrite etc. The API layer does transfer these calls to the file system layer. Currently there is only a FAT file system layer available for µC/FS, but the API layer can deal with different file system layers at the same time. So it is possible to use FAT and any other file system at the same time with µC/FS.  
    API层是µC/FS 提供给用户使用的接口层(fs_api.h),API层将各种调用传输到file system layer(文件系统层),目前对µC/FS(文件管理实现机制)来说 只有一个 FAT文件系统层被使用,API层可以处理不同的文件系统层,因此µC/FS可同时支持多种文件系统(API层类似于linux中的虚拟文件系统VFS,他的功能是提供给童用户标准的系统调用接口,VFS层下面才是具体实际的文件系统层,有FAT、EXT2等等,这个根据硬件的情况(格式化时使用什么文件系统)来调用具体的文件系统)。
   2:File System Layer 文件系统层【这层与linux是一样的,是具体的管理文件所用的文件系统】
   The file system layer translates file operations to logical block operations. After such a translation, the file system calls the logical block layer and specifies the corresponding device driver for a device.
   文件系统层将文件操作转换为逻辑块操作,之后,具体的文件系统调用逻辑块层函数并指定设备相应的驱动;【linux中,文件系统将各种系统调用转换为各种IO请求(系统请求即是对底层相应的扇区、柱面、等操作),IO请求被放到对应设备的IO请求队列中去,再由一个系统内核线程一直读取请求队列中的请求,没有请求时就睡眠,拿到请求就通过设备驱动实现具体的操作】
    uC_FS\FSL\fat\ 下面就是FAT文件系统的各个文件。
   3:Logical Block Layer 逻辑块
   the main purpose of the logical block layer is to synchronize accesses to a device driver and to have an easy interface for the file system layer.  The logical block layer does call a device driver to make a block operation. 
   逻辑块层的主要目的是同步访问设备驱动与文件系统的简易接口,逻辑块层调用设备驱动来实现设备的块操作。
   4: Device Driver Layer 设备驱动层
   Device drivers are low level routines that are used to access your hardware. 
The structure of the device driver is simple to allow easy integration of your own 
hardware.
  设备驱动是一些访问硬件的底层操作,µC/FS的设备驱动的架构很简单,不像linux架构非常复杂。
 
4: uC/FS的使用
   下面的流程是参考uC/FS用户手册上的例程来写的,可以参考。
   1:调用FS_Init()初始化文件系统,在使用uC/FS的任何功能函数之前须要首先调用该函数进行初始        化;
   2:进入MainTask()
           1:调用FS_IoCtl()【格式化:使硬件按指定的文件系统来管理文件】
              This call is used to format your RAM disk in order to be able to write data to                     it. Formatting your RAM disk should not cause any problem.
           2:调用应用程序中的函数_write_file()【非uc/fs的函数】
                 _write_file()中又依次调用: FS_FOpen(), FS_FWrite(),FS_FClose()
              FS_FOpen():在这个例程中FS_FOpen()创建了一个叫default.txt的文件,如果创建成功,函数                           将返回一个FS_FILE结构体的指针,如果出错将返回0
              FS_FWrite():文件创建成功之后,将一串字符串写入该文件中,如果写入时出错,例程中调                              FS_FError()处理错误。
              FS_FClose():关闭上面打开的文件,返回主任务中
    【大致过程就是如上过程】
     读: FS_FOpen()->FS_FRead()->FS_FClose()
   3:不使用uC/FS文件管理系统时调用FS_Exit().


5: uC/FS加入到工程中
   1:Create a simple project without µC/FS 
      We recommend, that you create a small “hello world” program for your system. That project should already use your OS and there should be a way to display text on a screen or serial port. 
   2: Add your µC/FS configuration
      In order to configure  µC/FS for your system, you should create a new sub-directory in µC/FS’s config directory and copy the files  fs_conf.h  and fs_port.h from one of the other sub-directories to your directory. For the following chapters, we assume that you have created a directory FS\CONFIG\myconfig. Usually, the only file  you have to modify is fs_conf.h. For an easy startup, we recommend, that you disable all drivers except the RAM disk driver. Please check out the chapter “Configuration of µC/FS” for detailed information about the configuration.  
   3Add µC/FS generic source code 
     Add all source files in the following directories:
        FS\API: µC/FS提供给用户的接口函数
        FS\FSL: 具体的文件系统层
        FS\LBL: 逻辑块层 
        FS\OS :
        FS\DEVICE\RAM 
     and their sub-directories to your project.  
   4Configure the search path[为开发环境添加所以代码的路径]
        FS\API 
        FS\CONFIG\myconfig 
        FS\LBL 
        FS\OS
   5Add generic sample code
       For a quick and easy test of your µC/FS integration, you should use the code 
       found in FS\sample\main.c.  
 
6: uC/FS配置
 1:fs_conf.h
     This is the main configuration file for the file system. You define which drivers you want to use and, the configurations for these drivers.
      【用户手册中列出了一个配置样本】,包括下面的各个配置项目
    1:OS support 
      You can specify whether you are using uC/OS-II, embOS, Windows or no OS support at all.  Please set  FS_OS_UCOS_II,  FS_OS_EMBOS, FS_OS_WINDOWS to 1, respectively. For no OS support at all, set all of them to 0. If you need support for an additional OS, you will have to provide functions described in the chapter “OS integration”. 
    2:Time/Date support 
      If you want to be able to add date and times to your files, you will need to set 
FS_OS_TIME_SUPPORT  to 1. 
    3:File System Layer Support
      µC/FS can support different file system at the same time. You can enable them by setting FS_USE_XXX_FSL, where XXX is the name of the file system layer. The current version of µC/FS only supports the FAT file system, so you will need to set FS_USE_FAT_FSL to 1.
   4:Device Driver Support and configuration
I      RAM Disk:     
      FS_USE_RAMDISK_DRIVER 
 to  1
       Windows Driver:    FS_USE_WINDRIVE_DRIVER to 1     
       Smart Media Card(SMCs): 
                          FS_USE_SMC_DRIVER  to 1
       MultiMedia card:
             µC/FS can support  MultiMedia & SD cards. You can enable the driver by setting FS_USE_MMC_DRIVER to 1. In order to use it, you will have to provide low-level I/O functions for your card reader hardware. Please take a look at the chapter “MultiMedia & SD card device driver” for details. 
      CompactFlash card & IDE:FS_USE_IDE_DRIVER to 1
 
 2:fs_port.h【一般是与CPU相关的数据类型】
      Usually this file only requires minor modifications, if you are using a very specific CPU. Please also check the type declarations in this file to ensure that they fit with your target processor and compiler. 

6: API函数说明
 
  FS_IoCtl:执行命令(SD卡等可以通过电脑格式化)
 
  x = FS_IoCtl("ram:",FS_CMD_FORMAT_MEDIA,FS_MEDIA_RAM_16KB,0);

  Directory functions
  int FS_CloseDir(FS_DIR *dirp);
  int FS_MkDir(const char *dirname);
  FS_DIR *FS_OpenDir(const char *dirname);
  struct FS_DIRENT *FS_ReadDir(FS_DIR *dirp);
  void FS_RewindDir(FS_DIR *dirp); 
  int FS_RmDir(const char *dirname); 

7: 设备驱动Device drivers
  µC/FS可以与任何种类的硬件配合工作;µC/FS需要配备与硬件设备对应的设备驱动程序,这些设备驱动程序包含 基本的访问硬件的I/O函数 以及 一张全局表,这张表中存放各个函数的指针。【联想到LINUX中系统调用的系统调用号】
  要使你的µC/FS支持特定的硬件设备,你必须写好该设备的驱动,本节告诉你需要实现哪些函数以及怎么样将驱动整合到µC/FS中去。
   【下面是ramdisk的设备驱动函数表的例子:设备驱动函数表中的各个函数,在PDF中已给出具体的代码,其他的设备,如MMC&SD也是类似的,需要提供这相关的几个函数,名字可以不同,在功能函数的次序要一样】
   

 
  In this chapter, you will find a detailed description of the device driver functions required by µC/FS. Please note that the names used for these functions are not really relevant for  µC/FS because the file system accesses those functions through a function table.
  因为是通过一张全局表来管理各个设备驱动函数,所以各设备驱动函数的名称与µC/FS没有太多的关联性。
  _FS_DevIoCtl():Execute special command on device.
     static int _FS_DevIoCtl(FS_u32 id, FS_i32 cmd, FS_i32 aux,void *buffer);  
                             Parameter  Meaning 
                             id         Number of media (0…N) 
                             cmd        Command 
                             aux        Parameter for command 
                             buffer     Pointer to data required by command
     该函数被用来完成一些设备驱动中特殊的命令,对uc/FS来说目前需要支持的这类特殊命令有:FS_CMD_FLUSH_CACHE,这个命令用来告诉设备的缓冲逻辑 所有的缓冲必须清零,如果你的设备没有缓冲逻辑,该函数不需要做任何事情。
  _FS_DevRead():Read block from media 
      static int _FS_DevRead(FS_u32 id, FS_u32 block, void *buffer); 
                             id      Number of media (0…N) 
                             block   Block number to be read from the media 
                             buffer  Data buffer to which the data is transferred
   The function should transfer 0x0200 (i.e. 512) bytes, which is the default value for an MS-DOS/MS-Windows compatible FAT file systems. µC/FS can support any block size but, if you use the FAT file system layer, you have to use this default block size. 
   函数应该传送0x0200(512)个字节,这个是兼容FAT文件系统的MS-DOS/MS-Windows系统的默认值,µC/FS能支持任何大小的块,但是如果你的文件系统层使用FAT,那么你必须使用这个默认的块大小。
  _FS_DevStatus():Return current status of your device. 
     static int _FS_DevStatus(FS_u32 id); id  Number of media (0…N) 
   The main purpose of this function is to detect a media change. All µC/FS file operation calls this function to check, if the device can be accessed. 
  该函数的主要目的是检测一个介质的改变。如果一个设备能够被访问,所有的µC/FS 文件操作都调用这个函数进行检测,
   The function returns 0 if the device can be accessed. If the media has changed (e.g. a card removed or replaced) and the device can be accessed, the return value has to be FS_LBL_MEDIACHANGED. Any value < 0 is interpreted as an error. 
  _FS_DevWrite():Write block to media 
      static int _FS_DevWrite(FS_u32 id, FS_u32 block,void *buffer);
                              id      Number of media (0…N) 
                              block   Block number to be written on media 
                              buffer  Pointer to data for transfer to the media. 
      The function should transfer 0x0200 (i.e. 512) bytes, which is the default value for an MS-DOS/MS-Windows compatible FAT file systems. µC/FS can support any block size but, if you use the FAT file system layer, you have to use this default block size. 

  Device driver function table
   To use a device driver with µC/FS, a global function table is required, which  
holds pointers to the device driver functions.  Each entry in the table contains 
 
five values as shown in the example below. 
     const FS__device_type FS__ramdevice_driver = { 
       "RAMDISK device", 
       _FS_DevStatus, 
       _FS_DevRead, 
       _FS_DevWrite, 
       _FS_DevIoCtl 
     }; 
    If you want to use your own device driver, you have to tell µC/FS, which device 
name you would like to use for your device and which  File System Layer 
(currently only FAT is supported) you want to use. 
    You do this by setting appropriate value for FS_DEVINFO in your FS_conf.h , 
which is used to initialize µC/FS’s global device information table.  
   如果你想要使用自己的设备驱动,你必须要告诉μC/FS,你的设备使用的是哪一个设备名称以及使用哪一个文件系统来管理(目前只支持FAT)。
   可以将 FS_conf.h中的FS_DEVINFO 设置一个合适的值来完成上面的动作,FS_DEVINFO被用来初始化μC/FS的全局设备信息表。
 【上面是ramdisk的设备驱动函数表的例子,_FS_DevStatus, _FS_DevRead, _FS_DevWrite, 
 _FS_DevIoCtl,就是ramdisk所需的底层驱动函数
  这里如果是MMC&SD存储设备,下面是源码。
#ifndef FS_DEVINFO
 #if FS_USE_SMC_DRIVER
   #define FS_DEVINFO_DEVSMC     
      {"smc",&FS__fat_functable,&FS__smcdevice_driver,FS_CACHEINFO_SMC_DRIVER 0 },
 #else
   #define FS_DEVINFO_DEVSMC
 #endif
 
 #if FS_USE_MMC_DRIVER
   #define FS_DEVINFO_DEVMMC     
     {"mmc", &FS__fat_functable,&FS__mmcdevice_driver, FS_CACHEINFO_MMC_DRIVER 0 },
 #else
   #define FS_DEVINFO_DEVMMC
 #endif
 ......
 #endif

从源码中可以看出,如果没有定义 
FS_DEVINFO宏,且如果没有定义
 FS_DEVINFO_DEVSMC,系统会自动以FS_DEVINFO_DEVSMC,即采用系统默认的设备驱动。
我们可以在fs_conf.h中配置   
FS_USE_MMC_DRIVER = 1;再定义 
#define FS_DEVINFO \ 
        "mmc",    &FS__fat_functable, &FS__mmcdevice_driver, 0,
FS_USE_MMC_DRIVER = 1,即使没有定义
 FS_DEVINFO,系统也会有一个默认的定义
FS__fat_functable:这个表不用填充,因为这里面的函数在API层已经实现,且系统已经设置好。用户只需更改FS__mmcdevice_driver里面的各个函数即可。
 
【这里支持多个设备"smc"和"mydev"
#define FS_DEVINFO \ 
        "smc",    &FS__fat_functable, &FS__smcdevice_driver, 0, \ 
        "mydev",  &FS__fat_functable, &FS__mydevice_driver , 0 
【如果不指定,系统将使用默认值】
   The first parameter is a device name, which you want to use for  µC/FS’s API 
calls.  
   The second parameter is a pointer to a  File System Layer function table; 
currently only FAT is supported.  
   The third parameter is a pointer to a Device Driver function table.  
   The last parameter is reserved for future use and should be zero. 
 
8: 各目录介绍 

在工程中添加上面的文件组:API层、FAT层、LBL层、DEV层、OS、CLIB
DEV包含的就是文件系统需要使用的最底层的操作硬件的接口,
这里使用文件系统标准的操作接口:_FS_XXX_Devxxx(....):在这几个函数中调用具体的SD卡的操作函数来实现与硬件驱动的链接。
CLIB中包含的是文件系统使用的一些C标准函数。
OS组中包含的文件fs_x_ucos_ii.c中包含的就是一些函数接口,这些接口是连接操作系统的接口(当然在裸机下也是这些接口),如果使用操作系统,这些函数接口中 就是与操作系统相关的代码,如果没有操作系统,这些接口就是很简单的一些与系统无关的代码,总之是文件系统是需要这些接口(有无操作系统接口是一样的);
各接口函数如下:
FS_X_OS_Init:创建一些文件系统运行时需要使用的一些信号量(用来访问资源)
FS_X_OS_Exit:删除上面创建的各信号量
其他的都是配对的Lock/Unlock,也就是申请获取一个对应的信号量,使用后再释放这个信号量。
就是这么简单。
 
9: MultiMedia & SD card device driver 
   µC/FS includes a generic driver for MultiMedia & SD cards. The driver accesses 
cards using SPI mode.
   µC/FS包含一个针对MMC/SD卡的通用驱动,该驱动通过SPI模式访问卡。
   To use the driver in your system, you will have to provide basic I/O functions for 
accessing your card reader hardware. You can find samples of these routines in 
the directory device\mmc_sd\hardware. 
   在你的系统中使用该驱动时,你必须要提供基本的能访问你的读卡器硬件的I/O函数。你可以在“device\mmc_sd\hardware”路径下找到例子。
   【在源代码的目录中官方提供了相关设备的通用底层驱动的例程】
   [下面蓝色的就是官方提供的MMC&SD卡的通用驱动,但是需要你提供基本的IO访问函数]
   [在device\xxx\hardware目录下,存放的就是对应的xxx设备的通用驱动,                smc_X_hw.c、smc_X_hw.h]
   FS_MMC_HW_X_BusyLedOff():Turns off busy LED of the card reade
       void FS_MMC_HW_X_BusyLedOff(FS_u32 id); 
       id:ID of card reader (0…N).
   FS_MMC_HW_X_BusyLedOn():Turns on busy LED of the card reader.
       void FS_MMC_HW_X_BusyLedOn (FS_u32 id); 
       id:ID of card reader (0…N). 
   FS_MMC_HW_X_ClockCard() 
   FS_MMC_HW_X_SetCS()
   FS_MMC_HW_X_AdjustFOP() 
   FS_MMC_HW_X_CheckOCR()
   FS_MMC_HW_X_GetFOP() 
   FS_MMC_HW_X_CheckWP() 
   FS_MMC_HW_X_DetectStatus()
   FS_MMC_HW_X_WaitBusy() 
   FS_MMC_HW_X_ReadByte()
   FS_MMC_HW_X_ReadByteNoSync()  
   FS_MMC_HW_X_ReadSingleBlock() 
   FS_MMC_HW_X_WriteByte() 
   FS_MMC_HW_X_WriteByteSingleBlock()
关闭窗口