高级会员

- 积分
- 801
- 金钱
- 801
- 注册时间
- 2012-6-29
- 在线时间
- 0 小时
|

楼主 |
发表于 2012-12-11 11:15:21
|
显示全部楼层
我写一个代码,用于擦除任意起始地址的诺干字节空间:
//可以擦除任意位置Byte空间。
void SPI_ExFlash_Erase_Bytes(u32 EraseAddr, u8 ByteNum)
{
int i=0,j=0,k=0;
int FirstSectorAddr_Num = EraseAddr>>12;//4096
int SecondSectorAddr_Num = FirstSectorAddr_Num+1;
u32 FirstSectorAddr_Start = FirstSectorAddr_Num<<12;
u32 FirstSectorAddr_End = FirstSectorAddr_Start+4095;
u32 SecondSectorAddr_Start = FirstSectorAddr_End+1;
int Length_SectorStarAddrtToEraseAddr = EraseAddr - FirstSectorAddr_Start;//EraseAddr - (SectorAddr_Start-1) + 1
//如果EraseAddr = 4090;则SectorAddr_End = 4095;那么第一Sector有6个字节:4095-4090+1=5+1;
int BytesInFirstSector = FirstSectorAddr_End-EraseAddr+1;
//如果ByteNum = 23;则 EraseAddr+ByteNum= 4090 + 23 = 4113;减去SSectorAddr_End(=4095),则前三个参数结果=18;
//而第一Sector共有6个字节,那么第二个Sector共有17个字节。所以,前面三个参数要再减去1;
int BytesInSecondSector = EraseAddr+ByteNum-FirstSectorAddr_End-1;
//【备注】BytesInFirstSector+BytesInSecondSector刚好=ByteNum。
u32 EraseAddr_End = EraseAddr+ByteNum-1;//如果EA=4090,ByteNum=6,而EA_E刚好是4095。EA+ByteNum=4096。故要减1;
if(EraseAddr_End<=FirstSectorAddr_End)
{
SPI_ExFlash_Read(SPI_ExFlash_BufSector_1, FirstSectorAddr_Start, W25X_WholeSectorLength);
for(i=0;i<ByteNum;i++)
{
SPI_ExFlash_BufSector_1[Length_SectorStarAddrtToEraseAddr+i]= (u8)0xFF;//擦除后,写入0xFF 是Dummy Write.
}
SPI_ExFlash_Erase_Sector(FirstSectorAddr_Num);//需要150ms
SPI_ExFlash_Write_BlockMax_EraseFirst_Not(SPI_ExFlash_BufSector_1, FirstSectorAddr_Start, W25X_WholeSectorLength);
}else
{
SPI_ExFlash_Read(SPI_ExFlash_BufSector_1, FirstSectorAddr_Start, W25X_WholeSectorLength);
SPI_ExFlash_Read(SPI_ExFlash_BufSector_2, SecondSectorAddr_Start, W25X_WholeSectorLength);
for(j=0;j<BytesInFirstSector;j++)
{
SPI_ExFlash_BufSector_1[Length_SectorStarAddrtToEraseAddr+j]= (u8)0xFF;//擦除后,写入0xFF 是Dummy Write.
}
for(k=0;k<BytesInSecondSector;k++)
{
SPI_ExFlash_BufSector_2[k]= (u8)0xFF;//擦除后,写入0xFF 是Dummy Write.
}
SPI_ExFlash_Erase_Sector(FirstSectorAddr_Num);//需要150ms
SPI_ExFlash_Erase_Sector(SecondSectorAddr_Num);//需要150ms
SPI_ExFlash_Write_BlockMax_EraseFirst_Not(SPI_ExFlash_BufSector_1, FirstSectorAddr_Start, W25X_WholeSectorLength);
SPI_ExFlash_Write_BlockMax_EraseFirst_Not(SPI_ExFlash_BufSector_2, SecondSectorAddr_Start, W25X_WholeSectorLength);
}
}
这个代码测试OK。
现在要写一个在任意起始地址上写入若干字节的数据。发现数据只能写前一个page,后面一个page写不进去。
所以来问一下,是否可以连续调用你提供的page写入函数。
我调用的结果是,写入第二个page的时候,出问题了,写不进去。
> |
|