日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]因?yàn)橐脙?nèi)部FLASH代替外部EEPROM,把參數(shù)放在STM32的0x08000000+320K處,其中20K是bootloader,300K是應(yīng)用程序。原理:先要把整頁(yè)FLASH的內(nèi)容搬到RAM中,然后在RAM中改動(dòng),然后擦除整頁(yè)FLASH,再把改動(dòng)后的內(nèi)容寫(xiě)入

因?yàn)橐脙?nèi)部FLASH代替外部EEPROM,把參數(shù)放在STM32的0x08000000+320K處,其中20K是bootloader,300K是應(yīng)用程序。


原理:先要把整頁(yè)FLASH的內(nèi)容搬到RAM中,然后在RAM中改動(dòng),然后擦除整頁(yè)FLASH,再把改動(dòng)后的內(nèi)容寫(xiě)入原Flash頁(yè)。下面程序調(diào)試通過(guò)。

/*******************************************************************************

* 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頁(yè)的開(kāi)始端*********************/

if( (tempaddress%PAGE_SIZE) != 0)

{ printf("startptr not in Page head rn");

if( ((startaddress%PAGE_SIZE)+NumByteToWrite) > PAGE_SIZE ) /*超出一頁(yè)范圍

{

I2C_EE_BufferRead(0,0,(tempaddress-(tempaddress % PAGE_SIZE)),PAGE_SIZE,buf1); /*把起始地址所在頁(yè)的內(nèi)容讀到內(nèi)存buf1中

memcpy(buf1+(tempaddress % PAGE_SIZE),pRomData,PAGE_SIZE-(tempaddress % PAGE_SIZE)); /*把需要寫(xiě)入的數(shù)據(jù)覆蓋到buf1中


while( FLASHStatus == FLASH_ErasePage(tempaddress) ) /*buf1寫(xiě)入到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); 需要寫(xiě)入字節(jié)數(shù)減去,上面覆蓋上去的數(shù)據(jù)的字節(jié)數(shù)

tempaddress += PAGE_SIZE-(tempaddress % PAGE_SIZE); /*把ptr指針指向下一個(gè)頁(yè)起始位置


if((NumByteToWrite % PAGE_SIZE) != 0) /*末指針不在Flash頁(yè)的開(kāi)始端

{

//讀取1 PAGE 數(shù)據(jù)到內(nèi)存,修改,然后寫(xiě)進(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寫(xiě)入到Flash中*

{

i=PAGE_SIZE/4;

datasource = (uint32_t)buf2;

FlashAddress = (tempaddress+NumByteToWrite-(NumByteToWrite % PAGE_SIZE)); /*末地址指針的頁(yè)首

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 );

}

//寫(xiě)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 /*寫(xiě)的內(nèi)容沒(méi)有超出一頁(yè)范圍

{

printf("FlashWrire --in one page rn");

I2C_EE_BufferRead(0,0,(startaddress-(startaddress % PAGE_SIZE)),PAGE_SIZE,buf1); /*把起始地址所在頁(yè)的內(nèi)容讀到內(nèi)存buf1中

memcpy( (buf1+(tempaddress % PAGE_SIZE)),pRomData, NumByteToWrite ); /*把需要寫(xiě)入的數(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ù),看是否寫(xiě)入正確

{

printf("I2C_EE_BufferWrite error!rn");

return ;

}

datasource += 4;

FlashAddress += 4;

}

break;

}

}

}

/*******************起始指針在Flash頁(yè)的開(kāi)始端****************************/

else

{ printf("startptr in Page head rn");

if((NumByteToWrite % PAGE_SIZE) != 0)

{

//讀取1 PAGE 數(shù)據(jù)到內(nèi)存,修改,然后寫(xiě)進(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

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