⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fs_init.c

📁 基于东南大学开发的SEP3203的ARM7中的所有驱动
💻 C
字号:

//#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "ros33.h"
#include "filesys\fs.h"
#include "filesys\fstouser.h"
#include "file.h"
#include "filebuf.h"
#include "sys\flash.h"
#include "pcdisk.h"

#include "FAL.h"

#include "sys\gpc.h"

extern int init_area_array();
extern int mmcvoltage;

extern unsigned long CheckDirInfo( void );

#ifdef NORFLASH
#define ERASEAREA(area) \
do{\
			(*FlashUnlockBlockP)(area);\
			(*FlashEraseBlockP)((unsigned int)(area));\
}while(0)
	
#define INIT(name1, name2, length)\
do{\
	(*FlashWriteBlockWordP)( (unsigned int )(name2), (unsigned char *)(name1), length );\
}while(0)
#else
#define ERASEAREA(area) \
do{\
		int block_8;\
		for(block_8=0; block_8<8; block_8++)\
			nand_erase_block(area+block_8*32*512);\
}while(0)
	
#define INIT(name1, name2, length)\
do{\
	nand_write( (unsigned int )(name2), length , (unsigned char *)(name1));\
}while(0)
#endif

DEV_DSP_S device_dispcription[] = {
	/* devid, 	rbufsize,  	rbuf,		start,	read, 			write*/
	{FLASH_DEVID,  0,		NULL, 		 0,		drv_flash_read, 		drv_flash_write},
	// {ICCARD_DEVID, 256,	  	(unsigned char *)iccard_rbuf, 0,	0, 0},
	{0,				0,		NULL,		0,		NULL,	NULL}
};

#define AREA_START(i) ((unsigned int)(OLD_START_ADDRESS+(i)*FLASH_AREA_SIZE))
#define AREA_USER_BLK(i,j) (AREA_START(i)+(j)*BLOCKSIZE)

#define AREA_NUM	7

const AREA_DSP_S area_dispcription[AREA_NUM+1] = {
	/* devid, 		start,			size */
	{FLASH_DEVID,  AREA_START(0),	FLASH_AREA_SIZE},
	{FLASH_DEVID,  AREA_START(1),	FLASH_AREA_SIZE},
	{FLASH_DEVID,  AREA_START(2),	FLASH_AREA_SIZE},
	{FLASH_DEVID,  AREA_START(3),	FLASH_AREA_SIZE},
	{FLASH_DEVID,  AREA_START(4),	FLASH_AREA_SIZE},
	{FLASH_DEVID,  AREA_START(5),	FLASH_AREA_SIZE},
	{FLASH_DEVID,  AREA_START(6),	FLASH_AREA_SIZE},
//	{FLASH_DEVID,  AREA_START(7),	FLASH_AREA_SIZE},
//	{FLASH_DEVID,  AREA_START(8),	FLASH_AREA_SIZE},
//	{FLASH_DEVID,  AREA_START(9),	FLASH_AREA_SIZE},
	//{ICCARD_DEVID, AREA_START(10),	ICCARD_AREA_SIZE},
	//{ICCARD_DEVID, AREA_START(11),	ICCARD_AREA_SIZE},
	{0,				0,						0}
};

#define PARTITION_NUM	6

const PARTITION Partition[PARTITION_NUM] = {
	/* start,	num */
	{0,	1},
	{1,	2},
	{3,	1},
	{4,	1},
	{5, 1},
	{6, 1}
};

DIR_CACHE_S	cache_data;

/*
 * we assume the first block in area_1 stores the root_dir.
 * we assume the first block in the first area of every partition 
 *    stores the blockbitmap to flag the status(used or not) of all
 *    the blocks in this partition.
 * we assume the first block in the first area of every partition
 *    stores the dirctory of this partition.
 */


DIR_S RootDir[PARTITION_NUM] = {
	{ 
		{ 0, FILE_MAGIC, AREA_USER_BLK(0,1), NULL, 0, 0, "/", 5 },
		{
			{ AREA_USER_BLK(1,1), FS_DIR, "db1/" },
			{ AREA_USER_BLK(3,1), FS_DIR, "db2/" },
			{ AREA_USER_BLK(4,1), FS_DIR, "db3/" },
			{ AREA_USER_BLK(5,1), FS_DIR, "db4/" },
			{ AREA_USER_BLK(6,1), FS_DIR, "db5/" },
		}
	},
	{{ 0, FILE_MAGIC, AREA_USER_BLK(1,1), NULL, 0, 1, "db1/", 0 },{}},
	{{ 0, FILE_MAGIC, AREA_USER_BLK(3,1), NULL, 0, 2, "db2/", 0 },{}},
	{{ 0, FILE_MAGIC, AREA_USER_BLK(4,1), NULL, 0, 3, "db3/", 0 },{}},
	{{ 0, FILE_MAGIC, AREA_USER_BLK(5,1), NULL, 0, 4, "db4/", 0 },{}},
	{{ 0, FILE_MAGIC, AREA_USER_BLK(6,1), NULL, 0, 5, "db5/", 0 },{}},
};

//FILE_S GLOABLE_FILE;
int ERROR_FLAG = 0;
int error_flag = 0;

