分享

FATFS文件系统的中文长文件名配置的几个注意事项

 goodwangLib 2019-11-06

第一步,当然是下载,这个很容易,下载到最新的FF0.9a就行。

第二步,当然是加入工程,这个不展开,然后就是配置ffconf.h这个文件啦。

  1. #ifndef _FFCONF
  2. #define _FFCONF 4004 /* Revision ID */
  3. /*---------------------------------------------------------------------------/
  4. / Functions and Buffer Configurations
  5. /----------------------------------------------------------------------------*/
  6. #define _USE_STRFUNC 1 /* 0:Disable or 1-2:Enable */ //Ö§³Ö×Ö·û´®ÀຯÊý
  7. /* To enable string functions, set _USE_STRFUNC to 1 or 2. */
  8. #define _USE_MKFS 1 /* 0:Disable or 1:Enable */ //ʹÄܸñʽ»¯
  9. /* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */
  10. #define _USE_FORWARD 0 /* 0:Disable or 1:Enable */
  11. /* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */
  12. #define _USE_FASTSEEK 1 /* 0:Disable or 1:Enable */ //ʹÄÜ¿ìËÙ²éÕÒÌØÐÔ
  13. /* To enable fast seek feature, set _USE_FASTSEEK to 1. */
  14. /*---------------------------------------------------------------------------/
  15. / Locale and Namespace Configurations
  16. /----------------------------------------------------------------------------*/
  17. #define _CODE_PAGE 936 //
  18. /* The _CODE_PAGE specifies the OEM code page to be used on the target system.
  19. / Incorrect setting of the code page can cause a file open failure.
  20. /
  21. / 932 - Japanese Shift-JIS (DBCS, OEM, Windows)
  22. / 936 - Simplified Chinese GBK (DBCS, OEM, Windows)
  23. / 949 - Korean (DBCS, OEM, Windows)
  24. / 950 - Traditional Chinese Big5 (DBCS, OEM, Windows)
  25. / 1250 - Central Europe (Windows)
  26. / 1251 - Cyrillic (Windows)
  27. / 1252 - Latin 1 (Windows)
  28. / 1253 - Greek (Windows)
  29. / 1254 - Turkish (Windows)
  30. / 1255 - Hebrew (Windows)
  31. / 1256 - Arabic (Windows)
  32. / 1257 - Baltic (Windows)
  33. / 1258 - Vietnam (OEM, Windows)
  34. / 437 - U.S. (OEM)
  35. / 720 - Arabic (OEM)
  36. / 737 - Greek (OEM)
  37. / 775 - Baltic (OEM)
  38. / 850 - Multilingual Latin 1 (OEM)
  39. / 858 - Multilingual Latin 1 + Euro (OEM)
  40. / 852 - Latin 2 (OEM)
  41. / 855 - Cyrillic (OEM)
  42. / 866 - Russian (OEM)
  43. / 857 - Turkish (OEM)
  44. / 862 - Hebrew (OEM)
  45. / 874 - Thai (OEM, Windows)
  46. / 1 - ASCII only (Valid for non LFN cfg.)
  47. */
  48. #define _USE_LFN 3 /* 0 to 3 ÉèÖÃΪ1,Ö§³Ö³¤ÎļþÃû£¬²¢²ÉÓö¯Ì¬ÄÚ´æ*/
  49. #define _MAX_LFN 255 /* Maximum LFN length to handle (12 to 255) */
  50. /* The _USE_LFN option switches the LFN support.
  51. /
  52. / 0: Disable LFN feature. _MAX_LFN and _LFN_UNICODE have no effect.
  53. / 1: Enable LFN with static working buffer on the BSS. Always NOT reentrant.
  54. / 2: Enable LFN with dynamic working buffer on the STACK.
  55. / 3: Enable LFN with dynamic working buffer on the HEAP.
  56. /
  57. / The LFN working buffer occupies (_MAX_LFN + 1) * 2 bytes. To enable LFN,
  58. / Unicode handling functions ff_convert() and ff_wtoupper() must be added
  59. / to the project. When enable to use heap, memory control functions
  60. / ff_memalloc() and ff_memfree() must be added to the project. */
  61. #define _LFN_UNICODE 0 /* 0:ANSI/OEM or 1:Unicode */
  62. /* To switch the character code set on FatFs API to Unicode,
  63. / enable LFN feature and set _LFN_UNICODE to 1. */
  64. #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的话就需要用这两个函数进行手动分配和回收内存。虽然麻烦,但是内存空间很大。

最后一步,也是最重要的一步:

在自己的用户程序里要记得添加如下两句话

  1. #if _USE_LFN
  2. fno.lfsize = _MAX_LFN * 2 + 1;
  3. fno.lfname = malloc(fno.lfsize);
  4. #endif

申请了内存空间后要及时通过free()函数回收内存,否则后面会出现程序跑飞或者硬件错误。

  1. #if _USE_LFN
  2. fn = *fno.lfname ? fno.lfname : fno.fname;
  3. #else
  4. fn = fno.fname;
  5. #endif

这条语句就是判断是否为长文件名,如果是,就用长文件名变量,如果不是就还是用短文件名变量。

整个过程就是这样,还忘了一个小细节就是,把STM32的启动文件下的Heap Configuration 下的heap size(in bytes)从0x0000 0200 设置成 0x0000 0000。

事情终是有些进展,下面将做中文文件名的文件操作实验!特提笔记录。


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多