Topic : EEPROM - HowTo?

Forum : ST7/STM8

Original Post
Post Information Post
August 2, 2010 - 4:28am
Guest

Greetings:

I am using the Raisonance STM8 compiler under STVD with the STM8S Discovery board equipped with the ST8S105C6 MPU. I have encountered a problem getting my application to write to EEPROM. I have not been able to find any examples to follow but I think it should be straight forward as my understanding is that "eeprom" declared variables are simply treated the same as RAM variables.

I have declared an eeprom variable as follows:

float eeprom Cstd_eep = 461.5e-12;

Initialization of Cstd_eep by the STVD debug programming tool is confirmed correct by the debugger memory dump. A four byte value at address 0x4000 consisting of "2F FD B6 5A" appears. If I place the variable within the Watch window it indicates the correct value assigned to Cstd_eep.

My application attempts to write to Cstd_eep using the following statement:

Cstd_eep = Cstd;

where Cstd = 462.4 e-12

The Cstd_eep varible in eeprom does not change. There are not any error or warning messages.

Is there anything obvious that I am doing wrong? Can anyone here point me to documentation with examples that would get me on the right track?

Is there a need to unlock the EEPROM or are there some library functions that I need to use that I am not aware of?

Thanks in advance for any help!

tma

Replies
Post Information Post
+1
0
-1
August 3, 2010 - 11:02pm
Guest

Greetings:

I finally found a method for writing to EEPROM on my own. As I never found any documentation that provided a recommended example I do not know if this is the best procedure or not but it does seem to work. I decided to post an example:

#include "stm8s_flash.h"

float eeprom Cstd_eep = 461.5e-12; // changed with calibration

FLASH_Unlock(FLASH_MEMTYPE_DATA);
Cstd_eep = Cstd;
FLASH_Lock(FLASH_MEMTYPE_DATA);

I hope this example is helpful for others. If someone in this forum notices something wrong with it or knows a better method please let the rest of us know.

Thanks in advance!

Best Regards,
Tom Alldread

+1
0
-1
August 5, 2010 - 10:46am
Guest

Hi, Raisonance compiler provide EEPROM writing support with EEPROM.c and EEPROM.h

I normally write to EEPROM for backup some variable, to avoid blackout lost of data.

There are:

void EEPROM_Read (unsigned char * PtrToUsrBuffer, unsigned char NbOfBytes, unsigned char data * PtrToE2Buffer)
void EEPROM_Write (unsigned char * PtrToUsrBuffer, unsigned char NbOfBytes, unsigned char data * PtrToE2Buffer)
Prog_Status EEPROM_Programming (void);

Quote:

long dayTime = 0;
long eeprom dayTime_backup = 0;

//for reading: long is 4*8bit
EEPROM_Read(&dayTime, 4, &dayTime_backup);

//for writing: when writing you must wait for EEPROM_Programming()!=0
if(dayTime == ((dayTime/EEPROM_BACKITUP_EVERYNSECS)*EEPROM_BACKITUP_EVERYNSECS)){
DisableInterrupts;
EEPROM_Write(&dayTime, 4, &dayTime_backup);
do{
eepromFlag = EEPROM_Programming();
} while (eepromFlag != 0x00);
EnableInterrupts;
}

+1
0
-1
August 8, 2010 - 6:38pm
Guest

Greetings GremlinC5:

Thank you for your detailed reply!

I looked up eeprom.c and then realized it is intended for external I2C EEPROM devices. I also then realized my post did not clearly specify ~internal~ EEPROM. My example given above is for the use of the internal EEPROM area of the STM105C6 on the Discovery board which is not factory equipped with an external EEPROM chip. However there is sufficient prototype area to add an external EEPROM thus it is certainly understandable why you thought I was referring to an I2C chip.

It is good to know how there is a path already blazed for the use external I2C EEPROM. Thanks again for your very comprehensive reply!

Best Regards,
Tom Alldread

+1
0
-1
August 9, 2010 - 11:15am
Guest

tma wrote:
Greetings GremlinC5:

Thank you for your detailed reply!

I looked up eeprom.c and then realized it is intended for external I2C EEPROM devices. I also then realized my post did not clearly specify ~internal~ EEPROM. My example given above is for the use of the internal EEPROM area of the STM105C6 on the Discovery board which is not factory equipped with an external EEPROM chip. However there is sufficient prototype area to add an external EEPROM thus it is certainly understandable why you thought I was referring to an I2C chip.

It is good to know how there is a path already blazed for the use external I2C EEPROM. Thanks again for your very comprehensive reply!

