[STM32F1]

STM32+SDIO+FATFS文件系统 直读SD卡

[复制链接]
1267|0
手机看帖
扫描二维码
随时随地手机跟帖
TXQDM|  楼主 | 2016-8-28 22:19 | 显示全部楼层 |阅读模式

网上关于小型嵌入式的文件系统有好多~当然要数 FATFS 很是出名 一来小巧,二来免费。当然了国产的振南的znFAT 一样开源好用而且极其的省资源~!非常适合51单片。更重要的是国语的支持,呵呵!这次在STM32上为SD卡移植文件系统还是非常简单顺利的,这多亏了ST 官方提供的驱动,而我自己不用动手编写SD卡的命令省了很多时间而且官方做的驱动虽然效率一般但是极其严谨我很是佩服。

FATFS的官方网站是

http://elm-chan.org/fsw/ff/00index_e.html

znFAT的官方网站是

http://www.znmcu.cn/softshow.asp?id=47

今天上午问个妹子要电话,没给我........唉~太失败了。

SD卡可以用SPI驱动 也可以直接用 SDIO 驱动 STM32 256KB FLASH 以上的片子全部都有SDIO,我们当然要用高速快捷的SDIO 方式了!至于 SDIO 又有 1位 4位 8 位的之分 我想不来8位SDIO 是怎么回事?SD卡上最多只能接4位嘛~网上有人说4位的SDIO 不好用多半是固件版本太老的缘故了。呵呵这里还是要靠库~STM32真适合懒人用。

网上关于的FATFS 的**很多 不过都太老旧,很多东西已经不适用了 。我建议阁下到官方去下载最新的版本 目前是最新是R0.08b ,使用最新的版本好处是很多网上很多要改来改去的地方只要你使用了新版本那就是完全可以规避的。另外STM32 的SDIO驱动也一定要用最新的,老版本问题很多不少人的失败就在这。我这次用的是V3.3的库没有任何改动就可以了,现在最新的好像在3.4以上了。好了说说移植 ffconf.h是配置的头文件 简单的修改宏就可以了,英文注释的很完全而且网上也有翻译我不多说了自己看http://www.openrtos.cn/fatfs主要在这里进行功能裁剪写写我的配置。

#define _FS_TINY  0 /* 0:Normal or 1:Tiny 完整的FATFS 精简版的是Tiny */

#define _FS_READONLY 0 /* 0:Read/Write or 1:Read only 能读能写*/

#define _FS_MINIMIZE 1 /* 0 to 3 简单的裁剪f_mkdir, f_chmod..这些功能没法用的*/
/* The _FS_MINIMIZE option defines minimization level to remove some functions.
/
/   0: Full function.
/   1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename
/      are removed.
/   2: f_opendir and f_readdir are removed in addition to 1.
/   3: f_lseek is removed in addition to 2. */


#define _USE_STRFUNC 0 /* 0:Disable or 1/2:Enable是否使用字符串文件接口 */
/* To enable string functions, set _USE_STRFUNC to 1 or 2. */


#define _USE_MKFS  0 /* 0:Disable or 1:Enable  制作文件系统我在PC上一般已经格式化好了*/
/* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */


#define _USE_FORWARD 0 /* 0:Disable or 1:Enable 发文件流?*/
/* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */


#define _USE_FASTSEEK 0 /* 0:Disable or 1:Enable 搜索*/
/* To enable fast seek feature, set _USE_FASTSEEK to 1. */

#define _CODE_PAGE 1 / /1    - ASCII only (Valid for non LFN cfg.)

#define _USE_LFN 0  /* 0 to 3 */
#define _MAX_LFN 255  /* Maximum LFN length to handle (12 to 255) 这些都是长文件名或是汉字文件支持很费资源所以不开启这些*/

#define _FS_SHARE 0 /* 0:Disable or >=1:Enable 不使用相对路径*/

#define _FS_SHARE 0 /* 0:Disable or >=1:Enable 文件共享多任务的操作系统会用到的*/

#define _FS_REENTRANT 0  /* 0:Disable or 1:Enable 这些是啥用?同步什么呢?默认就好了*/
#define _FS_TIMEOUT  1000 /* Timeout period in unit of time ticks */
#define _SYNC_t   HANDLE /* O/S dependent type of sync object. e.g. HANDLE, OS_EVENT*, ID and etc.. */

integer.h主要定义了文件的类型 若是最新的可以不用修改。

好了 说说最关键的I/O module 我自己建立一个文件diskio.c新的版本要自己建立函数文件官方连个模板都没提供作者似乎不怎么照顾新人呢~

都说要移植5个函数 其实两个就足以了。

/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2011        */
/*-----------------------------------------------------------------------*/
/* This is a stub disk I/O module that acts as front end of the existing */
/* disk I/O modules and attach it to FatFs module with common interface. */
/*-----------------------------------------------------------------------*/

#include "diskio.h"
#include "stm32f10x.h"
#include "stm32_eval_sdio_sd.h"

#define BLOCK_SIZE            512 /* Block Size in Bytes */


/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */

DSTATUS disk_initialize (
BYTE drv    /* Physical drive nmuber (0..) */
)
{
SD_Error  Status;
/* Supports only single driveFATFS支持多个设备 所以有个设备号drive nmuber当然了我就一个SD卡所以只有零号 */
if (drv)
{
  return STA_NOINIT;
}
/*-------------------------- SD Init ----------------------------- */
  Status = SD_Init();
if (Status!=SD_OK )
{
  return STA_NOINIT;
}
else
{
  return RES_OK;
}

}


/*-----------------------------------------------------------------------*/
/* Return Disk Status                                                    */

DSTATUS disk_status (
BYTE drv /* Physical drive nmuber (0..) */
)
{
return RES_OK; //懒的管了 有空写写 可以加个
}


/*-----------------------------------------------------------------------*/
/* 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) 读取的扇区数*/
)
{
// SD_Error Status;
if (count > 1)
{
  SD_ReadMultiBlocks(buff, sector*BLOCK_SIZE, BLOCK_SIZE, count); //扇区地址*512就是实际地址 默认一个扇区就是512个字节
}
else
{
  SD_ReadBlock(buff, sector*BLOCK_SIZE, BLOCK_SIZE);
}
return RES_OK;
}


/*-----------------------------------------------------------------------*/
/* 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) */
)
{
if (count > 1)
{
  SD_WriteMultiBlocks((uint8_t *)buff, sector*BLOCK_SIZE, BLOCK_SIZE, count);

/*这里大家看到了有个地址转换 因为DMA仅仅支持4字节指针的所以在函数内部还是有个转换的这里最好优化一下能提高效率*/
}
else
{
  SD_WriteBlock((uint8_t *)buff,sector*BLOCK_SIZE, BLOCK_SIZE);
}
return RES_OK;
}
#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;
}
        
/*-----------------------------------------------------------------------*/
/* Get current time                                                      */
/*-----------------------------------------------------------------------*/
DWORD get_fattime(void)
{
return ((2011UL-1980) << 25)       // Year = 2011
| (3UL << 21)       // Month = Mar
| (26UL << 16)       // Day = 26
| (13U << 11)       // Hour = 13
| (19U << 5)       // Min =19
| (0U >> 1)       // Sec = 0
;
}

最终无优化编译后 :Program Size: Code=10904 RO-data=336 RW-data=56 ZI-data=2304

RO是程序中的指令和常量
RW是程序中的已初始化变量
ZI是程序中的未初始化的变量
我觉得这个大小对于STM32103FZE来说完全可以接受
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

110

主题

593

帖子

11

粉丝