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

前言

接續著上一篇再來是介紹CLI菜單所提供的功能

CLI菜單內容

	"[Disk contorls]\n"
	" di <pd#> - Initialize disk\n"
	" dd [<pd#> <lba>] - Dump a secrtor\n"
	" ds <pd#> - Show disk status\n"
	"[Buffer controls]\n"
	" bd <ofs> - Dump working buffer\n"
	" be <ofs> [<data>] ... - Edit working buffer\n"
	" br <pd#> <lba> [<count>] - Read disk into working buffer\n"
	" bw <pd#> <lba> [<count>] - Write working buffer into disk\n"
	" bf <val> - Fill working buffer\n"
	"[File system controls]\n"
	" fi <ld#> [<mount>]- Force initialized the volume\n"
	" fs [<path>] - Show volume status\n"
	" fl [<path>] - Show a directory\n"
	" fo <mode> <file> - Open a file\n"
	" fc - Close the file\n"
	" fe <ofs> - Move fp in normal seek\n"
	" fd <len> - Read and dump the file\n"
	" fr <len> - Read the file\n"
	" fw <len> <val> - Write to the file\n"
	" fn <org.name> <new.name> - Rename an object\n"
	" fu <name> - Unlink an object\n"
	" fv - Truncate the file at current fp\n"
	" fk <name> - Create a directory\n"
	" fa <atrr> <mask> <object name> - Change attribute of an object\n"
	" ft <year> <month> <day> <hour> <min> <sec> <name> - Change timestamp of an object\n"
	" fx <src.file> <dst.file> - Copy a file\n"
	" fg <path> - Change current directory\n"
	" fq - Show current directory\n"
	" fb <name> - Set volume label\n"
	" fm <ld#> <type> <csize> - Create file system\n"
	" fz [<len>] - Change/Show R/W length for fr/fw/fx command\n"
	"[Misc commands]\n"
	" md[b|h|w] <addr> [<count>] - Dump memory\n"
	" mf <addr> <value> <count> - Fill memory\n"
	" me[b|h|w] <addr> [<value> ...] - Edit memory\n"
	" t [<year> <mon> <mday> <hour> <min> <sec>] - Set/Show RTC\n"

[Disk contorls] 磁碟操控

di <pd#> - Initialize disk

>di 0	//initialize device 0
rc=0	//response success

在做其它操作前的必要執行指令

指令di 使用disk_initialize()對SD card做初始化(74 clocks + CMD0 +...一連串的設定)

返回值 0 為成功;除此之外的狀態如下


dd [<pd#> <lba>] - Dump a secrtor

>dd 0 0	//Dump device 0 sector 0

指令dd 使用disk_read()讀取出指定sector的內容


ds <pd#> - Show disk status

>ds 0	//show disk 0 status

指令ds 使用disk_ioctl()讀取磁碟狀態

輸出磁碟的狀態 Driver size,Block size,CSD,CID,OCR ...



[Buffer controls]

di <pd#> - Initialize disk

>di 0	//initialize device 0
rc=0	//response success


[File system controls] 檔案系統操作

fi <ld#> [<mount>]- Force initialized the volume

>fi 0	//initialize device 0
rc=0 FR_OK    //response success

在做檔案操作前的必要執行指令

指令fi 使用f_mount()掛載磁碟,返回值對應結果如下


fs [<path>] - Show volume status

>fs 0	//show device 0 volume status

顯示此磁碟使用的File System內容

指令fs 使用f_getfree()取得volume的size及未使用空間的大小,返回值對應結果如下


fl [<path>] - Show a directory

顯示指定的資料夾檔案列表,使用的空間及剩餘的空間

>fl folderName	//show "folder" content

指令fl 使用f_opendir()打定指定的資料夾,f_readdir()取得資料夾的內容,返回值對應結果如下


不加參數時則顯示最上層的檔案及資料夾


fo <mode> <file> - Open a file

開啟檔案

<mode>:開啟的模式(參考下圖)

<file>:預開啟的檔案名

>fo 1 filename	//open file by READ mode
rc=0 FR_OK	//success

指令fl 使用f_open()開啟檔案


fc - Close the file

關閉檔案

>fo    //Close file
rc=0 FR_OK	//success

指令fc 使用f_close()關閉檔案


fe <ofs> - Move fp in normal seek

移動 read/write時的指標 延展大小

<ofs> 延展/縮減的尺寸

>fe size_value   //Close file

指令fe 使用f_lseek()移動檔案指標


fd <len> - Read and dump the file

顯示檔案內容

<len> 顯示的長度

>fd lenght   //print file content

fr <len> - Read the file

計算檔案讀取時的速度

<len> 讀取長度

>fr length   //Read file

fw <len> <val> - Write to the file

寫入檔案

<len> 寫入長度

<val> 寫入內容(ASCII十進制)

>fw length data//Write to the file

指令fw 使用f_write()來寫入檔案

注意:官方此函式有問題,只能寫入1個字元


fn <org.name> <new.name> - Rename an object

改名

<org.name> 預改名稱的檔案

<new.name> 更改後的名稱

>fn oldFile newName//rename file

指令fw 使用f_rename()來為檔案更名


fu <name> - Unlink an object

刪除檔案

<name> 被刪除的檔案

>fn filename//delete file

指令fw 使用f_unlink()來刪除檔案


fv - Truncate the file at current fp

截斷檔案大小

>fv //rename file

指令fv 使用f_truncate()


fk <name> - Create a directory

建立資料夾

<name> 資料夾名稱

>fk foldername //create folder

指令fv 使用f_mkdir()來產生新的folder


fa <atrr> <mask> <name> - Change attribute of an object

改變物件的屬性

>fa //change object attribute

指令fa 使用f_chmod()


ft <year> <month> <day> <hour> <min> <sec> <name> - Change timestamp of an object

修改檔案的時間標記

>fa //change object timestamp

指令ft 使用f_utime()


fx <src.name> <dst.name> - Copy a file

複製檔案

<src.name> 檔案來源

<dst.name> 新檔案名稱

>fx oldname newname //copy a file

fg <path> - Change current directory

切換路徑

>fg path //change path

指令ft 使用f_chdir()


fq - Show current dir path

顯示目前路徑

>fq //show current path

指令ft 使用f_getcwd()


fb <name> - Set volume label

設定Volume標籤

<name> 標籤名稱

>fb name //set volume label

指令ft 使用f_setlabel()


fm <ld#> <type> <csize> - Create file system

建立File System

<ld#> 標籤名稱

<type> 標籤名稱

<csize> 標籤名稱

>fm //show current path

指令ft 使用f_mkfs()


fz [<len>] - Change/Show R/W length for fr/fw/fx command

改變/顯示 讀/寫的長度

<len>  改變的長度

>fz length//change r/w length

t [<year> <mon> <mday> <hour> <min> <sec>] - Set/Show RTC

顯示/改變RTC時間


留言