NandFlash的规范程序代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
nandflash裸机驱动程序的分析
nandflash在嵌入式设备中广泛的应用,学些nandflash的重要性不言而喻,这里分析一段实例代码,不管是编码规范还是程序的结构都是很有价值的。下边是K9F1208U0M的实例代码。
首先看nand.h文件:
#ifndef __NAND_Flash__
#define __NAND_Flash__
extern void InitNandCfg(void); //初始化K9F1208UOM NAND flash 配置extern unsigned int ReadChipId(void); //读取NAND Flash的ID号extern unsigned short ReadStatus(void); //读取NAND Flash的状态extern unsigned int EraseBlock(unsigned int addr); //NAND Flash块擦除extern void ReadPage(unsigned int addr, unsigned char *buf); //K9F1208U0M nand flash 的页数据读
extern void WritePage(unsigned int addr, unsigned char *buf); //K9F1208U0M nand flash 的页数据写
extern void MarkBadBlk(unsigned int addr); //屏蔽K9F1208U0M nand flash 的坏块
extern int CheckBadBlk(unsigned int addr); //检查K9F1208U0M nand flash 的坏块
extern void InitNandFlash(void); //K9F1208U0M nand flash 的初始化
#endif
需要详细看的是nandflash.c文件:
//==================================================== // 常量定义区
//==================================================== #define EnNandFlash() (rNFCONF |= 0x8000) //bit15=1 enable NAND flash controller
#define DsNandFlash() (rNFCONF &= ~0x8000) //bit15=1 disable NAND flash controller
#define InitEcc() (rNFCONF |= 0x1000) //bit12=1 initialize ECC
#define NoEcc() (rNFCONF &= ~0x1000) //bit12=0 initialize ECC
#define NFChipEn() (rNFCONF &= ~0x800) //bit11=0 NAND flash nFCE = L (active)
#define NFChipDs() (rNFCONF |= 0x800) //bit11=1 NAND flash nFCE = H (inactive)
#define WrNFCmd(cmd) (rNFCMD = (cmd)) //write commond to nand flash
#define WrNFAddr(addr) (rNFADDR = (addr)) //write address to nand flash
#define WrNFDat(dat) (rNFDATA = (dat)) //write data to nand flash
#define RdNFDat() (rNFDATA) //read data from nand flash
#define RdNFStat() (rNFSTAT) //read status from nand flash
#define NFIsBusy() (!(rNFSTAT&1)) //whether nand flash is busy?
#define NFIsReady() (rNFSTAT&1) //whether nand flash is ready?
#define READCMD0 0 //Read0 model command == Page addr 0~127
#define READCMD1 1 //Read1 model command == Page addr 128~511
#define READCMD2 0x50 //Read2 model command == Page addr 512~527
#define ERASECMD0 0x60 //Block erase command 0
#define ERASECMD1 0xd0 //Block erase command 1
#define PROGCMD0 0x80 //page write command 0
#define PROGCMD1 0x10 //page write command 1
#define QUERYCMD 0x70 //query command
#define RdIDCMD 0x90 //read id command
//==================================================== // 函数定义区
//==================================================== #include "NAND_Flash.h"
#include "2410addr.h"
static unsigned short NandAddr;
//等待NAND FLASH不忙
void wait_idle(void)
{
int i;
while(!(rNFSTAT & 0x1)) //如果是忙则一直等待下去.
for(i=0; i<10; i++);
}
/* 在第一次实用NAND Flash前,复位一下NAND Flash */
void reset_nand()
{
int i=0;
rNFCONF &= ~0x800; //激活NANDFlash
for(; i<10; i++);
rNFCMD = 0xff; //看数据手册第9页上的命令.
wait_idle();
}
//==================================================== // 语法格式:void InitNandCfg(void)
// 功能描述: 初始化K9F1208U0M nand flash 配置
// 入口参数: 无
// 出口参数: 无
//====================================================