| 059 | BYTEdrv,/* Physical drive nmuber (0..) */ |
| 060 | BYTE*buff,/* Data buffer to store read data */ |
| 061 | DWORDsector,/* Sector address (LBA) */ |
| 062 | BYTEcount/* Number of sectors to read (1..255) */ |
| 068 | returnRES_PARERR;//仅支持单磁盘操作,count不能等于0,否则返回参数错误 |
| 072 | returnRES_NOTRDY;//没有检测到SD卡,报NOT READY错误 |
| 077 | if(count==1)//1个sector的读操作 |
| 079 | res = SD_ReadSingleBlock(sector, buff); |
| 083 | res = SD_ReadMultiBlock(sector, buff, count); |
| 088 | if(SD_ReadSingleBlock(sector, buff)!=0) |
| 096 | //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值 |
| 109 | /*-----------------------------------------------------------------------*/ |
| 114 | BYTEdrv,/* Physical drive nmuber (0..) */ |
| 115 | constBYTE*buff,/* Data to be written */ |
| 116 | DWORDsector,/* Sector address (LBA) */ |
| 117 | BYTEcount/* Number of sectors to write (1..255) */ |
| 124 | returnRES_PARERR;//仅支持单磁盘操作,count不能等于0,否则返回参数错误 |
| 128 | returnRES_NOTRDY;//没有检测到SD卡,报NOT READY错误 |
| 134 | res = SD_WriteSingleBlock(sector, buff); |
| 138 | res = SD_WriteMultiBlock(sector, buff, count); |
| 150 | #endif /* _READONLY */ |
| 154 | /*-----------------------------------------------------------------------*/ |
| 155 | /* Miscellaneous Functions */ |
| 158 | BYTEdrv,/* Physical drive nmuber (0..) */ |
| 159 | BYTEctrl,/* Control code */ |
| 160 | void*buff/* Buffer to send/receive control data */ |
| 168 | returnRES_PARERR;//仅支持单磁盘操作,否则返回参数错误 |
| 171 | //FATFS目前版本仅需处理CTRL_SYNC,GET_SECTOR_COUNT,GET_BLOCK_SIZ三个命令 |
| 193 | *(DWORD*)buff = SD_GetCapacity(); |
| 204 | /*-----------------------------------------------------------------------*/ |
| 205 | /* User defined function to give a current time to fatfs module */ |
| 206 | /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */ |
| 207 | /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */ |
| 208 | DWORDget_fattime (void) |
这里的结构函数为Fatfs提供和SD卡的通信接口。 在 最新版本的Fatfs中还加入了对中文文件名的支持,需要修改 ffconf.h
#define _CODE_PAGE 936 //- Simplified Chinese GBK (DBCS, OEM, Windows)
同时应该添加 option/cc936.c文件。但是这个文件有700多K占相当大的ROM, 像stm32F103RBT6这种小FLASH的MCU根本不行 ,加入当前工程文件中代码将增加160KB 左右。
配置好Stm32的串口和SPI等IO口设置后,就可以使用Fatfs做一些文件操作了。
4. Fatfs 文件操作
文件分配表FAT(File AllocationTable)用来记录文件所在位置的表格.它对于硬盘的使用是非常重要的,假若丢失文件分配表,那么硬盘上的数据就会因无法定位而不能使用了。
Fatfs 文件系统减轻了操作SD卡的工作量,调用其提供的函数就可以方便的操作文件,读写删改等。
这里提供一个main.c 示例:
| 004 | FRESULT scan_files (char* path); |
| 006 | #define F_PUTS 1 //测试向文件写入字符串 |