Best Regards,
Tom Alldread

My example is for writing and reading the internal EEPROM of a ST7FLITE39. Raisonance library use the internal EEPROM.

I think you should re-read the .c and .h files.

My Raisonance EEPROM.c use the register EECSR of the ST7FLITE39 to control read/write actions. A FOR cycle read/write the data to the latches. So you must wait until the latches internal to the uController will write the right EEPROM area; you have for this the:

Prog_Status EEPROM_Programming (void);

that return the status...

I hope this help, if you need the internal EEPROM those are what you need ;)

If you need to use a I2C chip, I dont know :( I never have to use that

+1
0
-1
August 9, 2010 - 5:36pm
Guest

Greetings GremlinC5:

I am sorry I think I found a different EEPROM.c file. The one I found was in the Raisonance example directory within the RIDE download package. It is all about I2C. The file description is the following:

* DESCRIPTION:
*
* This file contains the REva EEPROM (M24C01 chip on I2C bus) handling
* functions.
*

Please point me to the EEPROM.c file your referring to. Should I look on the Raisonance website?

I guess EEPROM.c is a common name for an eeprom file, (:/)).

Thanks!

Best Regards,
Tom Alldread

+1
0
-1
March 21, 2011 - 10:28pm
Guest

Hi,

I'm now using the Reva 3.3 with the STM8S105 board...

I've problem with the code:

Quote:

u16 dayTimeInThetaSeconds = 100;
u16 eeprom dayTime_backup = 10;

DisableInterrupts();
FLASH_Unlock(FLASH_MEMTYPE_DATA);
dayTimeInThetaSeconds = FLASH_ReadInt(&dayTime_backup);
FLASH_Lock(FLASH_MEMTYPE_DATA);
EnableInterrupts();

It enter in the ASSERT macro in FLASH_ReadInt() function because (&dayTime_backup == 0x03)!!! It'seems to be a char...

Quote:

u16 FLASH_ReadInt(u32 Address)
{
/* Check parameter */
assert_param(IS_FLASH_ADDRESS_OK(Address));
return(*((PointerAttr u16*) Address)); /* Read byte */
}

assert_param(IS_FLASH_ADDRESS_OK(Address))
want an address between:
((u32)0x004000)
and:
((u32)0x0043FF)

How could I force compiler to reach that area? Why "eeprom" in the variable definition isn't right?

I've tried to also:
u16 near dayTime_backup = 10;
u16 far dayTime_backup = 10;
with the same (&dayTime_backup == 0x03)

Thank you.

+1
0
-1
March 21, 2011 - 11:41pm
Guest

GremlinC5 wrote:
Hi,

I'm now using the Reva 3.3 with the STM8S105 board...

I've problem with the code:

Quote:

u16 dayTimeInThetaSeconds = 100;
u16 eeprom dayTime_backup = 10;

DisableInterrupts();
FLASH_Unlock(FLASH_MEMTYPE_DATA);
dayTimeInThetaSeconds = FLASH_ReadInt(&dayTime_backup);
FLASH_Lock(FLASH_MEMTYPE_DATA);
EnableInterrupts();

It enter in the ASSERT macro in FLASH_ReadInt() function because (&dayTime_backup == 0x03)!!! It'seems to be a char...

Quote:

u16 FLASH_ReadInt(u32 Address)
{
/* Check parameter */
assert_param(IS_FLASH_ADDRESS_OK(Address));
return(*((PointerAttr u16*) Address)); /* Read byte */
}

assert_param(IS_FLASH_ADDRESS_OK(Address))
want an address between:
((u32)0x004000)
and:
((u32)0x0043FF)

How could I force compiler to reach that area? Why "eeprom" in the variable definition isn't right?

I've tried to also:
u16 near dayTime_backup = 10;
u16 far dayTime_backup = 10;
with the same (&dayTime_backup == 0x03)

Thank you.

Fuuu...

That code work... Raisonance compiler do it's job... I don't -> Raisonance compiler could not put the "u16 eeprom" at 0x4000 if defined into the main!!! "near" "far" do nothing if are defined in the main.

So, the rule is: all "eeprom" must be globals? is it right?

Thank you.

Michele.

+1
0
-1
March 22, 2011 - 10:30am
Raisonance Support Team

Hi Michele,

Yes you are right: eeprom-qualified variables must be global.
But you can declare them as static within a function, which will work (Do not forget the initializer in such a case).

Also be careful about your programming options in Ride7 or STVD: They must explicitly write the EEPROM upon programming time.

Regards,