📄 28081.c
字号:
#include "spce061v004.h"
unsigned char databuffer[528];
unsigned int badblock_table[21];
unsigned char badblock_index=1;
unsigned int blocknum=0;
unsigned int ij=0;
unsigned char badblock_flag=0;
unsigned int sendnum=0;
unsigned char sendchar[2];
void badblock_check(void);
void Write_Data(unsigned int count)
{
unsigned int i;
unsigned char *p;
p=databuffer;
for(i=0;i<count;i++)
{
SP_WriteData(*p++);
}
}
void Write_Data_Eoc(unsigned int count)
{
unsigned int i;
unsigned char *p;
p=databuffer;
for(i=0;i<count;i++)
{
SP_WriteData_Eoc(*p++);
}
}
void serial_read(unsigned int count)
{
unsigned int i;
unsigned char *p;
SP_Read_Init();
p=databuffer;
for(i=0;i<count;i++)
{
*p++=SP_serial_Read();
}
SP_Read_Over();
}
void serial_read1(unsigned int count)
{
unsigned int i;
unsigned char *p;
SP_Read_Init();
for(i=0;i<count;i++)
{
*p++=SP_serial_Read1_Eoc();
}
SP_Read_Over();
}
void status_check()
{
unsigned int status,temp;
SP_WriteCmd(0x0070);
SP_Read_Init();
status=SP_serial_Read();
SP_Read_Over();
while((status&0x0040)==0);
// if(status&0x0001) databuffer[527]=0x33;
}
void block_erase(unsigned int addr)
{
SP_WriteCmd(0x0060);
addr=addr<<5;
SP_WriteAddr(addr);
addr=addr>>8;
SP_WriteAddr(addr);
SP_WriteCmd(0x00d0);
F_Delay_blockErase_Time();
// status_check();
}
void page_program(unsigned int addr)
{
SP_WriteCmd(0x0080);
SP_WriteAddr(0x00);
SP_WriteAddr(addr);
addr=addr>>8;
SP_WriteAddr(addr);
Write_Data(528);
SP_WriteCmd(0x0010);
F_Delay_Program_Time();
// status_check();
}
void Sequential_read(unsigned int addr)
{
SP_WriteCmd(0x0000);
SP_WriteAddr(0x00);
SP_WriteAddr(addr);
addr=addr>>8;
SP_WriteAddr(addr);
F_Delay_read_Time();
serial_read(528);
}
unsigned int logicaddr_to_phyaddr(unsigned int logic_addr)
{
unsigned int temp,i,phy_addr;
temp=badblock_table[0];
phy_addr=logic_addr;
for(i=temp;i>=1;i--)
{
if(logic_addr>=badblock_table[i]*32)
{
phy_addr=logic_addr+i*32;
break;
}
}
return(phy_addr);
}
unsigned int read_word()
{
unsigned int first,two,valid;
first=SP_serial_Read();
two =SP_serial_Read();
valid=((two<<8)|0x00ff)&(first|0xff00);
return(valid);
}
void Sequential_read_init(unsigned int addr)
{
SP_WriteCmd(0x0000);
SP_WriteAddr(0x00);
SP_WriteAddr(addr);
addr=addr>>8;
SP_WriteAddr(addr);
F_Delay_read_Time();
SP_Read_Init();
}
/*
void page_write(unsigned int page_addr)
{
unsigned int block,page,temp,swap_addr;
unsigned char temp_data[528];
for(temp=0;temp<528;temp++) //待写入的页数据暂存
{temp_data[temp]=databuffer[temp];}
block= page_addr/32; //求得欲写入的BLOCK数
page = page_addr%32; //求得欲写入的所站BLOCK中的页数
temp=badblock_table[0]; //得到坏块数
temp=badblock_table[temp]; //得到最后坏块的地址
if(temp!=1023) swap_addr=1023; //若1023块GOOD,则SWAP块=1023 BLOCK
else
{temp=badblock_table[temp-1]; //否则用1022BLOCK====SWAP BLOCK
if(temp!=1022) swap_addr=1022;}
block_erase(swap_addr); //察除SWAP BLOCK
for(temp=0;temp<32;temp++)
{if(temp!=page) //读出待写入块的其他页的数据存入SWAP BLOCK
{ Sequential_read(block*32+temp); //
page_program(swap_addr*32+temp);}
}
block_erase(block); //察除欲写入块
for(temp=0;temp<528;temp++) //取得暂存的欲写入的528字节数据
{databuffer[temp]=temp_data[temp];}
page_program(page_addr); //写入欲写入的页
Sequential_read(page_addr);
for(temp=0;temp<32;temp++) //在从SWAP BLOCK 中回写其他不需要改变的数据
{if(temp!=page)
{ Sequential_read(swap_addr*32+temp);
page_program(block*32+temp);}
}
}
*/
void badblock_check(void)
{
unsigned char *p;
badblock_index=1;
*P_SystemClock=0x00;
for(ij=0;ij<21;ij++)
{
badblock_table[ij]=0;
}
for(blocknum=0;blocknum<1024;blocknum++)
{
if((blocknum&0x00f)==0x00f)
{
sendnum=blocknum&0x03ff;
sendchar[0]=(unsigned char)(sendnum&0x00ff);
sendchar[1]=(unsigned char)sendnum>>8;
F_D12_WriteEndpoint(5,2,sendchar); //发送格式化回应信号
}
*P_Watchdog_Clear=0x01;
badblock_flag=0;
p=databuffer;
for(ij=0;ij<528;ij++)
{
*p++=0x55;
}
block_erase(blocknum);
page_program(blocknum*32+0);
Sequential_read(blocknum*32+0);
p=databuffer;
for(ij=0;ij<528;ij++)
{
if((unsigned char)(*p++)!=0x55)
{
badblock_flag=1;
break;
};
}
/*
if(badblock_flag==0)
{
d=databuffer;
for(ij=0;ij<528;ij++)
{
*d++=0xaa;
}
*P_Watchdog_Clear=0x01;
block_erase(blocknum);
page_program(blocknum*32+0);
Sequential_read(blocknum*32+0);
s=databuffer;
for(ij=0;ij<528;ij++)
{
if((unsigned char)(*s++)!=0xaa)
{
badblock_flag=1;
break;
}
}
}
*/
if(badblock_flag==1)
{
badblock_table[badblock_index++]=blocknum;
badblock_flag=0;
if(badblock_index>20)
{
return;
}
}
} // end for(blocknum)
badblock_table[0]=badblock_index-1; // 更改总的索引块数
F_D12_WriteEndpoint(5,1,00); //发送格式化回应信号
}
readID()
{
unsigned char i,j;
SP_WriteCmd(0x90);
SP_WriteAddr(0x00);
SP_Read_Init();
i=SP_serial_Read();
j=SP_serial_Read();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -