玩转X-CTR100 l STM32F4 l SD卡FatFs文件系统

我造轮子,你造车,创客一起造起来!塔克创新资讯【塔克社区 www.xtark.cn 】【塔克博客 www.cnblogs.com/xtark/

X-CTR100控制器具有SD卡接口,本教程使用免费开源FatFs文件系统,实现对SD卡上文件的操作。

SD卡对于X-CTR100控制器,相当于硬盘对于电脑,可以实现大文件存储,可以进行长时间数据采集。

原理

SD卡(Secure Digital Memory Card)在我们生活中已经非常普遍了,控制器对SD卡进行读写通信操作一般有两种通信接口可选,一种是SPI 接口,另外一种就是SDIO 接口,X-CTR100控制器通过SPI接口与SD卡连接。SD卡很少直接对数据进行读写,SD卡一般用来存放文件,所以都需要加载文件系统到里面。常见的windows 下的文件系统格式包括FAT32、NTFS、exFAT等。

FatFs是面向小型嵌入式系统的一种通用的FAT文件系统。它完全是由AISI C 语言编写并且完全独立于底层的I/O介质。因此它可以很容易地不加修改地移植到其他的处理器当中,如8051、PIC、AVR、SH、Z80、H8、ARM等。FatFs支持FAT12、FAT16、FAT32 等格式,把FatFs 文件系统代码移植到工程之中,就可以利用文件系统的各种接口函数,对SD卡上的文件进行读写操作了。

FatFs 文件系统的源码可以从官网下载: http://elm-chan.org/fsw/ff/00index_e.html

FatFs函数接口

文件操作

f_open - Open/Create a file

f_close - Close an open file

f_read - Read data from the file

f_write - Write data to the file

f_lseek - Move read/write pointer, Expand size

f_truncate - Truncate file size

f_sync - Flush cached data

f_forward - Forward data to the stream

f_expand - Allocate a contiguous block to the file

f_gets - Read a string

f_putc - Write a character

f_puts - Write a string

f_printf - Write a formatted string

f_tell - Get current read/write pointer

f_eof - Test for end-of-file

f_size - Get size

f_error - Test for an error

路径操作

f_opendir - Open a directory

f_closedir - Close an open directory

f_readdir - Read an directory item

f_findfirst - Open a directory and read the first item matched

f_findnext - Read a next item matched

文件和路径管理

f_stat - Check existance of a file or sub-directory

f_unlink - Remove a file or sub-directory

f_rename - Rename/Move a file or sub-directory

f_chmod - Change attribute of a file or sub-directory

f_utime - Change timestamp of a file or sub-directory

f_mkdir - Create a sub-directory

f_chdir - Change current directory

f_chdrive - Change current drive

f_getcwd - Retrieve the current directory and drive

介质管理和系统配置

f_mount - Register/Unregister the work area of the volume

f_mkfs - Create an FAT volume on the logical drive

f_fdisk - Create logical drives on the physical drive

f_getfree - Get total size and free size on the volume

f_getlabel - Get volume label

f_setlabel - Set volume label

f_setcp - Set active code page

例程-SD卡FatFs文件系统-移植及基本操作

移植FatFs文件系统到X-CTR100控制器,获取SD卡容量信息显示,打开文件NewText.txt写入字符串数据,如果文件不存在则创建文件。

硬件说明

硬件资源:

  • 串口UART1
  • TF卡接口
  • SD卡(TF)(需自备)

X-CTR100控制器通过SPI1与SD卡连接,电路如下。

FatFs移植

本教程移植的版本为R0.12b版本。解压源码后可以得到两个文件夹:doc和src。doc里面主要是对FATFS的介绍,而src里面是我们需要的源码,包含文件如下所示。

ff.h :文件系统实现头文件,定义有文件系统所需的数据结构

diskio.h :底层驱动头文件,就一些状态宏的定义和底层驱动函数的申明

integer.h:仅实现数据类型重定义,增加系统的可移植性

