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

📄 nfshemu.cpp

📁 PDA上的CF CARD 文件系统的建立程式
💻 CPP
字号:
//#include <ar2kreg.h>
//#include <artsm.h>
#include <errno.h>
#include "../include/nfshemu.h"
//#include "../../ffs_nand/include/fss_nand.h"

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>

#include "typedefine.h"
/*************************************************************************/
/***               Public Variable Declaration                         ***/
/*************************************************************************/

#define NAND_FFS_START_BLOCK		256		// the starting block number of FFS_NAND

#define K16M_byte	16*1024*1024
#define K32M_byte	32*1024*1024
#define K64M_byte	64*1024*1024
#define K128M_byte	128*1024*1024

#define LONG_PAGE_LENTH         528
#define LONG_BLOCK_LENTH        (32*528)
#define BLOCK_NUM               1024

//***********************************************************
// Since NAND_File system structure , we only support the chip (block erase size=16k bytes)
// We don't support 1M,2M,4M,and 8M bytes NAND flash card .
// Note : The prg_cycle was different between (16M, 32M) and (64M, 128M).

flash_chip  flash_table[] =
{
 // evice_id  prg_cycle  block_size  total_size    device_name
    0xec73  	,0	, 16*1024   , K16M_byte   , "K9f2808" 	,   //Samsung 16M
    0xec75  	,0	, 16*1024   , K32M_byte   , "K9f5608" 	,   //Samsung 32M
    0xec76  	,1	, 32*1024   , K64M_byte   , "K9d1208" 	,   //Samsung 64M
    0xec79  	,1	, 32*1024   , K128M_byte  , "K9d1G08" 	,   //Samsung 128M
    0x9873  	,0	, 16*1024   , K16M_byte   , "TC58NS128"	,   //Toshiba 16M
    0x9875  	,0	, 16*1024   , K32M_byte   , "TC58NS256" ,   //Toshiba 32M
    0x9876  	,1	, 32*1024   , K64M_byte   , "TH58NS512" ,   //Toshiba 64M
    0x9879  	,1	, 32*1024   , K128M_byte  , "TH58NS100" ,   //Toshiba 128M
    0		,0	,0	    ,0		  ,0
};

int ghFlashDev=-1;
flash_chip *current_nand_chip ;

/*************************************************************************/
/***               Internally Visible Constants and Static Data        ***/
/*************************************************************************/
unsigned int	seqIn, seqOut;
unsigned char	*seqAddr;

int nfshCheckBadBlock( unsigned long offset );
int nfshReadID( void );

int OpenEmuNFsh()
{
	int i, ret;
	unsigned char *pBuffer;

	_mkdir("c:\\EmuBin");
	ghFlashDev = _open("c:\\EmuBin\\nfsh16m.bin",_O_RDWR | _O_CREAT | _O_BINARY,_S_IREAD | _S_IWRITE);
	if(ghFlashDev == -1)
		return -1;
	_lseek(ghFlashDev, 0, SEEK_END);
	if(_tell(ghFlashDev) > 0)
		return STATUS_OK;
	pBuffer=(unsigned char *)malloc(LONG_BLOCK_LENTH);
	memset(pBuffer, -1, LONG_BLOCK_LENTH);
	_lseek(ghFlashDev, 0, SEEK_SET);
	for(i=0; i<BLOCK_NUM; i++)
	{
		//ret=_write(ghFlashDev,(unsigned char *)gBlockBuffer, BLOCK_LENTH);
		ret=_write(ghFlashDev,(unsigned char *)pBuffer, LONG_BLOCK_LENTH);
		if(ret == -1)
			return -1;
	}
	free(pBuffer);
}

unsigned int nfshInit( void )
{

	int chip_ID ;
	unsigned short count=0 ;

	chip_ID = nfshReadID();

	current_nand_chip = &flash_table[count++] ;
	while ( current_nand_chip->device_ID != 0 )
	{
		if ( chip_ID == current_nand_chip->device_ID )
			break;
		current_nand_chip = &flash_table[count++] ;
	}

	return current_nand_chip->total_size ;
}

