FatFs Module Application Note

Started by tha, May 05, 2022, 10:36:15 AM

Previous topic - Next topic

tha

Long File Name

FatFs module รองรับ the long file name (LFN) extension ของ the FAT filesystem. The two different file names, short file name (SFN) and LFN, ของ a file เปิดเผยบน the API. การรองรับสำหรับ LFN feature ถูกปิดการใช้งานโดยเริ่มต้น. เพื่อเปิดการใช้งาน the LFN, เซ็ต FF_USE_LFN เป็น 1, 2 or 3, และเพิ่ม ffunicode.c ไปยัง the project. The LFN feature ต้องการ a certain working buffer. The buffer size สามารถถูกกำหนดค่าโดย FF_MAX_LFN สอดคล้องกับ the available memory. The length of LFN สามารถยาวถึง 255 characters, ดังนั้น the FF_MAX_LFN ควรถูกเซ็ตถึง 255 สำหรับ any existing file names. ถ้า the size of working buffer ไม่เพียงพอสำหรับ the input file name, the file function ล้มเหลวด้วย FR_INVALID_NAME. เมื่อใช้การกลับเข้ามาใหม่ใดๆไปยัง the API ด้วย LFN feature ใน RTOS environment, FF_USE_LFN ต้องถูกเซ็ตเป็น 2 or 3. ในกรณีนี้, the file function จัดสรร the working buffer บน the stack or heap. The LFN working buffer ครอบครอง (FF_MAX_LFN + 1) * 2 bytes และเพิ่มเติม (FF_MAX_LFN + 44) / 15 * 32 bytes เมื่อ exFAT ถูกเปิดการใช้งาน.

tha

Impact upon Module Size

เมื่อ the LFN ถูกเปิดการใช้งาน, the module size จะถูกเพิ่มขึ้นขึ้นอยู่กับ the configured code page. ตารางด้านขวาแสดงการเพิ่มขึ้นของ code size ในบาง code pages. โดยเฉพาะอย่างยิ่ง, ใน the CJK region, หลายหมื่น characters กำลังถูกใช้. น่าเสียดาย, มันต้องการ a huge OEM-Unicode bidirectional conversion table และ the module size จะถูกเพิ่มขึ้นอย่างมากตามที่แสดงในตาราง.
ด้วยเหตุนี้, the FatFs ที่มี LFN ที่เปิดการใช้งานด้วย DBCS code pages จะไม่สามารถถูกพอร์ตบน the most 8-bit MCU systems. ถ้า the target system อยู่ใน legacy-free, ในเฉพาะ Unicode และ any ANSI/OEM code ไม่ถูกใช้ได้เลย, the code page setting จะไม่มีความหมาย. คุณจะสามารถลด the code size ได้โดยการกำหนดค่า FatFs สำหรับ Unicode API ด้วย any SBCS code page.
มีข้อจำกัดบางประการเกี่ยวกับการใช้ LFN สำหรับ open source project, เนื่องจาก the LFN extension บน the FAT filesystem เคยเป็นสิทธิบัตรของ Microsoft Corporation. อย่างไรก็ตามสิทธิบัตรที่เกี่ยวข้องทั้งหมดได้หมดอายุและการใช้ the LFN feature นั้นฟรีสำหรับ projects ใดๆ.


tha

Unicode API

โดยค่าเริ่มต้น, FatFs ใช้ ANSI/OEM code เซ็ตบน the API แม้ใน LFN configuration. FatFs ยังสามารถสวิทช์ the character encoding บน the API ไปยัง Unicode โดย configuration option FF_LFN_UNICODE. นี้หมายความว่า FatFs เป็นไปตาม the full featured LFN specification. The data type TCHAR ระบุ path name strings บน the API เป็นนามแฝงของอย่างใดอย่างหนึ่ง char(ANSI/OEM or UTF-8), WCHAR(UTF-16) or DWORD(UTF-32) ขึ้นอยู่กับ option นั้น. สำหรับข้อมูลเพิ่มเติม โปรดดูที่คำอธิบายใน the file name.
โปรดทราบว่าการเซ็ต code page, FF_CODE_PAGE, นั้นไม่มีความหมายจริง ๆ เมื่อ FatF ถูกกำหนดค่าสำหรับ the Unicode API. มันควรถูกเซ็ต 437 เพื่อลด the module size. อย่างไรก็ตาม มันยังคงส่งผลต่อ code conversion ของ string I/O functions เมื่อ FF_STRF_ENCODE == 0, และยังเข้ากันได้แบบย้อนหลังกับระบบเดิม. ในกรณีนี้ , the code page อาจจำเป็นต้องถูกกำหนดค่าอย่างเหมาะสมถ้ามันถูกพิจารณาว่ามีปัญหา.


tha

exFAT Filesystem

