|
关于 三星的NAND Flash K9K8G08U0A (8Gbit)求助
[复制链接]
关于 三星的NAND Flash K9K8G08U0A (8Gbit)求助
我能够读出它的ID. 与手册上的能够对应上(EC, D3, 51, 95, 58).
但是我进行写数据操作(按页进行),然后读出数据操作(按页进行),我发现读出来的数据并不是我想要的,我读出来的前面很很大部分全部是1E,后面有FF, 都是挨到的相同的(1E 1E .... 1E 1E
FF FF FF... FF FF FF), 我不知道是写操作有问题,还是读操作有问题,还是两个都有问题,那位大哥弄过了的,帮忙看下.
我的代码如下:
#define FPGA_NAND_IO07 数据线0-7
#define FPGA_NAND_CLE CLE-Command_Latch Enable
#define FPGA_NAND_ALE ALE Address Latch Enable
#define FPGA_NAND_CE_ CE Chip Enable
#define FPGA_NAND_RE_ RE Read Enable
#define FPGA_NAND_WE_ WE Write Enable
#define FPGA_NAND_WP_ WP Write Protect /*它一直是高电平*/
#define FPGA_NAND_RB_ RB Ready/Busy_
/*******************************************************************
功能: 发送命令
*******************************************************************/
void NANDFLASH_cmd_Send(unsigned char chCMD)
{
/*busy check , if low ,busy. if high, ready*/
while(!FPGA_NAND_RB_);
FPGA_NAND_CE_ = 0;
NANDFLASH_DelayNS(20); /*20NS*/
FPGA_NAND_CLE = 1;
FPGA_NAND_ALE = 0;
FPGA_NAND_RE_ = 1;
FPGA_NAND_WE_ = 0;
FPGA_NAND_IO07 = chCMD; /*放入数据*/
NANDFLASH_DelayNS(12); /*12NS*/
FPGA_NAND_WE_ = 1;
NANDFLASH_DelayNS(5);/*5NS*/
FPGA_NAND_CLE = 0;
}
/***************************************************************************
发送地址的五个字节
unsigned int nColAddr 这个是列地址值 它在列地址的低12位.
unsigned int nRowAddr 这个是行地址值 它在行地址的低19位.
1st. [ 7 - 0]
2nd. [11 - 8]
3rd. [19 -12]
4th. [27 -20]
5th. [30-28]
***************************************************************************/
void NANDFLASH_address_Send5(unsigned int nColAddr, unsigned int nRowAddr)
{
int i;
unsigned char chAddr;
/*纠错*/
nColAddr &= 0xfff;
nRowAddr &= 0x7ffff;
/*check busy*/
while(!FPGA_NAND_RB_);
FPGA_NAND_CE_ = 0;
NANDFLASH_DelayNS(20);
FPGA_NAND_CLE = 0;
for(i=0; i<5; i++)
{
/*check busy*/
while(!FPGA_NAND_RB_);
FPGA_NAND_ALE = 1;
FPGA_NAND_WE_ = 0;
/*共5次,每次取出数据*/
switch(i)
{
case 0:
case 1:
chAddr = (nColAddr>>(i*8)) & 0xff;
break;
case 2:
case 3:
case 4:
chAddr = (nRowAddr>>((i-2)*8)) & 0xff;
break;
}
FPGA_NAND_IO07 = chAddr; /*写入地址*/
NANDFLASH_DelayNS(12);
FPGA_NAND_WE_ = 1;
NANDFLASH_DelayNS(5);
FPGA_NAND_ALE = 0;
NANDFLASH_DelayNS(10);
}
}
/***********************************************************************************************
读取状态
读取状态寄存器.
unsigned char chCmd command 它只能是0x70, or 0x7b;
0x70 Read Status
0x7b Read EDC Status
***********************************************************************************************/
unsigned char NANDFLASH_ReadStatus(unsigned char chCmd)
{
unsigned char chData;
/*0x70 or 0x7b*/
NANDFLASH_cmd_Send(chCmd);
NANDFLASH_DelayNS(60);/*60*/
FPGA_NAND_RE_ = 0;
NANDFLASH_DelayNS(5);/*5*/
chData = FPGA_NAND_IO07;
FPGA_NAND_RE_ = 1;
NANDFLASH_DelayNS(15);/*15*/
return chData;
}
/************************************************************************************
读取器件ID: ReadID
************************************************************************************/
unsigned int NANDFLASH_ReadIDOperation()
{
int i;
unsigned char chtemp[5];
memset(chtemp, 0, 5);
/*Send command 90*/
NANDFLASH_cmd_Send(0x90);
/*地址1*/
chtemp[0] = 0;
NANDFLASH_address_Send_N(chtemp, 1);
FPGA_NAND_CLE = 0;
FPGA_NAND_CE_ = 0;
FPGA_NAND_WE_ = 1;
FPGA_NAND_ALE = 0;
NANDFLASH_DelayNS(10);
/*0=makecode 1=DeviceCode 2=3rdcyc. 3=4thcyc. 4=5thcyc.*/
for(i=0; i<5; i++)
{
NANDFLASH_DelayNS(10);
FPGA_NAND_RE_ = 0;
while(!FPGA_NAND_RB_);
chtemp = FPGA_NAND_IO07;
FPGA_NAND_RE_ = 1;
}
printf("Compare ID: EC D3 51 95 58\n");
printf("Read ID Operation\nCurrent: ");
/*这里是EC D3 51 95 58*/
for(i=0; i<5; i++)
{ printf("%02X ", chtemp);
}
printf("\n");
}
/*************************************************************************
功 能: 写一页的数据到FLASH中
输入参数:
unsigned int nPageIndex 页索引. 范围: 0-512k. 512K = 2的19次方
unsigned char *pData 写入的数据
返 回 值:
0 成功
非0 失败
*************************************************************************/
unsigned char NANDFLASH_Page_WriteData(unsigned int nPageIndex, unsigned char *pData)
{
unsigned int i;
unsigned char chtemp;
if(!pData)
return 1;
if(nPageIndex >= 0x80000) /**/
return 2;
/*发送命令 80H*/
NANDFLASH_cmd_Send(0x80);
/*发送地址 col.addr and row.addr*/
NANDFLASH_address_Send5(0, nPageIndex); /*第一个参数是列地址A11-A0, 第二个参数是行地址A30-A12*/
FPGA_NAND_CLE = 0;
FPGA_NAND_CE_ = 0;
FPGA_NAND_ALE = 0;
FPGA_NAND_RE_ = 1;
NANDFLASH_DelayNS(10);
for(i=0; i<2112; i++) /*一页= 2112字节*/
{
FPGA_NAND_WE_ = 0;
FPGA_NAND_IO07 = pData; /*写入数据*/
NANDFLASH_DelayNS(10);
FPGA_NAND_WE_ = 1;
NANDFLASH_DelayNS(10);
}
/*发送命令 10h*/
NANDFLASH_cmd_Send(0x10);
while(!FPGA_NAND_RB_);
/*读状态寄存器0x70*/
NANDFLASH_DelayNS(10);
chtemp = NANDFLASH_ReadStatus(0x70);
/*检查I/O_0 是否为0 */
if((chtemp&0x1) == 0)
return 0;
printf("\nWrite Error:%02X\n", chtemp);
return 1;
}
/*************************************************************************
功能: 读取一页数据到BUFFER中.
unsigned int nPageIndex, 页索引, 取值范围: 0-512K
unsigned char *pData 读出来放入的内容
success: 0
falied: !0
*************************************************************************/
unsigned char NANDFLASH_Page_ReadData(unsigned int nPageIndex, unsigned char *pData)
{
unsigned int i;
if(!pData)
return 1;
if(nPageIndex >= 0x80000)
return 2;
/*发送命令 00H*/
NANDFLASH_cmd_Send(0);
/*发送地址col.addr and row.addr*/
NANDFLASH_address_Send5(0, nPageIndex);/*第一个参数是列地址A11-A0, 第二个参数是行地址A30-A12*/
/*发送命令 30H*/
NANDFLASH_cmd_Send(30);
/*等待空闲*/
while(!FPGA_NAND_RB_);
FPGA_NAND_RE_ = 1;
FPGA_NAND_CLE = 0;
FPGA_NAND_CE_ = 0;
FPGA_NAND_WE_ = 1;
FPGA_NAND_ALE = 0;
NANDFLASH_DelayNS(10);
for(i=0; i<2112; i++)/*一页数据 = 2112B*/
{
FPGA_NAND_RE_ = 0;
NANDFLASH_DelayNS(10);
pData = FPGA_NAND_IO07; /*读取1B*/
FPGA_NAND_RE_ = 1;
NANDFLASH_DelayNS(10);
}
return 0;
}
void main()
{
unsigned char szbuf[2112];
unsigned int i, nret;
/*准备数据*/
for(i=0; i<2112; i++)
szbuf = i%256;
/*写一页*/
nret = NANDFLASH_Page_WriteData(0, szbuf); /*写第0页*/
if(nret)
printf("写入失败\n");
else
printf("写入成功");
memset(szbuf, 0, 2112);
/*读取一页*/
nret = NANDFLASH_Page_ReadData(0, szbuf);
if(nret)
{
printf("读取失败\n");
return;
}
else
printf("读取成功");
/*这里打印出来的数据始终是1E 1E ... 1E FF FF ... FF 并不是我想要的内容*/
for(i=0; i<2112; i++)
{
if((i%16) == 0) /*每行16个*/
printf("\n");
printf("%02X ", szbuf);
}
return;
}
|
|