EmbeddedRelated.com
Forums
Memfault State of IoT Report

Writing Variables to Flash?

Started by David Troike March 19, 2005

Using an Example at Z-World I wrote this routine for saving user
variables to flash. These variables would only range once before
the customer placed the unit into service. They have to tailor
certian items and then let it run.

I have looked at many other people's code too and having a tough
time grasping the concepts for writing to flash.

Here is what I wrote but it does not work all the time. //*****************************************************************

//run-time variables copied to flash
typedef struct _run_time
{
unsigned long valid_flag;
char charPLC_Present_Flag;
char charCurrent_Level;
char charCurrent_Range;
unsigned int Alarm_hour;
unsigned int Alarm_minute;
} RTDATA;

// in main code read user block to ram and see if it is valid

//copy flash data to RAM.
readUserBlock(&RTData,0,sizeof(RTData));

// if data is not marked as valid, put in defaults
if (RTData.valid_flag!=0x55aaface)
{
RTData.charPLC_Present_Flag = 0;
RTData.charCurrent_Level = 0;
RTData.charCurrent_Range = 1;
RTData.Alarm_hour = 0;
RTData.Alarm_minute = 0;
RTData.valid_flag=0x55aaface;
writeUserBlock(0,(void *)&RTData,sizeof(RTData));
}

if (RTData.charPLC_Present_Flag != charPLC_Present_Flag)
{
RTData.charPLC_Present_Flag = charPLC_Present_Flag;
writeUserBlock(0,(void *)&RTData,sizeof(RTData));
}

/***************************************************

Go to User Setup Routine

***************************************************/

//Done with Setup Routine, save any changed data to flash.

if (RTData.charCurrent_Level != charCurrent_Level)
{
RTData.charCurrent_Level = charCurrent_Level;
writeUserBlock(0,(void *)&RTData,sizeof(RTData));
}

if (RTData.charCurrent_Range !=charCurrent_Range)
{
RTData.charCurrent_Range !=charCurrent_Range;
writeUserBlock(0,(void *)&RTData,sizeof(RTData));
}

if (RTData.Alarm_hour !=Alarm_hour)
{
RTData.Alarm_hour =Alarm_hour;
writeUserBlock(0,(void *)&RTData,sizeof(RTData));
}

if (RTData.Alarm_minute !=Alarm_minute)
{ RTData.Alarm_minute =Alarm_minute;
writeUserBlock(0,(void *)&RTData,sizeof(RTData));
}


I see this simple question is interesting for many people.

Many thanks for all you guys with your suggestions. Unfortunately I cannot
- add a battery to back-up the SRAM
- add FRAM
because the project is already installed at many customers sites, and my
boss said "we cannot modify the hardware". Moreover, I have a weird behaviour using the flash, sometimes after a power
off / power on of the Rabbit RCM2110 the data stored with writeUserBlock()
are gone.

I am working on this, adding more tracing to my code. I have also one doubt.
The RCM2110 core module is hosted on a board, developed by a contractor for
us.
I am not a electronic engineer, and the contractor isn't a Rabbit wizard.

Is it possible that some of the RCM2110 signals on J1 and J2 connectors,
linked to the motherboard, at power off / power on cause the flash to erase
(because the motherboard change their status) ?

The code in flash isn't erased, only the writeUserBlock() data.

Signals like:
SMODE1
SMODE0
/RESET
/RES_IN
STATUS
I know are used by the programming cable... ciao, Massimo



Memfault State of IoT Report