SD卡与fatfs文件系统(1)

    SD的优势之一是它的便携性,它可以自由插拔,可以在嵌入式设备和PC机之间交换数据。如果使用FAT(File Allocation Table)文件系统,它便可以方便在安装windows的PC和嵌入式设备之间交换数据。一个完整的FAT文件系统代码量非常庞大,不适合资源较少的嵌入式系统,于是就需要一个微型的FAT文件系统,FatFs就是基于这样的目的而开发的。

    FatFS是一个专为小型嵌入式系统设计的通用FAT文件系统模块。FatFs具有较高的可配置性,最小配置仅使用1K的RAM空间,非常适用于嵌入式系统。FatFs 的编写遵循ANSI C,并且完全与磁盘I/O层分开。因此,它独立(不依赖)于硬件架构。它可以被嵌入到低成本的微控制器中,如AVR, 8051, PIC, ARM, Z80, 68K 等等,而不需要做任何修改。

特点

  • Windows兼容的FAT文件系统
  • 不依赖于平台,易于移植
  • 代码和工作区占用空间非常小
  • 多种配置选项:
多卷(物理驱动器和分区)
多ANSI/OEM代码页,包括DBCS
在ANSI/OEM或Unicode中长文件名的支持
RTOS的支持
多扇区大小的支持
只读,最少API,I/O缓冲区等等


FatFs的源代码只有几个文件:diskio.c,ff.c,ff_util.c,tff.c及头文件。diskio.c是磁盘操作的代码文件(这个文件是移植时要实现的),ff.c是一般FatFs的代码文件,tff.c是微型FatFs的代码文件,ff_util.c是几个辅助函数。integer.h是内部基本类型的定义,ff.h是一般FatFs包含的头文件,tff.h是微型FatFs包含的头文件。

  1. #if _FATFS_TINY != 1  
  2. #include <fatfs/src/ff.h>  
  3. #else  
  4. #include <fatfs/src/tff.h>  
  5. #endif  
  6. #include <fatfs/src/ff_util.h>  
#if _FATFS_TINY != 1




#include <fatfs/src/ff.h> #else #include <fatfs/src/tff.h> #endif #include <fatfs/src/ff_util.h>

微型FatFs配置最小时仅占用内存1KB,但它是一个只读的FAT系统。

