分享

FATFS的长文件名特性相关代码

 goodwangLib 2019-11-06
今天看到LFN的相关代码,其中ff.c里有如下代码:

  1. #elif _USE_LFN == 3 /* LFN feature with dynamic working buffer on the heap */
  2. #define DEF_NAMEBUF BYTE sfn[12]; WCHAR *lfn
  3. #define INIT_BUF(dobj) { lfn = ff_memalloc((_MAX_LFN + 1) * 2); \
  4. if (!lfn) LEAVE_FF((dobj).fs, FR_NOT_ENOUGH_CORE); \
  5. (dobj).lfn = lfn; (dobj).fn = sfn; }
  6. #define FREE_BUF() ff_memfree(lfn)
继续查看发现在f_open()函数里对依次对上面三个宏定义进行了引用。
移植FATFS如果在配置文件里#define _USE_LFN 3 的话就是使用heap作为内存空间。因为使用stack的话可能会因为stack的空间有限而出现溢出,从而出现硬件错误。
而使用heap的话不得不提两个函数:
  1. #if _USE_LFN == 3 /* LFN with a working buffer on the heap */
  2. /*------------------------------------------------------------------------*/
  3. /* Allocate a memory block */
  4. /*------------------------------------------------------------------------*/
  5. /* If a NULL is returned, the file function fails with FR_NOT_ENOUGH_CORE.
  6. */
  7. void* ff_memalloc ( /* Returns pointer to the allocated memory block */
  8. UINT size /* Number of bytes to allocate */
  9. )
  10. {
  11. return malloc(size);
  12. }
  13. /*------------------------------------------------------------------------*/
  14. /* Free a memory block */
  15. /*------------------------------------------------------------------------*/
  16. void ff_memfree(
  17. void* mblock /* Pointer to the memory block to free */
  18. )
  19. {
  20. free(mblock);
  21. }
  22. #endif
我做了一个实验,在程序里加入了一条语句usart3.printf("%x \n",malloc(200));
因为我按一下按键程序就循环一下,所以
输出的结果有   :
20001650
        20001720
0
0
0
结果分析:前两次输出结果刚好相差200。说明,前面两次分配内存成功,把内存的首地址输出了。后面几次,可能是内存耗尽,内存分配不成功,返回的是空值。
因此,移植FATFS不需自己写内存分配函数,直接调用#include <stdlib.h>       这个库就行了。
另外,看MDK的help文档,Compiler eight-byte alignment features
The compiler has the following eight-byte alignment features:

The Procedure Call Standard for the ARM Architecture (AAPCS) requires that the stack is eight-byte aligned at all external interfaces. The compiler and C libraries preserve the eight-byte alignment of the stack. In addition, the default C library memory model maintains eight-byte alignment of the heap.
因此像在openedv的STM32论坛里一个帖子所讲的由于malloc()函数的输出地址字节不对齐而引起的硬件错误硬是不存在的。
malloc字节对齐问题而引起的硬件错误

总结如下:
FATFS在0.9a 版本后,肯定是可以支持中文长文件名的,但是还需不断摸索其使用方法!

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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多