📄 复件 zlg_ffs.c
字号:
return FALSE;
}
}
}
}
}
return TRUE;
err:
Index->VBlockInfo[VBlockIndex] = 0x00000000;
FFSBlockErase(Index, BlockIndex);
return FALSE;
}
/*********************************************************************************************************
** Function name: FFSDiskRead
** Descriptions: 读逻辑扇区 read logistic sector
** Input:Index: 底层驱动信息 low driver info
** Buf: 存储要读到的数据的缓冲区 Data buf
** SecIndex:扇区索引 sector index
** Output: TRUE: 成功 OK
** FALSE: 失败 NOT OK
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static uint8 FFSDiskRead(FFSDisk *Index, uint8 *Buf, uint32 SecIndex)
{
uint32 BlockIndex;
const FlashDriver *Drive;
uint16 Rt;
Rt = DISK_READ_NOT_OK;
Drive = Index->Drive;
BlockIndex = SecIndex / Drive->SecsPreBlock;
if (Index != NULL)
if (Drive != NULL)
if (BlockIndex < Index->BlockSum)
{
Rt = DISK_READ_OK;
BlockIndex = Index->VBlockInfo[BlockIndex]; /* 获得物理块号 */
if ((BlockIndex < Index->BlockSum)
&& (BlockIndex != 0))
{
SecIndex = SecIndex % Drive->SecsPreBlock; /* 获得块内偏移 */
SecIndex = SecIndex + BlockIndex * Drive->SecsPreBlock; /* 获得物理扇区号 */
Drive->SectorRead(Drive->Index, Buf, SecIndex);
}
}
return Rt;
}
/*********************************************************************************************************
** Function name: FildBlockB
** Descriptions: 查找空闲块中擦除次数最少的块 find block, which erasure sum is Minimal,in free block.
** Input:Index: 底层驱动信息 low driver info
**
** Output: 0: 没找到 not find
** other: 块索引 block index
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static uint32 FildBlockB(FFSDisk *Index)
{
uint32 BlockIndex;
uint32 i, j, temp2;
uint32 min;
uint8 *BlockState, temp1;
uint32 *BlockEaseSum;
uint8 *BufA;
const FlashDriver *Drive;
Drive = Index->Drive;
j = Drive->BlockPreDisk;
BufA = Drive->BufA;
min = ~0;
BlockIndex = 0;
i = j - 1;
BlockState = Index->BlockState + 1;
BlockEaseSum = Index->BlockEaseSum + 1;
do
{
temp1 = *BlockState++;
temp2 = *BlockEaseSum++;
if (temp1 == FREE_BLOCK)
if (temp2 < min)
{
min = temp2;
BlockIndex = j - i;
if (min == 0)
{
break;
}
}
} while (--i != 0);
if (BlockIndex < j)
{
/* 新块是否需要擦除 */
i = Drive->SecsPreBlock;
temp1 = BlockIndex * i;
do
{
Drive->ExSectorRead(Drive->Index, BufA, temp2);
if (BufA[5] != 0xff)
{
if (FFSBlockErase(Index, BlockIndex) != TRUE)
{
return 0;
}
break;
}
} while (--i != 0);
return BlockIndex;
}
return 0;
}
/*********************************************************************************************************
** Function name: FildBlockA
** Descriptions: 查找擦除次数最少的块(非空闲块要加权,以尽量在空闲块重分配)
** find block which erasure sum is Minimal
** Input:Index: 底层驱动信息 low driver info
**
** Output: 0: 没找到 not find
** other: 块索引 block index
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static uint32 FildBlockA(FFSDisk *Index)
{
uint32 BlockIndex;
uint32 i, j, temp2;
uint32 min;
uint8 *BlockState, temp1;
uint32 *BlockEaseSum;
uint8 *BufA;
const FlashDriver *Drive;
Drive = Index->Drive;
j = Drive->BlockPreDisk;
BufA = Drive->BufA;
min = ~0;
BlockIndex = 0;
i = j - 1;
BlockState = Index->BlockState + 1;
BlockEaseSum = Index->BlockEaseSum + 1;
do
{
temp1 = *BlockState++;
temp2 = *BlockEaseSum++;
if (temp1 != BAD_BLOCK)
if (temp2 < min)
{
min = temp2;
BlockIndex = j - i;
if (min == 0)
{
break;
}
}
} while (--i != 0);
if (BlockIndex < j)
{
if (Index->BlockState[BlockIndex] != USR_BLOCK) /* 此块没有使用 */
{
/* 新块是否需要擦除 */
i = Drive->SecsPreBlock;
temp1 = BlockIndex * i;
do
{
Drive->ExSectorRead(Drive->Index, BufA, temp2);
if (BufA[5] != 0xff)
{
if (FFSBlockErase(Index, BlockIndex) != TRUE)
{
return 0;
}
break;
}
} while (--i != 0);
}
return BlockIndex;
}
return 0;
}
/*********************************************************************************************************
** Function name: BlockCopyA
** Descriptions: 块拷贝1 block copy 1
** Input:Index: 底层驱动信息 low driver info
** SourBlockIndex: 源块索引 source block index
** DestBlockIndex: 目标块索引 dest block index
** Output: TRUE: 成功 OK
** FALSE: 失败 NOT OK
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static uint8 BlockCopyA(FFSDisk *Index, uint32 SourBlockIndex, uint32 DestBlockIndex)
{
unsigned int i;
uint32 temp1, temp2;
const FlashDriver *Drive;
uint8 *BufA;
uint8 C[4];
C[0] = 0xff;
C[1] = 0xff;
C[2] = 0xff;
C[3] = 0xff;
/* 拷贝数据到空闲块 */
Drive = Index->Drive;
i = Drive->SecsPreBlock;
temp1 = SourBlockIndex * i;
temp2 = DestBlockIndex * i;
BufA = Drive->BufA;
do
{
Drive->ExSectorRead(Drive->Index, BufA, temp1);
if (BufA[5] != 0xff)
{
C[0] = BufA[0];
C[1] = BufA[1];
C[2] = BufA[2];
C[3] = BufA[3];
Drive->ExSectorRead(Drive->Index, BufA, temp2);
BufA[0] = C[0];
BufA[1] = C[1];
BufA[2] = C[2];
BufA[3] = C[3];
BufA[5] = 0x55;
if (Drive->ExSectorWrite(Drive->Index, BufA, temp2) != TRUE)
{
FFSBlockErase(Index, DestBlockIndex);
return FALSE;
}
if (Drive->ExSectorCheck(Drive->Index, BufA, temp2) != TRUE)
{
FFSBlockErase(Index, DestBlockIndex);
return FALSE;
}
Drive->SectorRead(Drive->Index, BufA, temp1);
if (Drive->SectorWrite(Drive->Index, BufA, temp2) != TRUE)
{
FFSBlockErase(Index, DestBlockIndex);
return FALSE;
}
if (Drive->SectorCheck(Drive->Index, BufA, temp2) != TRUE)
{
FFSBlockErase(Index, DestBlockIndex);
return FALSE;
}
}
temp1++;
temp2++;
} while (--i != 0);
i = Drive->SecsPreBlock;
temp2 = DestBlockIndex * i;
do
{
Drive->ExSectorRead(Drive->Index, BufA, temp2);
if (BufA[5] == 0x55)
{
BufA[5] = 0;
if (Drive->ExSectorWrite(Drive->Index, BufA, temp2) != TRUE)
{
FFSBlockErase(Index, DestBlockIndex);
return FALSE;
}
if (Drive->ExSectorCheck(Drive->Index, BufA, temp2) != TRUE)
{
FFSBlockErase(Index, DestBlockIndex);
return FALSE;
}
}
temp2++;
} while (--i != 0);
temp1 = C[2] | (C[3] << 8);
temp1 = temp1 << 16;
temp1 |= (C[0] | (C[1] << 8));
if (temp1 < Index->Drive->BlockPreDisk)
{
temp2 = BufA[10] | (BufA[11] << 8);
temp2 = temp2 << 16;
temp2 |= (BufA[8] | (BufA[9] << 8));
Index->VBlockInfo[temp1 - 1] = DestBlockIndex;
Index->BlockEaseSum[DestBlockIndex] = temp2 + USR_BLOCK_ADD;
Index->BlockState[DestBlockIndex] = USR_BLOCK;
}
/* 块擦除 */
if (FFSBlockErase(Index, SourBlockIndex) != TRUE)
{
return FALSE;
}
return TRUE;
}
/*********************************************************************************************************
** Function name: BlockCopyB
** Descriptions: 块拷贝,但不拷贝指定扇区 block copy,but do not copy one sector of the block.
** Input:Index: 底层驱动信息 low driver info
** SourBlockIndex: 源块索引 source block index
** DestBlockIndex: 目标块索引 dest block index
** UCopySecIndex: 无需拷贝的块内扇区索引 uncopy sector index of block
** Output: TRUE: 成功 OK
** FALSE: 失败 NOT OK
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static uint8 BlockCopyB(FFSDisk *Index, uint32 SourBlockIndex, uint32 DestBlockIndex,
unsigned int UCopySecIndex)
{
unsigned int i, j;
uint32 temp1, temp2;
const FlashDriver *Drive;
uint8 *BufA;
uint8 C[4];
C[0] = 0xff;
C[1] = 0xff;
C[2] = 0xff;
C[3] = 0xff;
/* 拷贝数据到空闲块 */
Drive = Index->Drive;
i = Drive->SecsPreBlock;
temp1 = SourBlockIndex * i - 1;
temp2 = DestBlockIndex * i - 1;
BufA = Drive->BufA;
j = UCopySecIndex;
do
{
temp1++;
temp2++;
if (j-- == 0)
{
continue;
}
Drive->ExSectorRead(Drive->Index, BufA, temp1);
if (BufA[5] != 0xff)
{
C[0] = BufA[0];
C[1] = BufA[1];
C[2] = BufA[2];
C[3] = BufA[3];
Drive->ExSectorRead(Drive->Index, BufA, temp2);
BufA[0] = C[0];
BufA[1] = C[1];
BufA[2] = C[2];
BufA[3] = C[3];
BufA[5] = 0x55;
if (Drive->ExSectorWrite(Drive->Index, BufA, temp2) != TRUE)
{
FFSBlockErase(Index, DestBlockIndex);
return FALSE;
}
if (Drive->ExSectorCheck(Drive->Index, BufA, temp2) != TRUE)
{
FFSBlockErase(Index, DestBlockIndex);
return FALSE;
}
Drive->SectorRead(Drive->Index, BufA, temp1);
if (Drive->SectorWrite(Drive->Index, BufA, temp2) != TRUE)
{
FFSBlockErase(Index, DestBlockIndex);
return FALSE;
}
if (Drive->SectorCheck(Drive->Index, BufA, temp2) != TRUE)
{
FFSBlockErase(Index, DestBlockIndex);
return FALSE;
}
}
} while (--i != 0);
return TRUE;
}
/*********************************************************************************************************
** Function name: BlockCopyBE
** Descriptions: 函数BlockCopyB结束处理 do something after BlockCopyB
** Input:Index: 底层驱动信息 low driver info
** SourBlockIndex: 源块索引 source block index
** DestBlockIndex: 目标块索引 dest block index
** Output: TRUE: 成功 OK
** FALSE: 失败 NOT OK
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -