stm32 挂载FatFs时格式化硬盘使用函数f_mkfs出错
在f_mkfs之前已调用f_mout函数返回无文件系统正常,但是格式化SD卡时调用f_mkfs返回1,请问一下这可能是什么导致的?
typedef enum {
FR_OK = 0, /* (0) Succeeded */
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
FR_INT_ERR, /* (2) Assertion failed */
FR_NOT_READY, /* (3) The physical drive cannot work */
FR_NO_FILE, /* (4) Could not find the file */
FR_NO_PATH, /* (5) Could not find the path */
FR_INVALID_NAME, /* (6) The path name format is invalid */
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
FR_EXIST, /* (8) Access denied due to prohibited access */
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
FR_NOT_ENABLED, /* (12) The volume has no work area */
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */
FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_LOCK */
FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
} FRESULT;
按照这个说法应该是底层IO硬件错误,但是前面SD卡初始化已经正常了呀,问题可能出在哪里呢
即使 SD 卡初始化成功,但是在格式化时仍然有可能出现底层 IO 硬件的问题。可能是因为在格式化时需要对 SD 卡进行更复杂的读写操作,而初始化只是简单的读取 SD 卡的信息。因此,即使初始化成功,仍然可能在格式化时遇到底层 IO 硬件的问题。
除了硬件问题之外,还有一些其它的可能原因,比如使用的 FATFS 版本不兼容或者使用的 SD 卡不支持某些特定的格式化方式等。您可以先检查一下 SD 卡的规格和 FATFS 版本是否兼容,以及格式化时的参数是否正确设置。
你看看下面这几个你试着来排查:
1、硬件:错误码 FR_DISK_ERR 表示底层的 IO 硬件存在问题,可能是由于SD卡损坏或其它原因导致的。可以换SD卡或换另一个 SD 卡槽。
2、参数错误:错误码 FR_MKFS_ABORTED 表示格式化过程被中止了,可能是因为输入参数不正确。检查你的调用 f_mkfs 函数时的参数是否对了没有;
3、访问权限问题:错误码 FR_DENIED 表示访问权限受到了限制。检查 SD 卡的访问权限是否正确设置。
4、写保护问题:错误码 FR_WRITE_PROTECTED 表示 SD 卡处于写保护状态,可能需要关闭写保护才能格式化。都试试吧
注:函数具体功能及参数配置见链接:
链接:
FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
//打开或创建一个文件
FRESULT f_close (FIL* fp); /* Close an open file object */
//关闭文件
FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from a file */
//从文件里面读取数据
FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to a file */
//向文件里面写入数据
FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
FRESULT f_lseek (FIL* fp, DWORD ofs); /* Move file pointer of a file object */
//移动文件内部数据指针到指定位置,或者扩展存储区
FRESULT f_truncate (FIL* fp); /* Truncate file */
FRESULT f_sync (FIL* fp); /* Flush cached data of a writing file */
FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */
FRESULT f_closedir (DIR* dp); /* Close an open directory */
FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */
FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */
FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */
//创建一个子文件夹
FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
//删除存在的文件或文件夹
FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
//重命名或移动文件(文件夹)
FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of the file/dir */
FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change times-tamp of the file/dir */
FRESULT f_chdir (const TCHAR* path); /* Change current directory */
FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
//创建逻辑工作区
FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au); /* Create a file system on the volume */
FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work); /* Divide a physical drive into some partitions */
int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
//写入一个字符型数据
int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */
//写入字符串
int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
//写入标准化字符串
TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
//从文件中获取字符串