Jon V
Jon V

Reputation: 536

Write default values to eeprom

This is my Setup:

Atmel studio 7.0
avr-gcc
ATmega644PA

I'm trying to write default values to the eeprom. I mean: in code, predefined values at a specific place in the eeprom.

I can use:

__attribute__((section(".eeprom")))

Which works to put the variables in the eeprom, but this links the variables automatic to a location. I want to manually set the location where the variable is stored. How can I do this?

Thanks!

Ps: this needs to work for >300 variables

Upvotes: 0

Views: 2049

Answers (3)

Julien
Julien

Reputation: 1910

The solution depends whether you want to intialise the EEPROM on programing time or at each startup of the device. I will describe the solution for programming time (It is what I understand from the question).

The __attribute__((section(".eeprom"))) will say to the linker : "put this variable in the .eep output file". The .eep file is a intel hex file format that will be flashed to EEPROM. If you want to define specific location for your variable you could either generate and flash the .eep file manually or do a complete EEPROM map letting 0xFF for uninitialised values :

__attribute__((section(".eeprom"))) uint8_t eepContent[6] = {0,1,0xFF,3,4,5};

You could also manually define sections but this is only convenient if you want to use whole block of EEPROM memory not located at the beginning.

Upvotes: 0

fisehara
fisehara

Reputation: 176

you may encapsulate your need. E.g. you may use this ASF API: http://asf.atmel.com/docs/3.22.0/xmegae/html/nvm_8h.html

Then write your default values into the EEPROM plus an identifier that your init routine has already set the default or not. Pseudo-code:

if defaultWritten == true
myAppDataStruct = readFromEeprom()
else
initEeprom(yourDesiredAddress, myDefaultAppDataStruct)

You then use the myAppDataStruct as the representation of you EEPROM data.

Otherwise, you may use the approach of declaring the variables into the data section ".eeprom" but than the linker will align it for you.

Upvotes: 0

Gabriel Cséfalvay
Gabriel Cséfalvay

Reputation: 539

You can place all variables in one struct. The variables will be placed in EEPROM in the specified order.

struct {
   uint8_t  var1;
   uint8_t  var2;
   uint16_t var3;
   ...
} eeprom_data  __attribute__ ((section(".eeprom")));

Upvotes: 2

Related Questions