📄 flashmanager.c
字号:
//--------------------------------------------------------------------------//
// ERROR_CODE ReadData() //
// //
// Purpose: Read a buffer from flash to SRAM. //
// //
// Inputs: unsigned long ulStart - offset in flash to start the reads at //
// int nCount - number of elements to read, in this case bytes //
// int nStride - number of locations to skip between reads //
// int *pnData - pointer to data buffer to fill //
// //
//--------------------------------------------------------------------------//
ERROR_CODE ReadData(unsigned long ulStart,long lCount,long lStride,int *pnData)
{
long i = 0; // loop counter
int j = 0; // inner loop counter
unsigned long ulOffset = ulStart; // current offset to write
int iShift = 0; // shift value by iShift bits
int iNumWords = 2; // number of words in a long
int nHi,nLow;
int nSector=0; // store the sector number
ERROR_CODE ErrorCode = NO_ERR; // tells whether we had an error while filling
// write the buffer up to BUFFER_SIZE items
for (i = 0;i<lCount; i++)
{
for (iShift=0,j= 0;j< iNumWords;j+=2 )
{
//Check to see whether it is a valid sector
ErrorCode=SectorValityCheck(ulStart,lCount,lStride);
if(ErrorCode!=NO_ERR)
{
return ErrorCode;
}
// check to see that the address is within a valid sector
ErrorCode = GetSectorNumber(ulOffset,&nSector );
if( NO_ERR == ErrorCode )
{
// Read flash
ReadFlash( ulOffset, &nLow );
ulOffset += (lStride * 2);
ReadFlash( ulOffset, &nHi );
ulOffset += (lStride * 2);
pnData[i] = (nHi << 16) | nLow;
}
else
{
LedIndicator(ErrorCode);
return ErrorCode;
}
}
}
// return the appropriate error code
LedIndicator(ErrorCode);
return ErrorCode;
}
/*--------------------------------------------------------------------------
***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
---------------------------------------------------------------------------*/
//Note:The following fuctions are to be invoked by specific application.
//--------------------------------------------------------------------------//
// ERROR_CODE ErrorHandler() //
// //
// Purpose:Search the address where it stopped writting at the last time. //
// //
// Input(s):iStuff,representing what kind of stuff // //
//--------------------------------------------------------------------------//
ERROR_CODE ErrorHandler(int iStuff)
{
int i;
int nSector=0;
ERROR_CODE ErrorCode=NO_ERR;
//Here we take a more simple but not good way to handle the error
nSector=STUFF_OFFSET_ADDRESS[iStuff]&0xffff0000;
nSector=STUFF_OFFSET_ADDRESS[iStuff]>>16;
nSector=nSector&0x000ff;
for(i=nSector;i<nSector+STUFF_ERASE_STRIDE[iStuff]/BF533_FLASH_SECTOR_SIZE;i++)
{
EraseFlashBlock(i);
}
LedIndicator(ErrorCode);
return ErrorCode;
}
//--------------------------------------------------------------------------//
// ERROR_CODE wStateMachine() //
// //
// Purpose:Get the the flash writting state. //
// //
// Input(s):iStuff,representing what kind of stuff // //
//--------------------------------------------------------------------------//
ERROR_CODE wStateMachine(int iStuff) //Flash write state
{
//Sketch map:
//-------------------------------------------//
//Take four blocks as an example
//|------------------|
//||||||||||||||||||||
//||||||||||||||||||||
//|------------------|<--Critical address
//||||||||||||||||||||
//||||||||||||||||||||<--Common address
//|------------------|
//||||||||||||||||||||
//| |
//|------------------|
//| |
//| |
//|------------------|<--Tail address
//-------------------------------------------//
ERROR_CODE ErrorCode=NO_ERR;
if(!((wStuffCurrentAddress[iStuff]-STUFF_OFFSET_ADDRESS[iStuff])%STUFF_ERASE_STRIDE[iStuff]))
{
if(wStuffCurrentAddress[iStuff]==STUFF_OFFSET_ADDRESS[iStuff]+STUFF_NUM[iStuff]*STUFF_SIZE[iStuff])
wState=2; //at tail address
else
wState=1; //at critical address
}
else
{
wState=0; //at common address
}
LedIndicator(ErrorCode);
return ErrorCode;
}
//--------------------------------------------------------------------------//
// ERROR_CODE rStateMachine() //
// //
// Purpose: Get the flash reading state //
// //
// Input(s):iStuff,representing what kind of stuff // //
//--------------------------------------------------------------------------//
ERROR_CODE rStateMachine(int iStuff) //Flash write state
{
//Sketch map:
//-------------------------------------------//
//Take four blocks as an example
//0.Head is empty(after the first round)
//|------------------|
//| |
//| |
//|------------------|<--Critical address
//||||||||||||||||||||
//||||||||||||||||||||<--Common address
//|------------------|
//||||||||||||||||||||
//||||||||||||||||||||
//|------------------|
//||||||||||||||||||||
//||||||||||||||||||||
//|------------------|<--Tail address
//1.Middle is empty(after the first round)
//|------------------|
//||||||||||||||||||||
//||||||||||||||||||||
//|------------------|<--Critical address
//||||||||||||||||||||
//||||||||||||||||||||<--Common address
//|------------------|
//||||||||||||||||||||
//| |
//|------------------|
//||||||||||||||||||||
//||||||||||||||||||||
//|------------------|<--Tail address
//2.Tail is empty(at the first round)
//|------------------|
//||||||||||||||||||||
//||||||||||||||||||||
//|------------------|<--Critical address
//||||||||||||||||||||
//||||||||||||||||||||<--Common address
//|------------------|
//||||||||||||||||||||
//| |
//|------------------|
//| |
//| |
//|------------------|<--Tail address
//-------------------------------------------//
ERROR_CODE ErrorCode=NO_ERR;
ErrorCode=GetStuffTotalNum(iStuff);
if(ErrorCode==NO_ERR)
{
ErrorCode=GetStuffPartialNum(iStuff);
}
if(ErrorCode==NO_ERR)
{
if(nStuffPartialNum)
{
if(nStuffTotalNum==nStuffPartialNum)
{
rState=2;//tail is empty
}
else
{
rState=1;//middle is empty
}
}
else
{
if(nStuffTotalNum)
{
rState=0;//head is empty
}
else
{
rState=3;//No data are available
}
}
}
}
//--------------------------------------------------------------------------//
// ERROR_CODE GetStuffTotalNum() //
// //
// Purpose: Get the total number of stuff items in flash //
// //
// Input(s):iStuff,representing what kind of stuff // //
//--------------------------------------------------------------------------//
ERROR_CODE GetStuffTotalNum(int iStuff)
{
int i;
int tmp=0;
int Data=0;
long lCount=1;
long lStride=0x1;
unsigned long ulOffset;
ERROR_CODE ErrorCode=NO_ERR;
ulOffset=STUFF_OFFSET_ADDRESS[iStuff];
for(i=0;i<STUFF_NUM[iStuff];i++)
{
ErrorCode=ReadData(ulOffset,lCount,lStride,&Data);
if(ErrorCode==NO_ERR)
{
if(Data!=FLASH_FLAG) break;
}
else
{
ErrorCode=READ_ERROR;
}
ulOffset+=STUFF_SIZE[iStuff];
}
if(i==STUFF_NUM[iStuff])
{
ErrorHandler(iStuff); //Flash is filled totally with stuff
ErrorCode=READ_ERROR;
LedIndicator(ErrorCode);
return ErrorCode;
}
tmp=i;
//Go on searching
for(;i<STUFF_NUM[iStuff];i++)
{
ErrorCode=ReadData(ulOffset,lCount,lStride,&Data);
if(ErrorCode==NO_ERR)
{
if(Data==FLASH_FLAG) break;
}
else
{
ErrorCode=READ_ERROR;
LedIndicator(ErrorCode);
return ErrorCode;
}
ulOffset+=STUFF_SIZE[iStuff];
}
//Get total number
nStuffTotalNum=STUFF_NUM[iStuff]-i+tmp;
LedIndicator(ErrorCode);
return ErrorCode;
}
//--------------------------------------------------------------------------//
// ERROR_CODE GetStuffPartialNum() //
// //
// Purpose: Get the partial number of stuff items in flash //
// //
// Input(s):iStuff,representing what kind of stuff // //
//--------------------------------------------------------------------------//
ERROR_CODE GetStuffPartialNum(int iStuff)
{
//Stuff
//|------------------|-------
//|||||||||||||||||||| |
//|||||||||||||||||||| | Stuff
//|------------------| | Partial
//|||||||||||||||||||| | Number
//|||||||||||||||||||| |
//|------------------| |
//||||||||||||||||||||-------
//| |
//|------------------|
//||||||||||||||||||||
//||||||||||||||||||||
//|------------------|
int i;
int Data=0;
long lCount=1;
long lStride=0x1;
unsigned long ulOffset;
ERROR_CODE ErrorCode=NO_ERR;
ulOffset=STUFF_OFFSET_ADDRESS[iStuff];
for(i=0;i<STUFF_NUM[iStuff];i++)
{
ErrorCode=ReadData(ulOffset,lCount,lStride,&Data);
if(ErrorCode==NO_ERR)
{
if(Data!=FLASH_FLAG) break;
}
else
{
ErrorCode=READ_ERROR;
}
ulOffset+=STUFF_SIZE[iStuff];
}
if(i==STUFF_NUM[iStuff])
{
ErrorHandler(iStuff); //Flash is filled totally with stuff
ErrorCode=READ_ERROR;
LedIndicator(ErrorCode);
return ErrorCode;
}
//Get partial number
nStuffPartialNum=i;
LedIndicator(ErrorCode);
return ErrorCode;
}
//--------------------------------------------------------------------------//
// ERROR_CODE Relocate() //
// //
// Purpose: Get where to write stuff when system has just booted //
// //
// Input(s):iStuff,representing what kind of stuff // //
//--------------------------------------------------------------------------//
ERROR_CODE Relocate(int iStuff)
{//Initialization of operation on flash
int i=0;
int Data=0;
long lCount=1;
long lStride=0x1;
unsigned long ulOffset=0x0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -