功能:先擦除一块nand flash地址,然后往里面写一字符串,最后再读出来
实现,擦除、写、读功能。 芯片型号:K9F2G08UOB
/****************************************************
擦除nand flash 0x500000 地址
主函数:erase.c
****************************************************/
#define NFCONF *(volatile unsigned long *)0x4E000000
#define NFCONT *(volatile unsigned long *)0x4E000004
#define NFCMMD *(volatile unsigned long *)0x4E000008
#define NFADDR *(volatile unsigned long *)0x4E00000C
#define NFDATA *(volatile unsigned char *)0x4E000010
#define NFSTAT *(volatile unsigned long *)0x4E000020
void (*printf)(char *, ...) = 0x33f9291C;
void erase_nand(unsigned long addr);
void _start(void)
{
//configure for nand flash controller
NFCONF = (0x2 << 12) | (0x7 << 8) | (0x1 << 4);
//control
//NAND flash 开始工作
NFCONT = 1;
//设置擦除地址
erase_nand(0x500000);
}
//------发送地址参照K9F2G08UOA数据手册第9页--------
void send_addr(unsigned long addr)
{
NFADDR = (addr >> 11) & 0xff;
NFADDR = (addr >> 19) & 0xff;
NFADDR = (addr >> 27) & 0x1;
}
/*****************************************
块擦除
对照K9F2G08UOA手册第28页
******************************************/
void erase_nand(unsigned long addr)
{
unsigned int stat;
//发送指令60h块擦除设置指令
NFCMMD = 0x60;
//发送三次行地址
send_addr(addr);
//nandflash ready
//发送d0擦除指令
NFCMMD = 0xd0;
//等待忙状态
while(!(NFSTAT & 0x1))
;
// 发送70h读状态指令
NFCMMD = 0x70;
//读取数据
stat = NFDATA;
//最后一位判断成功与否
if(stat & 0x1){
printf("erase nand flash device err\n");
return ;
}
printf("erase nand flash device success\n");
}
/****************************************************
写nand flash 0x500000 地址 一个字符串
主函数:write.c
****************************************************/
#define NFCONF *(volatile unsigned long *)0x4E000000
#define NFCONT *(volatile unsigned long *)0x4E000004
#define NFCMMD *(volatile unsigned long *)0x4E000008
#define NFADDR *(volatile unsigned long *)0x4E00000C
#define NFDATA *(volatile unsigned char *)0x4E000010
#define NFSTAT *(volatile unsigned long *)0x4E000020
void (*printf)(char *, ...) = 0x33f9291C;
void write_nand(char *buf, unsigned long addr, int size);
char buf[11];
void _start(void)
{
int i;
//configure for nand flash controller
NFCONF = (0x2 << 12) | (0x7 << 8) | (0x1 << 4);
//control
NFCONT = 1;
//准备一个全写a的数组
for(i = 0; i < 11; i++)
{
buf[i] = 'a';
}
//往0x500000写字符串buf 11个字节
write_nand(buf, 0x500000, 11);
}
//------发送地址参照K9F2G08UOA数据手册第9页--------
void send_addr(unsigned long addr)
{
NFADDR = addr & 0xff;
NFADDR = (addr >> 8) & 0x7;
NFADDR = (addr >> 11) & 0xff;
NFADDR = (addr >> 19) & 0xff;
NFADDR = (addr >> 27) & 0x1;
}
/*****************************************
写数据
对照K9F2G08UOA手册第25页
******************************************/
void write_nand(char *buf, unsigned long addr, int size)
{
int i;
unsigned int stat;
//发80h串口数据输入指令
NFCMMD = 0x80;
//发送两次列地址,三次行地址
send_addr(addr);
//nandflash 数据准备
for(i = 0; i < (size); i++)
{
NFDATA = buf[i];
}
//发送10h写指令
NFCMMD = 0x10;
//等待忙状态
while(!(NFSTAT & 0x1))
;
// 发送70h读状态指令
NFCMMD = 0x70;
stat = NFDATA;
//最后一位判断成功与否
if(stat & 0x1){
printf("write nand flash device err\n");
return ;
}
printf("write nand flash device success\n");
}
/****************************************************
读nand flash 0x500000 地址 10个字节
主函数:read.c
****************************************************/
#define NFCONF *(volatile unsigned long *)0x4E000000
#define NFCONT *(volatile unsigned long *)0x4E000004
#define NFCMMD *(volatile unsigned long *)0x4E000008
#define NFADDR *(volatile unsigned long *)0x4E00000C
#define NFDATA *(volatile unsigned char *)0x4E000010
#define NFSTAT *(volatile unsigned long *)0x4E000020
void (*printf)(char *, ...) = 0x33f9291C;
void read_nand(char *buf, unsigned long addr, int size);
void _start(void)
{
char buf[11];
//configure for nand flash controller
NFCONF = (0x2 << 12) | (0x7 << 8) | (0x1 << 4);
//control
NFCONT = 1;
//往0x500000读数据
read_nand(buf, 0x500000, 10);
buf[11] = 0;
printf("read from nandflash: %s\n", buf);
}
//------发送地址参照K9F2G08UOA数据手册第9页--------
void send_addr(unsigned long addr)
{
NFADDR = addr & 0xff;
NFADDR = (addr >> 8) & 0x7;
NFADDR = (addr >> 11) & 0xff;
NFADDR = (addr >> 19) & 0xff;
NFADDR = (addr >> 27) & 0x1;
}
/*****************************************
读数据
对照K9F2G08UOA手册第23页
******************************************/
void read_nand(char *buf, unsigned long addr, int size)
{
int i;
NFCMMD = 0x00;
send_addr(addr);
NFCMMD = 0x30;
while(!(NFSTAT & 0x1 ))
;
//nandflash ready
for(i = 0; i < (size); i++)
{
buf[i] = NFDATA;
}
}
实验效果:
[HAPPY_LEE]# tftp 30000000 erase.bin //加载擦除函数
[HAPPY_LEE]# tftp 31000000 write.bin //加载写函数
[HAPPY_LEE]# tftp 32000000 read.bin //加载读函数
[HAPPY_LEE]# go 30000000
## Starting application at 0x30000000 ...
erase nand flash device success //擦除成功
## Application terminated, rc = 0x0
[HAPPY_LEE]# go 31000000
## Starting application at 0x31000000 ...
write nand flash device success //写成功
## Application terminated, rc = 0x0
[HAPPY_LEE]# go 32000000
## Starting application at 0x32000000 ...
read from nandflash: aaaaaaaaaa3 //读数据
## Application terminated, rc = 0x0
陈润息 于 2011-04-21 09:41:31发表:
路过
txgc_wm 于 2011-03-31 23:51:27发表:
void Nandflash_Identify_Invalid_Blocks(void)
{
int start_addr=0;
unsigned char data[2112];
for(start_addr=0;start_addr<2048; )
{
Nandflash_Page_Read(start_addr<<18,data);
if(data[2048]==0xff)
{
invalid_block[start_addr]=1;
Uart0_SendString(" a invalid block!");
start_addr++;
continue;
}
else
{
Nandflash_Page_Read(((start_addr<<6)+1)<<12,data);
if(data[2048]==0xff)
{
invalid_block[start_addr]=1;
Uart0_SendString(" a invalid block!");
start_addr++;
continue;
}
else
{
start_addr++;
}
}
}
return;
}
void Nandflash_Check_Invalid_Blocks(unsigned long addr)
{
unsigned long block_addr=addr>>18;
if(invalid_block[block_addr]==1)
Uart0_SendString("this a invalid block!");
else
Uart0_SendString("this a valid block,can be used to program!");
return;
}
以上是坏块的检查函数,请大家check一下!
孙晓 于 2011-03-31 16:13:46发表:
谢谢,你说了我现在知道了