EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Writing Variables to Flash?

Started by David Troike March 19, 2005

I need to write a few variables to flash. Or I think I do?

What I need to do is initialize a variable only once on the first
power up ever. Then once the options are set up powering down the
unit will retain these values always unless changed by the user.

Normally I would set this variable in an EEPROM when burning the
micro then just read the variable from the EEPROM always. If the
variable is changed it just writes it to the EEPROM.

I looked at some of the examples of how to save to the flash, but did
not understand them. I am new to C in general and much newer to the
Rabbit. All my previous work was done in assembler for 8051's and
PIC's



--- In rabbit-semi@rabb..., "David Troike" <dave@m...> wrote:
>
> I need to write a few variables to flash. Or I think I do?
>
> What I need to do is initialize a variable only once on the first
> power up ever. Then once the options are set up powering down the
> unit will retain these values always unless changed by the user.

Here's what I do. I just wrote this sample, didn't test it:

#define FACTORYVALUE 0 // or whatever value you want

// While developing, change this string to force superinitialization
#define INITFLAG "This string gets written to User Block offset 0"

//compiler puts this in flash
const int flashvariableTORYVALUE;

int runtimeVariable;
char buffer[sizeof(INITFLAG)];

// On program startup:

// Check if we need SuperInitialization (1rst run)
if(!(readUserBlock(buffer, 0, sizeof(INITFLAG)))) {
if(memcmp(buffer, INITFLAG, sizeof(INITFLAG))) {

memcpy(buffer, INITFLAG, sizeof(INITFLAG));
writeUserBlock(0, buffer, sizeof(INITFLAG));
// put variable value after init string
writeUserBlock(sizeof(INITFLAG),&flashvariable,sizeof
(flashvariable));

}
}
else{

// Not first run, just read variable
readUserBlock(runtimeVariable,sizeof(INITFLAG),sizeof
(runtimeVariable));

}

if the value gets changed, then rewrite the userblock:

writeUserBlock(sizeof(INITFLAG),&runtimeVariable,sizeof
(runtimeVariable));
>
> Normally I would set this variable in an EEPROM when burning the
> micro then just read the variable from the EEPROM always. If the
> variable is changed it just writes it to the EEPROM.
>
> I looked at some of the examples of how to save to the flash, but
did
> not understand them. I am new to C in general and much newer to
the
> Rabbit. All my previous work was done in assembler for 8051's and
> PIC's



I read your interesting post.

I need to keep "syncronized" a variable with a persistent copy in
non-volatile memory.

If the application halts / there is a power failure, when the Rabbit board
will restart it must read the last value of the variable.

The variable is updated about once per minute. Is writeUserBlock() the right solution ? ciao, Massimo ----- Original Message -----
From: "bmurthazw" <bmurthazw@bmur...>
To: <rabbit-semi@rabb...>
Sent: Saturday, March 19, 2005 9:11 PM
Subject: [rabbit-semi] Re: Writing Variables to Flash? > Here's what I do. I just wrote this sample, didn't test it:
>
> #define FACTORYVALUE 0 // or whatever value you want
>
> // While developing, change this string to force superinitialization
> #define INITFLAG "This string gets written to User Block offset 0"
>
> //compiler puts this in flash
> const int flashvariableTORYVALUE;
>
> int runtimeVariable;
> char buffer[sizeof(INITFLAG)];
>
> // On program startup:
>
> // Check if we need SuperInitialization (1rst run)
> if(!(readUserBlock(buffer, 0, sizeof(INITFLAG)))) {
> if(memcmp(buffer, INITFLAG, sizeof(INITFLAG))) {
>
> memcpy(buffer, INITFLAG, sizeof(INITFLAG));
> writeUserBlock(0, buffer, sizeof(INITFLAG));
> // put variable value after init string
> writeUserBlock(sizeof(INITFLAG),&flashvariable,sizeof
> (flashvariable));
>
> }
> }
> else{
>
> // Not first run, just read variable
> readUserBlock(runtimeVariable,sizeof(INITFLAG),sizeof
> (runtimeVariable));
>
> }
>
> if the value gets changed, then rewrite the userblock:
>
> writeUserBlock(sizeof(INITFLAG),&runtimeVariable,sizeof
> (runtimeVariable));





--- In rabbit-semi@rabb..., "Tecnowatt - Massimo Sala"
<massimo.sala@t...> wrote:
> I read your interesting post.
>
> I need to keep "syncronized" a variable with a persistent copy in
> non-volatile memory.
>
> If the application halts / there is a power failure, when the
Rabbit board
> will restart it must read the last value of the variable.
>
> The variable is updated about once per minute. > Is writeUserBlock() the right solution ?

No, that is too frequent and will wear out the flash. Depending on
the part used, flash sectors could wear out in as few as 10,000
writes. Use battery backed RAM and 'protected' variables instead.
See reset_demo.c in the /samples folder.

>
>
> ciao, Massimo > ----- Original Message -----
> From: "bmurthazw" <bmurthazw@y...>
> To: <rabbit-semi@rabb...>
> Sent: Saturday, March 19, 2005 9:11 PM
> Subject: [rabbit-semi] Re: Writing Variables to Flash? > > Here's what I do. I just wrote this sample, didn't test it:
> >
> > #define FACTORYVALUE 0 // or whatever value you want
> >
> > // While developing, change this string to force
superinitialization
> > #define INITFLAG "This string gets written to User Block offset
0"
> >
> > //compiler puts this in flash
> > const int flashvariableTORYVALUE;
> >
> > int runtimeVariable;
> > char buffer[sizeof(INITFLAG)];
> >
> > // On program startup:
> >
> > // Check if we need SuperInitialization (1rst run)
> > if(!(readUserBlock(buffer, 0, sizeof(INITFLAG)))) {
> > if(memcmp(buffer, INITFLAG, sizeof(INITFLAG))) {
> >
> > memcpy(buffer, INITFLAG, sizeof(INITFLAG));
> > writeUserBlock(0, buffer, sizeof(INITFLAG));
> > // put variable value after init string
> > writeUserBlock(sizeof(INITFLAG),&flashvariable,sizeof
> > (flashvariable));
> >
> > }
> > }
> > else{
> >
> > // Not first run, just read variable
> > readUserBlock(runtimeVariable,sizeof(INITFLAG),sizeof
> > (runtimeVariable));
> >
> > }
> >
> > if the value gets changed, then rewrite the userblock:
> >
> > writeUserBlock(sizeof(INITFLAG),&runtimeVariable,sizeof
> > (runtimeVariable));



Does using static variables solve your problem?

If you have a static variable declared and then the
board resets, it keeps its last value. I am not sure,
but I guess only changing the variable's value does
not wear out the flash.

> From: "Tecnowatt - Massimo Sala"
>
> I read your interesting post.
>
> I need to keep "syncronized" a variable with a
> persistent copy in
> non-volatile memory.
>
> If the application halts / there is a power failure,
> when the Rabbit board
> will restart it must read the last value of the
> variable.
>
> The variable is updated about once per minute.

_____________________________________________
Daniel Teixeira Chaves
Engenheiro de Controle e Automacao
"The squid is not dead"
__________________________________



> Does using static variables solve your problem?
>
> If you have a static variable declared and then the
> board resets, it keeps its last value.

When it resets, yes, but not when it powers down. Static variables are
in ram, and it needs power to stay there.

> I am not sure,
> but I guess only changing the variable's value does
> not wear out the flash.

That's true, because it's not in the flash. :o)

Maurits.
--

HiTECHnologies Industrial Automation B.V.
Industrieweg 30, 3401 MA, IJsselstein, The Netherlands
P.O. box 242, 3400 AE, IJsselstein, The Netherlands
Tel: +31 30 6875335
Fax: +31 30 6875333
E-mail: info@info...
Web: www.hitechnologies.nl

===================================================================
The information contained in this communication is confidential and may
be legally privileged. It is intended solely for the use of the
individual or entity to whom it is addressed and others authorized to
receive it. If you are not the intended recipient you are hereby
notified that any disclosure, copying, distribution or taking any action
in reliance on the contents of this information is strictly prohibited
and may be unlawful.
HiTECHnologies B.V. is neither liable for the contents, nor for the
proper, complete and timely transmission of the information contained in
this communication.
===================================================================



Sorry, I assumed you have the backup-battery connected and charged.

--- In rabbit-semi@rabb..., Maurits van de Kamp <mvk@h...> wrote:

> When it resets, yes, but not when it powers down. Static variables are
> in ram, and it needs power to stay there.





> Sorry, I assumed you have the backup-battery connected and charged.

Yes, that would be a solution. :o)

(I'm not the original poster though)

Maurits. --

HiTECHnologies Industrial Automation B.V.
Industrieweg 30, 3401 MA, IJsselstein, The Netherlands
P.O. box 242, 3400 AE, IJsselstein, The Netherlands
Tel: +31 30 6875335
Fax: +31 30 6875333
E-mail: info@info...
Web: www.hitechnologies.nl

===================================================================
The information contained in this communication is confidential and may
be legally privileged. It is intended solely for the use of the
individual or entity to whom it is addressed and others authorized to
receive it. If you are not the intended recipient you are hereby
notified that any disclosure, copying, distribution or taking any action
in reliance on the contents of this information is strictly prohibited
and may be unlawful.
HiTECHnologies B.V. is neither liable for the contents, nor for the
proper, complete and timely transmission of the information contained in
this communication.
===================================================================


So if static variables are stored in RAM, and their values does not change as long the power there, what does this function do and why do we need to call it at beginning of the code to recover variables   _sysIsSoftReset()

 

Regards,
Hisham Abed

From: Maurits van de Kamp [mailto:m...@hitechnologies.nl]
Sent: Wednesday, March 30, 2005 7:06 AM
To: r...@yahoogroups.com
Subject: Re: [rabbit-semi] Re: Writing Variables to Flash?

 

> Does using static variables solve your problem?
>
> If you have a static variable declared and then the
> board resets, it keeps its last value.

When it resets, yes, but not when it powers down. Static variables are
in ram, and it needs power to stay there.

> I am not sure,
> but I guess only changing the variable's value does
> not wear out the flash.

That's true, because it's not in the flash. :o)

Maurits.
--

HiTECHnologies Industrial Automation B.V.
Industrieweg 30, 3401 MA, IJsselstein, The Netherlands
P.O. box 242, 3400 AE, IJsselstein, The Netherlands
Tel: +31 30 6875335
Fax: +31 30 6875333
E-mail: i...@hitechnologies.nl
Web: www.hitechnologies.nl

==================================================================
The information contained in this communication is confidential and may
be legally privileged. It is intended solely for the use of the
individual or entity to whom it is addressed and others authorized to
receive it. If you are not the intended recipient you are hereby
notified that any disclosure, copying, distribution or taking any action
in reliance on the contents of this information is strictly prohibited
and may be unlawful.
HiTECHnologies B.V. is neither liable for the contents, nor for the
proper, complete and timely transmission of the information contained in
this communication.
===================================================================
           






--- In rabbit-semi@rabb..., "H Abed" <mesho@s...> wrote:
> So if static variables are stored in RAM, and their values does not
change
> as long the power there, what does this function do and why do we
need to
> call it at beginning of the code to recover variables
_sysIsSoftReset()

It gives an extra level of protection for non-atomic writes that
might be happening at reset or power down. Also, in some boards such
as the RCM32xx, RCM33xx and BL2600, variables normally go in non-
battery backed fast RAM. The 'bbram' keyword would be adequate for
char variables there, but 'protected' would provide extra safety for
multibyte variables. - Brian

>
> Regards,
> Hisham Abed >
> _____
>
> From: Maurits van de Kamp [mailto:mvk@h...]
> Sent: Wednesday, March 30, 2005 7:06 AM
> To: rabbit-semi@rabb...
> Subject: Re: [rabbit-semi] Re: Writing Variables to Flash? >
> > Does using static variables solve your problem?
> >
> > If you have a static variable declared and then the
> > board resets, it keeps its last value.
>
> When it resets, yes, but not when it powers down. Static variables
are
> in ram, and it needs power to stay there.
>
> > I am not sure,
> > but I guess only changing the variable's value does
> > not wear out the flash.
>
> That's true, because it's not in the flash. :o)
>
> Maurits.
> --
>
> HiTECHnologies Industrial Automation B.V.
> Industrieweg 30, 3401 MA, IJsselstein, The Netherlands
> P.O. box 242, 3400 AE, IJsselstein, The Netherlands
> Tel: +31 30 6875335
> Fax: +31 30 6875333
> E-mail: info@h...
> Web: www.hitechnologies.nl
>
> ===================================================================
> The information contained in this communication is confidential and
may
> be legally privileged. It is intended solely for the use of the
> individual or entity to whom it is addressed and others authorized
to
> receive it. If you are not the intended recipient you are hereby
> notified that any disclosure, copying, distribution or taking any
action
> in reliance on the contents of this information is strictly
prohibited
> and may be unlawful.
> HiTECHnologies B.V. is neither liable for the contents, nor for the
> proper, complete and timely transmission of the information
contained in
> this communication.
> =================================================================== >
>
<http://us.ard.yahoo.com/SIG9nqfk2e/M)8184.6018725.7038619.300117
6/D=gr
>
oups/S06554205:HM/EXP12270761/A%93423/R=0/SIGel9gslf/*http:
/www.n
> etflix.com/Default?mqso`190075> click here >
> <http://us.adserver.yahoo.com/l?
M)8184.6018725.7038619.3001176/D=groups/S=
> :HM/A%93423/randd7710532 >
> _____
>
> > Terms of Service.




Memfault Beyond the Launch