#include <stdio.h>
#include "pin2440.h"
#include "jtag.h"
#include "K9F2G08.h"
#include "sjf.h"
//#include "def.h"
#define BAD_CHECK (0) //是否进行坏块检查 0:否 1:是
#define ECC_CHECK (0) // 是否进行ECC检查 0:否 1:是
//*************** JTAG dependent functions ***************
void K9F2G08_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 K9F2G08_PrintBlock(void);
void K9F2G08_Program(void);
int tmp;
U32 imageSize;
static U32 targetBlock; //要烧录的块
static U32 targetPage; //要烧录的页
static U32 targetSize; //bin文件的大小
static U8 blockBuf[0x20000];
static U8 pageBuf[0x800]; //pageBuf占2K字节
static char *function[][2]=
{
(void *)K9F2G08_Program, "K9F2G08 Program ",
(void *)K9F2G08_PrintBlock, "K9F2G08 Print page ",
(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!=K9F2G08_ID)
{
printf("ERROR: K9F2G08 is not detected. Detected ID=0x%x.\n",id);
return;
}
else
{
printf("\nK9F2G08 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("\nSelect the function to test :\n >");
tmp = scanf("%d",&i);
switch(i)
{
case 0: K9F2G08_Program();break;
case 1: K9F2G08_PrintBlock();break;
case 2: return; //Exit menu
default:break;
}
}
}
void K9F2G08_Program(void)
{
int i;
int BlockNum = 0; //表示要占用的块数
int PageNum = 0;
int programError=0;
U32 blockIndex;
int noLoad=0;
int imagefile;
int page_num = 1;
char f_name[30];
U32 progSize=0;
U8 spareBuf[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
};
U8 *srcPt; //srcPt 指向blockBuf
printf("\n[SMC(K9F2G08U0A) NAND Flash Writing Program]\n");
inputfile:
printf("Input file name:\n >");
tmp = scanf("%s",f_name);
//filename = f_name;
tmp = OpenImageFile(f_name);
if(!tmp)
goto inputfile;
BlockNum = ((imageSize+0x20000-1)/0x20000); //表示要占用的块数,BlockNum>=1
PageNum = ((imageSize-0x20000*(BlockNum-1))+0x800-1)/0x800;
printf("Source size:0x00B~0x%xB\n",imageSize-1);
printf("It will use ");
if(BlockNum >1)
printf("%d block(s) and %d page(s)",BlockNum-1,PageNum);
else
printf("only %d page(s)",PageNum);
printf("\nAvailable target block number: 0~2047\n");
printf("Input target block number:\n >");
tmp = scanf("%d",&targetBlock);
printf("\nAvailable target page number: 0~63\n");
printf("Input target page number:\n >");
tmp = scanf("%d",&targetPage); //targetPage表示当前要操作(写)的页
printf("\n");
printf("target start block number = %d\n",targetBlock);
printf("target size (128kB/block) = %d\n",BlockNum);
blockIndex = targetBlock;
printf("start flashing...\n");
while(BlockNum>=1)
{
if(noLoad==0) //noLoad 表示的是 blockBuf 中是否有导入数据,
{
imagefile = LoadImageFile(blockBuf,0x20000); // 读1block,imagefile中存储了文件的实际字节数
noLoad=1; //表示 blockBuf中有了数据
srcPt=blockBuf;
}
tmp = NF_EraseBlock(blockIndex);
if(!tmp && blockIndex != 0)
{
blockIndex++; //如果当前要写的块是坏的 且 不是第零块,则 blockIndex 加1
noLoad=1;
continue;
}
//确定了当前块是好块,则开始写页,如下
while(targetPage<64)
{
/*
if(NF_IsBadBlock(blockIndex) && blockIndex!= 0 ) // 1:bad 0:good
{
//blockIndex++; // for next block
noLoad=1;
continue;
}
*/
tmp = NF_WritePage(blockIndex,targetPage,srcPt,NULL);
if (!tmp)
{
programError=1;
break;
}
targetPage++;
srcPt+=2048;
}
if (programError == 1)
{
noLoad=1;
blockIndex ++;
programError=0;
continue;
}
BlockNum--;
blockIndex ++;
noLoad = 0;
targetPage=0;
progSize+=0x20000;
if(progSize>=imageSize)
break; // Exit while loop
}
printf("end\n");
}
void K9F2G08_PrintBlock(void) //输出一页的内容
{
int i;
U16 id;
U32 block,page;
U8 buffer[2048+64]; //buffer用来存储读出的一页的数据
printf("\n[SMC(K9F2G08) NAND Flash block read]\n");
NF_Init();
printf("Input target block number(0 ~ 2047):\n >");
tmp = scanf("%d",&block);
printf("Input target page number(0 ~ 63 ):\n >");
tmp = scanf("%d",&page);
NF_ReadPage(block,page,buffer,buffer+2048); // buffer指向缓冲区首地址,buffer偏移2048后指向64位的额外空间的首地址
printf("block=%d,page=%d:",block,page);
for(i=0;i<2048;i++)
{
if(i%16==0)
printf("\n%6dB:",i);
printf("%02x ",buffer[i]);
}
printf("\n\n All:%08d\n",i);
for(i=2048;i<2048+64;i++)
{
if(i%16==0)
printf("\n%6dB:",i);
printf("%02x ",buffer[i]);
}
printf("\n\n End:%dB\n\n",i);
}
//*************************************************
//*************************************************
//** H/W dependent functions **
//*************************************************
//*************************************************
// NAND Flash Memory Commands
#define READ_ID (0x90)
#define RESET (0xFF)
#define READ_1 (0x00)
#define READ_2 (0x50)
#define PAGE_PROGRAM_1 (0x80)
#define PAGE_PROGRAM_2 (0x10)
#define BLOCK_ERASE_1 (0x60)
#define BLOCK_ERASE_2 (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.
*/
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=(2k+16)bytes x 64pages
// 2048blocks
// A[28:18][17:12]
// block page
static int NF_EraseBlock(U32 block)
{
U32 blockPage=(block<<6);
- 1
- 2
- 3
前往页