📄 nand.c
字号:
#include "def.h"
#include "44b.h"
#include "44blib.h"
#include "Nand.h"
#include <string.h>
///////////
#define SIZE_16M 0x01000000
#define SIZE_32M 0x02000000
#define SIZE_64M 0x04000000
#define SIZE_128M 0x08000000
#ifndef NAND_FLASH_16M
static U16 uiNandAddr;
#endif
static int bSupportNAND;
static U32 NandID;
struct NFChipInfo
{
U32 uiID;
U32 uiSize;
};
//Two Para ID and Size
static struct NFChipInfo NandFlashChip[] = {
{0xec73, SIZE_16M},
{0xec75, SIZE_32M},
{0xec76, SIZE_64M},
{0xec79, SIZE_128M},
{0, 0},
};
struct Partition
{
U32 uiOffset;
U32 uiSize;
char *name;
};
struct Partition *pNandPart;
U32 NandFlashSize;
////////////////
U8 NFWaitBusy(void) //Just wait for the Ans
{
U8 ucStatus;
NFWrCmd(QUERYCMD);
do
{
ucStatus = NFRdDat();
}
while( !(ucStatus&0x40) );
NFWrCmd(READCMD0);
return (ucStatus&1);
}
U32 NFReadID(void)
{
U32 id, loop = 0;
NFEnable();
NFWrCmd(READIDCMD);
NFWrAddr(0);
while(NFIsBusy()&&(loop<10000)) loop++;
if(loop>=10000)
return 0;
id = NFRdDat()<<8;
id |= NFRdDat();
NFDisable();
return id;
}
void NandFlashInit(void)
{
U32 uiIDNumber;
bSupportNAND = FALSE;
NandID = NFReadID();
for(uiIDNumber=0; NandFlashChip[uiIDNumber].uiID!=0; uiIDNumber++)
{
if(NandFlashChip[uiIDNumber].uiID==NandID)
{
//NandID = uiIDNumber;
NandFlashSize = NandFlashChip[uiIDNumber].uiSize;
bSupportNAND = TRUE;
if(!pNandPart[0].uiSize)
{
pNandPart[0].uiOffset = 0;
pNandPart[0].uiSize = NandFlashSize;
pNandPart[1].uiSize = 0;
}
Uart_Printf("The NandFlash ID is 0x%x\n",NandID);
Uart_Printf("The NandFlash Size is 0x%x\n",NandFlashSize);
return;
}
}
return;
}
U8 NFReadStat(void)
{
U8 ucStatus;
NFEnable();
NFWrCmd(QUERYCMD); //Reading the State R
ucStatus = NFRdDat();
NFDisable();
return ucStatus;
}
U8 NFEraseBlock(U32 uiBlockAdd)
{
U8 ucStatus;
uiBlockAdd &=~0x1f; //Only A14~A23 is useful
NFEnable();
NFWrCmd(ERASECMD0);
NFWrAddr(uiBlockAdd); //Address
NFWrAddr(uiBlockAdd >> 8);
NFWrCmd(ERASECMD1); //Erase
ucStatus=NFWaitBusy(); //Getting the statue
NFDisable();
if(ucStatus==1)
{
Uart_Printf("Erase block 0x%08x %s\n failed", uiBlockAdd);
}
return ucStatus;
}
void NFReadPage(const U32 uiPageAddr, U8 *pcDst)
{
U32 i;
U8 ucStatus;
NFEnable();
NFWrCmd(READCMD0);
NFWrAddr(0);
NFWrAddr(uiPageAddr);
NFWrAddr(uiPageAddr >> 8);
ucStatus=NFWaitBusy();
for(i=0 ; i<512; i++)
{
pcDst[i] = NFRdDat();
}
NFDisable();
return;
}
U8 NFWritePage( const U32 uiPageAddr,const U8 *pcSrc )
{
int i,j;
U8 ucStatus;
NFEnable();
NFWrCmd( PROGCMD0 );
NFWrAddr( 0);
NFWrAddr( uiPageAddr );
NFWrAddr( uiPageAddr >> 8 );
for(i=0; i<512; i++) //Writing the Buffer Data
{
NFWrDat(pcSrc[i]);
}
if(!uiPageAddr)
{
NFWrDat( 'b' );
NFWrDat( 'o' );
NFWrDat( 'o' );
NFWrDat( 't' );
}
NFWrCmd( PROGCMD1 );
ucStatus = NFWaitBusy();
NFDisable();
if(ucStatus) //Checking the Writing
{
Uart_Printf("Write nand flash 0x%x fail\n", uiPageAddr);
}
/*
验证工作:将写入的NandFlash内容再读出 放入ucReadData[]中
ucReadData[]与Buf[]
*/
else
{
U8 ucReadData[512]; //Checking the data
NFReadPage(uiPageAddr, ucReadData); //
for(j=0 ;j<512 ;j++)
{
if( ucReadData[j] != pcSrc[j] )
{
Uart_Printf("Check data fail\n");
ucStatus = 1;
break;
}
}
}
return ucStatus;
}
U8 NFChkBadBlk( U32 uiBlockAddr )
{
U8 ucData;
uiBlockAddr &= ~0x1f; //不用A9~A13
NFEnable();
NFWrCmd(READCMD2); //point to area c
NFWrAddr(5); //mark offset 5 Why ??? By cazy
NFWrAddr(uiBlockAddr);
NFWrAddr(uiBlockAddr >> 8);
NFWaitBusy();
ucData = NFRdDat();
NFWrCmd(READCMD0); //point to area a
NFDisable();
return ( ucData != 0xff );
//if ucData=0xff --> (ucData!= 0xff)=0--->不是坏块
}
/*
坏块的处理: 在坏块的第一页的 空闲区域 的 第6个字节 写上 非ff的内容
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -