随想录(fatfs的学习)


【 声明:版权全部,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】


    上学的时候就对文件系统非常有兴趣。可是苦于没有合适的fs代码能够学习。市面上的fs代码,要么太大。像linux一样,动不动就是几万行,几十万行。要么就是没有开源,你仅仅会使用它的接口,却不太清楚里面是怎么实现的。


    一直到后来工作的时候,发现了fatfs这么一个开源库代码。

fatfs大小合适。内容也比較紧凑。仅仅要做好底层的接口移植就能够使用了。眼下fatfs用来管理最多的还是sd卡设备,nandflash用的比較少。


    fatfs固然短小精悍,可是在pc上学习确实还是不方便,要是让fatfs能够在vc环境下自由地仿真调试就好了。

今天。还真找了这么一份代码,大家能够到http://www.raw-os.org/Download.html看一看,当中有一份Raw-OS 1.042 + yaffs + fatfs VC平台移植版的下载文档。执行环境是vs2010。有过windows开发经验的同学肯定不陌生。

当然,这份代码你不光能够学习fatfs,还能够学习yaffs文件系统。


    事实上移植开源库基本上包含三个方面:(1)底层接口移植,通常是设备驱动等等;(2)数据移植,一般依据编译器重定义数据类型。(3)os的匹配移植,依据os设计自己须要的一些系统函数,比方创建thread、生成信号量、分配内存等等。等到做好了这些,就能够使用库提供的api,做我们自己想做的事情了。

    

    最后。给出fatfs底层简单的代码移植方法,我认为还是蛮有意思的。

/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2012        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control module to the FatFs module with a defined API.        */
/*-----------------------------------------------------------------------*/
#include "raw_api.h"
#include "diskio.h"		/* FatFs lower layer API */
#include "ff.h"
//#include "usbdisk.h"	/* Example: USB drive control */
#include <string.h>
#include <stdio.h>


static RAW_U8 *simulated_space;
static int init_flag;
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE drv				/* Physical drive nmuber (0..) */
)
{

	if (init_flag == 0) {
		simulated_space = malloc(5 * 1024 *1024);
		raw_memset(simulated_space, 0, 5 * 1024 *1024);
		
		if (simulated_space == 0) {

			RAW_ASSERT(0);
			
		}

		init_flag = 1;
	}
	
	return 0;	
}



/*-----------------------------------------------------------------------*/
/* Get Disk Status                                                       */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE drv		/* Physical drive nmuber (0..) */
)
{

	return 0;	
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count)

{
	
	raw_memcpy(buff, simulated_space + (512 * sector), 512 * count);
	
	
	return RES_OK;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count)
{

	raw_memcpy(simulated_space + (512 * sector), buff, 512 * count);
	
	return RES_OK;
}



/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE ctrl,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res = RES_PARERR;
	
		switch (ctrl) {
		case CTRL_SYNC:
			res = RES_OK;
			break;
	
		case GET_SECTOR_COUNT:
			*(DWORD*)buff = (10240); /*5M space*/
			res = RES_OK;
			break;
	
		case GET_SECTOR_SIZE:
			*(WORD*)buff = 512;
			res = RES_OK;
			break;
	
		default:
			break;
	
		}
	
		return res;
}

DWORD get_fattime (void)
{

 	return 0;
}




原文地址:https://www.cnblogs.com/mfmdaoyou/p/6784439.html