ffconf.h :文件系统配置

ff.c :文件系统实现。

diskio.c 底层驱动

option可选的外部功能(比如支持中文等)

FatFs的移植,一般只需要修改2个文件,即ffconf.h和diskio.c。

ffconf.h:这个头文件包含了对FatFs 功能配置的宏定义,通过修改这些宏定义就可以裁剪FatFs 的功能。如需要支持简体中文,需要把ffconf.h 中的_CODE_PAGE的宏改成936 并把上面的cc936.c 文件加入到工程之中。FATFS模块的所有配置项都是存放在ffconf.h里面,我们可以通过配置里面的一些选项,来满足自己的需求。

diskio.c文件是移植的核心,需要进行底层驱动编写,包括几个接口函数。

基于X工程模板移植,工程框架如下

SD卡操作文件如下

ax_sdcard.c——SD卡操作源文件

ax_sdcard.h——SD卡操作头文件

满足X-SOFT规范,接口函数如下

u8 AX_SD_Init(void); //SD初始化

u8 AX_SD_Read(u8 *buf,u32 sector,u8 count); //SD读取

u8 AX_SD_Write(u8 *buf,u32 sector,u8 count); //SD写入

u32 AX_SD_GetSectorCount(void); //SD获取总扇区数

u32 AX_SD_GetCapacity(void); //SD获取容量

u8 AX_SD_WaitReady(void); //SD等待准备好

软件说明

主程序代码如下,首先对SD卡进行初始化,内存初始化,挂载SD卡,获取SD卡总容量和剩余容量,并显示。最后创建文件,并写入数据。

int main(void)

{    

    uint32_t total,free;

    UINT bw;

    FRESULT res;

    

    //X-CTR100初始化

    AX_Init(115200);

    printf("***X-CTR100 SDFatFs文件系统移植及基本例程***\r\n\r\n");

      

//SD卡初始化

    while(AX_SD_Init())//检测不到SD

    {

        printf("SD卡初始化错误,请检查!\r\n");

        AX_Delayms(800);

    }

    

    //内存初始化

    AX_MEM_Init();

    fs=(FATFS*)AX_MEM_Malloc(sizeof(FATFS));    //为磁盘0工作区申请内存    

    file=(FIL*)AX_MEM_Malloc(sizeof(FIL));        //file申请内存

    ftemp=(FIL*)AX_MEM_Malloc(sizeof(FIL));        //ftemp申请内存

    fatbuf=(uint8_t*)AX_MEM_Malloc(512);        //fatbuf申请内存

    

    //挂载SD     

    f_mount(fs,"0:",1);

    

    //得到SD卡的总容量和剩余容量

    while(ax_getfree("0", &total, &free))    

    {

        printf("SD Card Error!");

        AX_Delayms(1000);

    }

    printf("FATFS OK! \r\n");

    printf("SD总空间: %d KB\r\n",total);

    printf("SD空闲空间: %d KB\r\n",free);

    

    //打开文件,写入字符串,如果文件不存在则创建新文件

    res = f_open(file, "NewText.txt", FA_WRITE | FA_CREATE_ALWAYS);

    if (res == FR_OK)

    {

     printf("create file ok!\r\n");

     printf("start write! \r\n");

     do

     {

     res = f_write(file, buffer, 50, &bw);

     if(res)

     {

     printf("write error : %d\r\n",res);

     break;

     }

     printf("write ok!\r\n");

     }

     while (bw < 50); // 判断是否写完

    }

    f_close(file); // 关闭文件

    

    while (1)

    {

        AX_Delayms(100);

        AX_LEDG_Toggle();

    }

}

实现效果

插入8G SD卡,格式化为FAT32格式,获取总空间和剩余空间信息如下,显示创建文件成功。

通过读卡器查看SD卡内容如下

posted @ 2018-07-02 15:34  xtarker  阅读(1400)  评论(0编辑  收藏  举报