现在的位置: 首页 > 综合 > 正文

EFSL文件系统移植记录

2012年04月07日 ⁄ 综合 ⁄ 共 2572字 ⁄ 字号 评论关闭

       在使用SD卡的过程中用到了文件系统,这里选择了efsl,并做了相应的移植与验证。

       efsl全称为embeded file system library,是一个开源的SD卡文件系统,所占内存空间少,移植起来比较方便,适合中小容量的单片机。efsl兼容fat12/16/32,同时支持多设备及多文件操作。每个设备的驱动程序,只需要提供扇区写和扇区读两个函数即可。

       efsl的结构是

       其具有的直接分层结构使其看起来非常简单。当执行一个文件File操作时,其调用文件系统Filesystem,文件系统调用分区对象Partition已完成为文件分配一块儿相对地址,再通过Disc对象将分区表与缓存管理器IOMan联系在一起,其就可以通过接口对底层的sector进行操作。我们需要做的就是完成文件系统与底层接口的对接。

       硬件底层包括三个基本功能:1、初始化硬件。 2、从SD卡读一个sector(512bytes)。 3、向SD卡写一个sector(512bytes)。只要实现这几个功能,移植efsl剩下的工作将会非常简单。

      一,下载源码,加入工程

             在http://efsl.be/上下载efsl下载源码,(不知道现在为何不能下载),在为了管理方便在工程中建立efsl_inc以及efsl_src两个文件夹,将相应的文件添加进去,例如:

     

      二,在config.h中添加#define HW_ENDPOINT_STM32F103RB_SD,表示添加与自己硬件相关的宏定义。

      三,根据单片机的类型修改数据类型。在inc/types.h中加入新的定义

         

     四,在inc/interface.h加入SD卡驱动接口函数声明文件,

      #if defined(HW_ENDPOINT_STM32F103RB_SD)
      #include "interfaces/STM32F103RB.h"

      #endif

    里面包含基本的数据结构和硬件底层三个基本函数的声明:

    struct  hwInterface{
    /*FILE  *imageFile;*/
    eint32   sectorCount;
    };
    typedef struct hwInterface hwInterface;

    extern esint8 if_initInterface(hwInterface* file,eint8* opts);
    extern esint8 if_readBuf(hwInterface* file,euint32 address,euint8* buf);
    extern esint8 if_writeBuf(hwInterface* file,euint32 address,euint8* buf);
    extern esint8 if_setPos(hwInterface* file,euint32 address);

   使用不同单片机时可以将这段程序拷贝过去,更改.h和.c文件名即可,这样就可以调用到STM32F103RB.c这个文件了。

     五,修改在STM32F103RB.h中声明函数的具体实现:

   /*****************************************************************************/

esint8 if_initInterface(hwInterface* file, eint8* opts)
  {
   euint8 temp = 0;

 temp = SD_Init();//initialize the sd card
 file->sectorCount=4;
 if(temp == 0)
   return(0);
 else
   return(-1);
}
/*****************************************************************************/

esint8 if_readBuf(hwInterface* file,euint32 address,euint8* buf)
{
 return (SD_ReadSingleBlock(address, buf));
}
/*****************************************************************************/

esint8 if_writeBuf(hwInterface* file,euint32 address,euint8* buf)
{
 return (SD_WriteSingleBlock(address, buf));
}
/*****************************************************************************/

esint8 if_setPos(hwInterface* file,euint32 address)
{
 return(0);
}
/*****************************************************************************/

六,配置efsl

       efsl默认的设置就可以正常完成所有工作,将SD卡在PC机上格式化为FAT32、FAT16或FAT12后,efsl都可以自动识别,不需要额外的操作。

另外主要各个文件的包含路径是否正确,设置正确后才可以编译通过。

七,使用

       使用时需要定义数据变量、初始化文件系统后就可以读写文件了。

       EmbeddedFileSystem efs;
       EmbeddedFile file;
       eint8 filename[15];

       if(efs_init(&efs,0)!= 0) {

       return -1;

       }

      file_fopen(&file,&efs.myFs,filename,'w'));

      file_read(&file,3000,SdReadBuffer);

 

 

    

    

     

抱歉!评论已关闭.