标题:
STM32 对内部FLASH读写接口函数(3)
[打印本页]
作者:
yuyang911220
时间:
2014-9-25 23:21
标题:
STM32 对内部FLASH读写接口函数(3)
NumByteToWrite-= PAGE_SIZE-(startaddress %PAGE_SIZE);
需要写入字节数减去,上面覆盖上去的数据的字节数
tempaddress+= PAGE_SIZE-(tempaddress %PAGE_SIZE);
/*把ptr指针指向下一个页起始位置
if((NumByteToWrite% PAGE_SIZE) != 0)
/*末指针不在Flash页的开始端
{
//读取1 PAGE数据到内存,修改,然后写进去
I2C_EE_BufferRead(0,0,tempaddress,PAGE_SIZE,buf2);
memcpy(buf2,pRomData+PAGE_SIZE-startaddress%PAGE_SIZE+NumByteToWrite-NumByteToWrite%PAGE_SIZE,(NumByteToWrite%PAGE_SIZE));
while( FLASHStatus == FLASH_ErasePage( tempaddress+NumByteToWrite))
/*把buf2写入到Flash中*
{
i=PAGE_SIZE/4;
datasource = (uint32_t)buf2;
FlashAddress = (tempaddress+NumByteToWrite-(NumByteToWrite %PAGE_SIZE));
/*末地址指针的页首
while(i-->0)
{
FLASH_ProgramWord(FlashAddress,*(uint32_t*)datasource);
if (*(uint32_t*)FlashAddress !=*(uint32_t*)datasource)
{
printf("I2C_EE_BufferWriteerror!\r\n");
return ;
}
datasource += 4;
FlashAddress +=4;
}
break;
}
}
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;
}
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0