//发送命令和地址
int write_cmd_addr(int addr)
{
int column_addr,page_address;
column_addr = addr % PAGE_SIZE;
page_address = addr >> 9;
WRITE_CMD(0x80);
WRITE_ADDR(column_addr & 0xff);
WRITE_ADDR(page_address & 0xff);
WRITE_ADDR((page_address >> 8) & 0xff);
WRITE_ADDR((page_address >> 16) & 0xff);
return 0;
}
/*
*注意:不管你要写多少个字节,每次写占用的最少
*是一个页的大小。
*/
int write_nand_page(int addr,char str[])
{
int statu,new_addr = addr;
int i = 0,j = 0;
int nbyte = my_strlen(str),n;
uart0_printf("write %d pages.\n",nbyte/PAGE_SIZE);
if(addr % PAGE_SIZE != 0)
{
uart0_printf("The addr is invalid.\r\n");
return -1;
}
/*
*不够一页的按一页处理
*超过一页的按两页处理
*/
n = nbyte / PAGE_SIZE;
if(nbyte % PAGE_SIZE != 0) n ++;
if(n < 1) n = 1;
NFChipEn();
for(i = 0;i < n;i ++)
{
write_cmd_addr(new_addr);
for(j = 0;j < PAGE_SIZE && *str != '\0';j ++,str ++)
{
WRITE_DATA(*str);
}
WRITE_CMD(0x10);
while(!NFREADY());
statu = read_statu();
if((statu & 0x1) != 0)
{
uart0_printf("write error.\r\n");
return -1;
}
new_addr += PAGE_SIZE;
}
#ifdef _DEBUG_
uart0_printf("*str = %c n = %d nbyte = %d.\r\n",*str,n,nbyte);
#endif
uart0_printf("write %d bytes at addr : %#x.\r\n",nbyte,addr);
NFChipDs();
return ;
}