📄 flash.c
字号:
}
At45DB081_Deselect();
if(At45DB081_ReadStatus()==0xff)return False;//fail to write
if(!At45DB081_AutoPageReWrite())return False;//fail to auto page rewrite
return True;//write successfully
}
/*************************************************************************************
Warning:When write one word or long to flash,the lowwest byte ahead
**************************************************************************************/
uchar FlashWriteData(ulong FlashAdd,ulong ByteNum,uchar * Data)//write flash
//return True:write successfully, return False:fail to write
{
ulong idata temp1,temp3;
uint idata temp2,temp4;
uint idata i;
// uchar idata j;
if(FlashAdd+ByteNum > FlashMaxByte)return False;
temp1=FlashAdd;
temp2=0;
for(;;)
{
i=0;
temp3=temp1;
temp4=temp2;
do
{
i++;
temp2++;
temp1++;
if(temp2==ByteNum)break;
}
while(temp1%FlashPageBytes);//not exceed the current page range
if(!At45DB081_WritBytesInPage(temp3,i,(Data+temp4))) return False;//fail to write
if(temp2==ByteNum)break;
}
/*
j=0;
do//make check sum
{
if((FlashAdd>FlashRangeAdd[j])&&(FlashAdd<FlashRangeAdd[j+1]))
{
if(FlashMakeCheckSum(j))return True;//make check sum to Flash
else return False;
}
else j++;
}while(j<FlashDataRangeNum);
return False;//fail to write
*/
return True;//successfully to write
}
void FlashReadData(ulong FlashAdd,ulong ByteNum,uchar * Data)//read flash
{
ulong idata temp1,temp3;
uint idata temp2,temp4;
uint idata i;
if(FlashAdd+ByteNum > FlashMaxByte)return;
temp1=FlashAdd;
temp2=0;
for(;;)
{
i=0;
temp3=temp1;
temp4=temp2;
do
{
i++;
temp2++;
temp1++;
if(temp2==ByteNum)break;
}
while(temp1%FlashPageBytes);//not exceed the current page range
At45DB081_ReadBytesInPage(temp3,i,(Data+temp4));//read bytes from one page
if(temp2==ByteNum)break;
}
}
uchar FlashGetCheckSum(uchar RangeNum)//get check sum form Flash
//return True:true return False:error
{
uint i;
ulong temp;
uchar CheckSumTemp,m;
uchar FlashTempBuf;
if(RangeNum>FlashDataRangeNum)return False;
CheckSumTemp=0;
temp=0;
At45DB081_Select();
At45DB081_WriteCmd(Opcode_ContinuousRead,FlashRangeAdd[RangeNum]);
At45DB081_SendDoNotCareBits(32);//wait the AT45DB081 read finish
ClrSCLK();
SetSCLK();
for(;;)
{
for(i=0;i<8;i++)//send command,MSB
{
ClrSCLK();
m<<=1;
if(SI) m |= 0x01;
SetSCLK();
}
CheckSumTemp+=m;
if(++temp==FlashRangeNum[RangeNum])break;
}
At45DB081_Deselect();
At45DB081_ReadBytesInPage(FlashAdd_CheckSum+RangeNum*2,1,&FlashTempBuf);
if(FlashTempBuf!=CheckSumTemp)return False;
return True;
}
uchar FlashMakeCheckSum(uchar RangeNum)//make check sum to Flash
//return True:wrire true return False:error
{
uint i;
ulong temp;
uchar CheckSumTemp,m;
if(RangeNum>FlashDataRangeNum)return False;
//printf("MakeSum,RangeNum=%x\n",(uint)RangeNum);
CheckSumTemp=0;
temp=0;
At45DB081_Select();
At45DB081_WriteCmd(Opcode_ContinuousRead,FlashRangeAdd[RangeNum]);
At45DB081_SendDoNotCareBits(32);//wait the AT45DB081 read finish
ClrSCLK();
SetSCLK();
for(;;)
{
for(i=0;i<8;i++)//send command,MSB
{
ClrSCLK();
m<<=1;
if(SI) m |= 0x01;
SetSCLK();
}
CheckSumTemp+=m;
if(++temp==FlashRangeNum[RangeNum])break;
}
At45DB081_Deselect();
//printf("MakeSum,Add=%lx,Sum=%x\n",(ulong)FlashRangeAdd[RangeNum],(uint)CheckSumTemp);
if(At45DB081_WritBytesInPage(FlashAdd_CheckSum+RangeNum*2,1,&CheckSumTemp))return True;
return False;
}
uchar FlashWritePassCode(uchar PassCodeNum,uchar * PassCodeAdd)
//write passcode to Flash,return 1:write successfully,return 1:Write Fail
{
//uchar j;
//printf("Write PassCode=");
//for(j=0;j<FlashDataPassCodeLen;j++)
//printf("%x ",(uint)*(PassCodeAdd+j));
//printf("\n");
if(PassCodeNum>FlashDataPassCodeNum)return False;//write error
if(!At45DB081_WritBytesInPage(FlashAdd_PassCode+FlashDataPassCodeLen*PassCodeNum,
FlashDataPassCodeLen,PassCodeAdd))//write bytes to one page
return False;
return (FlashMakeCheckSum(0));//make check sum to Flash
}
uchar FlashComparePassCode(uchar *PassCodeAdd)
//compare the *PassCodeAdd with the Flash Saved,if true,return the PassCode Number,else return 0xff
{
uchar idata i,j;
uchar idata FlashTempBuf[FlashDataPassCodeLen];
//printf("Be compared PassCode=");
//for(j=0;j<FlashDataPassCodeLen;j++)
//printf("%x ",(uint)*(PassCodeAdd+j));
//printf("\n");
for(i=0;i<FlashDataPassCodeNum;i++)
{
At45DB081_ReadBytesInPage(FlashAdd_PassCode+FlashDataPassCodeLen*i,
FlashDataPassCodeLen,&FlashTempBuf[0]);//read bytes from PassCode Add
//printf("Read PassCode %d=",(uint)i);
//for(j=0;j<FlashDataPassCodeLen;j++)
//printf("%x ",(uint)FlashTempBuf[j]);
//printf("\n");
for(j=0;j<FlashDataPassCodeLen;j++)
{
if(*(PassCodeAdd+j) != FlashTempBuf[j])break;
}
if(j==FlashDataPassCodeLen)return i;
}
return 0xff;
}
uchar FlashSelfTest(void)
//return 0x00:test true;return 10XX XXXX(the bit 7=1):error,the low 7 bits note
//the error CheckSum sector(<64);return 0xff,all datas in flash are invalid
{
uchar idata i;
uchar idata FlashTempBuf[FlashDataValidFlagNum];
//read the Valid Data flag bytes
FlashReadData(FlashAdd_DataValidFlag,4,&FlashTempBuf[0]);
for(i=0;i<FlashDataValidFlagNum;i++)
{
if(FlashTempBuf[i]!=FlashDataValidFlag[i])//found the flash datas are invalid
return 0xff;
}
//declare the CheckSum,from the Top of the flash
//1st CheckSum:the Chinese Character Lib's CheckSum(0xC6000-0x108000)
//2nd CheckSum:the self learn message's CkeckSum(0x74040-0xC6000)
//3rd CheckSum:the operation record's CheckSum(0x5B020-0x74040)
//4th CheckSum:the recall message's CheckSum(0x30048-0x5B020)
//5th CheckSum:the reserved sector's CheckSum(0x210-0x30048)
//5th CheckSum:the frist 264*2 bytes(0x00-0x210)(they are used for saving some system message)
for(i=0;i<FlashDataRangeNum;i++)
{
if(!FlashGetCheckSum(i))//check sum error
return (0x80|i);
}
return 0;
}
uchar FlashWriteOptRecord(ulong FlashAdd,uchar *Data,uint DataLen)//write operation record to Flash
//return False:fail,return True:success
{
if((FlashAdd+DataLen)>FlashNum_OptRecord)return False;
return (FlashWriteData(FlashAdd+FlashAdd_OptRecord,DataLen,Data));//write flash
}
ulong FlashReadOptRecord(ulong FlashAdd,uchar * OptData)
//if FlashAdd=0,frist read
//Read operation Item,if True:return the Next Operation Item address.
//When no next Operation Item,return 0
{
uchar idata i;
if(!FlashAdd)FlashAdd=FlashAdd_OptRecord;
FlashReadData(FlashAdd,LockIDByteLen,(uchar *)OptData);//read Lock ID
for(i=0;i<LockIDByteLen;i++)
{
if(*(uchar *)OptData != 0x00)break;
((uchar *)OptData)++;
}
if(i==LockIDByteLen)return 0;//no other Operation Item
FlashAdd += LockIDByteLen;
FlashReadData(FlashAdd,LockIDOptSortByteLen,(uchar *)OptData);//read Lock Opt Sort
FlashAdd++;
((uchar *)OptData)++;
while(1)
{
FlashReadData(FlashAdd,1,(uchar *)OptData);//read Lock Name
FlashAdd++;
if(!(*(uchar *)OptData))break;
((uchar *)OptData)++;
}
return FlashAdd;//return next Opt Item Address
}
uchar FlashWriteSelfLrn(ulong FlashAdd,uchar *Data,uint DataLen)//write operation record to Flash
//return False:fail,return True:success
{
if((FlashAdd+DataLen)>FlashNum_SelfLrn)return False;
return (FlashWriteData(FlashAdd+FlashAdd_SelfLrn,DataLen, Data));//write flash
}
/*
void At45DB081_Buf1Write(uint Add,uint ByteNum,uchar * Data)
{
uint j;
uchar i;
printf("Write Bytes To Buf1!Add=%x,ByteNum=%x,DataPtr=%x\n",(uint)Add,(uint)ByteNum,(uint)Data);
At45DB081_Select();
At45DB081_WriteCmd(Opcode_Buf1Write,Add);
for(j=0;j<ByteNum;j++)
{
for(i=0;i<8;i++)//send bytes,MSB
{
ClrSCLK();
if((*(Data+j)) & Or8BitTab[7-i])
{
SetSO();
}
else
{
ClrSO();
}
SetSCLK();
}
}
At45DB081_Deselect();
}
void At45DB081_Buf1Read(uint Add,uint ByteNum,uchar * Data)
{
uint j;
uchar i;
printf("Read Bytes From Buf1!Add=%x,ByteNum=%x,DataPtr=%x\n",(uint)Add,(uint)ByteNum,(uint)Data);
At45DB081_Select();
At45DB081_WriteCmd(Opcode_Buf1Read,Add);
At45DB081_SendDoNotCareBits(8);
ClrSCLK();
SetSCLK();
for(j=0;j<ByteNum;j++)
{
for(i=0;i<8;i++)//send command,MSB
{
ClrSCLK();
_nop();
if(SI) (*(Data+j)) |= Or8BitTab[7-i];
else (*(Data+j)) &= And8BitTab[7-i];
SetSCLK();
}
printf("%02x ",(uint)(*(Data+j)));
}
printf("\n");
At45DB081_Deselect();
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -