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

📄 k9s1208.cpp

📁 可以在linux和wince下运行
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include <string.h>
#include "def.h"
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"
#include "2410slib.h" 


#define BAD_CHECK	(0)
#define ECC_CHECK	(0)

static U32 targetBlock;	    // Block number (0 ~ 4095)
static U32 targetSize;	    // Total byte size 
U32 blockIndex;

U32 downloadAddress;
U32 downloadProgramSize=0x0;
//*************** JTAG dependent functions ***************
static void NF_CMD(U8 cmd);
static void NF_ADDR(U8 addr);
static void NF_nFCE_L(void);
static void NF_nFCE_H(void);
static U8 NF_RDDATA(void);
static void NF_WRDATA(U8 data);
static void NF_WAITRB(void);
//*************** H/W dependent functions ***************
static U16 NF_CheckId(void);
static int NF_EraseBlock(U32 blockNum);
static int NF_ReadPage(U32 block,U32 page,U8 *buffer,U8 *spareBuf);
static int NF_WritePage(U32 block,U32 page,U8 *buffer,U8 *spareBuf);
	//buffer size is 512 bytes
static int NF_IsBadBlock(U32 block);
static int NF_MarkBadBlock(U32 block);
static void NF_Reset(void);
static void NF_Init(void);
//*******************************************************
void K9S1208_ReadCurrentBlock(U8 * buffer);
void K9S1208_ReadAllBlock(U8 * buffer);
void K9S1208_EraseAllBlock();
void K9S1208_ProgramBlock(U8 flag,U8*  srcPt,U32 len);

int TestECC(void)
{
    int i;
    U32 colECC=0,lowECC=0;
    U32 col=0, low=0;


	return 0;
}

void K9S1208_PrintBadBlockNum(void)
{
    int i;
    U16 id;

    Uart_Printf("\n[SMC(K9S1208V0M) NAND Flash bad block check]\n");
    
    NF_Init();
    id=NF_CheckId();
    Uart_Printf("ID=%x(0xec76)\n",id);
    if(id!=0xec76)
	return;
    
    for(i=0;i<4096;i++)
    {
	NF_IsBadBlock(i);   // Print bad block
    }
}
 

void K9S1208_ProgramBlock(U8 flag,U8*  srcPt,U32 len)
{
    int i;
    int programError=0;
    int noLoad=0;
    U8 spareBuf[16]=
	{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
    U32 progSize=0;

	targetBlock=blockIndex;
    targetSize=len;
	
#if BAD_CHECK       
	if(NF_IsBadBlock(blockIndex) && blockIndex!=0 )	// 1:bad 0:good
        {
	    blockIndex++;   // for next block
	    noLoad=1;
	    continue;
	}
#endif

    if (flag == 0)
	{
		;
	}
	else
	{
	  blockIndex++;
	  if(!NF_EraseBlock(2048))
	  {
	    blockIndex++;   // for next block
	    noLoad=1;
	    return ;
	  }
 	  memcpy(spareBuf,&blockIndex,2);
	  for(i=0;i<1;i++)
	  {
	      if(!NF_WritePage(2048,i,spareBuf,NULL/*spareBuf*/))// block num, page num, buffer
		  {
	        programError=1;
	        return;
		  }

	  }
    }
    if ((blockIndex >= 2049) && (blockIndex < 3328))
	{

	if(!NF_EraseBlock(blockIndex))
	{
	    blockIndex++;   // for next block
	    noLoad=1;
	    return;
	}

	for(i=0;i<(len/512 +1);i++)
	{
	    if(!NF_WritePage(blockIndex,i,srcPt,NULL/*spareBuf*/))// block num, page num, buffer
	    {
	        programError=1;
	        return;
	    }
	}
        if(programError==1)
	{
	    blockIndex++;
	    noLoad=1;
	    programError=0;
	    return;
	}
	}
	else
	{
	  blockIndex=2049;
	  if(!NF_EraseBlock(2048))
	  {
	    blockIndex++;   // for next block
	    noLoad=1;
	    return ;
	  }
 	  memcpy(spareBuf,&blockIndex,2);
	  for(i=0;i<1;i++)
	  {
	      if(!NF_WritePage(2048,i,spareBuf,NULL/*spareBuf*/))// block num, page num, buffer
		  {
	        programError=1;
	        return;
		  }

	  }
	}
	return;
}


void K9S1208_ReadFirstBlock()// Printf one page
{
    int i;
    U16 id,*block_count;
    U16 block,page;
    U8 spareBuf[16];
    U8	buffer[512+16];

    Uart_Printf("\n[SMC(K9S1208) NAND Flash block read]\n");	
    
    NF_Init();
    id=NF_CheckId();
    Uart_Printf("ID=%x(0xec76)\n",id);
    if(id!=0xec76)
	return;

	block=2048;
	page=0;
    
    NF_ReadPage(block,page,buffer,buffer+512);
    block_count=(U16 *)&buffer;
	block=*block_count;
    if ((block == 0xffff) || (block >= 3328) || (block == 0))
	{
		blockIndex = 2049;
	    if(!NF_EraseBlock(2048))
		{
	     blockIndex++;   // for next block
		 return;
		}
 	    memcpy(spareBuf,&blockIndex,2);
	    for(i=0;i<1;i++)
		{
	      if(!NF_WritePage(2048,i,spareBuf,NULL/*spareBuf*/))// block num, page num, buffer
		  {
	        return;
		  }

	  }
	}
	else
        blockIndex = block;
}

void K9S1208_EraseAllBlock()// Printf one page
{
    int i,j;
    U16 id;
	U8 *block_count;
    U16 block;


    NF_Init();
    id=NF_CheckId();
    Uart_Printf("ID=%x(0xec76)\n",id);
    if(id!=0xec76)
	return;
    for(j=0;j<=(blockIndex - 2048);j++)
	{
        NF_EraseBlock(j+2048);
	}
}
void K9S1208_ReadAllBlock(U8 * buffer)// Printf one page
{
    int i,j;
    U16 id;
	U8 *block_count;
    U16 block;


    NF_Init();
    id=NF_CheckId();
    Uart_Printf("ID=%x(0xec76)\n",id);
    if(id!=0xec76)
	return;
	block_count=buffer;
    for(j=0;j<(blockIndex - 2048);j++)
	{
      for (i=0;i<32;i++)
	  {
        NF_ReadPage(j+2049,i,buffer,NULL);
	    buffer=buffer +512;
	  }
        Uart_Printf("block=%d\n",j);
	  /** thread queue send to Aplication route*/
	  buffer=block_count;
	}
}

void K9S1208_ReadCurrentBlock(U8 * buffer)// Printf one page
{
    int i;
    U16 id,*block_count;
    U16 block,page;


    Uart_Printf("\n[SMC(K9S1208) NAND Flash block read]\n");	
    
    NF_Init();
    id=NF_CheckId();
    Uart_Printf("ID=%x(0xec76)\n",id);
    if(id!=0xec76)
	return;

    for (i=0;i<32;i++)
	{
      NF_ReadPage(blockIndex,i,buffer,NULL);
	  buffer=buffer +512;
    Uart_Printf("block=%d,page=%d:\n",block,i);
	}

}
    U8 flash_data[512]={'H','E','L','L','O',',','W','O','R','L','D'};

void K9S1208_Program(void)
{
    int i;
    int programError=0;
    U32 blockIndex;
    int noLoad=0,imageSize;
    U8 spareBuf[16]=
	{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
    U8 *srcPt;
    U32 progSize=0;

    Uart_Printf("\n[SMC(K9S1208V0M) NAND Flash Writing Program]\n");
    
    Uart_Printf("\nAvailable target block number: 0~1023\n");
    Uart_Printf("Input target block number:");
    targetBlock=Uart_GetIntNum();
targetSize=0x4000;
    Uart_Printf("target start block number     =%d\n",targetBlock);
    Uart_Printf("target size        (0x4000*n) =0x%x\n",targetSize);
    Uart_Printf("STATUS:");
    blockIndex=targetBlock;
    imageSize=0x4000;
#if BAD_CHECK       
	if(NF_IsBadBlock(blockIndex) && blockIndex!=0 )	// 1:bad 0:good
        {
	    blockIndex++;   // for next block
	    noLoad=1;
	    return ;
	}
#endif


	if(!NF_EraseBlock(blockIndex))
	{
	    blockIndex++;   // for next block
	    noLoad=1;
	    return ;
	}
 
	Uart_Printf("E");
	srcPt=(U8 *)flash_data;

	for(i=0;i<32;i++)
	{
	    if(!NF_WritePage(blockIndex,i,srcPt,NULL/*spareBuf*/))// block num, page num, buffer
	    {
	        programError=1;
	        break;
	    }

	    srcPt+=512;	// Increase buffer addr one pase size
	    Uart_Printf("p");
	}
	Uart_Printf("\n");

        if(programError==1)
	{
	    blockIndex++;
	    noLoad=1;
	    programError=0;
	    return;
	}
	progSize+=0x4000;
	if(progSize>=imageSize)
	    return;	// Exit while loop
	blockIndex++;
}

void K9S1208_PrintBlock(void)// Printf one page
{
    int i;
    U16 id;
    U32 block,page;
    U8	buffer[512+16];

    printf("\n[SMC(K9S1208) NAND Flash block read]\n");	
    
    NF_Init();
    id=NF_CheckId();
    Uart_Printf("ID=%x(0xec76)\n",id);
    if(id!=0xec76)
	return;

    Uart_Printf("Input target block number:");
    block=Uart_GetIntNum();
    Uart_Printf("Input target page number:");   
    page=Uart_GetIntNum();
    
    NF_ReadPage(block,page,buffer,buffer+512);
    
    Uart_Printf("block=%d,page=%d:",block,page);
    for(i=0;i<512;i++)
    {
        if(i%16==0)
	    Uart_Printf("\n%3xh:",i);
        Uart_Printf("%02x ",buffer[i]);
    }
    Uart_Printf("\nS.A.:",i);

    for(i=512;i<512+16;i++)
    {
        Uart_Printf("%02x ",buffer[i]);
    }

    Uart_Printf("\n");    	
}

//*************************************************
//*************************************************
//**           H/W dependent functions           **
//************************************************* 
//*************************************************

// NAND Flash Memory Commands
#define	SEQ_DATA_INPUT			(0x80)
#define	READ_ID				(0x90)
#define	RESET				(0xFF)
#define	READ_1_1			(0x00)
#define	READ_1_2			(0x01)
#define	READ_2				(0x50)
#define	PAGE_PROGRAM			(0x10)
#define	BLOCK_ERASE			(0x60)
#define	BLOCK_ERASE_CONFIRM		(0xD0)
#define	READ_STATUS			(0x70)


// block0: reserved for boot strap
// block1~4095: used for OS image
// badblock SE: xx xx xx xx xx 00 ....
// good block SE: ECC0 ECC1 ECC2 FF FF FF ....

#define WRITEVERIFY  (0)  //verifing is enable at writing.

⌨️ 快捷键说明

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