📄 smt_fat.c
字号:
}
return(iRetVal);
}
/************************************************************************
* smtChkPrty
* Type : int
* Ret val : Error code
* SMT_SUCCESS ( 0) ... Successful
* SMT_FAILURE (-1) ... Failure
* Argument : unsigned short wLbaBlk ... "Block Address Area"
* Function : This function does the check of "Block Address Area".
************************************************************************/
int smtChkPrty(
unsigned short wLbaBlk // Logic block address
)
{
unsigned int iLoop; // Loop counter
unsigned int iParity; // Parity value
iParity = 0; // The check of parity bit is done referring
iLoop = 0; // - to "Block Address Area".
while(iLoop < 16)
{
if(wLbaBlk & SMT_M_MASK(iLoop))
{
iParity++;
}
iLoop++;
}
if(iParity % 2)
{
smtErrInf.iSmtErr = SMT_E_PRTYCHK;
return(SMT_E_FAILURE);
}
return(SMT_E_SUCCESS);
}
/************************************************************************
* smtChkBadBlk
* Type : int
* Ret val : Error code
* SMT_SUCCESS ( 0) ... Successful
* SMT_FAILURE (-1) ... Failure
* Argument : unsigned char bBlkSts ... "Block Status"
* Function : This function does the check of "Block Status".
************************************************************************/
int smtChkBadBlk(
unsigned char bBlkSts // Block status
)
{
int iLoop; // Loop counter
int iZero; // Zero counter
iZero = 0; // The check of "Bad Block" is done referring
iLoop = 0; // - to "Block Status".
while(iLoop < 8)
{
if(!(bBlkSts & SMT_M_MASK(iLoop)))
{
if(++iZero >= 2)
{
return(SMT_E_FAILURE);
}
}
iLoop++;
}
return(SMT_E_SUCCESS);
}
/************************************************************************
* smtGetFreeBlk
* Type : int
* Ret val : Error code
* Argument : unsigned long ulZone ... Zone address
* unsigned long *pFreeBlk ... Free block address (Physics)
* Function : This function acquires a free block.
************************************************************************/
int smtGetFreeBlk(
unsigned long ulZone, // Zone address
unsigned long *pFreeBlk // Free block address
)
{
int iLoop; // Loop counter
unsigned long ulPhyBlkMax; // Max value of physics block address.
unsigned char bIndex; // Zone index
///////////////////////
// BLOCK EMPTY CHECK //
///////////////////////
if(!ulZone) // The number of the free block is checked.
{
bIndex = 0;
}
else
{
bIndex = 1;
}
if(smt_pDevInf->pSizeInf->ulNumBlk < SMT_ZON_MAX)
{
ulPhyBlkMax = smt_pDevInf->pSizeInf->ulNumBlk;
}
else
{
ulPhyBlkMax = SMT_ZON_MAX;
}
////////////////////
// GET FREE BLOCK //
////////////////////
iLoop = 0;
while(iLoop < 2) // A free block is searched,
{ // - and an address is returned.
while(smtBlkTbl[bIndex].ulBlockNum < ulPhyBlkMax)
{
if(smtBlkTbl[bIndex].bBlkPhyInf[smtBlkTbl[bIndex].ulBlockNum / 8]
& SMT_M_MASK(smtBlkTbl[bIndex].ulBlockNum % 8))
{
smtBlkTbl[bIndex].bBlkPhyInf[smtBlkTbl[bIndex].ulBlockNum / 8]
&= ~SMT_M_MASK(smtBlkTbl[bIndex].ulBlockNum % 8);
*pFreeBlk = smtBlkTbl[bIndex].ulBlockNum;
return(SMT_E_SUCCESS);
}
smtBlkTbl[bIndex].ulBlockNum++;
}
smtBlkTbl[bIndex].ulBlockNum = 0;
iLoop++;
}
smtErrInf.iSmtErr = SMT_E_NOEMPTY;
return(SMT_E_FAILURE);
}
/************************************************************************
* smtEntryBadBlk
* Type : int
* Ret val : Error code
* Argument : unsigned long ulZone ... Zone address
* unsigned long ulPhyBlk ... Physics block address
* Function : This function registers bad block.
************************************************************************/
int smtEntryBadBlk(
unsigned long ulZone, // Zone address
unsigned long ulPhyBlk // Physics block address
)
{
int iRetVal; // Return Value
int iLoop; // Loop counter
unsigned long ulPage; // Page address
smtBadBlkCnt++; // "Bad Block" information is written in
// - all sector in the block.
if(!ulZone)
{
smtBlkTbl[0].bBlkPhyInf[ulPhyBlk / 8] &= ~SMT_M_MASK(ulPhyBlk % 8);
}
else
{
smtBlkTbl[1].bBlkPhyInf[ulPhyBlk / 8] &= ~SMT_M_MASK(ulPhyBlk % 8);
}
memset(smtWrite.stBuff.bProlix, 0xFF, smt_pDevInf->pSizeInf->uwPrlSize);
smtWrite.stDatBuff.bBlkSts = 0xF0;
iLoop = 0;
while(iLoop < smt_pDevInf->pSizeInf->uwBlkSize)
{
ulPage = smtChgPgNum(ulZone, ulPhyBlk, iLoop);
iRetVal = pSmt_ProlixProgram(0x00, ulPage, smtWrite.stBuff.bProlix);
if(iRetVal != SMT_E_SUCCESS)
{
break;
}
iLoop++;
}
if(smtBadBlkCnt > smt_pDevInf->pSizeInf->uwBadBlk) // An error is returned when it exceeds
{ // - the value which "Bad Block" is decided as.
iRetVal = SMT_E_FAILURE;
smtErrInf.iSmtErr = SMT_E_BADBLCK;
}
return(iRetVal);
}
/************************************************************************
* smtSetProlDat
* Type : void
* Ret val : None
* Argument : unsigned long ulLogBlk ... Logic block address
* unsigned char *pBuff ... Write data buffer
* Function : This function establishes information in prolix part.
************************************************************************/
void smtSetProlDat(
unsigned long ulLogBlk, // Logic block address
unsigned char *pBuff // Write data buffer
)
{
int iEccVal; // ECC value
unsigned long ulLbaBlk; // Block address area data
memset(smtProlBuff.bFlatBuff, 0xFF, SMT_PRL_MAX);
//////////////////////////////
/// SET BLOCK ADDRESS AREA ///
//////////////////////////////
ulLbaBlk = 0; // A logic block address is set up in the
if((long)ulLogBlk == -2)
{
ulLbaBlk = (unsigned long)-1;
}
else if((long)ulLogBlk != -1) // - writing buffer.
{
ulLbaBlk = smtChgBlkNum(ulLogBlk);
}
smtProlBuff.stDatBuff.bBlkAdr1[0] = SMT_M_BYTE(ulLbaBlk, 1);
smtProlBuff.stDatBuff.bBlkAdr1[1] = SMT_M_BYTE(ulLbaBlk, 0);
smtProlBuff.stDatBuff.bBlkAdr2[0] = SMT_M_BYTE(ulLbaBlk, 1);
smtProlBuff.stDatBuff.bBlkAdr2[1] = SMT_M_BYTE(ulLbaBlk, 0);
//////////////////////
/// COPY ECC VALUE ///
//////////////////////
#ifndef L05DMT
iEccVal = smtCalcEccVal(pBuff); // ECC value is set up in the writing buffer.
smtProlBuff.stDatBuff.bEcc1[0] = SMT_M_BYTE(iEccVal, 0);
smtProlBuff.stDatBuff.bEcc1[1] = SMT_M_BYTE(iEccVal, 1);
smtProlBuff.stDatBuff.bEcc1[2] = SMT_M_BYTE(iEccVal, 2);
iEccVal = smtCalcEccVal(pBuff + 256);
smtProlBuff.stDatBuff.bEcc2[0] = SMT_M_BYTE(iEccVal, 0);
smtProlBuff.stDatBuff.bEcc2[1] = SMT_M_BYTE(iEccVal, 1);
smtProlBuff.stDatBuff.bEcc2[2] = SMT_M_BYTE(iEccVal, 2);
#endif
}
/************************************************************************
* smtWtBlkAddr
* Type : void
* Ret val : Error code
* Argument : unsigned long ulZone ... Zone Address
* unsigned long ulPhyBlk ... Physical block address
* unsigned long ulLogBlk ... Logic block address
* Function : This function writes a logic block address in all sector.
************************************************************************/
int smtWtBlkAddr(
unsigned long ulZone,
unsigned long ulPhyBlk,
unsigned long ulLogBlk
)
{
int iLoop; // Loop counter
unsigned long ulPage; // Page address
///////////////////////////////
/// INITIALIZE WRITE BUFFER ///
/////////////////////////////// // Data are set up in the write buffer.
memset(smtWrite.stBuff.bProlix, 0xFF, smt_pDevInf->pSizeInf->uwPrlSize);
if((long)ulLogBlk != -1)
{
ulLogBlk = smtChgBlkNum(ulLogBlk);
}
else
{
ulLogBlk = 0;
}
smtWrite.stDatBuff.bBlkAdr1[0] = SMT_M_BYTE(ulLogBlk, 1);
smtWrite.stDatBuff.bBlkAdr1[1] = SMT_M_BYTE(ulLogBlk, 0);
smtWrite.stDatBuff.bBlkAdr2[0] = SMT_M_BYTE(ulLogBlk, 1);
smtWrite.stDatBuff.bBlkAdr2[1] = SMT_M_BYTE(ulLogBlk, 0);
/////////////////////////
/// WRITE ALL SECTORS ///
/////////////////////////
iLoop = 0; // A logic block address is written in
while(iLoop < smt_pDevInf->pSizeInf->uwBlkSize) // - all sector in the block.
{
ulPage = smtChgPgNum(ulZone, ulPhyBlk, iLoop);
if(pSmt_ProlixProgram(0x00, ulPage, smtWrite.stBuff.bProlix) != SMT_E_SUCCESS)
{
return(SMT_E_FAILURE);
}
iLoop++;
}
return(SMT_E_SUCCESS);
}
/************************************************************************
* smtDupliSect
* Type : int
* Ret val : Error code
* SMT_SUCCESS ( 0) ... Successful
* SMT_FAILURE (-1) ... Failure
* Argument : unsigned long ulZone ... Zone address
* unsigned long ulNewPhyBlk ... New block address
* unsigned long ulOldPhyBlk ... Old block address
* unsigned long ulTopSct ... Top sector address
* unsigned long ulBtmSct ... Bottom sector address
* unsigned char bVerify ... Verify Enable/Disable flag
* Function : This function writes sector of the range that it is
* specified in another block.
************************************************************************/
int smtDupliSect(
unsigned long ulZone,
unsigned long ulNewPhyBlk,
unsigned long ulOldPhyBlk,
unsigned long ulTopSect,
unsigned long ulBtmSect,
unsigned char bVerify
)
{
unsigned long ulLoop; // Loop Counter
unsigned long ulOldPage; // Old Page Address
unsigned long ulNewPage; // Old Page Address
unsigned long ulLogBlk; // Logical block address
ulLoop = ulTopSect; // Sector on the range that it is specified
while(ulLoop <= ulBtmSect) // - are copied on another block.
{
ulOldPage = smtChgPgNum(ulZone, ulOldPhyBlk, ulLoop);
if(smtPageReadWrap(SMT_C_DREAD_1, 0x00, ulOldPage, smtRead.bBuff) != SMT_E_SUCCESS)
{
smtErrInf.iSmtErr = SMT_E_PAGREAD;
return(SMT_E_FAILURE);
}
ulLogBlk = smtChgLogNum(smtRead.stDatBuff.bBlkAdr1[0], smtRead.stDatBuff.bBlkAdr1[1]);
smtSetProlDat(ulLogBlk, smtRead.bBuff);
ulNewPage = smtChgPgNum(ulZone, ulNewPhyBlk, ulLoop);
if(pSmt_PageProgram(0x00, ulNewPage, smtRead.bBuff, bVerify) != SMT_E_SUCCESS)
{
smtEntryBadBlk(ulZone, ulNewPhyBlk);
return(SMT_E_FAILURE);
}
ulLoop++;
}
return(SMT_E_SUCCESS);
}
/************************************************************************
* smtMerror
* Type : int
* Ret val : Error code
* Argument : None
* Function : The error code acquisition function which is
* characteristic of smart media.
************************************************************************/
int smtMerror(
void
)
{
return(smtErrInf.iSmtErr); // The error which is characteristic of
// - SmartMedia is returned.
}
/************************************************************************
* smtMclearErr
* Type : void
* Ret val : None
* Argument : None
* Function : The error code clear function which is
* characteristic of smart media.
************************************************************************/
void smtMclearErr(
void
)
{
smtErrInf.iSmtErr = SMT_E_SUCCESS; // The error which is characteristic of
// - SmartMedia is cleared.
}
/************************************************************************
* smtOpenLib
* Type : int
* Ret val : Error code
* SMT_SUCCESS ( 0) ... Successful
* SMT_FAILURE (-1) ... Failure
* Argument : FatFcs_t pstFcs ... File control structure
* FatFile_t pfpFile ... FAT File structure
* FatFile_t pfpFp ... Fat File structure array
* FatSecBuf_t pstSecBuf ... Sector Buffer structure
* unsigned char bMaxFiles... Open file max
* unsigned char bMaxBuffers... Open buffer max
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -