本实验使用KF92G08U0B芯片。每页2k空间,总共256M。
主要问题在写地址上:实验中将main函数在内的2k复制到0x30000000之后,nand每页2k空间.
一页分成主要的字段(2 k)和备用字段(64字节)两部分
而我们只使用2 k,所以只用它的低11(A0~A11,12)位。因为页面地址总是2k字节对齐的,所以它的低11总是零。详细参照:
http://blog.chinaunix.net/uid-26310563-id-3142134.html和
。
代码:
nand.c:
#define NAND_SECTOR_SIZE 2048
#define NAND_BLOCK_MASK (NAND_SECTOR_SIZE -1)
#define BUSY 1
typedef unsigned int REG32;
typedef struct {
REG32 NFCONF;
REG32 NFCONT;
REG32 NFCMD;
REG32 NFADDR;
REG32 NFDATA;
REG32 NFMECCD0;//not use
REG32 NFMECCD1;//not use
REG32 NFSECCD; //not use
REG32 NFSTAT;
REG32 NFESTAT0;//not use
REG32 NFESTAT1;//not use
REG32 NFMECC0; //not use
REG32 NFMECC1; //not use
REG32 NFSECC; //not use
REG32 NFSBLK; //not use
REG32 NFEBLK; //not use
} NAND;
static NAND * nand = (NAND *)0x4e000000;
void nand_init(void);
void nand_read(unsigned char *buf, unsigned long start_addr, int size);
static void nand_reset(void);
static void wait_idle(void);
static void nand_select_chip(void);
static void nand_deselect_chip(void);
static void write_cmd(int cmd);
static void write_addr(unsigned int addr);
static unsigned char read_data(void);
static void nand_reset(void)
{
nand_select_chip();
write_cmd(0xff);
wait_idle();
nand_deselect_chip();
}
static void wait_idle(void)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&nand->NFSTAT;
while(!(*p & BUSY))
for(i=0; i<10; i++);
}
static void nand_select_chip(void)
{
int i;
nand->NFCONT &= ~(1<<1);
for(i=0; i<10; i++);
}
static void nand_deselect_chip(void)
{
nand->NFCONT |= (1<<1);
}
static void write_cmd(int cmd)
{
volatile unsigned char *p = (volatile unsigned char *)&nand->NFCMD;
*p = cmd;
}
static void write_addr(unsigned int addr)
{
int i;
int col,page;
addr = addr >> 11;
col = addr & NAND_BLOCK_MASK;
page = addr / NAND_SECTOR_SIZE;
volatile unsigned char *p = (volatile unsigned char *)&nand->NFADDR;
//two ways reach the result
// the first method
// *p = col & 0xff;
// for(i=0; i<10; i++);
// *p = (col >> 8) & 0x1f;
// for(i=0; i<10; i++);
// *p = page & 0xff;
// for(i=0; i<10; i++);
// *p = (page >> 8) & 0xff;
// for(i=0; i<10; i++);
// *p = (page >> 16) & 0x03;
// for(i=0; i<10; i++);
//the second method
//firstly:one Page is divided into the Main Field (2 k)
//and Spare Field (64 byte) two parts
//and we only use the 2k,so it's low 11(not 12)fulfil.
//Because the page address is always 2048 bytes aligned,
// so its low 11 is always zero
*p = 0;
for(i=0; i<10; i++);
*p = 0;
for(i=0; i<10; i++);
*p = addr & 0xff;
for(i=0; i<10; i++);
*p = (addr >> 8) & 0xff;
for(i=0; i<10; i++);
*p = (addr >> 16) & 0xff;
for(i=0; i<10; i++);
}
static unsigned char read_data(void)
{
volatile unsigned char *p = (volatile unsigned char *)&nand->NFDATA;
return *p;
}
void nand_init(void)
{
#define TACLS 0
#define TWRPH0 3
#define TWRPH1 0
nand->NFCONF = (TACLS<<12)|(TWRPH0<<8)|(TWRPH1<<4);
nand->NFCONT = (1<<4)|(1<<1)|(1<<0);
nand_reset();
}
void nand_read(unsigned char *buf, unsigned long start_addr, int size)
{
int i, j;
if ((start_addr & NAND_BLOCK_MASK) || (size & NAND_BLOCK_MASK)) {
return ;
}
nand_select_chip();
for(i=start_addr; i < (start_addr + size);) {
write_cmd(0);
write_addr(i);
write_cmd(0x30);
wait_idle();
for(j=0; j < NAND_SECTOR_SIZE; j++, i++) {
*buf = read_data();
buf++;
}
}
nand_deselect_chip();
return ;
}
7_nand.zip
阅读(1001) | 评论(0) | 转发(2) |