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

📄 荤夯 - k9f2g08.c

📁 三星6410的烧写工具源码
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include "..\include\pin6410.h"
#include "..\include\Jtag.h"
#include "..\include\K9F2G08.h"
#include "..\include\sjf6410.h"


//#define BAD_CHECK	(0)
#define BAD_CHECK	(1)
#define ECC_CHECK	(0)

//#define unsigned char U8

//extern void enc_reg(msg_len, fdin, chk_parity);
extern void ECC_GenM(U8 *pEcc, U32 *pBuf, U8 nBW);
static void Make_1bit_ECC(int spareBuf_addr);

// 1bit ECC use only 4Byte, 4th Byte is 0xff
static U8 chk_parity_1bitECC[4] = {0xff,0xff,0xff,0xff};	// 1bit ECC Data

// 4bit ECC use only 7Byte, 8th Byte is 0x00.
//static unsigned char chk_parity[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};	// 4bit ECC Data


static U8 spareBuf[64]=
	{0xff,0x00,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};
	 
static U8 spareBuf2[64]=
	{0xff,0x00,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};
	 
U8 spareECC_Buf[512];	
U8 spareECC_Buf2[512];
	 
static int msg_len = 512;
static U8 *srcPt;
static U32 *srcPt32;


//*************** JTAG dependent functions ***************
void K9F2G08_JtagInit(void);
static void NF_CMD(U8 cmd);
static void NF_ADDR(U8 addr);
static void NF_Xm0CSn2_OUT_L(void);
static void NF_Xm0CSn2_OUT_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);
static int NF_IsBadBlock(U32 block);
static int NF_MarkBadBlock(U32 block);
static void NF_Reset(void);
static void NF_Init(void);
//*******************************************************

void K9F2G08_PrintBlock(void);
void K9F2G08_Program(void);
void K9F2G08_Program_SLC(void);


static U32 targetBlock;	    
static U32 UseBlockNumber;	
static U32 targetSize;	    
static U32 barcount=0;
static U32 spacecount=0;
static U8 bar[34];
static U8 space[34];

static U8 blockBuf[0x80000]; 

static void *function[][2]=
{
    (void *)K9F2G08_Program,		"K9F2G08 Program",
    (void *)K9F2G08_PrintBlock,		"K9F2G08 Print blkPage  ",
    (void *)1,			    	    "Exit                ",
    0,0
};


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

	printf("\n[K9F2G08 NAND Flash JTAG Programmer]\n");
    K9F2G08_JtagInit();
    NF_Init();    
    

    id=NF_CheckId();
    
	if((id!=0xecda)&&(id!=0xecaa)) 
    {
		printf("ERROR: K9F2G08 or 6410X52 MCP is not detected.\n Detected ID=0x%x.\n",id);
		return;
    }
    else if (id == 0xecda)
		printf("K9F2G08U is detected. ID=0x%x\n\n",id);
    else if (id == 0xecaa)
		printf("K9F2G08R or 6410X52 MCP is detected. ID=0x%x\n\n",id);

    while(1)
    {

	i=0;
    	while(1)
	{   //display menu
	    printf( "%2d : %s\n",i,function[i][1]);
	    i++;
	    if((int)(function[i][0])==0)
	    {
		printf("\n");
		break;
	    }	    
	}
	
	for(barcount=0;barcount<34;barcount++){
		bar[barcount] = '=';
		space[barcount] = ' ';
	}

	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 K9F2G08_Program(void)
{
    U32 i;
    int programError=0;
    U32 blockIndex;
    int noLoad=0;    
    U32 percent=0;
    U32 progSize=0;

	printf("\n[K9F2G08 NAND Flash Writing Program]\n");    
    printf("\nSource size:0h~%xh\n",imageSize-1);
	printf("\nAvailable target block number : 0~4096\n"); // msp4 2006.09.27 
    printf("Input target block number : ");
    scanf("%d",&targetBlock);    
    blockIndex=targetBlock;
    printf("blockIndex = %d\n", blockIndex);
    while(1)
    {
	if(noLoad==0)
	{
		LoadImageFile(blockBuf,imageSize);
	}                                       
	noLoad=0;		
	
#if BAD_CHECK       
	if(NF_IsBadBlock(blockIndex) && blockIndex!=0 )	// 1:bad 0:good
	{
	    blockIndex++;   // for next block
	    noLoad=1;
	    continue;
	}
#endif

	for(i=0;i<(imageSize/(2048*128)+1);i++){
		if(!NF_EraseBlock(blockIndex + i))
		{
		    blockIndex++;   // for next block
		    noLoad=1;
		    continue;
		}
		printf("block #%d erase done\n",blockIndex + i);
	}
		
	srcPt=blockBuf;	

#if 0 // in case of "for(i=0;i<34;i++)", display.
	printf("Page Program |");
#else
	printf("page programing : #0");
#endif
	printf("%c",0x0D);
	

	//for(i=0;i<34;i++)
	for(i=0;i<(imageSize/2048 + 1);i++)
	{						
		/*********** fill 'spareBuf' for SPARE AREA ************/
		// must be added : 4 ~ 15(Spare Context) area. Data is 0xFF now.
		Make_1bit_ECC(16);// 16 ~ 19(Sector 0)
		Make_1bit_ECC(20);// 20 ~ 23(Sector 1)
		Make_1bit_ECC(24);// 24 ~ 27(Sector 2)
		Make_1bit_ECC(28);// 28 ~ 31(Sector 3)
				
		srcPt=blockBuf+(i*2048);// back to the 'srcPt' value.		
		/*******************************************************/	
		
		if(!NF_WritePage(blockIndex,i,srcPt,spareBuf))// block num, page num, buffer
	    {
	        programError=1;
	        break;
	    }

		srcPt+=2048;	// Increase buffer addr one page size

	    //printf("page #%d program done", i);

#if 0	// in case of "for(i=0;i<34;i++)", display.
	    printf("Page Program |");
	    for(barcount=0; barcount<(i+1); barcount++)
	    	printf("%c",bar[barcount]);
	    for(spacecount=0; spacecount<34-(i+1); spacecount++)
	    	printf("%c",space[spacecount]);
	    printf("| %d%% page #0 ~ #%d",(i+1)*3,i);
	    printf("%c",0x0D);
#else
		printf("page programing : #0 ~ #%d", i);
	    printf("%c",0x0D);
#endif
	}
	
	printf("\npage program done\n\n");
	//printf("block %d, %d program complete!\n\n", blockIndex, blockIndex+1);	
	
    if(programError==1)
	{
	    blockIndex++;
	    noLoad=1;
	    programError=0;
	    continue;
	}
	

	progSize+=imageSize;
	if(progSize>=imageSize)
	    break;	// Exit while loop
	    
	blockIndex++;	
    }
}

/** SLC Spare Area Layout for WinCE 6.0(64Byte per 1page(2KByte)) **/
//
//  0      : Bad Mark(0xff)
//  1      : Clean Mark(0x00)
//  2 ~ 3  : Reserved(0xff)
//  4 ~ 15 : Spare context(0xff)
// 16 ~ 19 : Sector0 ECC
// 20 ~ 23 : Sector1 ECC
// 24 ~ 27 : Sector2 ECC
// 28 ~ 31 : Sector3 ECC
// 32 ~ 63 : NOT USED(0xff)
/********************************************************************/
static void Make_1bit_ECC(int spareBuf_addr)
{
	int spareBuf_count=0;				
	
	//ECC_GenM(U8 *pEcc, U32 *pBuf, U8 nBW);
	ECC_GenM(chk_parity_1bitECC, srcPt, 0);
	srcPt += 512;
		
	for(spareBuf_count=0;spareBuf_count<4;spareBuf_count++)
		spareBuf[spareBuf_addr + spareBuf_count] = chk_parity_1bitECC[spareBuf_count];	
}

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

	printf("\n[K9F2G08 or 6410X52 MCP NAND Flash block read]\n");	
    
    NF_Init();
    id=NF_CheckId();

	printf("ID=%x(0xecxx)\n",id);

	if((id!=0xecda)&&(id!=0xecaa))   // K9F2G08 or 6410X52 MCP
		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("\nblock=%d, page=%d \n\nData Area",block,page);
    for(i=0;i<2048;i++)
    {
        if(i%16==0)
	    	printf("\n%3xh : ",i);
	    	
        printf("%02x ",buffer[i]);
    }
    printf("\n\nSpare Area",i);

    for(i=2048;i<2048+64;i++)
    {
    	if(i%16==0)
    		printf("\n%3xh : ",i-2048);
        printf("%02x ",buffer[i]);
    }

    printf("\n\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.

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:14][13:9]
//  block   page

static int NF_EraseBlock(U32 block)
{
    U32 blockPage=(block<<7);
    //U32 blockPage=(block<<8);

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

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

    NF_ADDR(blockPage&0xff);	    // Page number=A[7:0]=0
	NF_ADDR((blockPage>>8)&0xff);	    // Page number=A[11:8]=0
	NF_ADDR((blockPage>>16)&0xff);	    // Page number=A[11:8]=0

    //NF_ADDR((blockPage>>16)&0xff);   
    //NF_ADDR((blockPage>>24)&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_Xm0CSn2_OUT_H();
	printf("[ERASE_ERROR:block#=%d]\n",block);
	NF_MarkBadBlock(block);
	return 0;
    }
    else 
    {
    	NF_Xm0CSn2_OUT_H();
        return 1;
    }
}


static int NF_IsBadBlock(U32 block)
{
    unsigned int blockPage, page = 127;
    U8 data;    
    
    blockPage=(block<<7) + page;	// page(0 ~ 127), bad block mark in 127 page
    
    NF_Xm0CSn2_OUT_L();        

    //NF_CMD(0x50);		// Spare array read command
	NF_CMD(0x0);		// read command
    NF_ADDR((2048+0) & 0xf);		// 
    NF_ADDR(((2048+0)>>8) & 0xff);	// 
	NF_ADDR((blockPage) & 0xff);	// 

⌨️ 快捷键说明

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