STM32 + FatFs + SD card via SPI【三】FatFS指令操作II

1. 系統初始與掛載

這是每次啟動時都要做的動作

>di 0	//disk_initialize() SD卡的上電程序
rc=0	//回覆成功

>ds 0	//disk_ioctl() 查看SD卡狀態(非必要)
Drive size: 15130624 sectors
Block size: 8192 sectors
Media type: 20
CSD:
00000000  40 0E 00 32 5B 59 00 00 39 B7 7F 80 0A 40 00 3B  @..2[Y..9....@.;
CID:
00000000  02 54 4D 53 41 30 38 47 14 44 2C DC 5A 00 F2 F9  .TMSA08G.D,.Z...
OCR:
00000000  C0 FF 80 00  ....
SD Status:
00000000  00 00 00 00 03 00 00 00 02 02 90 01 00 AA 00 00  ................
00000010  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000020  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000030  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

>fi 0	//f_mount() 掛載volume
rc=0 FR_OK	//回覆成功


2. disk檔案列表

指令fl不加參數 顯示當前目錄

>fl	//f_readdir() 讀取目錄內容
D-HS- 2024/09/19 10:19         0  System Volume Information
D---- 2072/09/13 15:25         0  folder2
D---- 2024/09/19 13:54         0  folder1
----A 2024/08/29 15:48    230454  st_logo1.bmp
----A 2072/09/13 13:29        24  abc.txt
   2 File(s),    230478 bytes total
   3 Dir(s), 7729836032 bytes free

查看資料夾內容

>fl folder1	//查看folder1資料夾
----A 2024/09/19 13:55        22  test2.txt
   1 File(s),        22 bytes total
   0 Dir(s), 7729836032 bytes free


3. 讀取檔案

>fo 1 abc.txt	//f_open() 讀取檔案,參數 1 為read mode
rc=0 FR_OK

>fd 24	//顯示檔案24個字元,length大於檔案長度也沒關係
00000000  48 65 6C 6C 6F 20 41 42 43 20 57 6F 72 6C 64 21  Hello ABC World!
00000010  32 30 32 34 0D 0A 00 78  2024...x

再次執行fd 24則沒有任何資料顯示,因為上一個fd 24執行後,當前指標已移動到第25個字元位置

使用指令 fe 可以定位pointer

>fe 0	//f_lseek() 定位read/wirte時的指標位置
rc=0 FR_OK
fptr=0(0x0)

>fd 24	//在重新定址後就可以再次顯示內容
00000000  48 65 6C 6C 6F 20 41 42 43 20 57 6F 72 6C 64 21  Hello ABC World!
00000010  32 30 32 34 0D 0A 00 78  2024...x

執行完操作後把檔案關閉

>fc	//f_close()
rc=0 FR_OK


4. 建立新檔案並寫入內容

產生名為 xyz.txt檔案

>fo 4 xyz.txt	//f_open() 建立檔案,參數 4 為create mode
rc=0 FR_OK
>fl
D-HS- 2024/09/19 10:19         0  System Volume Information
D---- 2072/09/13 15:25         0  folder2
----A 2072/09/13 13:57         0  xyz.txt
D---- 2024/09/19 13:54         0  folder1
----A 2024/08/29 15:48    230454  st_logo1.bmp
----A 2072/09/13 13:29        24  abc.txt
   3 File(s),    230478 bytes total
   3 Dir(s), 7729836032 bytes free

為xyz.txt寫入數值0~9十個數

>fo 2 xyz.txt	//f_open() 開啟檔案,參數 2 為write mode
rc=0 FR_OK

>fw 10 48 49 50 51 52 53 54 55 56 57	//f_write()寫入檔案,長度10,ASCII十進制字元
10 bytes written with 0 kB/sec.

>fc	//關閉檔案
rc=0 FR_OK

>fl	//可以看到xyz.txt的size變為10 bytes
D-HS- 2024/09/19 10:19         0  System Volume Information
D---- 2072/09/13 15:25         0  folder2
----A 2072/09/13 14:01        10  xyz.txt
D---- 2024/09/19 13:54         0  folder1
----A 2024/08/29 15:48    230454  st_logo1.bmp
----A 2072/09/13 13:29        24  abc.txt
   3 File(s),    230488 bytes total
   3 Dir(s), 7729831936 bytes free
   
>fo 1 xyz.txt
rc=0 FR_OK

>fd 10	//顯示xyz.txt的內容
00000000  30 31 32 33 34 35 36 37 38 39  0123456789


5. 刪除檔案

>fl
D-HS- 2024/09/19 10:19         0  System Volume Information
D---- 2072/09/13 15:25         0  folder2
----A 2072/09/13 14:01        10  xyz.txt
D---- 2024/09/19 13:54         0  folder1
----A 2024/08/29 15:48    230454  st_logo1.bmp
----A 2072/09/13 13:29        24  abc.txt
   3 File(s),    230488 bytes total
   3 Dir(s), 7729831936 bytes free
   
>fu xyz.txt	//f_unlink()刪除檔案
rc=0 FR_OK

>fl	//檔案xyz.txt消失了
D-HS- 2024/09/19 10:19         0  System Volume Information
D---- 2072/09/13 15:25         0  folder2
D---- 2024/09/19 13:54         0  folder1
----A 2024/08/29 15:48    230454  st_logo1.bmp
----A 2072/09/13 13:29        24  abc.txt
   2 File(s),    230478 bytes total
   3 Dir(s), 7729836032 bytes free