//1.页开始的地址,cmd 0x00,size 512
//2.半页开始的地址,cmd 0x01,size 256
int read_page(int addr,unsigned char *dest_addr)
{
int column_addr,page_address;
int size,i;
column_addr = addr % PAGE_SIZE;
page_address = addr >> 9;
NFChipEn();
ResetNand();
#ifdef _DEBUG_
uart0_printf("column_addr = %d.\r\n",column_addr);
#endif
if(column_addr >= HALF_PAGE)
{
uart0_printf("Half page.\r\n");
WRITE_CMD(0x01);
size = 256;
}else{
WRITE_CMD(0x00);
size = 512;
}
WRITE_ADDR(column_addr & 0xff);//1st Cycle
WRITE_ADDR(page_address & 0xff);//2st Cycle
WRITE_ADDR((page_address >> 8)&0xff);//3rd Cycle
WRITE_ADDR((page_address >> 16)&0xff);//4th Cycle
//等待BUSY
while(!NFREADY());
uart0_printf("nand read ok.\r\n");
for(i = 0;i < size;i ++)
{
dest_addr[i] = READ_DATA();
}
NFChipDs();
return size;
}
/*把nand的内容读到内存中去,
*dest_addr:内存地址,src_addr:nand flash地址
*size : 大小
*/
void nand_read(unsigned char *dest_addr,int src_addr,int size)
{
int i,nbytes;
unsigned char *new_addr = dest_addr;
int n = size / PAGE_SIZE;
//不够一页,按一页计算
if(n < 1) n= 1;
//判断是否是半页对齐的地址
if(src_addr % HALF_PAGE != 0)
{
uart0_printf("Invaild addr.\r\n");
return -1;
}
//n值表示需要读多少个页
for(i = 0;i < n;i ++)
{
nbytes = read_page(src_addr,new_addr);
//处理半页地址的情况
if(src_addr % HALF_PAGE == 0 && src_addr % PAGE_SIZE != 0)
{
src_addr = src_addr - HALF_PAGE;
}
src_addr = src_addr + PAGE_SIZE;
new_addr = new_addr + nbytes;
}
return;
}