- #ifndef__TEST_H__
- #define__TEST_H__
- #include”def.h”
- #defineMAX_NAND_BLOCK2048//总共2048块
- #defineNAND_PAGE_SIZE2048//每块main区2k字节=2048
- typedefstructnand_id_info//芯片的ID信息
- {
- U8IDm;//厂商ID
- U8IDd;//设备ID
- U8ID3rd;
- U8ID4th;
- U8ID5th;
- }nand_id_info;
- typedefstructbad_block_info//挂号坏块
- {
- U8area[MAX_NAND_BLOCK];//0一共非坏块,1一共坏块
- U32sum;//坏块的总数
- }bad_block_info;
- //NAND操作指令
- #defineNAND_CMD_READ_1st0x00
- #defineNAND_CMD_READ_2st0x30
- #defineNAND_CMD_RANDOM_WRITE0x85//随机写
- #defineNAND_CMD_RANDOM_READ_1st0x05
- #defineNAND_CMD_RANDOM_READ_2st0xe0
- #defineNAND_CMD_READ_CB_1st0x00//将NAND里一块内容写进另一块
- #defineNAND_CMD_READ_CB_2st0x35
- #defineNAND_CMD_READ_ID0x90
- #defineNAND_CMD_RES0xff//复位指令
- #defineNAND_CMD_WRITE_PAGE_1st0x80
- #defineNAND_CMD_WRITE_PAGE_2st0x10
- #defineNAND_CMD_BLOCK_ERASE_1st0x60//擦除指令
- #defineNAND_CMD_BLOCK_ERASE_2st0xd0
- #defineNAND_CMD_READ_STATUS0x70
- //NAND中止向量
- #defineINT_NFCON24
- //NFCONFHCLK=100MHZ
- #defineS3C2440_NFCONF_TACLS_init(1<<12)
- #defineS3C2440_NFCONF_TWRPH0_init(4<<8)
- #defineS3C2440_NFCONF_TWRPH1_init(0<<4)
- #defineS3C2440_NFCONF_BusWidth_init(0)
- #defineS3C2440_NFCONF_init()(rNFCONF=S3C2440_NFCONF_TACLS_init|/
- S3C2440_NFCONF_TWRPH0_init|/
- S3C2440_NFCONF_TWRPH1_init|/
- S3C2440_NFCONF_BusWidth_init)
- //NFCONT
- #defineS3C2440_NFCONT_LockTight_init(0<<13)
- #defineS3C2440_NFCONT_SoftLock_init(0<<12)
- #defineS3C2440_NFCONT_EnbIllegalAccINT_init(1<<10)
- #defineS3C2440_NFCONT_EnbRnBINT_init(0<<9)
- #defineS3C2440_NFCONT_RnB_TransMode_init(0<<8)
- #defineS3C2440_NFCONT_SpareECCLock_init(1<<6)
- #defineS3C2440_NFCONT_MainECCLock_init(1<<5)
- #defineS3C2440_NFCONT_InitECC_init(1<<4)
- #defineS3C2440_NFCONT_Reg_nCE_init(1<<1)//初始装备片选无效
- #defineS3C2440_NFCONT_MODE_init(0)
- #defineS3C2440_NFCONT_init()(rNFCONT=S3C2440_NFCONT_LockTight_init|/
- S3C2440_NFCONT_SoftLock_init|/
- S3C2440_NFCONT_EnbIllegalAccINT_init|/
- S3C2440_NFCONT_EnbRnBINT_init|/
- S3C2440_NFCONT_RnB_TransMode_init|/
- S3C2440_NFCONT_SpareECCLock_init|/
- S3C2440_NFCONT_MainECCLock_init|/
- S3C2440_NFCONT_InitECC_init|/
- S3C2440_NFCONT_Reg_nCE_init|/
- S3C2440_NFCONT_MODE_init)
- //NFSTAT
- #defineS3C2440_NFSTAT_init()(rNFSTAT&=0x3)
- //NFESTAT0
- #defineS3C2440_NFESTAT0_init()(rNFESTAT0=0)
- //NFESTAT1
- #defineS3C2440_NFESTAT1_init()(rNFESTAT1=0)
- //
- #defineselect_nand()(rNFCONT&=~(1<<1))
- #definedis_select_nand()(rNFCONT|=1<<1)
- #definecontroller_enable()(rNFCONT|=1)
- #definecontroller_disable()(rNFCONT&=~1)
- //
- voidnand_flash_init(void);//初始化
- voidnand_read_id(void);
- externintnand_block_erase(U32num);//num要删去的块号
- externintnand_page_write(U32addr,U8*buffer,U32size);//addr要写的起始页地址,buffer要写的缓存,size要写的字节巨细最大为4G
- externintnand_page_read(U32addr,U8*buffer,U32size);//addr开端页地址,从每页00地址开端读
- externintnand_random_read(U32paddr,U32offset,U8*data);//随机读数据paddr页地址,offset页内偏移地址
- externintnand_random_write(U32paddr,U32offset,U8data);//随机写,paddr页地址,offset页内区最终一个地偏移地址
- externvoidnand_test_bad_block(void);//测验坏块函数,并符号在nand_bbi变量里和spare里(假如非0xff则为坏块)
- #endif
- #include”2440addr.h”
- #include”test.h”
- #include”def.h”
- #defineNAND_DEBUG1//打印一些串口调试信息
- #defineUSE_ECC1//运用ECC验证
- nand_id_infonand_id;//界说挂号芯片ID的全局变量
- bad_block_infonand_bbi;//界说来挂号坏的全局变量
- voidinit_nand_bbi(void)//初始化变量
- {
- U32i;
- nand_bbi.sum=0;
- for(i=0;i
- nand_bbi.area[i]=0;//悉数初始化为0
- }
- voidnand_mask_bad_block(U32n)//标志坏块,n是坏块的块号
- {
- #ifdefNAND_DEBUG
- Uart_Printf(“NANDfoundandmaskabadblock=%d.”,n);
- #endif
- if(nand_bbi.area[n]!=1)
- {
- nand_bbi.area[n]=1;
- nand_bbi.sum++;
- nand_random_write(n*64,2048+64-1,0);//每块的第一个spare的最终一个字节,标志本块是否为坏块,非0xff为坏块
- }
- }
- intdetect_nand_busy(void)//检测是否忙
- {
- U32a;
- a=0;
- while(!(rNFSTAT&(1<<2)))
- {
- a++;
- if(a==5000000)//等候超时
- {
- Uart_Printf(“/r/nError:DetectNandBusytimeout!!!/r/n”);
- rNFSTAT|=(1<<2);//清忙标志
- return-1;//过错回来-1
- }
- }
- rNFSTAT|=(1<<2);//清忙标志
- return1;
- }
- voidnand_reset(void)//NAND复位
- {
- rNFCMD=NAND_CMD_RES;
- detect_nand_busy();//检测忙
- }
- voidcontrol_start(void)//芯片舱位
- {
- select_nand();
- controller_enable();
- rNFSTAT|=(1<<2);//清忙标志
- nand_reset();
- }
- voidcontrol_end(void)//芯片封闭
- {
- dis_select_nand();
- controller_disable();
- }
- voidecc_main_init(void)//初始化ECC值
- {
- rNFCONT|=1<<4;//initEcc
- }
- voidecc_main_start(void)//开锁mainECC
- {
- rNFCONT&=~(1<<5);//unlock
- }
- voidecc_main_end(void)//确定mainECC
- {
- rNFCONT|=1<<5;//lock
- }
- voidecc_spare_start(void)//开锁spareECC
- {
- rNFCONT&=~(1<<6);//unlock
- }
- voidecc_spare_end(void)//确定spareECC
- {
- rNFCONT|=1<<6;//lock
- }
- void__irqnandINT(void)//NAND中止函数
- {
- //此处写处理代码
- #ifdefNAND_DEBUG
- Uart_Printf(“/r/nNandError…Ininterruptnow!!!”);//只需过错才会进入中止
- #endif
- rSRCPND|=0x1<
- rINTPND|=0x1<
- }
- voidnand_read_id(void)//读取芯片ID信息
- {
- control_start();//开操控
- rNFCMD=NAND_CMD_READ_ID;
- rNFADDR=0;
- //读芯片ID
- nand_id.IDm=(U8)rNFDATA8;
- nand_id.IDd=(U8)rNFDATA8;
- nand_id.ID3rd=(U8)rNFDATA8;
- nand_id.ID4th=(U8)rNFDATA8;
- nand_id.ID5th=(U8)rNFDATA8;
- //打印ID信息
- #ifdefNAND_DEBUG
- Uart_Printf(“/r/nReadNANDFlashID:”);
- Uart_Printf(“/r/nNANDMarkcode:0x%x”,nand_id.IDm);
- Uart_Printf(“/r/nNANDDevicecode:0x%x”,nand_id.IDd);
- Uart_Printf(“/r/nNAND3rdIDcode:0x%x”,nand_id.ID3rd);
- Uart_Printf(“/r/nNAND4thIDcode:0x%x”,nand_id.ID4th);
- Uart_Printf(“/r/nNAND5thIDcode:0x%x”,nand_id.ID5th);
- #endif
- control_end();//关操控
- }
- //擦出时只需给定块地点页的地址,就能擦除整个块
- intnand_block_erase(U32num)//num要删去的块号
- {
- num=num*64;//每块的第一页
- control_start();//开操控
- nand_reset();//复位
- rNFCMD=NAND_CMD_BLOCK_ERASE_1st;
- rNFADDR=num&0xff;
- rNFADDR=(num>>8)&0xff;
- rNFADDR=(num>>16)&0xff;
- rNFCMD=NAND_CMD_BLOCK_ERASE_2st;
- detect_nand_busy();
- rNFCMD=NAND_CMD_READ_STATUS;//读状况
- if(rNFDATA8&1)//最低位能够判别擦除和写是否成功
- {
- #ifdefNAND_DEBUG
- Uart_Printf(“/r/nError:nanderaseerror…block=0x%x”,num/64);
- #endif
- control_end();//关操控
- nand_mask_bad_block(num/64);//挂号为坏块
- return-1;//删去过错回来0
- }
- control_end();//关操控
- #ifdefNAND_DEBUG
- Uart_Printf(“/r/nNANDblock%derasecompleted.”,num/64);
- #endif
- return1;//擦除成功
- }
- intnand_page_write(U32addr,U8*buffer,U32size)//addr要写的起始页地址,buffer要写的缓存,size要写的字节巨细最大为4G
- {
- U32i,n,p,temp,ecc;
- U8*bu;
- bu=buffer;
- temp=0;
- n=size/2048+(((size%2048)==0)?0:1);//计算出要写的页数,小于一页的部分当作一页
- for(i=0;i
- {
- control_start();//开操控
- nand_reset();//复位
- #ifdefUSE_ECC
- ecc_main_init();
- ecc_main_start();//能够发生main区ECC
- #endif
- rNFCMD=NAND_CMD_WRITE_PAGE_1st;
- rNFADDR=0;//从每页的0地址开端
- rNFADDR=0;//从每页的0地址开端
- rNFADDR=(addr)&0xff;
- rNFADDR=(addr>>8)&0xff;
- rNFADDR=(addr>>16)&0xff;
- for(p=0;p<2048;p++)//写入一页
- {
- temp=temp+1;
- if(temp>size)
- {
- rNFDATA8=0xff;//剩余的填写0xff
- }
- else
- {
- rNFDATA8=*(bu+p);
- }
- }
- //delay_lhg(100,100);//
- #ifdefUSE_ECC
- ecc_main_end();//确定main区ecc
- ecc=rNFMECC0;
- ecc_spare_start();//解锁spare区ECC
- //mainECC值写入备用区的头0~4个地址内
- rNFDATA8=ecc&0xff;
- rNFDATA8=(ecc>>8)&0xff;
- rNFDATA8=(ecc>>16)&0xff;
- rNFDATA8=(ecc>>24)&0xff;
- ecc_spare_end();//确定spare区ECC
- //delay_lhg(100,100);//
- ecc=rNFSECC;//spareECC值写入备用区的5~6两个地址内
- rNFDATA8=ecc&0xff;
- rNFDATA8=(ecc>>8)&0xff;
- #endif
- bu=bu+2048;//页增量
- addr++;
- rNFCMD=NAND_CMD_WRITE_PAGE_2st;
- detect_nand_busy();//检测忙
- rNFCMD=NAND_CMD_READ_STATUS;//读状况
- if(rNFDATA8&1)
- {
- #ifdefNAND_DEBUG
- Uart_Printf(“/r/nnandwritepageerror:pageaddr=0x%d”,addr-1);//写入失利,今后改善
- #endif
- control_end();//关操控
- nand_mask_bad_block((addr-1)/64);//挂号为坏块
- return-1;//写入过错回来-1
- }
- control_end();//关操控
- }
- return1;//成功回来1
- }
- intnand_page_read(U32addr,U8*buffer,U32size)//addr开端页地址,从每页00地址开端读,size为需求读的字节数
- {
- U32i,n,p,temp,ecc;
- U8*bu,no;
- bu=buffer;
- temp=0;
- n=size/2048+(((size%2048)==0)?0:1);//计算出要读的页数,小于一页的部分当作一页
- for(i=0;i
- {
- control_start();//开操控
- nand_reset();//复位
- #ifdefUSE_ECC
- rNFESTAT0=0;//复位过错标志位
- ecc_main_init();
- ecc_main_start();//能够发生main区ECC
- #endif
- rNFCMD=NAND_CMD_READ_1st;
- rNFADDR=0;
- rNFADDR=0;
- rNFADDR=addr&0xff;
- rNFADDR=(addr>>8)&0xff;
- rNFADDR=(addr>>16)&0xff;
- rNFCMD=NAND_CMD_READ_2st;
- detect_nand_busy();
- for(p=0;p<2048;p++)
- {
- temp=temp+1;
- if(temp>size)
- {
- no=rNFDATA8;//剩余的读出来丢掉
- }
- else
- {
- *(bu+p)=rNFDATA8;
- }
- }
- #ifdefUSE_ECC
- rNFESTAT0=0;
- ecc_main_end();//确定main区ECC
- //delay_lhg(100,100);//
- ecc_spare_start();//解锁spare区ecc
- ecc=rNFDATA8;//从flash读出main区ECC,四个字节
- no=rNFDATA8;
- ecc|=((U32)no)<<8;
- no=rNFDATA8;
- ecc|=((U32)no)<<16;
- no=rNFDATA8;
- ecc|=((U32)no)<<24;
- rNFMECCD0=((ecc&0xff00)<<8)|(ecc&0xff);//硬件检验mainECC
- rNFMECCD1=((ecc&0xff000000)>>8)|((ecc&0xff0000)>>16);
- ecc_spare_end();//确定spare区ecc
- //delay_lhg(100,100);//
- ecc=rNFDATA8;//从flash读出spare区ECC的值
- no=rNFDATA8;
- ecc|=((U32)no)<<8;
- rNFSECCD=((ecc&0xff00)<<8)|(ecc&0xff);//硬件检验spareECC
- //delay_lhg(100,100);//延时一会
- ecc=rNFESTAT0&0xffffff;//ecc仅仅暂时用一下过错状况,并非ecc内容
- if(ecc!=0)//有过错
- {
- //今后再优化
- #ifdefNAND_DEBUG
- Uart_Printf(“/r/nNandecccheckerror…pageaddr=0x%x,NFESTAT0=0x%x”,addr,ecc);
- #endif
- nand_mask_bad_block((addr+i)/64);//挂号为坏块
- return-1;
- }
- #endif
- bu=bu+2048;
- addr++;
- control_end();//关操控
- }
- return1;
- }
- intnand_random_read(U32paddr,U32offset,U8*data)//随机读数据paddr页地址,offset页内偏移地址
- {
- control_start();//开操控
- nand_reset();//复位
- rNFCMD=NAND_CMD_READ_1st;
- rNFADDR=0;
- rNFADDR=0;
- rNFADDR=paddr&0xff;
- rNFADDR=(paddr>>8)&0xff;
- rNFADDR=(paddr>>16)&0xff;
- rNFCMD=NAND_CMD_READ_2st;
- detect_nand_busy();
- rNFCMD=NAND_CMD_RANDOM_READ_1st;
- rNFADDR=offset&0xff;//写入页内偏移地址
- rNFADDR=(offset>>8)&0xff;
- rNFCMD=NAND_CMD_RANDOM_READ_2st;
- *data=rNFDATA8;
- control_end();
- return1;
- }
- intnand_random_write(U32paddr,U32offset,U8data)//随机写,paddr页地址,offset页内偏移地址
- {
- control_start();//开操控
- nand_reset();//复位
- rNFCMD=NAND_CMD_WRITE_PAGE_1st;
- rNFADDR=0;
- rNFADDR=0;
- rNFADDR=paddr&0xff;
- rNFADDR=(paddr>>8)&0xff;
- rNFADDR=(paddr>>16)&0xff;
- rNFCMD=NAND_CMD_RANDOM_WRITE;
- rNFADDR=offset&0xff;//写入页内偏移地址
- rNFADDR=(offset>>8)&0xff;
- rNFDATA8=data;
- rNFCMD=NAND_CMD_WRITE_PAGE_2st;
- detect_nand_busy();//检测忙
- rNFCMD=NAND_CMD_READ_STATUS;//读状况
- if(rNFDATA8&1)
- {
- #ifdefNAND_DEBUG
- Uart_Printf(“/r/nError:nandrandomwriteerror…paddr=0x%x,offset=0x%x”,paddr,offset);
- #endif
- return-1;//删去过错回来0
- }
- control_end();
- return1;//成功回来1
- }
- voidnand_test_bad_block(void)//测验坏块函数,并符号spare区最终一个地址,假如非0xff则为坏块
- {
- U8dest[64*2048];//一个块的main区容量
- U8src[64*2048];
- U32i,k;
- #ifdefNAND_DEBUG
- Uart_Printf(“/r/ntestandmaskbadblockisbegain./r/n”);
- #endif
- //main区检测
- for(i=0;i<64*2048;i++)
- {
- dest[i]=0xff;//初始化缓冲区
- src[i]=0;
- }
- //删去一切块
- for(i=0;i
- {
- nand_block_erase(i);
- }
- for(i=0;i
- {
- nand_page_write(i*64,src,64*2048);
- nand_page_read(i*64,dest,64*2048);//运用了ecc校验读出来即可挂号坏块信息
- }
- for(i=0;i<64*2048;i++)
- {
- dest[i]=0;//初始化缓冲区
- src[i]=0xff;
- }
- //删去一切块
- for(i=0;i
- {
- nand_block_erase(i);
- }
- for(i=0;i
- {
- nand_page_write(i*64,src,64*2048);
- nand_page_read(i*64,dest,64*2048);//运用了ecc校验读出来即可挂号坏块信息
- }
- //
- //spare区检测
- for(i=0;i<64;i++)
- {
- dest[i]=0xff;//初始化缓冲区
- src[i]=0;
- }
- //删去一切块
- for(i=0;i
- {
- nand_block_erase(i);
- }
- for(i=0;i
- {
- if(nand_bbi.area[i/64]==1)//假如是坏块则越过
- continue;
- for(k=0;k<64;k++)
- {
- nand_random_write(i,2048+k,src[k]);
- nand_random_read(i,2048+k,&dest[k]);
- if(dest[k]!=src[k])//不相等则挂号为坏块
- {
- nand_mask_bad_block(i/64);
- break;
- }
- }
- }
- for(i=0;i<64;i++)
- {
- dest[i]=0x0;//初始化缓冲区
- src[i]=0xff;
- }
- //删去一切块
- for(i=0;i
- {
- nand_block_erase(i);
- }
- for(i=0;i
- {
- if(nand_bbi.area[i/64]==1)//假如是坏块则越过
- continue;
- for(k=0;k<64;k++)
- {
- nand_random_write(i,2048+k,src[k]);
- nand_random_read(i,2048+k,&dest[k]);
- if(dest[k]!=src[k])//不相等则挂号为坏块
- {
- nand_mask_bad_block(i/64);
- break;
- }
- }
- }
- #ifdefNAND_DEBUG
- Uart_Printf(“/r/ntestandmaskbadblockisover./r/n”);
- #endif
- }
- voidnand_flash_init(void)//初始化
- {
- #ifdefNAND_DEBUG
- Uart_Printf(“/r/nNANDFLASHinit”);//
- #endif
- //中止进口地址
- pISR_NFCON=(U32)nandINT;
- //装备GPIO
- rGPGUP|=0x7<<13;//GPG13~15封闭上位
- rGPGCON&=~((U32)0x3f<<26);//GPG13~15为输入
- //初始化各寄存器
- S3C2440_NFCONF_init();
- S3C2440_NFCONT_init();
- S3C2440_NFSTAT_init();
- S3C2440_NFESTAT0_init();
- S3C2440_NFESTAT1_init();
- //关于中止
- rINTMSK&=~(0x1<
- rINTMOD&=~(0x1<
- rSRCPND|=0x1<
- rINTPND|=0x1<
- //init_nand_bbi();//初始化全局变量
- nand_read_id();//读ID
- //nand_test_bad_block();//测验并挂号坏块
- }
NANDFlashd的读写(根据s3c2440)
#ifndef__TEST_H__#define__TEST_H__#includedef.h#defineMAX_NAND_BLOCK2048//一共2048块#defineNAND_PAGE_SI
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/bandaoti/ic/264426.html