cancel
Showing results for 
Search instead for 
Did you mean: 

Save initial EEPROM data when burning MCU

PiSquared
Associate II

When initially burning the MCU, I want to save some EEPROM data. Lets say the data 123 is to be saved in location 1 of EEPROM. If I write the below C code before the main function, will it work? Once the data is stored the first time the program is burnt into the MCU, I do not want it to be re-saved every time the program is run.

void EEPROM_InitData()

{

uint8_t data = 123;

FLASH_ProgramByte((FLASH_DATA_EEPROM_START_PHYSICAL_ADDRESS + 1) , data);

}

Thanks for your help!

PiSquared

5 REPLIES 5
JoniS
Senior

Usually the eeprom is full of 0x00 or 0xFF, or any other known state when it's new.

I always use that knowledge and reserve one byte just for that, when you start the program first time and read X byte you know it should be contain value Y, If it does, store your initial values to eeprom and change that one check byte as something else, now next time your program runs it knows the eeprom is modified allready by reading byte X and you can continue without writing initial values.

PiSquared
Associate II

This is the solution I want to avoid!

I want a way to initialize an EEPROM address with a value when the MCU is burnt, not when the program is running ...

Piranha
Chief II

In a real product you need the ability to reset the configuration data to default values. That requires configuration data initialization code anyway. Just combine it with JoniS's idea and use the same code for both - initialization after production and reset by user.

PiSquared
Associate II

The below link describes how to initialise EEPROM at program download time (compiler specific):

https://www.iar.com/support/tech-notes/compiler/initializing-the-eeprom-at-program-download/

A constant array is saved into EEPROM, byte by byte, I think. If the array has as many elements as the number of EEPROM bytes, then EEPROM location zero is initialised to array[0], EEPROM location 1 is initialised to array[1], all the way until the very last EEPROM location/array element. This way one can be sure of the initial value of every EEPROM locations. If you only cared about location[7] say then you can define array[7] as required, all other array elements can be set to 0.

Does the above approach work? What about other compilers?

Hi all,

How can I find out what the default EEPROM values are for an STM8 MCU? Just having quickly checked the datasheet, not sure information is in there (it does say registers are reset to 0, so presumably EEPROM/FLASH have 0 default values?). I read that PIC has a default value of 1 (0xFF for each byte) ...