FATFS 初学之 f_mount

 1 /*-----------------------------------------------------------------------*/
 2 /* Mount/Unmount a Logical Drive                                         */
 3 /*-----------------------------------------------------------------------*/
 4 
 5 FRESULT f_mount (
 6     BYTE vol,        /* Logical drive number to be mounted/unmounted */
 7     FATFS *fs        /* Pointer to new file system object (NULL for unmount)*/
 8 )
 9 {
10     FATFS *rfs;
11 
12 
13     if (vol >= _VOLUMES)        /* Check if the drive number is valid */
14         return FR_INVALID_DRIVE;
15     rfs = FatFs[vol];            /* Get current fs object */
16 
17     if (rfs) {
18 #if _FS_SHARE
19         clear_lock(rfs);
20 #endif
21 #if _FS_REENTRANT                /* Discard sync object of the current volume */
22         if (!ff_del_syncobj(rfs->sobj)) return FR_INT_ERR;
23 #endif
24         rfs->fs_type = 0;        /* Clear old fs object */
25     }
26 
27     if (fs) {
28         fs->fs_type = 0;        /* Clear new fs object */
29 #if _FS_REENTRANT                /* Create sync object for the new volume */
30         if (!ff_cre_syncobj(vol, &fs->sobj)) return FR_INT_ERR;
31 #endif
32     }
33     FatFs[vol] = fs;            /* Register new fs object */
34 
35     return FR_OK;
36 }
View Code

 1 FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical drives) */ 

函数功能:注册/注销一个工作区(挂载/注销分区文件系统)

描述:在使用任何其它文件函数之前,必须使用该函数为每个使用卷注册一个工作区。要注销一个工作区,只要指定 fs为 NULL即可,然后该工作区可以被丢弃。

该函数只初始化给定的工作区,以及将该工作区的地址注册到内部表中,不访问磁盘I/O层。卷装入过程是在 f_mount函数后或存储介质改变后的第一次文件访问时完成的。

通俗了说就是给磁盘分配文件系统的:计算机中的盘符是 C: D: E;FATFS的盘符是 0: 1: 2:

f_mount(0,&fs);  // 为 0号盘符分配新的文件系统 fs,fs是 FATFS类型,用于记录逻辑磁盘工作区
 1 /*-----------------------------------------------------------------------*/
 2 /* Mount/Unmount a Logical Drive                                         */
 3 /*-----------------------------------------------------------------------*/
 4 FRESULT f_mount (
 5     BYTE vol,        /* 逻辑驱动器安装/卸载 */
 6     FATFS *fs        /* 新的文件系统对象指针(卸载NULL) */
 7 )
 8 {
 9     FATFS *rfs;
10 
11     if (vol >= _VOLUMES)        /* 检查驱动器号是否有效 */
12         return FR_INVALID_DRIVE;
13     rfs = FatFs[vol];            /* 获得原盘符下对象 */
14 
15     if (rfs) {                    /* 检查原盘符下是否挂有文件系统,若有则释放掉 */
16 #if _FS_SHARE
17         clear_lock(rfs);
18 #endif
19 #if _FS_REENTRANT                /* Discard sync object of the current volume */
20         if (!ff_del_syncobj(rfs->sobj)) return FR_INT_ERR;
21 #endif
22         rfs->fs_type = 0;        /* Clear old fs object */
23     }
24 
25     if (fs) {                    /* 检测参数,若为 NULL则表示卸载盘符,下面两步执行无意义 */
26         fs->fs_type = 0;        /* 0: 未装载 */
27 #if _FS_REENTRANT                /* Create sync object for the new volume */
28         if (!ff_cre_syncobj(vol, &fs->sobj)) return FR_INT_ERR;
29 #endif
30     }
31     FatFs[vol] = fs;            /* 注册新的对象 */
32 
33     return FR_OK;
34 }
原文地址:https://www.cnblogs.com/Danhuise/p/3909198.html