今天终于把FATFS文件系统的中文长文件名弄个端倪出来了。可以在串口输出根目录甚至是二级目录下的文件,虽然还是有些小问题需要进一步调试。
第一步,当然是下载,这个很容易,下载到最新的FF0.9a就行。
第二步,当然是加入工程,这个不展开,然后就是配置ffconf.h这个文件啦。
#ifndef _FFCONF #define _FFCONF 4004 /* Revision ID */ /*---------------------------------------------------------------------------/ / Functions and Buffer Configurations /----------------------------------------------------------------------------*/ #define _USE_STRFUNC 1 /* 0:Disable or 1-2:Enable */ //Ö§³Ö×Ö·û´®ÀຯÊý /* To enable string functions, set _USE_STRFUNC to 1 or 2. */ #define _USE_MKFS 1 /* 0:Disable or 1:Enable */ //ʹÄܸñʽ»¯ /* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */ #define _USE_FORWARD 0 /* 0:Disable or 1:Enable */ /* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */ #define _USE_FASTSEEK 1 /* 0:Disable or 1:Enable */ //ʹÄÜ¿ìËÙ²éÕÒÌØÐÔ /* To enable fast seek feature, set _USE_FASTSEEK to 1. */ /*---------------------------------------------------------------------------/ / Locale and Namespace Configurations /----------------------------------------------------------------------------*/ #define _CODE_PAGE 936 // /* The _CODE_PAGE specifies the OEM code page to be used on the target system. / Incorrect setting of the code page can cause a file open failure. / / 932 - Japanese Shift-JIS (DBCS, OEM, Windows) / 936 - Simplified Chinese GBK (DBCS, OEM, Windows) / 949 - Korean (DBCS, OEM, Windows) / 950 - Traditional Chinese Big5 (DBCS, OEM, Windows) / 1250 - Central Europe (Windows) / 1251 - Cyrillic (Windows) / 1252 - Latin 1 (Windows) / 1253 - Greek (Windows) / 1254 - Turkish (Windows) / 1255 - Hebrew (Windows) / 1256 - Arabic (Windows) / 1257 - Baltic (Windows) / 1258 - Vietnam (OEM, Windows) / 437 - U.S. (OEM) / 720 - Arabic (OEM) / 737 - Greek (OEM) / 775 - Baltic (OEM) / 850 - Multilingual Latin 1 (OEM) / 858 - Multilingual Latin 1 + Euro (OEM) / 852 - Latin 2 (OEM) / 855 - Cyrillic (OEM) / 866 - Russian (OEM) / 857 - Turkish (OEM) / 862 - Hebrew (OEM) / 874 - Thai (OEM, Windows) / 1 - ASCII only (Valid for non LFN cfg.) */ #define _USE_LFN 3 /* 0 to 3 ÉèÖÃΪ1,Ö§³Ö³¤ÎļþÃû£¬²¢²ÉÓö¯Ì¬ÄÚ´æ*/ #define _MAX_LFN 255 /* Maximum LFN length to handle (12 to 255) */ /* The _USE_LFN option switches the LFN support. / / 0: Disable LFN feature. _MAX_LFN and _LFN_UNICODE have no effect. / 1: Enable LFN with static working buffer on the BSS. Always NOT reentrant. / 2: Enable LFN with dynamic working buffer on the STACK. / 3: Enable LFN with dynamic working buffer on the HEAP. / / The LFN working buffer occupies (_MAX_LFN + 1) * 2 bytes. To enable LFN, / Unicode handling functions ff_convert() and ff_wtoupper() must be added / to the project. When enable to use heap, memory control functions / ff_memalloc() and ff_memfree() must be added to the project. */ #define _LFN_UNICODE 0 /* 0:ANSI/OEM or 1:Unicode */ /* To switch the character code set on FatFs API to Unicode, / enable LFN feature and set _LFN_UNICODE to 1. */ #endif /* _FFCONFIG */
默认为0的我有些都删掉了,上面的配置信息供参考。注意两点:打开长文件名-选择方式3,codepage 936。然后添加option文件夹下的cc936.c和syscall.c两个文件到工程。
第三步:因为配置stack为dynamic working buffer 可能因为容量不够,所以会出现hard_handle错误,也就是内存错误。所以正对heap,必须用到stdlib.h库里的malloc和free两个函数。这两个函数在syscall.c这个文件下。采用heap作为buffer的话就需要用这两个函数进行手动分配和回收内存。虽然麻烦,但是内存空间很大。
最后一步,也是最重要的一步:
在自己的用户程序里要记得添加如下两句话
#if _USE_LFN fno.lfsize = _MAX_LFN * 2 + 1; fno.lfname = malloc(fno.lfsize); #endif
申请了内存空间后要及时通过free()函数回收内存,否则后面会出现程序跑飞或者硬件错误。
#if _USE_LFN fn = *fno.lfname ? fno.lfname : fno.fname; #else fn = fno.fname; #endif
这条语句就是判断是否为长文件名,如果是,就用长文件名变量,如果不是就还是用短文件名变量。
整个过程就是这样,还忘了一个小细节就是,把STM32的启动文件下的Heap Configuration 下的heap size(in bytes)从0x0000 0200 设置成 0x0000 0000。
事情终是有些进展,下面将做中文文件名的文件操作实验!特提笔记录。