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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM