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

📄 k9f1g08.c

📁 基于通用GPIO口的flash 烧写软件代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include "pin2440.h"
#include "Jtag.h"
#include "K9S1208.h"
#include "sjf2440.h"


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

//*************** JTAG dependent functions ***************
void K9F1G08_JtagInit(void);
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 K9F1G08_PrintBlock(void);
void K9F1G08_Program(void);
int check_flash_128M();

static U32 targetBlock;	    // Block number (0 ~ 4095)
static U32 targetSize;	    // Total byte size 
static U8 blockBuf[0x20000];

static void *function[][2]=
{
    (void *)K9F1G08_Program,		"K9F1G08 Program     ",
    (void *)K9F1G08_PrintBlock,		"K9F1G08 Pr BlkPage  ",
	(void *)check_flash_128M,		"K9F1G08 CHECK PROG	 ",
    (void *)1,			    	"Exit                ",
    0,0
};

void gres(int v , char *prop , char *log );
int check_flash_data_128M()
{
	unsigned char buf[4096],tmp[4096];
	int i,j;
	
	for(i=0;i<4096;i++)buf[i]=i&0xff;

    printf("e");
	if(!NF_EraseBlock(0))
	{
		gres(1,"Flash Erase Block 0 ERROR.","");
		return 1;
	}

	for(i=0;i<32;i++)
	{
	    if(!NF_WritePage(0,i,buf,NULL/*spareBuf*/))// block num, page num, buffer
	    {
			gres(1,"Flash Write Block 0 ERROR.","");
			return 1;
	    }

	    printf("p");
	}

	for(i=0;i<32;i++)
	{
		memset(buf,0,2048);
	    if(!NF_ReadPage(0,i,buf,buf+2048))
	    {
			gres(1,"Flash Read Block 0 ERROR.","");
			return 1;
	    }
		for(j=0;j<2048;j++)if(buf[j]!=(j&0xff))
		{
			sprintf(buf,"Check Flash Data Error, should be %x , was %x",j&0xff,buf[j]);
			gres(1,"Flash Read Block 0 ERROR.",buf);
			return 1;
		}

	    printf("r");
	}

	return 0;
}
int check_flash_128M()
{
	int id;
	char buf[255];

	K9F1G08_JtagInit();
    NF_Init();
    id=NF_CheckId();
    if(id == 0xecf1 )
	{
		gres(0,"Flash Detect OK\n","");
		return check_flash_data_128M();
	}
	sprintf(buf,"Check FLASH ID failed , should be 0xec76 , was %x\n",id);
	gres(1,"Flash Detect Fail\n",buf);
	return 1;
}

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

    printf("\n[K9F1G08 NAND Flash JTAG Programmer]\n");
    K9F1G08_JtagInit();
    NF_Init();

    id=NF_CheckId();
    
	if(id!=0xecf1)
    {
	printf("ERROR: K9F1G08 is not detected. Detected ID=0x%x.\n",id);
	return;
    }
    else
    {
    	printf("K9F1G08 is detected. ID=0x%x\n",id);
    }

    while(1)
    {

	i=0;
    	while(1)
	{   //display menu
	    printf("%2d:%s",i,function[i][1]);
	    i++;
	    if((int)(function[i][0])==0)
	    {
		printf("\n");
		break;
	    }
	    //if((i%4)==0)
		printf("\n");
	}

	printf("Select the function to test :");
	scanf("%d",&i);
	if( i>=0 && (i<((sizeof(function)/8)-2)) ) 
	    ( (void (*)(void)) (function[i][0]) )();  
	else
	    break; //Exit menu
    }
}



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

    printf("\n[SMC(K9F1G08V0M) NAND Flash Writing Program]\n");
    
    printf("\nSource size:0h~%xh\n",imageSize-1);
    printf("\nAvailable target block number: 0~1023\n");
    printf("Input target block number:");
    scanf("%d",&targetBlock);
    targetSize=((imageSize+0x20000-1)/0x20000)*0x20000;
    printf("target start block number     =%d\n",targetBlock);
    printf("target size        (0x20000*n) =0x%x\n",targetSize);
    printf("STATUS:");
    blockIndex=targetBlock;

    while(1)
    {
	if(noLoad==0)
	{
	    LoadImageFile(blockBuf,0x20000);
	}
	noLoad=0;
	
#if BAD_CHECK       
	if(NF_IsBadBlock(blockIndex) && blockIndex!=0 )	// 1:bad 0:good
        {
	    blockIndex++;   // for next block
	    noLoad=1;
	    continue;
	}
#endif
	if(!NF_EraseBlock(blockIndex))
	{
	    blockIndex++;   // for next block
	    noLoad=1;
	    continue;
	}

	printf("E");
	srcPt=blockBuf;

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

	    srcPt+=2048;	// Increase buffer addr one pase size
	    printf("p");
		if(progSize+i*2048>=imageSize)
			break;	// Exit while loop
	}
	printf("\n");

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




void K9F1G08_PrintBlock(void)// Printf one page
{
    int i;
    U16 id;
    U32 block,page;
    U8	buffer[2048+64];

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

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

    for(i=2048;i<2048+64;i++)
    {
        printf("%02x ",buffer[i]);
    }

    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				(0x00)
#define	READ_2				(0x30)
#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.

/*
#define NF_CMD(cmd)	{rNFCMD=cmd;}
#define NF_ADDR(addr)	{rNFADDR=addr;}	
#define NF_nFCE_L()	{rNFCONF&=~(1<<11);}
#define NF_nFCE_H()	{rNFCONF|=(1<<11);}
#define NF_RSTECC()	{rNFCONF|=(1<<12);}
#define NF_RDDATA() 	(rNFDATA)
#define NF_WRDATA(data) {rNFDATA=data;}

#define NF_WAITRB()    {while(!(rNFSTAT&(1<<0)));} 
	    //wait tWB and check F_RNB pin.   
*/
#define ID_K9F1G08V0M	0xecF1

static U8 seBuf[64]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};

// 1block=(2048+64)bytes x 64pages
// 1024block

// A[23:18][17:12]
//  block   page

static int NF_EraseBlock(U32 block)
{
    U32 blockPage=(block<<6);

#if BAD_CHECK
    if(NF_IsBadBlock(block) && block!=0) //block #0 can't be bad block for NAND boot
	return 0;
#endif

    NF_nFCE_L();
    
    NF_CMD(0x60);   // Erase one block 1st command

    NF_ADDR(blockPage&0xff);	    // Page number=0
    NF_ADDR((blockPage>>8)&0xff);   

    NF_CMD(0xd0);   // Erase one blcok 2nd command
    
    Delay(1); //wait tWB(100ns)

    NF_WAITRB();    // Wait tBERS max 3ms.
    NF_CMD(0x70);   // Read status command

    if (NF_RDDATA()&0x1) // Erase error
    {	
    	NF_nFCE_H();
		printf("[ERASE_ERROR:block#=%d]\n",block);
		NF_MarkBadBlock(block);

⌨️ 快捷键说明

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