int init_cache_data()
{
	int					num;
//	DIR_CACHE_NODE_S	*dir_node;
	
	/*
	 * initial the dirctory root to the first dir_cache_node
	 */
	
	cache_data.dir_cache_data[0].flag = 1;
	INIT_LIST_HEAD( &cache_data.dir_cache_data[0].child );
	INIT_LIST_HEAD( &cache_data.dir_cache_data[0].next );
	memcpy( cache_data.dir_cache_data[0].dir, &RootDir[0], sizeof(DIR_S) );
	
	/*
	 * initial the dir cache struct
	 */
	cache_data.freenum = DIR_CACHE_NODE_MAX_NUM - 1;
	cache_data.freehead = &(cache_data.dir_cache_data[1]);
	INIT_LIST_HEAD( &cache_data.treehead );
	list_add( &((cache_data.dir_cache_data[0]).next), &cache_data.treehead );
//	list_add( &cache_data.dir_cache_data[0].next, &cache_data.dir_cache_data[0].child );
//	dir_node = list_entry( &cache_data[0].treehead.next, DIR_CACHE_NODE_S, next );
	for( num = 1; num < DIR_CACHE_NODE_MAX_NUM; num++ )
	{
		memset( &(cache_data.dir_cache_data[num]), 0, DIR_CACHE_NODE_MAX_NUM );
	}

	return FS_OK;
}



SetupFS( ){
	init_cache_data();
	init_area_array();
	init_FILE_buf();
}

unsigned long CheckDirInfo( void )
{
	FCB_S	fcb;
	int		i, j;
	unsigned long	address;
	unsigned short	info_y = 26;
	unsigned long	error = 0;
	unsigned long	hGC;
	int *nandbuf;
	
	hGC = SysGetGC();
	
	for( i = 0; i < PARTITION_NUM; i++ )
	{
		address = AREA_START(Partition[i].start) + BLOCKSIZE;
		#ifdef NORFLASH
		memcpy( &fcb, address, sizeof( FCB_S ) );
		#else
		nandbuf = SysLmalloc(512);		//nand page has 512 bytes
		nand_read_page(address, nandbuf);
		memcpy(&fcb, nandbuf, sizeof( FCB_S ) );
		SysLfree(nandbuf);
		#endif
		
		if( fcb.magic != FILE_MAGIC || fcb.blkid != address || fcb.partition != i || strcmp( fcb.file_name, RootDir[i].curdir.file_name ) )
		{
			char	blkbitmap[BLOCKSIZE];
			char	info[50];
			
			
//			sprintf( info, "File %s Corrupt!", RootDir[i].curdir.file_name );
//			SysTextOut( hGC, GPC_BLACK, 10, info_y, info, GPC_REPLACE_STYLE );
//			info_y += 16;
			
			// initialize direction
			for ( j = 0; j < Partition[i].num; j++ ){
				ERASEAREA( AREA_START(Partition[i].start+j) );
			}		
//			sprintf( info, "..." );
//			SysTextOut( hGC, GPC_BLACK, 10, info_y, info, GPC_REPLACE_STYLE );

			INIT( &RootDir[i],	AREA_USER_BLK(Partition[i].start, 1 ), sizeof(DIR_S) );
			
//			sprintf( info, "... ..." );
//			SysTextOut( hGC, GPC_BLACK, 10, info_y, info, GPC_REPLACE_STYLE );

			memset( blkbitmap, 0xFF, BLOCKSIZE );
			blkbitmap[0] = 0x3f;
			INIT( blkbitmap, AREA_START(Partition[i].start), BLOCKSIZE );
		
//			sprintf( info, "... ... ..." );
//			SysTextOut( hGC, GPC_BLACK, 10, info_y, info, GPC_REPLACE_STYLE );
//			info_y += 16;

			blkbitmap[0] = 0x1f;
			for( j = 1; j < Partition[i].num; j++ )
				INIT( blkbitmap, AREA_START(Partition[i].start+j), BLOCKSIZE );

//			sprintf( info, "Retrieve Succeed!" );
//			SysTextOut( hGC, GPC_BLACK, 10, info_y, info, GPC_REPLACE_STYLE );
//			info_y += 16;
			
			error |= ( 1 << i );
		}
	}

	#ifdef NORFLASH
	for(i=0; i<AREA_NUM; i++)
	(*FlashUnlockBlockP)(OLD_START_ADDRESS + FLASH_AREA_SIZE*i);
	#endif

	return error;
}

unsigned int GFD_InitFatFS(int deviceno)
{
	/*
	  *0: ramdisk; 1: norflash; 2:mmc; 3:nandflash. 
	  */
	
	if (FAL_Sys_Init(deviceno) != NU_SUCCESS)
		 return(2);

	if(FAL_Set_Curr_Drive(FAL_Get_Curr_Drive()) != NU_SUCCESS)
		 return(3);
	
	return 0;
}


unsigned long InitFileSystem( void )
{
	unsigned long	error;
	
	error = CheckDirInfo();
	SetupFS();

	#ifdef FAT_FS
	GFD_InitFatFS(NOR_FLASH);
	if (mmcvoltage==1)
		GFD_InitFatFS(MMC);
	#endif
	
	return error;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -