FatFs文件系统:
ff11a.zip
来自网站:http://elm-chan.org/fsw/ff/00index_e.html
FatFs文件系统移植
视频教程:https://www.bilibili.com/video/BV1yW411Y7Gw?p=65
diskio.h
/*-----------------------------------------------------------------------// Low level disk interface modlue include file (C)ChaN, 2014 //-----------------------------------------------------------------------*/#ifndef _DISKIO_DEFINED#define _DISKIO_DEFINED#ifdef __cplusplusextern "C" {#endif#define _USE_WRITE 1 /* 1: Enable disk_write function */#define _USE_IOCTL 1 /* 1: Enable disk_ioctl fucntion */#include "integer.h"/* Status of Disk Functions */typedef BYTE DSTATUS;/* Results of Disk Functions */typedef enum {RES_OK = 0, /* 0: Successful */RES_ERROR, /* 1: R/W Error */RES_WRPRT, /* 2: Write Protected */RES_NOTRDY, /* 3: Not Ready */RES_PARERR /* 4: Invalid Parameter */} DRESULT;/*---------------------------------------*//* Prototypes for disk control functions */DSTATUS disk_initialize (BYTE pdrv);DSTATUS disk_status (BYTE pdrv);DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);/* Disk Status Bits (DSTATUS) */#define STA_NOINIT 0x01 /* Drive not initialized */#define STA_NODISK 0x02 /* No medium in the drive */#define STA_PROTECT 0x04 /* Write protected *//* Command code for disk_ioctrl fucntion *//* Generic command (Used by FatFs) */#define CTRL_SYNC 0 /* Complete pending write process (needed at _FS_READONLY == 0) */#define GET_SECTOR_COUNT 1 /* Get media size (needed at _USE_MKFS == 1) */#define GET_SECTOR_SIZE 2 /* Get sector size (needed at _MAX_SS != _MIN_SS) */#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at _USE_MKFS == 1) */#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) *//* Generic command (Not used by FatFs) */#define CTRL_POWER 5 /* Get/Set power status */#define CTRL_LOCK 6 /* Lock/Unlock media removal */#define CTRL_EJECT 7 /* Eject media */#define CTRL_FORMAT 8 /* Create physical format on the media *//* MMC/SDC specific ioctl command */#define MMC_GET_TYPE 10 /* Get card type */#define MMC_GET_CSD 11 /* Get CSD */#define MMC_GET_CID 12 /* Get CID */#define MMC_GET_OCR 13 /* Get OCR */#define MMC_GET_SDSTAT 14 /* Get SD status *//* ATA/CF specific ioctl command */#define ATA_GET_REV 20 /* Get F/W revision */#define ATA_GET_MODEL 21 /* Get model name */#define ATA_GET_SN 22 /* Get serial number */#ifdef __cplusplus}#endif#endif
diskio.c
/*-----------------------------------------------------------------------*//* Low level disk I/O module skeleton for FatFs (C)ChaN, 2013 *//*-----------------------------------------------------------------------*//* 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 "ff.h"#include "./flash/bsp_spi_flash.h"/* 为每个设备定义一个物理编号 */#define ATA 0 // 预留SD卡使用#define SPI_FLASH 1 // 外部SPI Flash/*-----------------------------------------------------------------------*//* 获取设备状态 *//*-----------------------------------------------------------------------*/DSTATUS disk_status (BYTE pdrv /* 物理编号 */){DSTATUS status = STA_NOINIT;switch (pdrv) {case ATA: /* SD CARD */break;case SPI_FLASH:/* SPI Flash状态检测:读取SPI Flash 设备ID */if(sFLASH_ID == SPI_FLASH_ReadID()){/* 设备ID读取结果正确 */status &= ~STA_NOINIT;}else{/* 设备ID读取结果错误 */status = STA_NOINIT;;}break;default:status = STA_NOINIT;}return status;}/*-----------------------------------------------------------------------*//* 设备初始化 *//*-----------------------------------------------------------------------*/DSTATUS disk_initialize (BYTE pdrv /* 物理编号 */){uint16_t i;DSTATUS status = STA_NOINIT;switch (pdrv) {case ATA: /* SD CARD */break;case SPI_FLASH: /* SPI Flash *//* 初始化SPI Flash */SPI_FLASH_Init();/* 延时一小段时间 */i=500;while(--i);/* 唤醒SPI Flash */SPI_Flash_WAKEUP();/* 获取SPI Flash芯片状态 */status=disk_status(SPI_FLASH);break;default:status = STA_NOINIT;}return status;}/*-----------------------------------------------------------------------*//* 读扇区:读取扇区内容到指定存储区 *//*-----------------------------------------------------------------------*/DRESULT disk_read (BYTE pdrv, /* 设备物理编号(0..) */BYTE *buff, /* 数据缓存区 */DWORD sector, /* 扇区首地址 */UINT count /* 扇区个数(1..128) */){DRESULT status = RES_PARERR;switch (pdrv) {case ATA: /* SD CARD */break;case SPI_FLASH:/* 扇区偏移2MB,外部Flash文件系统空间放在SPI Flash后面6MB空间 */sector+=512;SPI_FLASH_BufferRead(buff, sector <<12, count<<12);status = RES_OK;break;default:status = RES_PARERR;}return status;}/*-----------------------------------------------------------------------*//* 写扇区:见数据写入指定扇区空间上 *//*-----------------------------------------------------------------------*/#if _USE_WRITEDRESULT disk_write (BYTE pdrv, /* 设备物理编号(0..) */const BYTE *buff, /* 欲写入数据的缓存区 */DWORD sector, /* 扇区首地址 */UINT count /* 扇区个数(1..128) */){uint32_t write_addr;DRESULT status = RES_PARERR;if (!count) {return RES_PARERR; /* Check parameter */}switch (pdrv) {case ATA: /* SD CARD */break;case SPI_FLASH:/* 扇区偏移2MB,外部Flash文件系统空间放在SPI Flash后面6MB空间 */sector+=512;write_addr = sector<<12;SPI_FLASH_SectorErase(write_addr); // 擦掉SPI_FLASH_BufferWrite((u8 *)buff,write_addr,count<<12);status = RES_OK;break;default:status = RES_PARERR;}return status;}#endif/*-----------------------------------------------------------------------*//* 其他控制 *//*-----------------------------------------------------------------------*/#if _USE_IOCTLDRESULT disk_ioctl (BYTE pdrv, /* 物理编号 */BYTE cmd, /* 控制指令 */void *buff /* 写入或者读取数据地址指针 */){DRESULT status = RES_PARERR;switch (pdrv) {case ATA: /* SD CARD */break;case SPI_FLASH:switch (cmd) {/* 扇区数量:1536*4096/1024/1024=6(MB) */case GET_SECTOR_COUNT:*(DWORD * )buff = 1536;break;/* 扇区大小 */case GET_SECTOR_SIZE :*(WORD * )buff = 4096;break;/* 同时擦除扇区个数 */case GET_BLOCK_SIZE :*(DWORD * )buff = 1;break;}status = RES_OK;break;default:status = RES_PARERR;}return status;}#endif__weak DWORD get_fattime(void) {/* 返回当前时间戳 */return ((DWORD)(2015 - 1980) << 25) /* Year 2015 */| ((DWORD)1 << 21) /* Month 1 */| ((DWORD)1 << 16) /* Mday 1 */| ((DWORD)0 << 11) /* Hour 0 */| ((DWORD)0 << 5) /* Min 0 */| ((DWORD)0 >> 1); /* Sec 0 */}
main.c
/********************************************************************************* @file main.c* @author fire* @version V1.0* @date 2013-xx-xx* @brief SPI FLASH文件系统例程******************************************************************************* @attention** 实验平台:野火 STM32 F103-霸道 开发板* 论坛 :http://www.firebbs.cn* 淘宝 :https://fire-stm32.taobao.com********************************************************************************/#include "stm32f10x.h"#include "ff.h"#include "./flash/bsp_spi_flash.h"#include "./usart/bsp_usart.h"#include "./led/bsp_led.h"FATFS fs; /* FatFs文件系统对象 */FIL fnew; /* 文件对象 */FRESULT res_flash; /* 文件操作结果 */UINT fnum; /* 文件成功读写数量 */BYTE ReadBuffer[1024]={0}; /* 读缓冲区 */BYTE WriteBuffer[] = /* 写缓冲区*/"欢迎使用野火STM32开发板 今天是个好日子,新建文件系统测试文件\r\n";int main(void){/* 初始化LED */LED_GPIO_Config();LED_BLUE;/* 初始化调试串口,一般为串口1 */USART_Config();printf("****** 这是一个SPI FLASH 文件系统实验 ******\r\n");//在外部SPI Flash挂载文件系统,文件系统挂载时会对SPI设备初始化//初始化函数调用流程如下//f_mount()->find_volume()->disk_initialize->SPI_FLASH_Init()res_flash = f_mount(&fs,"1:",1);/*----------------------- 格式化测试 -----------------*//* 如果没有文件系统就格式化创建创建文件系统 */if(res_flash == FR_NO_FILESYSTEM){printf("》FLASH还没有文件系统,即将进行格式化...\r\n");/* 格式化 */res_flash=f_mkfs("1:",0,0);if(res_flash == FR_OK){printf("》FLASH已成功格式化文件系统。\r\n");/* 格式化后,先取消挂载 */res_flash = f_mount(NULL,"1:",1);/* 重新挂载 */res_flash = f_mount(&fs,"1:",1);}else{LED_RED;printf("《《格式化失败。》》\r\n");while(1);}}else if(res_flash!=FR_OK){printf("!!外部Flash挂载文件系统失败。(%d)\r\n",res_flash);printf("!!可能原因:SPI Flash初始化不成功。\r\n");while(1);}else{printf("》文件系统挂载成功,可以进行读写测试\r\n");}/*----------------------- 文件系统测试:写测试 -------------------*//* 打开文件,每次都以新建的形式打开,属性为可写 */printf("\r\n****** 即将进行文件写入测试... ******\r\n");res_flash = f_open(&fnew, "1:FatFs读写测试文件.txt",FA_CREATE_ALWAYS | FA_WRITE );if ( res_flash == FR_OK ){printf("》打开/创建FatFs读写测试文件.txt文件成功,向文件写入数据。\r\n");/* 将指定存储区内容写入到文件内 */res_flash=f_write(&fnew,WriteBuffer,sizeof(WriteBuffer),&fnum);if(res_flash==FR_OK){printf("》文件写入成功,写入字节数据:%d\n",fnum);printf("》向文件写入的数据为:\r\n%s\r\n",WriteBuffer);}else{printf("!!文件写入失败:(%d)\n",res_flash);}/* 不再读写,关闭文件 */f_close(&fnew);}else{LED_RED;printf("!!打开/创建文件失败。\r\n");}/*------------------- 文件系统测试:读测试 --------------------------*/printf("****** 即将进行文件读取测试... ******\r\n");res_flash = f_open(&fnew, "1:FatFs读写测试文件.txt",FA_OPEN_EXISTING | FA_READ);if(res_flash == FR_OK){LED_GREEN;printf("》打开文件成功。\r\n");res_flash = f_read(&fnew, ReadBuffer, sizeof(ReadBuffer), &fnum);if(res_flash==FR_OK){printf("》文件读取成功,读到字节数据:%d\r\n",fnum);printf("》读取得的文件数据为:\r\n%s \r\n", ReadBuffer);}else{printf("!!文件读取失败:(%d)\n",res_flash);}}else{LED_RED;printf("!!打开文件失败。\r\n");}/* 不再读写,关闭文件 */f_close(&fnew);/* 不再使用文件系统,取消挂载文件系统 */f_mount(NULL,"1:",1);/* 操作完成,停机 */while(1){}}/*********************************************END OF FILE**********************/
FatFs文件系统常用函数测试
视频教程:https://www.bilibili.com/video/BV1yW411Y7Gw?p=67
diskio.h
/*-----------------------------------------------------------------------// Low level disk interface modlue include file (C)ChaN, 2014 //-----------------------------------------------------------------------*/#ifndef _DISKIO_DEFINED#define _DISKIO_DEFINED#ifdef __cplusplusextern "C" {#endif#define _USE_WRITE 1 /* 1: Enable disk_write function */#define _USE_IOCTL 1 /* 1: Enable disk_ioctl fucntion */#include "integer.h"/* Status of Disk Functions */typedef BYTE DSTATUS;/* Results of Disk Functions */typedef enum {RES_OK = 0, /* 0: Successful */RES_ERROR, /* 1: R/W Error */RES_WRPRT, /* 2: Write Protected */RES_NOTRDY, /* 3: Not Ready */RES_PARERR /* 4: Invalid Parameter */} DRESULT;/*---------------------------------------*//* Prototypes for disk control functions */DSTATUS disk_initialize (BYTE pdrv);DSTATUS disk_status (BYTE pdrv);DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);/* Disk Status Bits (DSTATUS) */#define STA_NOINIT 0x01 /* Drive not initialized */#define STA_NODISK 0x02 /* No medium in the drive */#define STA_PROTECT 0x04 /* Write protected *//* Command code for disk_ioctrl fucntion *//* Generic command (Used by FatFs) */#define CTRL_SYNC 0 /* Complete pending write process (needed at _FS_READONLY == 0) */#define GET_SECTOR_COUNT 1 /* Get media size (needed at _USE_MKFS == 1) */#define GET_SECTOR_SIZE 2 /* Get sector size (needed at _MAX_SS != _MIN_SS) */#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at _USE_MKFS == 1) */#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) *//* Generic command (Not used by FatFs) */#define CTRL_POWER 5 /* Get/Set power status */#define CTRL_LOCK 6 /* Lock/Unlock media removal */#define CTRL_EJECT 7 /* Eject media */#define CTRL_FORMAT 8 /* Create physical format on the media *//* MMC/SDC specific ioctl command */#define MMC_GET_TYPE 10 /* Get card type */#define MMC_GET_CSD 11 /* Get CSD */#define MMC_GET_CID 12 /* Get CID */#define MMC_GET_OCR 13 /* Get OCR */#define MMC_GET_SDSTAT 14 /* Get SD status *//* ATA/CF specific ioctl command */#define ATA_GET_REV 20 /* Get F/W revision */#define ATA_GET_MODEL 21 /* Get model name */#define ATA_GET_SN 22 /* Get serial number */#ifdef __cplusplus}#endif#endif
diskio.c
/*-----------------------------------------------------------------------*//* Low level disk I/O module skeleton for FatFs (C)ChaN, 2013 *//*-----------------------------------------------------------------------*//* 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 "ff.h"#include "./flash/bsp_spi_flash.h"/* 为每个设备定义一个物理编号 */#define ATA 0 // 预留SD卡使用#define SPI_FLASH 1 // 外部SPI Flash/*-----------------------------------------------------------------------*//* 获取设备状态 *//*-----------------------------------------------------------------------*/DSTATUS disk_status (BYTE pdrv /* 物理编号 */){DSTATUS status = STA_NOINIT;switch (pdrv) {case ATA: /* SD CARD */break;case SPI_FLASH:/* SPI Flash状态检测:读取SPI Flash 设备ID */if(sFLASH_ID == SPI_FLASH_ReadID()){/* 设备ID读取结果正确 */status &= ~STA_NOINIT;}else{/* 设备ID读取结果错误 */status = STA_NOINIT;;}break;default:status = STA_NOINIT;}return status;}/*-----------------------------------------------------------------------*//* 设备初始化 *//*-----------------------------------------------------------------------*/DSTATUS disk_initialize (BYTE pdrv /* 物理编号 */){uint16_t i;DSTATUS status = STA_NOINIT;switch (pdrv) {case ATA: /* SD CARD */break;case SPI_FLASH: /* SPI Flash *//* 初始化SPI Flash */SPI_FLASH_Init();/* 延时一小段时间 */i=500;while(--i);/* 唤醒SPI Flash */SPI_Flash_WAKEUP();/* 获取SPI Flash芯片状态 */status=disk_status(SPI_FLASH);break;default:status = STA_NOINIT;}return status;}/*-----------------------------------------------------------------------*//* 读扇区:读取扇区内容到指定存储区 *//*-----------------------------------------------------------------------*/DRESULT disk_read (BYTE pdrv, /* 设备物理编号(0..) */BYTE *buff, /* 数据缓存区 */DWORD sector, /* 扇区首地址 */UINT count /* 扇区个数(1..128) */){DRESULT status = RES_PARERR;switch (pdrv) {case ATA: /* SD CARD */break;case SPI_FLASH:/* 扇区偏移2MB,外部Flash文件系统空间放在SPI Flash后面6MB空间 */sector+=512;SPI_FLASH_BufferRead(buff, sector <<12, count<<12);status = RES_OK;break;default:status = RES_PARERR;}return status;}/*-----------------------------------------------------------------------*//* 写扇区:见数据写入指定扇区空间上 *//*-----------------------------------------------------------------------*/#if _USE_WRITEDRESULT disk_write (BYTE pdrv, /* 设备物理编号(0..) */const BYTE *buff, /* 欲写入数据的缓存区 */DWORD sector, /* 扇区首地址 */UINT count /* 扇区个数(1..128) */){uint32_t write_addr;DRESULT status = RES_PARERR;if (!count) {return RES_PARERR; /* Check parameter */}switch (pdrv) {case ATA: /* SD CARD */break;case SPI_FLASH:/* 扇区偏移2MB,外部Flash文件系统空间放在SPI Flash后面6MB空间 */sector+=512;write_addr = sector<<12;SPI_FLASH_SectorErase(write_addr);SPI_FLASH_BufferWrite((u8 *)buff,write_addr,count<<12);status = RES_OK;break;default:status = RES_PARERR;}return status;}#endif/*-----------------------------------------------------------------------*//* 其他控制 *//*-----------------------------------------------------------------------*/#if _USE_IOCTLDRESULT disk_ioctl (BYTE pdrv, /* 物理编号 */BYTE cmd, /* 控制指令 */void *buff /* 写入或者读取数据地址指针 */){DRESULT status = RES_PARERR;switch (pdrv) {case ATA: /* SD CARD */break;case SPI_FLASH:switch (cmd) {/* 扇区数量:1536*4096/1024/1024=6(MB) */case GET_SECTOR_COUNT:*(DWORD * )buff = 1536;break;/* 扇区大小 */case GET_SECTOR_SIZE :*(WORD * )buff = 4096;break;/* 同时擦除扇区个数 */case GET_BLOCK_SIZE :*(DWORD * )buff = 1;break;}status = RES_OK;break;default:status = RES_PARERR;}return status;}#endif__weak DWORD get_fattime(void) {/* 返回当前时间戳 */return ((DWORD)(2015 - 1980) << 25) /* Year 2015 */| ((DWORD)1 << 21) /* Month 1 */| ((DWORD)1 << 16) /* Mday 1 */| ((DWORD)0 << 11) /* Hour 0 */| ((DWORD)0 << 5) /* Min 0 */| ((DWORD)0 >> 1); /* Sec 0 */}
main.c
/********************************************************************************* @file main.c* @author fire* @version V1.0* @date 2013-xx-xx* @brief SPI FLASH文件系统常用功能演示******************************************************************************* @attention** 实验平台:野火 STM32 F103-霸道 开发板* 论坛 :http://www.firebbs.cn* 淘宝 :https://fire-stm32.taobao.com********************************************************************************/#include "stm32f10x.h"#include "./usart/bsp_usart.h"#include "ff.h"#include "string.h"/********************************************************************************* 定义变量*******************************************************************************/FATFS fs; /* FatFs文件系统对象 */FIL fnew; /* 文件对象 */FRESULT res_flash; /* 文件操作结果 */UINT fnum; /* 文件成功读写数量 */char fpath[100]; /* 保存当前扫描路径 */char readbuffer[512];/********************************************************************************* 任务函数*******************************************************************************//* FatFs多项功能测试 */static FRESULT miscellaneous(void){DIR dir;FATFS *pfs;DWORD fre_clust, fre_sect, tot_sect;printf("\n*************** 设备信息获取 ***************\r\n");/* 获取设备信息和空簇大小 */res_flash = f_getfree("1:", &fre_clust, &pfs);/* 计算得到总的扇区个数和空扇区个数 */tot_sect = (pfs->n_fatent - 2) * pfs->csize;fre_sect = fre_clust * pfs->csize;/* 打印信息(4096 字节/扇区) */printf("》设备总空间:%10lu KB。\n》可用空间: %10lu KB。\n", tot_sect *4, fre_sect *4);printf("\n******** 文件定位和格式化写入功能测试 ********\r\n");res_flash = f_open(&fnew, "1:FatFs读写测试文件.txt",FA_OPEN_ALWAYS|FA_WRITE|FA_READ );if ( res_flash == FR_OK ){/* 文件定位 */res_flash = f_lseek(&fnew,f_size(&fnew));if (res_flash == FR_OK){/* 格式化写入,参数格式类似printf函数 */f_printf(&fnew,"\n在原来文件新添加一行内容\n");f_printf(&fnew,"》设备总空间:%10lu KB。\n》可用空间: %10lu KB。\n", tot_sect *4, fre_sect *4);/* 文件定位到文件起始位置 */res_flash = f_lseek(&fnew,0);/* 读取文件所有内容到缓存区 */res_flash = f_read(&fnew,readbuffer,f_size(&fnew),&fnum);if(res_flash == FR_OK){printf("》文件内容:\n%s\n",readbuffer);}}f_close(&fnew);printf("\n********** 目录创建和重命名功能测试 **********\r\n");/* 尝试打开目录 */res_flash=f_opendir(&dir,"1:TestDir");if(res_flash!=FR_OK){/* 打开目录失败,就创建目录 */res_flash=f_mkdir("1:TestDir");}else{/* 如果目录已经存在,关闭它 */res_flash=f_closedir(&dir);/* 删除文件 */f_unlink("1:TestDir/testdir.txt");}if(res_flash==FR_OK){/* 重命名并移动文件 */res_flash=f_rename("1:FatFs读写测试文件.txt","1:TestDir/testdir.txt");}}else{printf("!! 打开文件失败:%d\n",res_flash);printf("!! 或许需要再次运行“FatFs移植与读写测试”工程\n");}return res_flash;}FILINFO fno;/*** 文件信息获取*/static FRESULT file_check(void){/* 获取文件信息 */res_flash=f_stat("1:TestDir/testdir.txt",&fno);if(res_flash==FR_OK){printf("“testdir.txt”文件信息:\n");printf("》文件大小: %ld(字节)\n", fno.fsize);printf("》时间戳: %u/%02u/%02u, %02u:%02u\n",(fno.fdate >> 9) + 1980, fno.fdate >> 5 & 15, fno.fdate & 31,fno.ftime >> 11, fno.ftime >> 5 & 63);printf("》属性: %c%c%c%c%c\n\n",(fno.fattrib & AM_DIR) ? 'D' : '-', // 是一个目录(fno.fattrib & AM_RDO) ? 'R' : '-', // 只读文件(fno.fattrib & AM_HID) ? 'H' : '-', // 隐藏文件(fno.fattrib & AM_SYS) ? 'S' : '-', // 系统文件(fno.fattrib & AM_ARC) ? 'A' : '-'); // 档案文件}return res_flash;}/*** @brief scan_files 递归扫描FatFs内的文件* @param path:初始扫描路径* @retval result:文件系统的返回值*/static FRESULT scan_files (char* path){FRESULT res; //部分在递归过程被修改的变量,不用全局变量FILINFO fno;DIR dir;int i;char *fn; // 文件名#if _USE_LFN/* 长文件名支持 *//* 简体中文需要2个字节保存一个“字”*/static char lfn[_MAX_LFN*2 + 1];fno.lfname = lfn;fno.lfsize = sizeof(lfn);#endif//打开目录res = f_opendir(&dir, path);if (res == FR_OK){i = strlen(path);for (;;){//读取目录下的内容,再读会自动读下一个文件res = f_readdir(&dir, &fno);//为空时表示所有项目读取完毕,跳出if (res != FR_OK || fno.fname[0] == 0) break;#if _USE_LFNfn = *fno.lfname ? fno.lfname : fno.fname;#elsefn = fno.fname;#endif//点表示当前目录,跳过if (*fn == '.') continue;//目录,递归读取if (fno.fattrib & AM_DIR){//合成完整目录名sprintf(&path[i], "/%s", fn);//递归遍历res = scan_files(path);path[i] = 0;//打开失败,跳出循环if (res != FR_OK)break;}else{printf("%s/%s\r\n", path, fn); //输出文件名/* 可以在这里提取特定格式的文件路径 */}//else} //for}return res;}/*** @brief 主函数* @param 无* @retval 无*/int main(void){/* 初始化调试串口,一般为串口1 */USART_Config();printf("******** 这是一个SPI FLASH 文件系统实验 *******\r\n");//在外部SPI Flash挂载文件系统,文件系统挂载时会对SPI设备初始化res_flash = f_mount(&fs,"1:",1);if(res_flash!=FR_OK){printf("!!外部Flash挂载文件系统失败。(%d)\r\n",res_flash);printf("!!可能原因:SPI Flash初始化不成功。\r\n");while(1);}else{printf("》文件系统挂载成功,可以进行测试\r\n");}/* FatFs多项功能测试 */res_flash = miscellaneous();printf("\n*************** 文件信息获取测试 **************\r\n");res_flash = file_check();printf("***************** 文件扫描测试 ****************\r\n");strcpy(fpath,"1:");scan_files(fpath);/* 不再使用文件系统,取消挂载文件系统 */f_mount(NULL,"1:",1);/* 操作完成,停机 */while(1){}}/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
