今天看到LFN的相关代码,其中ff.c里有如下代码:
#elif _USE_LFN == 3 /* LFN feature with dynamic working buffer on the heap */
#define DEF_NAMEBUF BYTE sfn[12]; WCHAR *lfn
#define INIT_BUF(dobj) { lfn = ff_memalloc((_MAX_LFN + 1) * 2); \
if (!lfn) LEAVE_FF((dobj).fs, FR_NOT_ENOUGH_CORE); \
(dobj).lfn = lfn; (dobj).fn = sfn; }
#define FREE_BUF() ff_memfree(lfn)
继续查看发现在f_open()函数里对依次对上面三个宏定义进行了引用。
移植FATFS如果在配置文件里#define _USE_LFN 3 的话就是使用heap作为内存空间。因为使用stack的话可能会因为stack的空间有限而出现溢出,从而出现硬件错误。
而使用heap的话不得不提两个函数:
#if _USE_LFN == 3 /* LFN with a working buffer on the heap */
/*------------------------------------------------------------------------*/
/* Allocate a memory block */
/*------------------------------------------------------------------------*/
/* If a NULL is returned, the file function fails with FR_NOT_ENOUGH_CORE.
*/
void* ff_memalloc ( /* Returns pointer to the allocated memory block */
UINT size /* Number of bytes to allocate */
)
{
return malloc(size);
}
/*------------------------------------------------------------------------*/
/* Free a memory block */
/*------------------------------------------------------------------------*/
void ff_memfree(
void* mblock /* Pointer to the memory block to free */
)
{
free(mblock);
}
#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 版本后,肯定是可以支持中文长文件名的,但是还需不断摸索其使用方法!