📄 mass_stg.c
字号:
write_cmd(0x90);
write_addr(0x00);
Data_in = read_dat();
Data_in = read_dat();
CE = 1;
if(Data_in==FID)
LED = 1;
}
/***************************************************************************************
name: write_cmd
function: write a byte to the nandflash regs
pars: cmd
returns: no
__
CE \____________________
___________
CLE ____/ \______
______ ___________
WE \____/
____
DAT ________/ \_________
\____/
****************************************************************************************/
void write_cmd(uchar cmd)
{
_nop_();
_nop_();
CLE = 1;
_nop_();
_nop_();
WE = 0;
DIO = cmd;
_nop_();
_nop_();
WE = 1;
_nop_();
_nop_();
CLE = 0;
_nop_();
_nop_();
}
/******************************************************************************************
name: write_addr
function: write address to the flash regs
pars: addr
returns: no
___
CE \______________________________________________
__________________________________
ALE _____/ \_________
_______ ____ _____ _____________
WE \_____/ \_____/ \_____/
_____ _____ _____
ADDR _________/ADL \____/ADM \_____/ADH \____________
\_____/ \_____/ \_____/
******************************************************************************************/
void write_addr(uchar addr)
{
_nop_();
_nop_();
ALE = 1;
_nop_();
WE = 0;
DIO = addr;
_nop_();
_nop_();
WE = 1;
_nop_();
ALE = 0;
_nop_();
_nop_();
}
void write_addr_2byte(uchar addrL, uchar addrH)
{
_nop_();
_nop_();
ALE = 1;
_nop_();
WE = 0;
DIO = addrL;
_nop_();
_nop_();
WE = 1;
_nop_();
//ALE = 0;
_nop_();
_nop_();
_nop_();
_nop_();
//ALE = 1;
_nop_();
WE = 0;
DIO = addrH;
_nop_();
_nop_();
WE = 1;
_nop_();
ALE = 0;
_nop_();
_nop_();
}
void write_addr_3byte(uchar addrL, uchar addrM, uchar addrH)
{
//write low address
_nop_();
_nop_();
ALE = 1;
_nop_();
WE = 0;
DIO = addrL;
_nop_();
_nop_();
WE = 1;
_nop_();
//ALE = 0;
_nop_();
_nop_();
//write middle address
_nop_();
_nop_();
//ALE = 1;
_nop_();
WE = 0;
DIO = addrM;
_nop_();
_nop_();
WE = 1;
_nop_();
//ALE = 0;
_nop_();
_nop_();
//write high address
_nop_();
_nop_();
//ALE = 1;
_nop_();
WE = 0;
DIO = addrH;
_nop_();
_nop_();
WE = 1;
_nop_();
ALE = 0;
_nop_();
_nop_();
}
/*****************************************************************************************
name: write_dat
function: write data to the flash regs
pars: dat
returns: no
_____ _______
WE \____/
_____
DAT _______/ \____
\_____/
*****************************************************************************************/
void write_dat(uchar dat)
{
WE = 0;
DIO = dat;
_nop_();
_nop_();
WE = 1;
_nop_();
_nop_();
}
/******************************************************************************************
name: read_dat
function: read data from nandflash
par: no
returns: dat
_____ ______ ________
RE \_____/ \______/ \
________ ____________
DAT ________/ D1 \__/ D2 \_
\________/ \____________/
*****************************************************************************************/
uchar read_dat(void)
{
uchar dat;
DIO = 0xFF;
_nop_();
RE = 0;
_nop_();
_nop_();
_nop_();
dat = DIO;
_nop_();
RE = 1;
_nop_();
_nop_();
return dat;
}
/*****************************************************************************************
name: read_status
function: read the flash's status
pars: no
returns: status
*****************************************************************************************/
uchar read_status(void)
{
uchar status;
CE = 0;
write_cmd(Status);
status = read_dat();
CE = 1;
status &= 0x01;
return status;
}
/*****************************************************************************************
name: block_erase
function: erase the select flash block
pars: addrH, addrL
returns: no
*******************************************************************************************/
uchar block_erase(uchar addrL, uchar addrH)
{
CE = 0;
write_cmd(Block_Erase);
write_addr_2byte(addrL, addrH);
write_cmd(Block_Exc);
//tWB-write command to Busy start time is more than 100nS,
//this time should be reserved
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
while(~BUSY);
return read_status();
}
/********************************************************************************************
name: page_program
function: program the select page
pars: Dptr(input data pointer), addrL, addrM, addrH
returns: status
********************************************************************************************/
uchar page_program(uchar * Dptr, uchar addrL, uchar addrM, uchar addrH, uint num)
{
uint i;
CE = 0;
write_cmd(Page_Pro);
write_addr_3byte(addrL, addrM, addrH);
for(i=0;i<num;i++)
write_dat(*(Dptr+i));
write_cmd(Page_Exc);
//tWB-write command to Busy start time is more than 100nS,
//this time should be reserved
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
while(~BUSY);
return read_status();
}
/********************************************************************************************
name: copy_back
function: copy back the current block data to the dest block
pars: SaddrL, SaddrM, SaddrH, DaddrL, DaddrM, DaddrH,
returns: status
*********************************************************************************************/
uchar copy_back(uchar SaddrL, uchar SaddrM, uchar SaddrH, uchar DaddrL, uchar DaddrM, uchar DaddrH)
{
CE = 0;
write_cmd(Copy_Back);
write_addr_3byte(SaddrL, SaddrM, SaddrH);
//note the memory Plane address must be the same A14
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
while(~BUSY);
write_cmd(Copy_Exc);
write_addr_3byte(DaddrL, DaddrM, DaddrH);
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
while(~BUSY);
return read_status();
}
/*******************************************************************************************
name: page_read
function: read a page data from flash, Page Read without the Spare Memory 512-517 bytes
pars: addrL, addrM, addrH,
returns: status
*******************************************************************************************/
void page_read(uchar addrL, uchar addrM, uchar addrH)
{
//uchar * ptr;
uint i;
CE = 0;
write_cmd(Read1);
write_addr_3byte(addrL, addrM, addrH);
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
while(~BUSY);
for(i=0;i<512;i++)
buf[i] = read_dat();
_nop_();
_nop_();
_nop_();
CE = 1;
//break the sequential page read timing
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
//return ptr;
}
/****************************************************************************************
name: check_invalid
function: check out the invalid blocks
pars: no
returns: no
*****************************************************************************************/
/*void check_invalid(void)
{
uchar addrL, addrM, addrH,t;
xdata uchar buf[32];
uint addr, i;
uint *Bptr;
GND = 0;
addrL = 0x0;
for(i=0;i<2048;i++)
{
addr = i<<5;
addrM = (uchar)(addr & 0x00FF);
addrH = (uchar)((addr & 0xFF00)>>8);
CE = 0;
write_cmd(Read2);
write_addr_3byte(addrL, addrM, addrH);
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
while(~BUSY);
for(t=0;t<16;t++)
*(buf+t) = read_dat();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
while(~BUSY);
for(t=0;t<16;t++)
*(buf+t+16) = read_dat();
CE = 1;
if(buf[5]!=0xFF | buf[21]!=0xFF)
{
*Bptr = i;
Bptr++;
LED = 0;
//continue;
}
}
GND = 1;
}*/
/********************************************************************************************
Test the NandFlash operation functions
Test page_read, page_program, block_erase, copy_back, read_status NandFlash related functions
*********************************************************************************************/
/*void test_flash(void)
{
xdata uchar buf1[512];
xdata uchar *ptr;
uint i;
uchar flag;
if(flag = block_erase(0x0, 0x0))
LED = 0;
if(flag = page_program(DBR, 0x0, 0x0, 0x0, 512))
LED = 0;
ptr = page_read(0x0, 0x0, 0x0);
for(i=0;i<512;i++)
buf1[i] = *(ptr+i);
LED = 1;
if(flag = copy_back(0x0, 0x0, 0x0, 0x0, 0xc0, 0xff))
LED = 0;
ptr = page_read(0x0, 0xc0, 0xff);
for(i=0;i<512;i++)
buf1[i] = *(ptr+i);
LED = 1;
}*/
/*****************************************************************************************
name: fill_DBR
function: fill the flash page0 to page5 with DBR and FAT
pars: no
returns: no
*****************************************************************************************/
void fill_DBR(void)
{
uint i;
uchar flag;
uchar j;
buf[0] = 0xF8;
buf[1] = 0xFF;
buf[2] = 0xFF;
buf[3] = 0xFF;
for(i=4;i<512;i++) //create the FAT
buf[i] = 0;
//fill the DBR and FAT
for(i=0;i<16;i++)
if(flag = block_erase((uchar)(i<<5 & 0x00FF), (uchar)((i<<5 & 0xFF00)>>8)))
LED = 0;
if(flag = page_program(DBR, 0x0, 0x0, 0x0, 512))
LED = 0;
if(flag = page_program(buf, 0x0, 0x1, 0x0, 512))
LED = 0;
if(flag = page_program(buf, 0x0, 0xFB, 0x0, 512))
LED = 0;
for(i=0;i<512;i++)
buf[i] = 0;
for(j=2;j<0xFB;j++)
if(flag = page_program(buf, 0x0, j, 0x0, 512))
LED = 0;
for(i=0xFB;i<0x1F5;i++)
if(flag = page_program(buf, 0x0, (uchar)(i&0x00FF), (uchar)((i&0xFF00)>>8), 512))
LED = 0;
for(i=0x1F5;i<0x200;i++)
if(flag = page_program(buf, 0x0, (uchar)(i&0x00FF), (uchar)((i&0xFF00)>>8), 512))
LED = 0;
}
/*****************************************************************************************
name: block_buffer
function: buffer the current block data to the buffer block, get ready for HOST write
pars: LBAadr(logical block address)
returns: no
******************************************************************************************/
void block_buffer(uint LBAadr)
{
uint temp1;
uchar SaddrL,SaddrH, DaddrL, DaddrH;
uchar flag, i;
temp1 = LBAadr & A14;
if(temp1) //set the buffer block address
temp1 = 0xFFE0;
else
temp1 = 0xFFC0;
DaddrL = (uchar)(temp1 & 0x00FF);
DaddrH = (uchar)((temp1 & 0xFF00)>>8); //get the buffer flash block address
SaddrL = (uchar)(LBAadr & 0x00FF); //program start page
SaddrL &= 0xE0; //copy back from the Page0
SaddrH = (uchar)((LBAadr & 0xFF00)>>8);
if(flag = block_erase(DaddrL, DaddrH)) //erase the buffer block
LED =0;
for(i=0;i<32;i++) //copy the current block to the buffer block
if(flag = copy_back(0, SaddrL+i, SaddrH, 0, DaddrL+i, DaddrH))
LED = 0;
if(flag = block_erase(SaddrL, SaddrH)) //erase the current block
LED = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -