#define OK 1
#define FAULSE 0
bit flagFrame = 0;
bit flagTxd = 0;
unsigned char cntRxd = 0;
unsigned char a[32]={0};
unsigned char t1buf[250];
unsigned char sbuf[250];
UINT8 rbuf[25];
UINT8 T2buf[25];
UINT8 Tbuf[25];
UINT8 R1buf[32];
extern void SPI_Initial(void);
extern void W25QXX_Erase_Sector(UINT32 Dst_Addr);
extern void Flash_Program(UINT8* pBuffer,UINT32 WriteAddr,UINT16 NumByteToWrite);
extern void Flash_Read(UINT8* pBuffer,UINT32 ReadAddr,UINT16 NumByteToRead);
extern void SPI_Flash_Write_NoCheck(UINT8* pBuffer,UINT32 ReadAddr,UINT16 Len);
UINT8 SearchIDNum(UINT8 *buf,UINT8 *rbuf,UINT16 len);
void delay500ms(void)
{
unsigned char i,j,k;
for(i=15;i>0;i--) //??????????
for(j=202;j>0;j--) //??????????
for(k=81;k>0;k--); //???????з??
}
/////////////////??????0??63??????????//////////////////////////
void InitSector()
{
UINT32 sector;
for(sector=0;sector<64;sector++)
{
W25QXX_Erase_Sector(sector*4096);
}
}
//////////////////?????????????????0??????????15?////////////////
void PrintSector(UINT8 sector)
{
//UINT8 sector;
UINT8 page;
UINT8 ab[2];
for(page=0;page<16;page++)
{ ab[0]=sector;
ab[1]=page;
UartWrite(ab,2);
Flash_Read(sbuf,sector*4096+page*256,250);
UartWrite(sbuf,250);
if(page==15)
{
Flash_Read(sbuf,sector*4096+page*256,250);
UartWrite(sbuf,250);
Timer2_Delay(24000000,128,1000,1000);
}
}
}
////////////////////////////?洢????///////////////////////////////
//////////////////////?п?????棬??????0x00/////////////////
UINT8 storedata(UINT8 *buf,UINT8 *rbuf,UINT16 len)
{
UINT32 sector;
UINT8 i,page;
//bit flag=1;
//sector = buf[8]%64;
for(sector=0;sector<64;sector++)
{
for(page=0;page<16;page++)
{
for(i=0;i<10;)
{
Flash_Read(rbuf,sector*4096+page*256+25*i,len);
if((rbuf[5]!=0xff)&&(rbuf[6]!=0xff)&&(rbuf[7]!=0xff)&&(rbuf[8]!=0xff))
i++;
if((rbuf[5]==0xff)&&(rbuf[6]==0xff)&&(rbuf[7]==0xff)&&(rbuf[8]==0xff))
{Flash_Program(buf,sector*4096+page*256+25*i,len);
return OK;}
}
}
}
UART_Send_Data(UART0,0x00);
return FAULSE;
}
////////////???????ж?????///////////////////////////////
////////////?????????1???????????0??//////////////////
UINT8 SearchIDNum(UINT8 *buf,UINT8 *rbuf,UINT16 len)
{
UINT32 sector;
UINT8 i,page;
//sector = buf[9]%64;
for(sector=0;sector<64;sector++)
{
for(page=0;page<16;page++)
{
for(i=0;i<10;i++)
{
Flash_Read(rbuf,sector*4096+page*256+25*i,len);
if((rbuf[5]!=buf[5])&&(rbuf[6]!=buf[6])&&(rbuf[7]!=buf[7])&&(rbuf[8]!=buf[8]))
{continue;}
if((rbuf[5]==buf[5])&&(rbuf[6]==buf[6])&&(rbuf[7]==buf[7])&&(rbuf[8]==buf[8]))
{UART_Send_Data(UART0,0x01);return OK; }
}
}
}
UART_Send_Data(UART0,0x00);
return FAULSE;
}
//////////////////???SPI??flash???????к????????????????????λд0x00//////////
UINT8 DeleteNum(UINT8 *buf,UINT8 *rbuf,UINT16 len)
{
UINT32 sector;
UINT8 i,page,j;
//UINT8 i,j,m,k,page1,page2;
//sector = buf[9]%64;
for(sector=0;sector<64;sector++)
{
for(page=0;page<16;page++)
{
for(i=0;i<10;i++)
{
Flash_Read(rbuf,sector*4096+page*256+25*i,len);
if((rbuf[5]!=buf[5])&&(rbuf[6]!=buf[6])&&(rbuf[7]!=buf[7])&&(rbuf[8]!=buf[8]))
{continue;}
if((rbuf[5]==buf[5])&&(rbuf[6]==buf[6])&&(rbuf[7]==buf[7])&&(rbuf[8]==buf[8]))
{
for(j=0;j<25;j++)
buf[j]=0x00;
Flash_Program(buf,sector*4096+page*256+25*i,len);
return OK;
}
}
}
}
UART_Send_Data(UART0,0x00);
return FAULSE;
}
///////////////////////////// ???????/////////////////////////////////////////
////////////////////////////???0x00??????????λ?????????64????????/////
///////////////////////////?????0x00????????????????64????????????0x00???//
////////////////////////// ????????????????????????????????????64??????????
///////////////////////////???????????////////////////////////////////////////
UINT8 ModifyNum(UINT8 *buf,UINT8 *rbuf,UINT16 len)
{
bit flag=1;
UINT32 sector;
UINT8 i,j,m,k,page1,page2;
//UINT8 ab[2];
//sector = buf[9]%64;
for(sector=0;sector<64;sector++)
{
for(page1=0;page1<16;page1++)
{
for(i=0;i<10;i++)
{
Flash_Read(rbuf,sector*4096+page1*256+25*i,len);
if((rbuf[5]!=0x00)&&(rbuf[6]!=0x00)&&(rbuf[7]!=0x00)&&(rbuf[8]!=0x00))
{continue;}
if((rbuf[5]==0x00)&&(rbuf[6]==0x00)&&(rbuf[7]==0x00)&&(rbuf[8]==0x00))
{
//W25QXX_Erase_Sector(64*4096);
k=i;
for(page2=0;page2<16;page2++)
{
Flash_Read(sbuf,sector*4096+page2*256,250);
Flash_Program(sbuf,64*4096+page2*256,250);
if(page2==page1)
{
Flash_Read(sbuf,sector*4096+page2*256,250);
for(m=0;m<10;m++)
{
for(j=0;j<25;j++)
{
//if(m<k)
// t1buf[m*25+j] = sbuf[m*25+j];
if((m>=k)&&(m<9))
{
sbuf[m*25+j] = sbuf[(m+1)*25+j];
}
if(m==9)
{
sbuf[m*25+j] = buf[j];
}
}
}
UartWrite(sbuf,250);
//delay500ms();
Flash_Program(sbuf,64*4096+page2*256,250);
// PrintSector(64);
delay500ms();
UartWrite(sbuf,250);
}
}
/*W25QXX_Erase_Sector(sector*4096);
delay500ms();
for(page2=0;page2<16;page2++)
{
Flash_Read(sbuf,64*4096+page2*256,250);
Flash_Program(sbuf,sector*4096+page2*256,250);
} */
return OK;
}
}
}
}
UART_Send_Data(UART0,0x00);
return FAULSE;
}
void main()
{
UINT8 i=0,j=0,k=0,m=0;
MFP_P30_UART0_RXD;
P30_QUASI_MODE;
MFP_P31_UART0_TXD;
P31_QUASI_MODE;
MFP_P00_SPI0_MOSI;
P00_QUASI_MODE;
MFP_P01_SPI0_MISO;
P01_QUASI_MODE;
MFP_P02_SPI0_CLK;
P02_QUASI_MODE;
MFP_P03_SPI0_SS;
P03_QUASI_MODE;
SPI_Initial();
UART_Open(24000000,UART0_Timer1,115200);
UART_Interrupt_Enable(UART0,Enable);
//Timer_Interrupt_Enable(TIMER2);
ENABLE_GLOBAL_INTERRUPT;
Timer0_Delay(24000000,1,1000);
//flash_Chip_Erase();
// delay500ms();
InitSector();
W25QXX_Erase_Sector(64*4096);
//UINT8 storedata(Tbuf,rbuf,25);//
//Flash_Read(Rbuf,0x000000,32); //??N????
while(i<10)
{
if(flagFrame)
{
flagFrame = 0;
UartRead(Tbuf,R1buf,32);
storedata(Tbuf,rbuf,25);
//delay500ms();
//UartRead(T2buf,R1buf,32);
//SearchIDNum(T2buf,rbuf,25);
//UartWrite(Tbuf,25);
i++;
}
// UartWrite(Tbuf,25);
}
//PrintSector(0);
//while(1);
while(j<1)
{
if(flagFrame)
{
flagFrame = 0;
UartRead(T2buf,R1buf,32);
SearchIDNum(T2buf,rbuf,25);
j++;
}
}
j=0;
//PrintSector();
while(k<1)
{
if(flagFrame)
{
flagFrame = 0;
UartRead(T2buf,R1buf,32);
DeleteNum(T2buf,rbuf,25);
k++;
}
}
PrintSector(0);
while(m<1)
{
if(flagFrame)
{
flagFrame = 0;
UartRead(T2buf,R1buf,32);
ModifyNum(T2buf,rbuf,25);
m++;
}
}
PrintSector(64); |
|