FatFs的配置文件是fatfs_config.h:
  1. //——————————————————————————  
  2. //      General Definitions (previously in ff.h)  
  3. //——————————————————————————  
  4.   
  5. #define _FATFS_TINY 0  
  6. /* When _FATFS_TINY is set to 1, fatfs is compiled in Tiny mode 
  7. / Else, it is compiled in normal mode  
  8. / Tiny FatFs feature : Very low memory consumption, suitable for small memory  
  9. / system. (1KB RAM) : Supports only single drive, no disk format,  
  10. / only read functions, no write functions */  
  11.   
  12. //——————————————————————————  
  13. //      Definitions for normal FATFS (previously in ff.h)  
  14. //——————————————————————————  
  15.   
  16. #if _FATFS_TINY == 0  
  17.   
  18. #define _MCU_ENDIAN     2  
  19. /* The _MCU_ENDIAN defines which access method is used to the FAT structure. 
  20. /  1: Enable word access. 
  21. /  2: Disable word access and use byte-by-byte access instead. 
  22. /  When the architectural byte order of the MCU is big-endian and/or address 
  23. /  miss-aligned access results incorrect behavior, the _MCU_ENDIAN must be set to 2. 
  24. /  If it is not the case, it can also be set to 1 for good code efficiency. */  
  25.   
  26. #define _FS_READONLY    0  
  27. /* Setting _FS_READONLY to 1 defines read only configuration. This removes 
  28. /  writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename, 
  29. /  f_truncate and useless f_getfree. */  
  30.   
  31. #define _FS_MINIMIZE    0  
  32. /* The _FS_MINIMIZE option defines minimization level to remove some functions. 
  33. /  0: Full function. 
  34. /  1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename are removed. 
  35. /  2: f_opendir and f_readdir are removed in addition to level 1. 
  36. /  3: f_lseek is removed in addition to level 2. */  
  37.   
  38. #define _USE_STRFUNC    0  
  39. /* To enable string functions, set _USE_STRFUNC to 1 or 2. */  
  40.   
  41. #define _USE_FSINFO 1  
  42. /* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */  
  43.   
  44. #define _USE_SJIS   1  
  45. /* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise 
  46. /  only US-ASCII(7bit) code can be accepted as file/directory name. */  
  47.   
  48. #define _USE_NTFLAG 1  
  49. /* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved. 
  50. /  Note that the files are always accessed in case insensitive. */  
  51.   
  52. #define _USE_MKFS   1  
  53. /* When _USE_MKFS is set to 1 and _FS_READONLY is set to 0, f_mkfs function is 
  54. /  enabled. */  
  55.   
  56. #define _DRIVES     2  
  57. /* Number of logical drives to be used. This affects the size of internal table. */  
  58.   
  59. #define _MULTI_PARTITION    0  
  60. /* When _MULTI_PARTITION is set to 0, each logical drive is bound to same 
  61. /  physical drive number and can mount only 1st primaly partition. When it is 
  62. /  set to 1, each logical drive can mount a partition listed in Drives[]. */  
  63.   
  64. //——————————————————————————  
  65. //      Definitions for normal FATFS TINY (previously in tff.h)  
  66. //——————————————————————————  
  67.   
  68. #else  
  69.   
  70. #define _MCU_ENDIAN     2  
  71. /* The _MCU_ENDIAN defines which access method is used to the FAT structure. 
  72. /  1: Enable word access. 
  73. /  2: Disable word access and use byte-by-byte access instead. 
  74. /  When the architectural byte order of the MCU is big-endian and/or address 
  75. /  miss-aligned access results incorrect behavior, the _MCU_ENDIAN must be set to 2. 
  76. /  If it is not the case, it can also be set to 1 for good code efficiency. */  
  77.   
  78. #define _FS_READONLY    1  
  79. /* Setting _FS_READONLY to 1 defines read only configuration. This removes 
  80. /  writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename, 
  81. /  f_truncate, f_getfree and internal writing codes. */  
  82.   
  83. #define _FS_MINIMIZE    0  
  84. /* The _FS_MINIMIZE option defines minimization level to remove some functions. 
  85. /  0: Full function. 
  86. /  1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename are removed. 
  87. /  2: f_opendir and f_readdir are removed in addition to level 1. 
  88. /  3: f_lseek is removed in addition to level 2. */  
  89.   
  90. #define _USE_STRFUNC    0  
  91. /* To enable string functions, set _USE_STRFUNC to 1 or 2. */  
  92.   
  93. #define _USE_FSINFO 1  
  94. /* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */  
  95.   
  96. #define _USE_SJIS   1  
  97. /* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise 
  98. /  only US-ASCII(7bit) code can be accepted as file/directory name. */  
  99.   
  100. #define _USE_NTFLAG 1  
  101. /* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved. 
  102. /  Note that the files are always accessed in case insensitive. */  
  103.   
  104. #define _USE_FORWARD    0  
  105. /* To enable f_forward function, set _USE_FORWARD to 1. */  
  106.   
  107. #define _FAT32  1  
  108. /* To enable FAT32 support in addition of FAT12/16, set _FAT32 to 1. */  
  109.   
  110. #endif  
  111.   
  112. //——————————————————————————  
  113. //      Other definitions  
  114. //——————————————————————————  
  115.   
  116. /*———————————————————————–*/  
  117. /* Correspondence between drive number and physical drive                */  
  118. /* Note that Tiny-FatFs supports only single drive and always            */  
  119. /* accesses drive number 0.                                              */  
  120.   
  121. #define DRV_MMC          0  
  122. #define DRV_SDRAM        1  
  123. #define DRV_ATA          2  
  124. #define DRV_USB          3  
  125.   
  126.   
  127. #define SECTOR_SIZE_SDRAM  512  
  128. #define SECTOR_SIZE_SDCARD 512  
//------------------------------------------------------------------------------
//      General Definitions (previously in ff.h)
//------------------------------------------------------------------------------





