完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
自己移植的SD卡的FATFS文件系统,采用最新版FATFS R0.09,并且有详细的中文注释,和操作测试程序,完整的MDK STM32F103工程。下载即可使用。 /*----------------------------------------------------------------------------/ / FatFs - FAT file system module R0.09 (C)ChaN, 2011 /-----------------------------------------------------------------------------/ / FatFs module is a generic FAT file system module for small embedded systems. / This is a free software that opened for education, research and commercial / developments under license policy of following terms. / / Copyright (C) 2011, ChaN, all right reserved. / / * The FatFs module is a free software and there is NO WARRANTY. / * No restriction on use. You can use, modify and redistribute it for / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY. / * Redistributions of source code must retain the above copyright notice. / /-----------------------------------------------------------------------------/ /*--------------File Info------------------------------------------------------- ** 文 件 名: FATFS_Function.c ** 作 者:~风中的叶~ 整理 ** 最后修改日期: 2012.01.18 ** 版 本: V1.0 ** 描 述: FATFS常用功能测试函数 .连接PC机串口,观察超级终端输出 **------------------------------------------------------------------------------ ** Created by: Dt. ** Created date: *******************************************************************************/ #include "TEST_FATFS.h" #include "string.h" #ifdef TEST_FATFS_EN //检测磁盘是否插好 BOOL disk_detect_OK(void) { if( disk_status(0)==STA_NODISK )/* Physical drive nmuber (0..) */ { printf("rnrn%48srn","<未检测到磁盘,请检查SD卡是否插好...>"); return FALSE; } return TRUE; } //测试函数执行结果分析 void die(FRESULT res) { switch(res) { case FR_OK: //The function succeeded. { printf("rnThe function succeeded!rn"); break; } case FR_NOT_READY://The disk drive cannot work due to no medium in the drive or any other reason { printf("rnThe disk drive cannot work due to no medium in the drive or any other reason!rn"); break; } case FR_NO_FILE://Could not find the file. { printf("rnCould not find the file!rn"); break; } case FR_NO_PATH://Could not find the path { printf("rnCould not find the path!rn"); break; } case FR_INVALID_NAME://The path name is invalid { printf("rnThe path name is invalid!rn"); break; } case FR_INVALID_DRIVE://The drive number is invalid { printf("rnThe drive number is invalid!rn"); break; } case FR_DENIED://The directory cannot be created due to directory table or disk is full. { printf("rnThe directory cannot be created due to directory table or disk is full!rn"); break; } case FR_EXIST://A file or directory that has same name is already existing { printf("rnA file or directory that has same name is already existing!rn"); break; } // case FR_RW_ERROR://The function failed due to a disk error or an internal error /* case FR_RW_ERROR://The function failed due to a disk error or an internal error { printp("rnThe function failed due to a disk error or an internal error!rn"); break; } */ case FR_WRITE_PROTECTED://The medium is write protected { printf("rnThe medium is write protected!rn"); break; } case FR_NOT_ENABLED://The logical drive has no work area { printf("rnThe logical drive has no work area!rn"); break; } case FR_NO_FILESYSTEM://There is no valid FAT partition on the disk { printf("rnThere is no valid FAT partition on the disk!rn"); break; } case FR_INVALID_OBJECT://The file object is invalid { printf("rnThe file object is invalid!rn"); break; } //The function aborted before start in format due to a reason as follows. //The disk size is too small. //Invalid parameter was given to any parameter. //Not allowable cluster size for this drive. This can occure when number of clusters becomes around 0xFF7 and 0xFFF7. case FR_MKFS_ABORTED:// { printf("rnThe function aborted before start in format!rn"); break; } default: { printf("rnerror!rn"); break; } } return; } void Test_f_getfree(void)//获取卡的总容量及剩余容量 { FATFS fs; FATFS *pfs; DWORD clust; FRESULT res; // FatFs function common result code //检测磁盘是否插好 if( disk_detect_OK()==FALSE ) return; pfs=&fs;//指向 // Register a work area for logical drive 0 f_mount(0, &fs);//安装FATFS,就是给FATFS分配空间 // Get free clusters res = f_getfree("/", &clust, &pfs);//必须是根目录,默认磁盘0;"/"或者"0:/" if ( res==FR_OK ) { // Get free space printf("rn%d MB total disk space.rn%d MB available on the disk.rn", (DWORD)(pfs->n_fatent - 2) * pfs->csize /2/1024,//总的磁盘空间M =(总簇数-2)*每簇的扇区数/2/1024=可用簇数*每簇的扇区数/2/1024 clust * pfs->csize /2/1024);//空闲的磁盘空间M=剩余簇数*每簇的扇区数/2/1024 } else die(res);//测试函数执行结果分析 // Unregister a work area before discard it f_mount(0, NULL);//卸载FATFS,就是释放FATFS结构体所占空间 } void Test_f_read(void)//读文件的数据,如果没有此文件则返回错误 { FATFS fs; // Work area (file system object) for logical drive FIL fsrc; // file objects BYTE buffer[512]; // file copy buffer FRESULT res; // FatFs function common result code UINT br; // File R count u16 i; char path[20]; //检测磁盘是否插好 if( disk_detect_OK()==FALSE ) return; // Register a work area for logical drive 0 f_mount(0, &fs); printf("rnread file:>"); USART_Scanf_Name(path);//通过串口输入文件路径名/dir/file.txt或者0:dir/file.txt或者0:/dir/file.txt //Open source file res = f_open(&fsrc, path, FA_OPEN_EXISTING | FA_READ);//打开存在的文件,如果没有则返回错误 die(res); //buffer空间设大一点,会提高读的速度。 //如果文件实际大小512byte, //设为buffer[512]时,只需要循坏一次,如果设为buffer[1],需要循坏512次。 //下面两行主要是去除1s误差。 for (;;) { for(i=0;i |