Because the internal FLASH is used instead of the external EEPROM, the parameters are placed at 0x08000000+320K of the STM32, of which 20K is the bootloader and 300K is the application.
Principle: First move the contents of the entire FLASH page to RAM, then modify it in RAM, then erase the entire FLASH page, and then write the modified contents to the original Flash page. The following program is debugged and passed.
/*******************************************************************************
* 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; //Just assign value
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;
/*********************The start pointer is not at the beginning of the Flash page*********************/
if( (tempaddress%PAGE_SIZE) != 0)
{ printf("startptr not in Page head \r\n");
if( ((startaddress%PAGE_SIZE)+NumByteToWrite) > PAGE_SIZE ) /*Out of one page range
{
I2C_EE_BufferRead(0,0,(tempaddress-(tempaddress % PAGE_SIZE)),PAGE_SIZE,buf1); /*Read the contents of the page where the start address is located into memory buf1
memcpy(buf1+(tempaddress % PAGE_SIZE),pRomData,PAGE_SIZE-(tempaddress % PAGE_SIZE)); /*Overwrite the data to be written into 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!\r\n");
return ;
}
datasource += 4;
FlashAddress += 4;
}
break;
}
NumByteToWrite -= PAGE_SIZE-(startaddress % PAGE_SIZE); The number of bytes to be written minus the number of bytes of data to be overwritten
tempaddress += PAGE_SIZE-(tempaddress % PAGE_SIZE); /*Set the ptr pointer to the start of the next page
if((NumByteToWrite % PAGE_SIZE) != 0) /*The end pointer is not at the beginning of the Flash page
{
//Read 1 PAGE of data into memory, modify, and then write it in
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) ) /*Write buf2 to Flash*
{
i=PAGE_SIZE/4;
datasource = (uint32_t)buf2;
FlashAddress = (tempaddress+NumByteToWrite-(NumByteToWrite % PAGE_SIZE)); /*Head of the last address pointer
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;
}
break;
}
}
NumByteToWrite -= NumByteToWrite % PAGE_SIZE;
// Erase Flash
pagenumber = NumByteToWrite/PAGE_SIZE;
for (EraseCounter = 0; (EraseCounter < pagenumber) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
{
FLASHStatus = FLASH_ErasePage( tempaddress + PAGE_SIZE*EraseCounter );
}
//Write 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 /*The content written does not exceed the scope of one page
{
printf("FlashWrire --in one page \r\n");
I2C_EE_BufferRead(0,0,(startaddress-(startaddress % PAGE_SIZE)),PAGE_SIZE,buf1); /*Read the contents of the page where the start address is located into memory buf1
memcpy( (buf1+(tempaddress % PAGE_SIZE)),pRomData, NumByteToWrite ); /*Overwrite the data to be written into 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) /*Read the data in the Flash to see if it is written correctly
{
printf("I2C_EE_BufferWrite error!\r\n");
return ;
}
datasource += 4;
FlashAddress += 4;
}
break;
}
}
}
/*******************The starting pointer is at the beginning of the Flash page****************************/
else
{ printf("startptr in Page head \r\n");
if((NumByteToWrite % PAGE_SIZE) != 0)
{
//Read 1 PAGE of data into memory, modify, and then write it in
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
}
// Erase 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));
}
//Write Flash
if( pagenumber == 1) /* Only one page
{
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 /*When there are many pages, write the previous page first and buf1 last
{
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;
}
}
//Write the following pages
datasource = (uint32_t)buf1;
FlashAddress = startaddress+(pagenumber-1)*PAGE_SIZE;
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;
}
}
}
}
Previous article:Use DMA to directly drive GPIO to achieve the highest GPIO output rate
Next article:keil For ARM function absolute definition
Recommended ReadingLatest update time:2024-11-16 20:44
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [FM33LG0 series development board evaluation] 01. Prepare the development environment
- [XMC4800 Relax EtherCAT Kit Review] + Getting started with DAVE, quickly build your own webserver
- [GD32L233C-START Review] + Guess which light will be on next time
- TI Voltage Reference (VREF) Application Design Tips and Tricks
- What happens if the input voltage of the window comparator is equal to the upper and lower limits?
- Banknote number recognition system based on ARM.pdf
- My knowledge of computers
- Sampling rate of MCU ADC
- [Sipeed LicheeRV 86 Panel Review] 7-Waft Graphical Interface Development Test (Part 2)
- I don't understand the principle of ultrasonic signal processing.