#define _FATFS_TINY 0 /* When _FATFS_TINY is set to 1, fatfs is compiled in Tiny mode / Else, it is compiled in normal mode / Tiny FatFs feature : Very low memory consumption, suitable for small memory / system. (1KB RAM) : Supports only single drive, no disk format, / only read functions, no write functions */ //------------------------------------------------------------------------------ // Definitions for normal FATFS (previously in ff.h) //------------------------------------------------------------------------------ #if _FATFS_TINY == 0 #define _MCU_ENDIAN 2 /* The _MCU_ENDIAN defines which access method is used to the FAT structure. / 1: Enable word access. / 2: Disable word access and use byte-by-byte access instead. / When the architectural byte order of the MCU is big-endian and/or address / miss-aligned access results incorrect behavior, the _MCU_ENDIAN must be set to 2. / If it is not the case, it can also be set to 1 for good code efficiency. */ #define _FS_READONLY 0 /* Setting _FS_READONLY to 1 defines read only configuration. This removes / writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename, / f_truncate and useless f_getfree. */ #define _FS_MINIMIZE 0 /* 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 level 1. / 3: f_lseek is removed in addition to level 2. */ #define _USE_STRFUNC 0 /* To enable string functions, set _USE_STRFUNC to 1 or 2. */ #define _USE_FSINFO 1 /* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */ #define _USE_SJIS 1 /* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise / only US-ASCII(7bit) code can be accepted as file/directory name. */ #define _USE_NTFLAG 1 /* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved. / Note that the files are always accessed in case insensitive. */ #define _USE_MKFS 1 /* When _USE_MKFS is set to 1 and _FS_READONLY is set to 0, f_mkfs function is / enabled. */ #define _DRIVES 2 /* Number of logical drives to be used. This affects the size of internal table. */ #define _MULTI_PARTITION 0 /* When _MULTI_PARTITION is set to 0, each logical drive is bound to same / physical drive number and can mount only 1st primaly partition. When it is / set to 1, each logical drive can mount a partition listed in Drives[]. */ //------------------------------------------------------------------------------ // Definitions for normal FATFS TINY (previously in tff.h) //------------------------------------------------------------------------------ #else #define _MCU_ENDIAN 2 /* The _MCU_ENDIAN defines which access method is used to the FAT structure. / 1: Enable word access. / 2: Disable word access and use byte-by-byte access instead. / When the architectural byte order of the MCU is big-endian and/or address / miss-aligned access results incorrect behavior, the _MCU_ENDIAN must be set to 2. / If it is not the case, it can also be set to 1 for good code efficiency. */ #define _FS_READONLY 1 /* Setting _FS_READONLY to 1 defines read only configuration. This removes / writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename, / f_truncate, f_getfree and internal writing codes. */ #define _FS_MINIMIZE 0 /* 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 level 1. / 3: f_lseek is removed in addition to level 2. */ #define _USE_STRFUNC 0 /* To enable string functions, set _USE_STRFUNC to 1 or 2. */ #define _USE_FSINFO 1 /* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */ #define _USE_SJIS 1 /* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise / only US-ASCII(7bit) code can be accepted as file/directory name. */ #define _USE_NTFLAG 1 /* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved. / Note that the files are always accessed in case insensitive. */ #define _USE_FORWARD 0 /* To enable f_forward function, set _USE_FORWARD to 1. */ #define _FAT32 1 /* To enable FAT32 support in addition of FAT12/16, set _FAT32 to 1. */ #endif //------------------------------------------------------------------------------ // Other definitions //------------------------------------------------------------------------------ /*-----------------------------------------------------------------------*/ /* Correspondence between drive number and physical drive */ /* Note that Tiny-FatFs supports only single drive and always */ /* accesses drive number 0. */ #define DRV_MMC 0 #define DRV_SDRAM 1 #define DRV_ATA 2 #define DRV_USB 3 #define SECTOR_SIZE_SDRAM 512 #define SECTOR_SIZE_SDCARD 512




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: STC单片机是一种嵌入式系统的微控制器,而SD是一种可移动存储介质。在STC单片机上使用SD进行文件操作时,可以借助FATFS文件系统FATFS是一种开源的文件系统,旨在在嵌入式系统中实现对FAT文件系统的支持。它可以很好地兼容SD,并提供了一些简单易用的接口函数,方便我们在STC单片机上进行SD文件读写操作。 首先,我们需要在STC单片机上配置SPI接口或SDIO接口,以实现与SD的通信。接着,我们需要初始化FATFS文件系统并与SD进行连接。这一步需要调用相关的FATFS库函数,并传入合适的参数,以实现文件系统的初始化工作。 在文件操作过程中,我们可以使用FATFS提供的接口函数,如f_open、f_read和f_write等,来打开、读取和写入文件。这些接口函数需要传入文件名、打开方式、读写缓冲区等参数,以实现对文件的操作。 在进行文件操作前,我们可以通过调用f_opendir和f_readdir等函数,来打开和读取目录,并获取目录下的文件列表。这些函数需要传入合适的参数,以实现对目录的操作。 文件操作完成后,我们需要调用f_close和f_unmount等函数,来关闭文件和卸载文件系统。这些函数需要传入相应的参数,以释放相关的资源。 总之,STC单片机通过使用SDFATFS文件系统,可以实现对文件的读写操作。通过合理地配置和调用相应的接口函数,我们可以在STC单片机上方便地进行文件操作,提高嵌入式系统的数据存储和处理能力。 ### 回答2: STC单片机是一种常用的微控制器,可以用于实现各种嵌入式系统,包括支持SDFATFS文件操作。 SD是一种常见的存储设备,可用于在嵌入式系统中扩展存储容量。STC单片机通过SPI接口与SD进行通信。首先,需要初始化SPI接口和SD,包括设置时钟频率、初始化寄存器等。然后可以通过SPI发送命令与SD进行交互,如读取和写入数据、创建文件夹和文件等。需要注意的是,SD的命令和响应是通过特定的命令和标志位进行传输的。 FATFS是一个通用的文件系统模块,可用于在SD上实现文件操作。STC单片机可以使用FATFS库来实现文件的打开、读取、写入和关闭等操作。首先需要引入FATFS库,并初始化相关参数。然后可以使用FATFS提供的接口函数来操作文件,如f_open打开文件、f_read读取文件内容、f_write写入文件内容等。同时,还可以使用f_opendir和f_readdir等函数来遍历文件夹和读取文件列表。 在进行SDFATFS文件操作时,需要注意以下几点:1.正确配置和初始化SPI接口和SD,包括时钟频率和寄存器设置;2.合理处理SDFATFS的返回值和错误码,以便及时发现和处理错误;3.合理规划和管理存储空间,防止SD的容量不足或文件系统出现错误;4.保证操作的原子性,避免同时读写同一个文件导致的数据错误;5.灵活使用FATFS提供的接口函数,以满足不同的文件操作需求。 总之,STC单片机可以通过SPI接口与SD进行通信,通过FATFS库实现SD上的文件操作。掌握SDFATFS的相关知识和技术,可以在嵌入式系统中实现灵活可靠的存储功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值