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


今天看到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 版本后,肯定是可以支持中文长文件名的,但是还需不断摸索其使用方法!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值