2025-05-16 2:19 AM
Hello,
I am using an STM32F103VCT microcontroller and the EEPROM emulation library. I write data with the EE_WriteVariable() function and read it back with EE_ReadVariable(). However, whenever I download new firmware to the MCU via ST-Link V2 in STM32CubeIDE, the entire flash memory is erased—including the regions where I stored data using EE_WriteVariable().
Is there any way to download or debug new firmware without erasing the flash pages used by the EEPROM emulation?
For reference, I’m running STM32CubeIDE version 1.18.1. Any suggestions would be greatly appreciated.
Thank you!
2025-05-16 2:23 AM - edited 2025-05-16 2:28 AM
CubeIDE should only erase enough flash for the code:
Please give more details of how you are allocating Flash to the EEPROM emulation - it needs to be in separate pages from the code.
PS:
via: https://www.st.com/en/microcontrollers-microprocessors/stm32f103vc.html#documentation
2025-05-16 3:46 AM
Hi Andrew
thank you for your reponse.
Below is all of the code I’m using with the EEPROM emulation library. Are there any parts here that could be causing problems?
I would greatly appreciate it if you could let me know how to resolve this.
void read_eeprom()
{
EE_ReadVariable (0x0001, (uint16_t*)&gstSystem.nNumOfBlock);
EE_ReadVariable (0x0002, (uint16_t*)&gstMain.stRep.nInputVccMax);
EE_ReadVariable (0x0003, (uint16_t*)&gstMain.stRep.nInputVccMin);
EE_ReadVariable (0x0004, (uint16_t*)&gstMain.stRep.bBiastInOn);
EE_ReadVariable (0x0005, (uint16_t*)&gstMain.stRep.bBiastOut1On);
EE_ReadVariable (0x0006, (uint16_t*)&gstMain.stRep.bBiastOut2On);
EE_ReadVariable (0x0007, (uint16_t*)&gstMain.stRep.nTempMax);
EE_ReadVariable (0x0008, (uint16_t*)&gstMain.stRep.bShutdownOn);
EE_ReadVariable (0x0009, (uint16_t*)&gstMain.stRep.bAmpOn);
EE_ReadVariable (0x0010, (uint16_t*)&gstMain.stRep.bAlcOn);
EE_ReadVariable (0x0011, (uint16_t*)&gstMain.stRep.nOutputLevelMax);
EE_ReadVariable (0x0012, (uint16_t*)&gstMain.stRep.nAlcLevel);
EE_ReadVariable (0x0013, (uint16_t*)&gstMain.stRep.nAttenIn);
EE_ReadVariable (0x0014, (uint16_t*)&gstMain.stRep.nAttenOut);
EE_ReadVariable (0x0015, (uint16_t*)&gstMain.stRep.nAttenL1);
EE_ReadVariable (0x0016, (uint16_t*)&gstMain.stRep.nAttenL2);
}
void write_eeprom()
{
HAL_FLASH_Unlock();
EE_WriteVariable (0x0001, gstSystem.nNumOfBlock);
EE_WriteVariable (0x0002, gstMain.stRep.nInputVccMax);
EE_WriteVariable (0x0003, gstMain.stRep.nInputVccMin);
EE_WriteVariable (0x0004, gstMain.stRep.bBiastInOn);
EE_WriteVariable (0x0005, gstMain.stRep.bBiastOut1On);
EE_WriteVariable (0x0006, gstMain.stRep.bBiastOut2On);
EE_WriteVariable (0x0007, gstMain.stRep.nTempMax);
EE_WriteVariable (0x0008, gstMain.stRep.bShutdownOn);
EE_WriteVariable (0x0009, gstMain.stRep.bAmpOn);
EE_WriteVariable (0x0010, gstMain.stRep.bAlcOn);
EE_WriteVariable (0x0011, gstMain.stRep.nOutputLevelMax);
EE_WriteVariable (0x0012, gstMain.stRep.nAlcLevel);
EE_WriteVariable (0x0013, gstMain.stRep.nAttenIn);
EE_WriteVariable (0x0014, gstMain.stRep.nAttenOut);
EE_WriteVariable (0x0015, gstMain.stRep.nAttenL1);
EE_WriteVariable (0x0016, gstMain.stRep.nAttenL2);
HAL_FLASH_Lock();
}
void read_eeprom_table(uint8_t sel)
{
if(sel >= 2) return;
int pos = 40;
EE_ReadVariable (pos + 0, (uint16_t*)&gstTable[sel].nItem);
EE_ReadVariable (pos + 1, (uint16_t*)&gstTable[sel].step);
EE_ReadVariable (pos + 2, (uint16_t*)&gstTable[sel].max_val);
for(int i=0; i<MAX_TABLE_ITEM_NO; i++) {
EE_ReadVariable (pos + 10 + i, (uint16_t*)&gstTable[sel].table[i]);
}
if(gstTable[sel].nItem == 0 || gstTable[sel].nItem > 100) {
gstTable[sel].nItem = 71;
gstTable[sel].step = 10;
gstTable[sel].max_val = 100;
}
}
void write_eeprom_table(uint8_t sel)
{
if(sel >= 2) return;
int pos = 40;
HAL_FLASH_Unlock();
EE_WriteVariable (pos + 0, gstTable[sel].nItem);
EE_WriteVariable (pos + 1, gstTable[sel].step);
EE_WriteVariable (pos + 2, gstTable[sel].max_val);
for(int i=0; i<MAX_TABLE_ITEM_NO; i++) {
EE_WriteVariable (pos + 10 + i, gstTable[sel].table[i]);
}
HAL_FLASH_Lock();
}
2025-05-16 3:51 AM
Please see How to insert source code - the indentation is all lost now, so you'll need to copy again from your original.
You can edit the post:
I don't see in that code where you set the location of your emulated EEPROM within the Flash ?
Also, how much of the Flash is used by your code?
2025-05-16 5:12 AM
Hi Andrew
OK. Thank you
Below is all of the code I’m using with the EEPROM emulation library. Are there any parts here that could be causing problems?
I would greatly appreciate it if you could let me know how to resolve this.
void read_eeprom()
{
EE_ReadVariable (0x0001, (uint16_t*)&gstSystem.nNumOfBlock);
EE_ReadVariable (0x0002, (uint16_t*)&gstMain.stRep.nInputVccMax);
EE_ReadVariable (0x0003, (uint16_t*)&gstMain.stRep.nInputVccMin);
EE_ReadVariable (0x0004, (uint16_t*)&gstMain.stRep.bBiastInOn);
EE_ReadVariable (0x0005, (uint16_t*)&gstMain.stRep.bBiastOut1On);
EE_ReadVariable (0x0006, (uint16_t*)&gstMain.stRep.bBiastOut2On);
EE_ReadVariable (0x0007, (uint16_t*)&gstMain.stRep.nTempMax);
EE_ReadVariable (0x0008, (uint16_t*)&gstMain.stRep.bShutdownOn);
EE_ReadVariable (0x0009, (uint16_t*)&gstMain.stRep.bAmpOn);
EE_ReadVariable (0x0010, (uint16_t*)&gstMain.stRep.bAlcOn);
EE_ReadVariable (0x0011, (uint16_t*)&gstMain.stRep.nOutputLevelMax);
EE_ReadVariable (0x0012, (uint16_t*)&gstMain.stRep.nAlcLevel);
EE_ReadVariable (0x0013, (uint16_t*)&gstMain.stRep.nAttenIn);
EE_ReadVariable (0x0014, (uint16_t*)&gstMain.stRep.nAttenOut);
EE_ReadVariable (0x0015, (uint16_t*)&gstMain.stRep.nAttenL1);
EE_ReadVariable (0x0016, (uint16_t*)&gstMain.stRep.nAttenL2);
}
void write_eeprom()
{
HAL_FLASH_Unlock();
EE_WriteVariable (0x0001, gstSystem.nNumOfBlock);
EE_WriteVariable (0x0002, gstMain.stRep.nInputVccMax);
EE_WriteVariable (0x0003, gstMain.stRep.nInputVccMin);
EE_WriteVariable (0x0004, gstMain.stRep.bBiastInOn);
EE_WriteVariable (0x0005, gstMain.stRep.bBiastOut1On);
EE_WriteVariable (0x0006, gstMain.stRep.bBiastOut2On);
EE_WriteVariable (0x0007, gstMain.stRep.nTempMax);
EE_WriteVariable (0x0008, gstMain.stRep.bShutdownOn);
EE_WriteVariable (0x0009, gstMain.stRep.bAmpOn);
EE_WriteVariable (0x0010, gstMain.stRep.bAlcOn);
EE_WriteVariable (0x0011, gstMain.stRep.nOutputLevelMax);
EE_WriteVariable (0x0012, gstMain.stRep.nAlcLevel);
EE_WriteVariable (0x0013, gstMain.stRep.nAttenIn);
EE_WriteVariable (0x0014, gstMain.stRep.nAttenOut);
EE_WriteVariable (0x0015, gstMain.stRep.nAttenL1);
EE_WriteVariable (0x0016, gstMain.stRep.nAttenL2);
HAL_FLASH_Lock();
}
void read_eeprom_table(uint8_t sel)
{
if(sel >= 2) return;
int pos = 40;
EE_ReadVariable (pos + 0, (uint16_t*)&gstTable[sel].nItem);
EE_ReadVariable (pos + 1, (uint16_t*)&gstTable[sel].step);
EE_ReadVariable (pos + 2, (uint16_t*)&gstTable[sel].max_val);
for(int i=0; i<MAX_TABLE_ITEM_NO; i++) {
EE_ReadVariable (pos + 10 + i, (uint16_t*)&gstTable[sel].table[i]);
}
if(gstTable[sel].nItem == 0 || gstTable[sel].nItem > 100) {
gstTable[sel].nItem = 71;
gstTable[sel].step = 10;
gstTable[sel].max_val = 100;
}
}
void write_eeprom_table(uint8_t sel)
{
if(sel >= 2) return;
int pos = 40;
HAL_FLASH_Unlock();
EE_WriteVariable (pos + 0, gstTable[sel].nItem);
EE_WriteVariable (pos + 1, gstTable[sel].step);
EE_WriteVariable (pos + 2, gstTable[sel].max_val);
for(int i=0; i<MAX_TABLE_ITEM_NO; i++) {
EE_WriteVariable (pos + 10 + i, gstTable[sel].table[i]);
}
HAL_FLASH_Lock();
}
2025-05-16 5:17 AM
You still haven't shown where you set the location of your emulated EEPROM within the Flash.
Also, how much of the Flash is used by your code?
Are you using the correct EEPROM Emulation library?
You need AN2594 for STM32F1 - not X-CUBE-EEPROM:
2025-05-18 5:41 PM
Hello,
You mentioned that I need to predefine the Flash region to be used for EEPROM emulation.
I haven’t configured this yet.
I plan to add the following definitions to my eeprom.h file to reserve approximately 4 KB of Flash
#define ADDR_FLASH_PAGE_252 ((uint32_t)0x0803F000) /* Base @ of Page 124, 1 Kbytes */
#define ADDR_FLASH_PAGE_254 ((uint32_t)0x0803F800) /* Base @ of Page 126, 1 Kbytes */
/* Define the size of the sectors to be used */
#define PAGE_SIZE (uint32_t)FLASH_PAGE_SIZE /* Page size */
/* EEPROM start address in Flash */
#define EEPROM_START_ADDRESS ((uint32_t)ADDR_FLASH_PAGE_252) /* EEPROM emulation start address */
/* Pages 0 and 1 base and end addresses */
#define PAGE0_BASE_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + 0x0000))
#define PAGE0_END_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1)))
#define PAGE0_ID ADDR_FLASH_PAGE_252
#define PAGE1_BASE_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + 0x800))
#define PAGE1_END_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + 0x800 + PAGE_SIZE - 1))
#define PAGE1_ID ADDR_FLASH_PAGE_254
/* Used Flash pages for EEPROM emulation */
#define PAGE0 ((uint16_t)0x0000)
#define PAGE1 ((uint16_t)0x0002)
With this configuration, will the EEPROM emulation code I provided earlier run correctly without issues?
Thank you!