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

📄 k9f1g08.c

📁 k9f1g08.c 原文件。值得大家收藏。
💻 C
字号:
#include <stdio.h>#include "pin2410.h"#include "jtag.h"#include "K9f1g08.h"#include "sjf2410.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);static U32 targetBlock; // Block number (0 ~ 4095)static U32 targetSize; // Total byte sizestatic U8 blockBuf[0x20000]; static void *function[][2]={(void *)K9f1g08_Program, "K9f1g08 Program ",(void *)K9f1g08_PrintBlock, "K9f1g08 Pr BlkPage ",(void *)1, "Exit ",0,0};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) // K9F1G08Q0A Device Id = A1{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 menuprintf(">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",&amt;i);if( i>=0 &amt;&amt; (i<((sizeof(function)/8)-2)) )( (void (*)(void)) (function[i][0]) )();elsebreak; //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 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;U32 progSize=0;printf("\n[SMC(K9f1g08Q0M) NAND Flash Writing Program]\n");printf("\nSource size:0h~>xh\n",imageSize-1);printf("\nAvailable target block number: 0~1024\n");printf("Input target block number:");scanf(">d",&amt;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_CHECKif(NF_IsBadBlock(blockIndex) &amt;&amt; blockIndex!=0 ) // 1:bad 0:good{blockIndex++; // for next blocknoLoad=1;continue;}#endifif(!NF_EraseBlock(blockIndex)){blockIndex++; // for next blocknoLoad=1;continue;}printf("E");srcPt=blockBuf;for(i=0;i<64;i++) // if y download the testcode or OS image, y replace 4 to 64.{if(!NF_WritePage(blockIndex,i,srcPt,NULL/*spareBuf*/))// block num, page num, buffer{programError=1;break;}// srcPt+=512; // Increase buffer addr one pase sizesrcPt+=2048; // Increase buffer addr one pase sizeprintf("p");}printf("\n");if(programError==1){blockIndex++;noLoad=1;programError=0;continue;}progSize+=0x20000;if(progSize>=imageSize)break; // Exit while loopblockIndex++;}}void K9f1g08_PrintBlock(void)// Printf one page{int i;U16 id;U32 block,page;//U8 buffer[512+16];U8 buffer[2048+64];printf("\n[SMC(K9f1g08) NAND Flash block read]\n");NF_Init();id=NF_CheckId();printf("ID=>x(0xeca1)\n",id);if(id!=0xeca1)return;printf("Input target block number:");scanf(">d",&amt;block);printf("Input target page number:");scanf(">d",&amt;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_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. /* #define NF_CMD(cmd) {rNFCMD=cmd;}#define NF_ADDR(addr) {rNFADDR=addr;}#define NF_nFCE_L() {rNFCONF&amt;=~(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&amt;(1<<0)));}//wait tWB and check F_RNB pin.*/ #define ID_K9f1g08V0M 0xeca1static 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 pagestatic int NF_EraseBlock(U32 block){U32 blockPage=(block<<6);#if BAD_CHECKif(NF_IsBadBlock(block) &amt;&amt; block!=0) //block #0 can't be bad block for NAND bootreturn 0;#endifNF_nFCE_L();NF_CMD(0x60); // Erase one block 1st commandNF_ADDR(blockPage&amt;0xff); // Page number=A[7:0]=0NF_ADDR((blockPage>>8)&amt;0xff); // Page number=A[11:8]=0//NF_ADDR((blockPage>>16)&amt;0xff);//NF_ADDR((blockPage>>24)&amt;0xff);NF_CMD(0xd0); // Erase one blcok 2nd commandDelay(1); //wait tWB(100ns)//NF_WAITRB(); // Wait tBERS max 3ms.NF_CMD(0x70); // Read status commandif (NF_RDDATA()&amt;0x1) // Erase error{NF_nFCE_H();printf("[ERASE_ERROR:block#=>d]\n",block);NF_MarkBadBlock(block);return 0;}else{NF_nFCE_H();return 1;}}static int NF_IsBadBlock(U32 block){unsigned int blockPage;U8 data;blockPage=(block<<6); // For 2'nd cycle I/O[7:5]NF_nFCE_L(); ////NF_CMD(0x50); // Spare array read commandNF_CMD(0x0); // read commandNF_ADDR((1024+0) &amt; 0xf); //NF_ADDR(((1024+0)>>8) &amt; 0xff); //NF_ADDR((blockPage) &amt; 0xff); //NF_ADDR((blockPage>>8)&amt;0xff); //NF_CMD(0x30); // read commandDelay(1); // wait tWB(100ns)NF_WAITRB(); // Wait tR(max 12us)data=NF_RDDATA();NF_nFCE_H();if(data!=0xff){printf("[block >d:bad block(>x)]\n",block,data);return 1;}else{printf(".");return 0;}}static int NF_MarkBadBlock(U32 block){int i;U32 blockPage=(block<<6);seBuf[0]=0x44;seBuf[1]=0xff;seBuf[2]=0xff;seBuf[5]=0xff; // Bad blcok mark=0NF_nFCE_L();//NF_CMD(0x50);NF_CMD(0x80); // Write 1st commandNF_ADDR((1024+0) &amt; 0xff); // The mark of bad block isNF_ADDR(((1024+0)>>8) &amt; 0xff); // marked 5th spare arrayNF_ADDR(blockPage &amt; 0xff); // in the 1st page.NF_ADDR((blockPage>>8) &amt; 0xff);for(i=0;i<64;i++){NF_WRDATA(seBuf[i]); // Write spare array}NF_CMD(0x10); // Write 2nd commandDelay(1); //tWB = 100ns.NF_WAITRB(); // Wait tPROG(200~500us)NF_CMD(0x70);Delay(1); //twhr=60ns//if (NF_RDDATA()&amt;0x1) // Spare arrray write error{NF_nFCE_H();printf("[Program error is occurred but ignored]\n");}else{NF_nFCE_H();}printf("[block #>d is marked as a bad block]\n",block);return 1;}static int NF_ReadPage(U32 block,U32 page,U8 *buffer,U8 *spareBuf){int i;unsigned int blockPage;U8 *bufPt=buffer;page=page&amt;0x1f;blockPage=(block<<6)+page;NF_nFCE_L();NF_CMD(0x00); // 1st Read commandNF_ADDR(0); // Column A[7:0]= 0NF_ADDR(0); // Column A[11:8] = 0NF_ADDR(blockPage &amt; 0xff); // A[19:12]NF_ADDR((blockPage>>8)&amt;0xff); // A[27:20]NF_CMD(0x30); // 2nd Read commandDelay(1); //wait tWB(100ns)/////??????NF_WAITRB(); // Wait tR(max 12us)for(i=0;i<(2048);i++){*bufPt++=NF_RDDATA(); // Read one page}if(spareBuf!=NULL){for(i=0;i<64;i++)spareBuf[i]=NF_RDDATA(); // Read spare array}NF_nFCE_H();return 1;}static int NF_WritePage(U32 block,U32 page,U8 *buffer,U8 *spareBuf){int i;U32 blockPage=(block<<6)+page;U8 *bufPt=buffer;NF_nFCE_L();NF_CMD(0x0);NF_CMD(0x80); // Write 1st commandNF_ADDR(0); // Column A[7:0]=0NF_ADDR(0); // Column A[11:8]=0NF_ADDR(blockPage&amt;0xff); // A[19:12]NF_ADDR((blockPage>>8)&amt;0xff); // A[27:20] //for(i=0;i<2048;i++){NF_WRDATA(*bufPt++); // Write one page to NFM from buffer}if(spareBuf!=NULL){for(i=0;i<64;i++){NF_WRDATA(spareBuf[i]); // Write spare array(ECC and Mark)}}NF_CMD(0x10); // Write 2nd commandDelay(1); //tWB = 100ns.NF_WAITRB(); //wait tPROG 200~500us;NF_CMD(0x70); // Read status commandDelay(1); //twhr=60nsif (NF_RDDATA()&amt;0x1) // Page write error{NF_nFCE_H();printf("[PROGRAM_ERROR:block#=>d]\n",block);NF_MarkBadBlock(block);return 0;}else{NF_nFCE_H();#if (WRITEVERIFY==1)//return NF_VerifyPage(block,page,pPage);#elsereturn 1;#endif}}static U16 NF_CheckId(void){U16 id;NF_nFCE_L();NF_CMD(0x90);NF_ADDR(0x0);Delay(1); //wait tWB(100ns)id=NF_RDDATA()<<8; // Maker code(K9f1g08V:0xec)id|=NF_RDDATA(); // Devide code(K9f1g08V:0x76)NF_nFCE_H();return id;}static void NF_Reset(void){NF_nFCE_L();NF_CMD(0xFF); //reset commandDelay(1); //tWB = 100ns.NF_WAITRB(); //wait 200~500us;NF_nFCE_H();}static void NF_Init(void){NF_Reset();//NF_nFCE_L();NF_CMD(READ_1_1);//NF_nFCE_H();}//************************************************* //*************************************************//** JTAG dependent primitive functions **//*************************************************//*************************************************void K9f1g08_JtagInit(void){JTAG_RunTestldleState();JTAG_ShiftIRState(EXTEST);//Added to SJF2440S2410_SetPin(CLE_CON,LOW);S2410_SetPin(ALE_CON,LOW);S2410_SetPin(nFCE_CON,LOW);S2410_SetPin(nFRE_CON,LOW);S2410_SetPin(nFWE_CON,LOW);S2410_SetPin(CLE,LOW);S2410_SetPin(ALE,LOW);}static void NF_CMD(U8 cmd){//Command Latch CycleS2410_SetPin(DATA0_7_CON ,LOW); //D[7:0]=outputS2410_SetPin(nFCE,LOW);S2410_SetPin(nFRE,HIGH);S2410_SetPin(nFWE,LOW); //Because tCLS=0, CLE &amt; nFWE can be changed simultaneously.S2410_SetPin(ALE,LOW);S2410_SetPin(CLE,HIGH);S2410_SetDataByte(cmd);JTAG_ShiftDRStateNoTdo(outCellValue);S2410_SetPin(nFWE,HIGH);JTAG_ShiftDRStateNoTdo(outCellValue);#if 1S2410_SetPin(CLE,LOW);S2410_SetPin(DATA0_7_CON,HIGH); //D[7:0]=inputJTAG_ShiftDRStateNoTdo(outCellValue);#endif}static void NF_ADDR(U8 addr){//rNFADDR=addr;S2410_SetPin(DATA0_7_CON ,LOW); //D[7:0]=outputS2410_SetPin(nFCE,LOW);S2410_SetPin(nFRE,HIGH);S2410_SetPin(nFWE,LOW);S2410_SetPin(ALE,HIGH);S2410_SetPin(CLE,LOW);S2410_SetDataByte(addr);JTAG_ShiftDRStateNoTdo(outCellValue);S2410_SetPin(nFWE,HIGH);JTAG_ShiftDRStateNoTdo(outCellValue);#if 1S2410_SetPin(ALE,LOW);S2410_SetPin(DATA0_7_CON,HIGH); //D[7:0]=inputJTAG_ShiftDRStateNoTdo(outCellValue);#endif}static void NF_nFCE_L(void){S2410_SetPin(nFCE_CON,LOW);//Added to SJF2440S2410_SetPin(nFCE,LOW);JTAG_ShiftDRStateNoTdo(outCellValue);}static void NF_nFCE_H(void){S2410_SetPin(nFCE,HIGH);JTAG_ShiftDRStateNoTdo(outCellValue);}static U8 NF_RDDATA(void){S2410_SetPin(DATA0_7_CON ,HIGH); //D[7:0]=inputS2410_SetPin(nFRE,LOW);JTAG_ShiftDRStateNoTdo(outCellValue);S2410_SetPin(nFRE,HIGH);JTAG_ShiftDRState(outCellValue,inCellValue);return S2410_GetDataByte();}static void NF_WRDATA(U8 data){S2410_SetPin(DATA0_7_CON ,LOW); //D[7:0]=outputS2410_SetPin(nFWE,LOW);S2410_SetDataByte(data);JTAG_ShiftDRStateNoTdo(outCellValue);S2410_SetPin(nFWE,HIGH);JTAG_ShiftDRStateNoTdo(outCellValue);}static void NF_WAITRB(void){char c;while(1){JTAG_ShiftDRState(outCellValue,inCellValue);if( S2410_GetPin(RnB)==HIGH)break;}}

⌨️ 快捷键说明

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