NANDFlashd的读写(基于s3c2440)
时间:11-20
来源:互联网
点击:
- tf("/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);//计算出要读的页数,小于一页的部分当
- rINTPND|=0x1
NANDFlashd读写s3c244 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)