int nfshReadFrame( unsigned long offset, unsigned char data[] )
{
	int pageNo, ret, sectorOfs;

	if (offset> current_nand_chip->total_size)
		return NFLASH_FAIL;

	pageNo = offset>>9;
	//pageOffset = offset&0x1ff;
	if ( offset & 0x00000100 )
		sectorOfs = 256;
	else
		sectorOfs = 0;
	
	_lseek(ghFlashDev, pageNo*LONG_PAGE_LENTH, SEEK_SET);

	ret=_read(ghFlashDev, data, NFLASH_FRAME_SIZE);
	if(ret==-1)
		return NFLASH_FAIL;

	return NFLASH_OK; //Sean Fri 06-21-2002
}


int nfshProgramFrame( unsigned long offset, unsigned char data[] )
{
	unsigned char status;
	//unsigned char ecc_data[16] ,gen_ecc[3] ;
	int i, start;
	unsigned char sourcedata[528];
	unsigned int *pSource, *pDest;
	//int j;

	int pageNo, ret, sectorOfs; //, pageOffset;

	if (offset> current_nand_chip->total_size)
		return NFLASH_FAIL;

	pageNo = offset>>9;
	//pageOffset = offset&0x1ff;
	
	if ( offset & 0x00000100 )
		sectorOfs = 256;
	else
		sectorOfs = 0;

	start = pageNo*LONG_PAGE_LENTH+sectorOfs;
	_lseek(ghFlashDev, start, SEEK_SET);


	ret = _read(ghFlashDev, sourcedata, NFLASH_FRAME_SIZE);
	if(ret==-1)
		return NFLASH_FAIL;

	pSource = (unsigned int *)sourcedata;
	pDest = (unsigned int *)data;

	for(i=0; i<128; i++)
	{
		if(pDest[i]&(~pSource[i]))
		{
			status = 0;			
		}

		pDest[i] &= pSource[i];		
	}


	_lseek(ghFlashDev, start, SEEK_SET);

	ret=_write(ghFlashDev, data, NFLASH_FRAME_SIZE);
	if(ret==-1)
		return NFLASH_FAIL;
	else
		return NFLASH_OK; 

}

int nfshEraseBlock( unsigned long offset )
{
	int blockOfs, blockNo, ret;
	unsigned char *pBuffer;
	
	blockOfs = offset&0xFFFFC000;
	blockNo = (blockOfs>>14);

	if(ghFlashDev == -1 || blockNo >= BLOCK_NUM)
		return NFLASH_FAIL;

	pBuffer=(unsigned char *)malloc(LONG_BLOCK_LENTH);
	if(pBuffer==NULL)
	{
		return NFLASH_FAIL;
	}

	memset(pBuffer,-1,LONG_BLOCK_LENTH);
	_lseek(ghFlashDev,blockNo*LONG_BLOCK_LENTH,SEEK_SET);
	ret=_write(ghFlashDev,pBuffer,LONG_BLOCK_LENTH);

	free(pBuffer);

	if(ret==-1)
		return  NFLASH_FAIL;
	else
		return NFLASH_OK;
}

int nfshEraseAllBlock( void )
{
	int blockNo, ret;

	unsigned char *pBuffer;
	
	if(ghFlashDev == -1 )
		return NFLASH_FAIL;

	pBuffer=(unsigned char *)malloc(LONG_BLOCK_LENTH);
	if(pBuffer==NULL)
	{
		return NFLASH_FAIL;
	}
	memset(pBuffer,-1,LONG_BLOCK_LENTH);
	
	for(blockNo=0; blockNo<BLOCK_NUM; blockNo++)
	{
		_lseek(ghFlashDev,blockNo*LONG_BLOCK_LENTH, SEEK_SET);
		ret=_write(ghFlashDev,pBuffer,LONG_BLOCK_LENTH);
		if(ret==-1)
		{
			free(pBuffer);
			return  NFLASH_FAIL;
		}
	}

	free(pBuffer);

	return NFLASH_OK;
}


int nfshCheckBadBlock( unsigned long offset )
{

   	return NFLASH_OK;
}

int nfshReadID( void )
{

	unsigned char c1, c2;

	c1 = 0xEC;
	c2 = 0x73;

	return((c1<<8) | c2);
}

⌨️ 快捷键说明

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