The exFAT (Microsoft's Extended File Allocation Table) filesystem เป็นการสืบทอดของ the FAT/FAT32 filesystem ซึ่งถูกใช้อย่างแพร่หลายใน embedded systems, consumer devices และ portable storage media. มันถูกนำมาใช้โดย SDA (SD Association) เป็น the filesystem สำหรับ SDXC card, 64 GB และใหญ่กว่า, และกำลังถูกจัดส่งพร้อมกับรูปแบบนี้.  ดังนั้น the exFAT เป็นหนึ่งใน the standard filesystems สำหรับ removable media เช่นเดียวกับ FAT. The exFAT filesystem ยอมให้ the file size เกิน the 4 GB limit ที่ FAT filesystem ยอมให้สูงถึงและบาง filesystem overhead, โดยเฉพาะ cluster allocation delay, ถูกลดเช่นกัน. คุณลักษณะเหล่านี้ช่วยให้สามารถบันทึกข้อมูลขนาดใหญ่โดยไม่ต้องแบ่งออกเป็นไฟล์บางไฟล์และปรับปรุงปริมาณงานเขียนไปยังไฟล์.
โปรดทราบว่า the exFAT filesystem เป็นสิทธิบัตรของ Microsoft Corporation. The exFAT feature ของ FatFs เป็นการใช้งานตาม US. Pat. App. Pub. No. 2009/0164440 A1. FatFs module สามารถสวิทช์ the exFAT on หรือ off โดย a configuration option, FF_FS_EXFAT. เมื่อเปิดการใช้งาน the exFAT สำหรับ the commercial products,  ใบอนุญาตจาก Microsoft จะถูกต้องการขึ้นอยู่กับปลายทางสุดท้ายของผลิตภัณฑ์.
ข้อสังเกต: การเปิดการใช้งาน exFAT จะละทิ้งความเข้ากันได้กับ C89 และมันต้องการ C99 เนื่องจากต้องใช้ประเภทจำนวนเต็ม 64 บิต.

tha

64-bit LBA

LBA (Logical Block Addressing) เป็น an addressing method เพื่อระบุตำแหน่งของ data block, ที่เรียกว่า sector, บน the storage media. มันเป็น a simple linear address เริ่มต้นจาก 0 เป็น the first sector, 1 เป็น the second sector และต่อไป. The host system ไม่จำเป็นต้องพิจารณาว่าวิธีที่ the data block ถูกวางตำแหน่งและถูกจัดการอย่างไรใน the storage device. FatFs รองรับเฉพาะ LBA สำหรับ the media access. 32-bit LBA เป็น a common size ใน the most LBA scheme. มันสามารถแอดเดรสส์ได้มากถึง to 232 sectors, 2 TB ใน 512 bytes/sector. เมื่อ a storage device ที่ใหญ่กว่า 2 TB ถูกใช้, larger sector size หรือ 64-bit LBA จะถูกจำเป็นเพื่อแอดเดรสส์ the entire sectors ของ the storage device.
โดยค่าเริ่มต้น, FatFs ทำงานใน 32-bit LBA for media access. FatFs ยังสามารถสวิทช์มันไปยัง 64-bit LBA โดย a configuration option FF_LBA64. มันยังเปิดการใช้งาน GPT (GUID Partition Table) สำหรับ partiotion management บน the storage device ได้อีกด้วย. สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ GPT โปรดดูที่ f_mkfs และ f_fdisk function.

tha

Re-entrancy

The file operations ของ two tasks ไปยัง the different volumes แต่ละอย่างจะกลับเข้ามาใหม่เสมอโดยไม่คำนึงถึง the configurations ยกเว้นเมื่อ LFN ถูกเปิดการใช้งานด้วย static working buffer (FF_USE_LFN = 1). มันสามารถทำงานได้พร้อมกันโดยไม่มี mutual exclusion ใดๆ.
The file operations ของ two tasks ไปยัง the same volume จะไม่กลับเข้ามาใหม่โดยค่าเริ่มต้น. FatFs ยังสามารถถูกกำหนดค่าเพื่อทำให้มัน thread-safe โดย option FF_FS_REENTRANT. ในกรณีนี้, the OS dependent synchronization control functions อีกด้วย, ff_cre_syncobj/ff_del_syncobj/ff_req_grant/ff_rel_grant, จำเป็นต้องถูกเพิ่มไปยัง the project. มีบางตัวอย่างใน the ffsystem.c. เมื่อ a file function ถูกเรียกขณะที่ the volume กำลังถูกเข้าถึงโดยอีก task หนึ่ง, the file function to the volume จะถูกค้างไว้จนกระทั่ง task นั้นออกจาก the file function. ถ้า the wait time เกินกว่า a period ที่กำหนดโดย FF_TIMEOUT, the file function จะยกเลิกด้วย FR_TIMEOUT. The timeout feature อาจจะไม่ถูกรองรับบน the some RTOSs.
มีข้อยกเว้นบน the re-entrancy สำหรับ f_mount/f_mkfs function. These volume management functions ไม่กลับเข้ามาใหม่ไปยัง the same volume. เมื่อใช้ these functions, other tasks จำเป็นต้องหลีกเลี่ยงการเข้าถึง the volume.
หมายเหตุ: ส่วนนี้อธิบายเกี่ยวกับ the re-entrancy of the FatFs module เอง. The FF_FS_REENTRANT option เปิดการใช้งานเฉพาะการใช้อย่างพิเสษของแต่ละ filesystem objects และ FatFs does ไม่ได้ขัดขวางการกลับเข้าสู่ the storage device control functions. ดังนั้น the device control layer จำเป็นต้องเป็น thread-safe เสมออเมื่อ FatFs API ถูกเข้าสู่ใหม่สำหรับ different volumes. ตารางด้านขวาแสดงซึ่ง control function สามารถถูกกลับสู่เมื่อ FatFs API ถูกเข้าสู่ซ้ำบนบางเงื่อนไข.

tha

Duplicated File Open

FatFs module ไม่รองรับ the read/write collision control ของการเปิดซ้ำไปยัง a file. The duplicated open ถูกยอมให้เฉพาะเมื่อแต่ละ open method ไปยัง a file เป็น read mode. The duplicated open ด้วยอย่างน้อยหนึ่ง write mode ไปยัง a file ถูกห้ามเสมอ, และ open file ต้องไม่ถูกเปลี่ยนชื่อหรือถูกลบด้วย. การละเมิดกฎเหล่านี้สามารถเป็นเหตุให้ data collaption.
The file lock control สามารถถูกเปิดการใช้งานโดย FF_FS_LOCK option. ค่าของ option กำหนดจำนวนของ open objects เพื่อจัดการพร้อมกัน. ในกรณีนี้, ถ้าการ opening, renaming or removing ใดๆที่ขัดต่อ the file shareing rule ที่อธิบายข้างบนถูกพยายาม, the file function จะถูกปฏิเสธด้วย FR_LOCKED. ถ้าจำนวนของ open objects, files and sub-directories, เท่ากันกับ FF_FS_LOCK, an extra f_open/f_opendir function จะล้มเหลวด้วย FR_TOO_MANY_OPEN_FILES.

tha

Performance Effective File Access

เพื่อ good read/write throughput บน the small embedded systems ที่มีขนาดที่จำกัดของ memory, application programmer ควรพิจารณาว่า process อะไรที่ถูกทำใน the FatFs module. The file data บน the volume จะถูกส่งถ่ายในลำดับต่อไปนี้โดย f_read function.

The file I/O buffer คือ a sector buffer เพื่อ read/write ส่วนหนึ่งของ data บน the sector. The sector buffer เป็นอย่างใดอย่างหนึ่ง file private sector buffer บนแต่ละ file object หรือ shared sector buffer ใน the filesystem object. The buffer configuration option FF_FS_TINY กำหนดว่า sector buffer ใดถูกใช้สำหรับ the file data transfer. เมื่อ tiny buffer configuration (1) ถูกเลือก, data memory consumption จะถูกลดลง FF_MAX_SS bytes แต่ละ file object. ในกรณีนี้, FatFs module ใช้เฉพาะ a sector buffer ใน the filesystem object สำหรับ file data transfer และ FAT/directory access. ข้อเสียของ the tiny buffer configuration คือ: the FAT data ที่แคชใน the sector buffer จะสูญเสียโดย file data transfer และมันต้องถูกโหลดซ้ำที่ทุกๆ cluster boundary. อย่างไรก็ตามมันจะเหมาะสำหรับ application ส่วนใหญ่จากมุมมองของ the decent performance และ low memory comsumption.
Figure 1 แสดงว่า a partial sector, sector unaligned part of the file, ถูกส่งถ่ายโดยทาง the file I/O buffer. ที่ long data transfer ที่แสดงใน Figure 2, ตรงกลางของ transfer data ที่ครอบคลุมอย่างน้อยหนึ่ง sector ถูกส่งถ่ายไปยัง the application buffer โดยตรง. Figure 3 แสดงว่ากรณีของ transfer data ทั้งหมดถูกทำให้เป็นแถวเดียวกันกับ the sector boundary. ในกรณีนี้, file I/O buffer ไม่ถูกใช้. บน the direct transfer, the maximum extent of sectors ถูกอ่านด้วย disk_read function ในแต่ละครั้งแต่ the multiple sector transfer จะถูกแบ่งที่ cluster boundary ถึงแม้ว่ามันจะต่อเนื่องกันก็ตาม.
ดังนั้น การใช้ความพยายามเพื่อ sector aligned read/write accesss จะกำจัด buffered data transfer และ the read/write performance จะถูกปรับปรุงให้ดีขึ้น. นอกจากผลนี้แล้ว, cached FAT data จะไม่ถูกล้างโดย file data transfer ที่ the tiny configuration, ดังนั้นมันสามารถบรรลุประสิทธิภาพเช่นเดียวกับ non-tiny configuration ที่มี small memory footprint.