STM32 對內(nèi)部FLASH讀寫接口函數(shù)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
因?yàn)橐脙?nèi)部FLASH代替外部EEPROM,把參數(shù)放在STM32的0x08000000+320K處,其中20K是bootloader,300K是應(yīng)用程序。
原理:先要把整頁FLASH的內(nèi)容搬到RAM中,然后在RAM中改動,然后擦除整頁FLASH,再把改動后的內(nèi)容寫入原Flash頁。下面程序調(diào)試通過。
/*******************************************************************************
* Function Name : I2C_EE_BufferRead
* Description : Reads a block of data from the EEPROM.
* Input :
* -RomAddr
* -NumByteToRead
* -pRomData
* Output : None
* Return : None
*******************************************************************************/
void I2C_EE_BufferRead(u16 RomAddr,u16 NumByteToRead,u8 *pRomData)
{
u32 param_flashbase;
u8* ptr;
param_flashbase = 0x8000000+(300+20)*1024;
ptr = (u8*)(param_flashbase + RomAddr);
while( NumByteToRead-- >0)
{
*pRomData = *ptr; //直接賦值即可
printf("0x%x ",*pRomData);
pRomData++;
ptr++;
}
return;
}
/*******************************************************************************
* Function Name : I2C_EE_BufferWrite
* Description : Write a block of data to the EEPROM.
* Input :
* -RomAddr
* -NumByteToRead
* -pRomData
* Output : None
* Return : None
*******************************************************************************/
void I2C_EE_BufferWrite(u8 DeviceType,u8 SlaveAddr,u16 RomAddr,u16 NumByteToWrite,u8 *pRomData)
{
uint32_t param_flashbase;
uint32_t tempaddress;
uint32_t startaddress;
uint32_t FlashAddress;
uint32_t datasource;
u8 buf1[PAGE_SIZE];
u8 buf2[PAGE_SIZE];
u32 pagenumber = 0x0;
u32 EraseCounter = 0x0;
u32 i = 0;
FLASH_Status FLASHStatus = FLASH_COMPLETE;
param_flashbase = 0x8000000+(300+20)*1024;
startaddress=tempaddress = param_flashbase+RomAddr;
/*********************起始指針不在Flash頁的開始端*********************/
if( (tempaddress%PAGE_SIZE) != 0)
{ printf("startptr not in Page head rn");
if( ((startaddress%PAGE_SIZE)+NumByteToWrite) > PAGE_SIZE ) /*超出一頁范圍
{
I2C_EE_BufferRead(0,0,(tempaddress-(tempaddress % PAGE_SIZE)),PAGE_SIZE,buf1); /*把起始地址所在頁的內(nèi)容讀到內(nèi)存buf1中
memcpy(buf1+(tempaddress % PAGE_SIZE),pRomData,PAGE_SIZE-(tempaddress % PAGE_SIZE)); /*把需要寫入的數(shù)據(jù)覆蓋到buf1中
while( FLASHStatus == FLASH_ErasePage(tempaddress) ) /*buf1寫入到Flash
{
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)
{
printf("I2C_EE_BufferWrite error!rn");
return ;
}
datasource += 4;
FlashAddress += 4;
}
break;
}
NumByteToWrite -= PAGE_SIZE-(startaddress % PAGE_SIZE); 需要寫入字節(jié)數(shù)減去,上面覆蓋上去的數(shù)據(jù)的字節(jié)數(shù)
tempaddress += PAGE_SIZE-(tempaddress % PAGE_SIZE); /*把ptr指針指向下一個(gè)頁起始位置
if((NumByteToWrite % PAGE_SIZE) != 0) /*末指針不在Flash頁的開始端
{
//讀取1 PAGE 數(shù)據(jù)到內(nèi)存,修改,然后寫進(jìn)去
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_BufferWrite error!rn");
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!rn");
return ;
}
datasource += 4;
FlashAddress += 4;
}
}
}
else /*寫的內(nèi)容沒有超出一頁范圍
{
printf("FlashWrire --in one page rn");
I2C_EE_BufferRead(0,0,(startaddress-(startaddress % PAGE_SIZE)),PAGE_SIZE,buf1); /*把起始地址所在頁的內(nèi)容讀到內(nèi)存buf1中
memcpy( (buf1+(tempaddress % PAGE_SIZE)),pRomData, NumByteToWrite ); /*把需要寫入的數(shù)據(jù)覆蓋到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中的數(shù)據(jù),看是否寫入正確
{
printf("I2C_EE_BufferWrite error!rn");
return ;
}
datasource += 4;
FlashAddress += 4;
}
break;
}
}
}
/*******************起始指針在Flash頁的開始端****************************/
else
{ printf("startptr in Page head rn");
if((NumByteToWrite % PAGE_SIZE) != 0)
{
//讀取1 PAGE 數(shù)據(jù)到內(nèi)存,修改,然后寫進(jìn)去
I2C_EE_BufferRead(0,0,(u16)(tempaddress+NumByteToWrite-(NumByteToWrite % PAGE_SIZE)),PAGE_SIZE,buf1);
printf("already copy to bug1 rn");
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(sta