- UID
- 1029342
- 性别
- 男
|
NumByteToWrite -= NumByteToWrite % PAGE_SIZE;
//擦除Flash
pagenumber = NumByteToWrite/PAGE_SIZE; for (EraseCounter = 0; (EraseCounter < pagenumber) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
{
FLASHStatus = FLASH_ErasePage( tempaddress + PAGE_SIZE*EraseCounter );
}
//写Flash
datasource = *(uint32_t *)(pRomData+ PAGE_SIZE-(startaddress % PAGE_SIZE) );
FlashAddress = tempaddress;
while( pagenumber-- > 0 )
{
i=PAGE_SIZE/4;
while(i -- >0)
{
FLASH_ProgramWord(FlashAddress,*(uint32_t*)datasource);
if (*(uint32_t*)FlashAddress != *(uint32_t*)datasource)
{
printf("I2C_EE_BufferWrite error!\r\n");
return ;
}
datasource += 4;
FlashAddress += 4;
}
}
}
else /*写的内容没有超出一页范围
{
printf("FlashWrire --in one page \r\n");
I2C_EE_BufferRead(0,0,(startaddress-(startaddress % PAGE_SIZE)),PAGE_SIZE,buf1); /*把起始地址所在页的内容读到内存buf1中
memcpy( (buf1+(tempaddress % PAGE_SIZE)),pRomData, NumByteToWrite ); /*把需要写入的数据覆盖到buf1中
while( FLASHStatus == FLASH_ErasePage(tempaddress) )
{
i=PAGE_SIZE/4;
datasource = (uint32_t)buf1;
FlashAddress = tempaddress-(tempaddress % PAGE_SIZE);
while(i-- >0)
{
FLASH_ProgramWord(FlashAddress,*(uint32_t*)datasource);
if (*(uint32_t *)FlashAddress != *(uint32_t *)datasource) /*读取Flash中的数据,看是否写入正确
{
printf("I2C_EE_BufferWrite error!\r\n");
return ;
}
datasource += 4;
FlashAddress += 4;
}
break;
}
}
}
/*******************起始指针在Flash页的开始端****************************
else
{ printf("startptr in Page head \r\n");
if((NumByteToWrite % PAGE_SIZE) != 0)
{
//读取1 PAGE 数据到内存,修改,然后写进去
I2C_EE_BufferRead(0,0,(u16)(tempaddress+NumByteToWrite-(NumByteToWrite % PAGE_SIZE)),PAGE_SIZE,buf1);
printf("already copy to bug1 \r\n");
memcpy(buf1,pRomData+NumByteToWrite-(NumByteToWrite % PAGE_SIZE),(NumByteToWrite % PAGE_SIZE));
//end of debug
}
//擦除Flash
if( (NumByteToWrite%PAGE_SIZE) == 0 )
{
pagenumber = NumByteToWrite/PAGE_SIZE;
}
else
{
pagenumber = NumByteToWrite/PAGE_SIZE + 1;
}
for (EraseCounter = 0; (EraseCounter < pagenumber) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
{
FLASHStatus = FLASH_ErasePage(startaddress + (PAGE_SIZE * EraseCounter));
}
//写Flash
if( pagenumber == 1) /*只有一页
{
i=PAGE_SIZE/4;
datasource = (uint32_t)buf1;
FlashAddress = startaddress;
while(i-- >0)
{
FLASH_ProgramWord(FlashAddress,*(uint32_t *)datasource);
if (*(uint32_t *)FlashAddress != *(uint32_t *)datasource)
{
printf("I2C_EE_BufferWrite error!\r\n");
return ;
}
datasource +=4;
FlashAddress +=4;
}
}
else /*很多页时,先写前面页,最后写buf1
{
while( pagenumber-- > 1 )
{
datasource = (u32)pRomData;
FlashAddress = startaddress;
i=PAGE_SIZE/4;
while(i -- >0)
{
FLASH_ProgramWord( FlashAddress, *(uint32_t *)datasource );
if (*(uint32_t *)FlashAddress != *(uint32_t *)datasource)
{
printf("I2C_EE_BufferWrite error!\r\n");
return ;
}
datasource += 4;
FlashAddress += 4;
}
} |
|