SD卡fat文件系统移植
经过充分的研究,发现fatfs文件系统移植的比较简单!因为代码都已经被别人做好了!我们只需把io层稍稍做个处理就ok了;
至于sd卡的驱动请看我这篇博客:http://blog.csdn.net/ieczw/article/details/17378475
移植是以这个驱动为前提的!!
http://elm-chan.org/fsw/ff/00index_e.html
这个网站发布了所有版本的文件fatfs文件系统,我这次下载最新版的http://elm-chan.org/fsw/ff/ff9a.zip
直接解压,里面有两个文件,一个是src,另一个是doc;
把src放到自己的工程目录下面,并添加。首先看看00Readme.txt,我们主要是关注这些东西
FILES ffconf.h Configuration file for FatFs module.
ff.h Common include file for FatFs and application module.
ff.c FatFs module.
diskio.h Common include file for FatFs and disk I/O module.
diskio.c An example of glue function to attach existing disk I/O module to FatFs.
integer.h Integer type definitions for FatFs.
option Optional external functions. Low level disk I/O module is not included in this archive because the FatFs
module is only a generic file system layer and not depend on any specific
storage device. You have to provide a low level disk I/O module that written
to control your storage device.
所以我们主要修改的是diskio.c
DSTATUS disk_initialize (BYTE pdrv);
DSTATUS disk_status (BYTE pdrv);
DRESULT disk_read (BYTE pdrv, BYTE*buff, DWORD sector, BYTE count);
DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, BYTE count);
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
我们需要改的是这几个底层io函数,按照下面的修改,我想大家应该能懂!
/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2012 */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be */
/* attached to the FatFs via a glue function rather than modifying it. */
/* This is an example of glue functions to attach various exsisting */
/* storage control module to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/ #include "diskio.h" /* FatFs lower layer API */
#include "stm32_eval_sdio_sd.h" /* Definitions of physical drive number for each media */
#define ATA 0
#define MMC 1
#define USB 2 #define SECTOR_SIZE 512U /*-----------------------------------------------------------------------*/
/* Inidialize a Drive */ DSTATUS disk_initialize (
BYTE drv /* Physical drive nmuber (0..) */
)
{
return SD_Init();
} /*-----------------------------------------------------------------------*/
/* Return Disk Status */ DSTATUS disk_status (
BYTE drv /* Physical drive nmuber (0..) */
)
{
return SD_GetStatus();
} /*-----------------------------------------------------------------------*/
/* Read Sector(s) */ DRESULT disk_read (
BYTE drv, /* Physical drive nmuber (0..) */
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector address (LBA) */
BYTE count /* Number of sectors to read (1..255) */
)
{
return (DRESULT)SD_ReadBlock(buff,sector << 9 ,count);
} /*-----------------------------------------------------------------------*/
/* Write Sector(s) */ #if _READONLY == 0
DRESULT disk_write (
BYTE drv, /* Physical drive nmuber (0..) */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address (LBA) */
BYTE count /* Number of sectors to write (1..255) */
)
{
return (DRESULT)SD_WriteBlock((uint8_t *)buff,sector << 9 ,count);
}
#endif /* _READONLY */ /*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */ DRESULT disk_ioctl (
BYTE drv, /* Physical drive nmuber (0..) */
BYTE ctrl, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
return RES_OK;
} unsigned short get_fattime(void){
return 0;
}
这些修改完后,如果你直接编译会出现如下错误:
..\fatfs\cc936.c(11): error: #35: #error directive: This file is not needed in current configuration. Remove from the project.
找到cc936.c,他的头文件是这样定义的
/*------------------------------------------------------------------------*/
/* Unicode - OEM code bidirectional converter (C)ChaN, 2009 */
/* */
/* CP936 (Simplified Chinese GBK) */
/*------------------------------------------------------------------------*/ #include "../ff.h" #if !_USE_LFN || _CODE_PAGE != 936
#error This file is not needed in current configuration. Remove from the project.
#endif
他是找不到USE_LFN或者_CODE_PAGE !=936
那么可能头文件处理问题,大家应该发现问题了,把头文件修改下:#include "ff.h"
为什么要用936的呢?大家看ffconf.h,有这么一段注释
/*---------------------------------------------------------------------------/
/ Locale and Namespace Configurations
/----------------------------------------------------------------------------*/ #define _CODE_PAGE 936
/* The _CODE_PAGE specifies the OEM code page to be used on the target system.
/ Incorrect setting of the code page can cause a file open failure.
/
/ 932 - Japanese Shift-JIS (DBCS, OEM, Windows)
/ 936 - Simplified Chinese GBK (DBCS, OEM, Windows)
/ 949 - Korean (DBCS, OEM, Windows)
/ 950 - Traditional Chinese Big5 (DBCS, OEM, Windows)
/ 1250 - Central Europe (Windows)
/ 1251 - Cyrillic (Windows)
/ 1252 - Latin 1 (Windows)
/ 1253 - Greek (Windows)
/ 1254 - Turkish (Windows)
/ 1255 - Hebrew (Windows)
/ 1256 - Arabic (Windows)
/ 1257 - Baltic (Windows)
/ 1258 - Vietnam (OEM, Windows)
/ 437 - U.S. (OEM)
/ 720 - Arabic (OEM)
/ 737 - Greek (OEM)
/ 775 - Baltic (OEM)
/ 850 - Multilingual Latin 1 (OEM)
/ 858 - Multilingual Latin 1 + Euro (OEM)
/ 852 - Latin 2 (OEM)
/ 855 - Cyrillic (OEM)
/ 866 - Russian (OEM)
/ 857 - Turkish (OEM)
/ 862 - Hebrew (OEM)
/ 874 - Thai (OEM, Windows)
/ 1 - ASCII only (Valid for non LFN cfg.)
*/
所以,因为我们是中国人,所以我们要用936!哈哈。。。
然后编译又出现另一个问题:
..\output\stm32.axf: Error: L6218E: Undefined symbol get_fattime (referred from ff.o).
这个我们需要自己添加,我在上面修改diskio.c的时候已经添加了。
这些做完之后,也算移植玩了,简单吧,至于如何使用,我们还是看下载网页:http://elm-chan.org/fsw/ff/00index_e.html
或者双击doc/目录下的00index_e.html
Application Interface FatFs module provides following functions to the applications. In other words, this list describes what FatFs can do to access the FAT volumes.
f_open - Open/Create a file
f_close - Close an open file
f_read - Read file
f_write - Write file
f_lseek - Move read/write pointer, Expand file size
f_truncate - Truncate file size
f_sync - Flush cached data
f_forward - Forward file data to the stream
f_stat - Check existance of a file or sub-directory
f_opendir - Open a directory
f_closedir - Close an open directory
f_readdir - Read a directory item
f_mkdir - Create a sub-directory
f_unlink - Remove a file or sub-directory
f_chmod - Change attribute
f_utime - Change timestamp
f_rename - Rename/Move a file or sub-directory
f_chdir - Change current directory
f_chdrive - Change current drive
f_getcwd - Retrieve the current directory
f_getfree - Get free clusters
f_getlabel - Get volume label
f_setlabel - Set volume label
f_mount - Register/Unregister a work area
f_mkfs - Create a file system on the drive
f_fdisk - Divide a physical drive
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 on a file
f_size - Get size of a file
f_error - Test for an error on a file
里面对每个函数都做了说明,这样就有点像linux操作系统里面的文件io操作了。
我们可以任意点击去一个函数,里面都有例程,我们可以用这些例程来测试下
/* Read a text file and display it */ FATFS FatFs; /* Work area (file system object) for logical drive */ int main (void)
{
FIL fil; /* File object */
char line[82]; /* Line buffer */
FRESULT fr; /* FatFs return code */ /* Register work area to the default drive */
f_mount(&FatFs, "", 0); /* Open a text file */
fr = f_open(&fil, "message.txt", FA_READ);
if (fr) return (int)fr; /* Read all lines and display it */
while (f_gets(line, sizeof line, &fil))
printf(line); /* Close the file */
f_close(&fil); return 0;
}
好,基于STM32的fatfs文件系统移植就到这里了!!!!
蓝桥杯-嵌入式交流群
147520657
SD卡fat文件系统移植的更多相关文章
- 玩转X-CTR100 l STM32F4 l SD卡FatFs文件系统
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] X-CTR100控制器具有SD卡接口,本教程使用免费 ...
- SD卡FAT32文件系统格式
一.声明 1.本文来源和主旨 2.本文测试环境 二.SD卡FAT文件系统 1.SD卡FAT32文件系统的整体布局 2.FAT文件系统简介 ① 文件分配表 ② 目录项 三.DBR(DOS BOOT RE ...
- RTT下spi flash+elm fat文件系统移植小记
背景: MCU:STM32F207 SPI flash: Winbond W25Q16BV OS: RTT V1.1.1 bsp: STM32F20x 1 将spi_core.c,spi_dev.c及 ...
- 第37章 基于SD卡的FatFs文件系统—零死角玩转STM32-F429系列
第37章 基于SD卡的FatFs文件系统 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.co ...
- 用SD卡下载uboot、linux内核和文件系统
1. 移植mtd-utils: a) 下载utd-utils 下载地址为ftp://ftp.infradead.org/pub/mtd-utils/b) 交叉编译mtd-utilsi 修改Make ...
- SD卡中FAT32文件格式快速入门(图文详细介绍)【转】
本文转自:http://blog.csdn.net/mjx91282041/article/details/8904705 说明: MBR :Master Boot Record ( 主引导记录) D ...
- SD卡中FAT32文件格式快速入门(图文详细介绍)
说明: MBR :Master Boot Record ( 主引导记录) DBR :DOS Boot Record ( 引导扇区) FAT :File Allocation Table ( 文件分配表 ...
- SD卡中FAT32文件格式高速入门(图文具体介绍)
说明: MBR :Master Boot Record ( 主引导记录) DBR :DOS Boot Record ( 引导扇区) FAT :File Allocation Table ( 文件分配表 ...
- SD卡FAT32获得高速的文件格式(图文介绍)
说明: MBR :Master Boot Record ( 主引导记录) DBR :DOS Boot Record ( 引导扇区) FAT :File Allocation Table ( 文件分配表 ...
随机推荐
- windows 下 文件属性及目录列表操作
转:http://blog.sina.com.cn/s/blog_686d0fb001012tsg.html 我们需要一个结构体和几个函数.这些函数和结构体在<io.h>的头文件中,结构体 ...
- 【HDOJ】4366 Successor
基本思路是将树形结构转换为线性结构.然后,所求即为一个区间内大于abi的最大的loy指向的ID.将结点按照abi降序排序,注意abi可能相等.然后,使用线段树单点更新,区间查询可解. /* 4366 ...
- Windows使用virtualenv搭建flask开发环境
virtualenv: VirtualEnv用于在一台机器上创建多个独立的Python虚拟运行环境,多个Python环境相互独立,互不影响,它能够: 在没有权限的情况下安装新套件 不同应用可以使用不同 ...
- Java多线程性能优化
大家使用多线程无非是为了提高性能,但如果多线程使用不当,不但性能提升不明显,而且会使得资源消耗更大.下面列举一下可能会造成多线程性能问题的点: 死锁 过多串行化 过多锁竞争 切换上下文 内存同步 下面 ...
- bzoj3572
通过这题我知道了一个鬼故事,trunc(ln(128)/ln(2))=6……以后不敢轻易这么写了 好了言归正传,这题明显的构建虚树,但构建虚树后怎么树形dp呢? 由于虚树上的点不仅是议事会还有可能是议 ...
- Android Service即四大组件总结
原文转载自:http://www.cnblogs.com/bravestarrhu/archive/2012/05/02/2479461.html Service 服务: 一个Service 是一段长 ...
- HDU 5266 pog loves szh III
题意:给出一棵树,1为根节点,求一段区间内所有点的最近公共祖先. 解法:用一棵线段树维护区间LCA.LCA是dp做法.dp[i][j]表示点i的第2^j个祖先是谁,转移方程为dp[i][j] = dp ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.3.6
If $A$ is a contraction, show that $$\bex A^*(I-AA^*)^{1/2}=(I-A^*A)^{1/2}A^*. \eex$$ Use this to sh ...
- java并发之CountDownLatch、Semaphore和CyclicBarrier
JAVA并发包中有三个类用于同步一批线程的行为,分别是CountDownLatch.Semaphore和CyclicBarrier. CountDownLatch Java之CountDownLatc ...
- Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)
首先介绍常用布局类 FrameLayout 最简单的布局管理器. 这个布局管理类有几个特性: 添加组件默认在左上角的. 如果添加多个组件会叠加到一起,并且都在左上角.(可以通过一gravity属性改变 ...