cancel
Showing results for 
Search instead for 
Did you mean: 

trying to ''preinit'' EEPROM data in C code with Cosmic STM8 compiler

evh
Associate
Posted on August 24, 2010 at 17:23

trying to 'preinit' EEPROM data in C code with Cosmic STM8 compiler

8 REPLIES 8
lowpowermcu
Associate II
Posted on May 17, 2011 at 15:10

Hi Vincent,

I have used variable in data eeprom. In my main.c I have declared it as following:

@eeprom char my_var = 10;

When I check the eeprom with view memory I can find it.

I don't understand why you are using the linker file ?

A+,

MCU Lüfter

luca239955_stm1_st
Senior II
Posted on May 17, 2011 at 15:10

eeprom is usually used for variables whose value can can change, but must be kept when powering off the application... your use is somewhat atypical, but perfectly ok if it so fits you.

Regards,

Luca

luca239955_stm1_st
Senior II
Posted on May 17, 2011 at 15:10

Hello,

a couple of notes on the two posts above:

1) to the original poster: if the ''variables'' you are declaring are really constants (as suggeted by the syntax you post for the other processor), what's the point of putting them in eeprom intead of FLASH ?

2) to lowpowermcu: take care with the example you posted: as it is written, it should mean that your variable is initialized to 10 every time you restart your application, which is probably not what you really want (otherwise a normal variable in RAM would be enough). The linker ''understand'' this and does not initialize your variable at startup -> if you actually see the value 10 in it, it's because it has been written there is some other way (for example an explicit write in the code, or by the flashing sofwtare, but I think that's not its default behaviour).

Regards,

Luca (Cosmic)

lowpowermcu
Associate II
Posted on May 17, 2011 at 15:10

Hi ubiali,

I am using eeprom to minimize using ram and flash and since my variable is constant I would like to profit from eeprom availabilty in the device.

Otherwise when I should use the epprom ? and why it is implemented in the

device ?

Regards,

MCU Lüfter

wolfgang239955_stm1_st
Associate II
Posted on May 17, 2011 at 15:10

Hi,

I have to set my EEPROM to default values when producing my electronic devices. As already mentioned, it would be very helpful to set the EEPROM all in one, when programming the µC.

All known programming tools will supply this feature when the linker will bring out the specific EEPROM datas. Some known compiler/linker tools are able to satisfy it.

How can we do this with Cosmic tools?

Regards, WoRo

luca239955_stm1_st
Senior II
Posted on May 17, 2011 at 15:10

Hi WoRo,

in order to achieve what you want you can do the following in your linker file

+seg .eeprom -b 0x4000 -m0x200 -f0xaa

where

-b : eeprom start address

-m: eeprom size

-f: fill value for those eeprom variables that are not initialized

in this way, the .hex file will contain, for the eeprom addresses, either the value you initialize your variables at (see example above) or the fill value -> if the programming tool is enabled to program the eeprom, the content is there in the hex file.

Note that the typical linker file to be used with an eeprom device (including the one produced by STVD, if I remember well), should contain a -c flag like

+seg .eeprom -b 0x4000 -c

that will tell the linker not to produce any output for this segment (since you usually want the eeprom to contain values from the previous execution, and not ''first time'' values); remove the -c for your needs

Regards,

Luca (Cosmic)

lowpowermcu
Associate II
Posted on May 17, 2011 at 15:10

Hi Luca,

I have understood the difference between initializing variable using compiler and using linker but there is still one point.

How through the linker which isn't loaded in the MCU will detect after reset that the eeprom has been already initialized ?

I hope I am clear.

MCU Lüfter

evh
Associate
Posted on May 17, 2011 at 15:10

I use EEprom because data can be change by the user during the running of the program.

Also, I figured that I was doing everithing correctly... except that I needed to directly access the variable in the code to have the linker set the pre init data in the s19 file.

What I was doing, for code portability was using the Readflash function.

First off, I declare the variable in the myeeprom section :

#pragma section @eeprom @near {myeeprom} 

#pragma space @eeprom 

   

   UBYTE_8 ubyConfigByte;

 ...

#pragma space 

#pragma section {}

I then define a function that read the data that I use in my code ( ADD_CFGBYTE is also a define and need to be at the correct address, it's easy to find since the linker will assigned the adress in the order they appear in the declaration segment)

#define CONFIG_BYTE   (FLASH_ReadByte(ADD_CFGBYTE))      // Read CONFIG BYTE

Then in the code, I simply use CONFIG_BYTE to get the value

example : if ((CONFIG_BYTE & BITS_ERR) == ERR_TEMP) ...

By doing that, it seems that the linker will flush the variable declaration because it think it's not used.  In order to ''enable'' the linker to pre init the section, I simply had to put it in the code like that :

volatile unsigned char tmpData = CONFIG_BYTE;

even if I have a lot of other variable, only this previous line will permit the linker to set the preinit value for all the data in the section.

If someone has an idea on how not to use the line before.. i'll take it ;)

regards.