nandflash裸机驱动程序的分析nandflash在嵌入式设备中广泛的应用,学些nandflash的重要性不言而喻,这里分析一段实例代码,不管是编码规范还是程序的结构都是很有价值的。下边是K9F1208U0M的实例代码。首先看nand.h文件:#ifndef__NAND_Flash__#define__NAND_Flash__externvoidInitNandCfg(void);//初始化K9F1208UOMNANDflash配置externunsignedintReadChipId(void);//读取NANDFlash的ID号externunsignedshortReadStatus(void);//读取NANDFlash的状态externunsignedintEraseBlock(unsignedintaddr);//NANDFlash块擦除externvoidReadPage(unsignedintaddr,unsignedchar*buf);//K9F1208U0Mnandflash的页数据读externvoidWritePage(unsignedintaddr,unsignedchar*buf);//K9F1208U0Mnandflash的页数据写externvoidMarkBadBlk(unsignedintaddr);//屏蔽K9F1208U0Mnandflash的坏块externintCheckBadBlk(unsignedintaddr);//检查K9F1208U0Mnandflash的坏块externvoidInitNandFlash(void);//K9F1208U0Mnandflash的初始化#endif需要详细看的是nandflash.c文件://====================================================//常量定义区//====================================================#defineEnNandFlash()(rNFCONF|=0x8000)//bit15=1enableNANDflashcontroller#defineDsNandFlash()(rNFCONF&=~0x8000)//bit15=1disableNANDflashcontroller#defineInitEcc()(rNFCONF|=0x1000)//bit12=1initializeECC#defineNoEcc()(rNFCONF&=~0x1000)//bit12=0initializeECC#defineNFChipEn()(rNFCONF&=~0x800)//bit11=0NANDflashnFCE=L(active)#defineNFChipDs()(rNFCONF|=0x800)//bit11=1NANDflashnFCE=H(inactive)#defineWrNFCmd(cmd)(rNFCMD=(cmd))//writecommondtonandflash#defineWrNFAddr(addr)(rNFADDR=(addr))//writeaddresstonandflash#defineWrNFDat(dat)(rNFDATA=(dat))//writedatatonandflash#defineRdNFDat()(rNFDATA)//readdatafromnandflash#defineRdNFStat()(rNFSTAT)//readstatusfromnandflash#defineNFIsBusy()(!(rNFSTAT&1))//whethernandflashisbusy?#defineNFIsReady()(rNFSTAT&1)//whethernandflashisready?#defineREADCMD00//Read0modelcommand==Pageaddr0~127#defineREADCMD11//Read1modelcommand==Pageaddr128~511#defineREADCMD20x50//Read2modelcommand==Pageaddr512~527#defineERASECMD00x60//Blockerasecommand0#defineERASECMD10xd0//Blockerasecommand1#definePROGCMD00x80//pagewritecommand0#definePROGCMD10x10//pagewritecommand1#defineQUERYCMD0x70//querycommand#defineRdIDCMD0x90//readidcommand//====================================================//函数定义区//====================================================#includeNAND_Flash.h#include2410addr.hstaticunsignedshortNandAddr;//等待NANDFLASH不忙voidwait_idle(void){inti;while(!(rNFSTAT&0x1))//如果是忙则一直等待下去.for(i=0;i10;i++);}/*在第一次实用NANDFlash前,复位一下NANDFlash*/voidreset_nand(){inti=0;rNFCONF&=~0x800;//激活NANDFlashfor(;i10;i++);rNFCMD=0xff;//看数据手册第9页上的命令.wait_idle();}//====================================================//语法格式:voidInitNandCfg(void)//功能描述:初始化K9F1208U0Mnandflash配置//入口参数:无//出口参数:无//====================================================voidInitNandCfg(void){//enablenandflashcontrol,initilizeecc,chipdisable,//基本所有的falsh都可以公用的。rNFCONF=(115)|(112)|(111)|(78)|(74)|(7);//使用控制器,使用ECC,不激活NandFlash,持续时间都设定为HCLK*8.}//====================================================//语法格式:unsignedintReadChipId(void)//功能描述:读NandFlash的ID号//入口参数:无//出口参数:NandFlashID//====================================================unsignedintReadChipId(void){unsignedintid;NFChipEn();WrNFCmd(RdIDCMD);//写命令90HWrNFAddr(0);//写地址00Hwait_idle();id=RdNFDat()8;//读ECHid|=RdNFDat();//读DeviceCode.NFChipDs();returnid;}//====================================================//语法格式:unsignedshortReadStatus(void)//功能描述:读NandFlash的状态//入口参数:无//出口参数:NandFlash状态//====================================================unsignedshortReadStatus(void){unsignedshortstat;NFChipEn();WrNFCmd(QUERYCMD);//写命令0x70stat=RdNFDat();NFChipDs();returnstat;}//====================================================//语法格式:unsignedintEraseBlock(unsignedintaddr)//功能描述:NandFlash块擦除//入口参数:块地址//出口参数:擦除状态0为成功非0失败//====================================================unsignedintEraseBlock(unsignedintaddr){unsignedcharstat;addr&=~0x1f;//保留9-25位NFChipEn();//使能WrNFCmd(ERASECMD0);//写命令0x60WrNFAddr(addr);//擦除只针对块操作WrNFAddr(addr8);if(NandAddr)//判断Flash的型号WrNFAddr(addr16);WrNFCmd(ERASECMD1);//发送擦除命令0xD0Hwait_idle();//等待,直到操作完成为止。NFChipDs();//失能returnstat&1;}//====================================================//语法格式:voidReadPage(unsignedintaddr,unsignedchar*buf)//功能描述:读取页的内容//入口参数:1,页地址2,读出的页内数据//出口参数:无//====================================================/****K9F1208U0Mnandflash的页数据读****/voidReadPage(unsignedintaddr,unsignedchar*buf)//addr=pageaddress{//该程序是读取一页(512字节)内容.unsignedshorti;NFChipEn();WrNFCmd(READCMD0);WrNFAddr(0);//从0地址开始读取.WrNFAddr(addr);WrNFAddr(addr8);if(NandAddr)WrNFAddr(addr16);InitEcc();wait_idle();for(i=0;i512;i++)buf[i]=RdNFDat();NFChipDs();}//====================================================//语法格式:externunsignedintWritePage(unsignedintaddr,unsignedchar*buf)//功能描述:写页数据//入口参数:1,页地址2,写页内数据//出口参数:无//====================================================/****K9F1208U0Mnandflash的页数据写****/voidWritePage(unsignedintaddr,unsignedchar*buf){unsignedshorti;unsignedchartmp[3];NFChipEn();WrNFCmd(PROGCMD0);//写命令0x80WrNFAddr(0);//低八位为0WrNFAddr(addr);//页地址WrNFAddr(addr8);//if(NandAddr)WrNFAddr(addr16);InitEcc();for(i=0;i512;i++)WrNFDat(buf[i]