Retain Variable in Ram through Reset

Is there any way I can retain a long variable stored in ram, through a micro reset?

i would like my long variable that I am using to store time to be maintained.

Yes it never gets cleared from memory by the reset. However the trick is to stop your variable being initialised by your code as it starts again. How is your code going to distinguish between a reset and a power up?

Not in SRAM. Can put in EEPROM. Need to recognize power is going down, and have some millliSeconds of power left to complete the EEPROM write.

If have an RTC, like DS1307, there is battery backed SRAM to hold it.

I already have modified the boot loader to save the register which holds the power down reason. Do you know what is needed to have the variable not be initialized?

Does "attribute ((section (".noinit")))" work with AVR gcc?

Do you know what is needed to have the variable not be initialized?

That is more of a C programming question. However at a guess I would say use a pointer into ram. However how are you going to initialise it the first time. Why is the processor being reset anyway? You should not be writing code so that it requires to be reset.

Processor is being reset by an external device. (will be fixed in a later version of the hardware) For now the easiest fix for me would be to keep a check sum with the variable in RAM.

startup check variable with checksum if it is correct use the variable if not initialize to 0 while() { every second update variable and checksum }

attribute ((section (".noinit")))

Seems to Work! Thanks!

I was going to suggest adding a FRAM memory chip, fast access like serial SRAM but with EEPROM nonvolatility - thru hole parts have become ridicuously expensive. Small SMD part with a few k bytes to hold lots of info would be very little, just $1.40 for a 5V part. Pick up a DIP adapter from proto-advantage.com for easy breadboarding. http://www.mouser.com/Semiconductors/Memory/F-RAM/_/N-488wv?Keyword=fram+memory&FS=True&Ns=Pricing|0

How do you tell the attribute command what registers/area/whatever you want saved?

in the declarations example

unsigned long m_lRealTime attribute ((section (".noinit")));

so the 32-bit m_lRealTime would be the variable that is carried thru?

CrossRoads: I was going to suggest adding a FRAM memory chip, fast access like serial SRAM but with EEPROM nonvolatility - thru hole parts have become ridicuously expensive. Small SMD part with a few k bytes to hold lots of info would be very little, just $1.40 for a 5V part. Pick up a DIP adapter from proto-advantage.com for easy breadboarding. http://www.mouser.com/Semiconductors/Memory/F-RAM/_/N-488wv?Keyword=fram+memory&FS=True&Ns=Pricing|0

FM24C04 - 4Kb FRAM Serial Memory4K-bit (512 bytes) through-hole are available on eBay for a pretty reasonable cost I see two candidates... Qty 5, $8.99 Qty 10, $13.99

Both from China, free shipping.

so even a static global variable gets reinitialized by design? I had expected it to get initialized only on the 1st load of the program.

I'm now using attribute ((section (".noinit"))); accepting that the variable is uninitialized the first time after reloading the sketch.

bill2009: so even a static global variable gets reinitialized by design? I had expected it to get initialized only on the 1st load of the program.

I'm now using attribute ((section (".noinit"))); accepting that the variable is uninitialized the first time after reloading the sketch.

static used in file scope is very different than static used in block scope. It's quite a confusing keyword.

bill2009: so even a static global variable gets reinitialized by design? II'm now using attribute ((section (".noinit"))); accepting that the variable is uninitialized the first time after reloading the sketch.

The variable will loose its value with each power off, not only with reloading the sketch.

Schmidty1913: Is there any way I can retain a long variable stored in ram, through a micro reset?

i would like my long variable that I am using to store time to be maintained.

If it is time related: There are RTC modules which offer a couple of bytes RAM (backup battery buffered). I.e. the DS1307 module, operated by I2C, offers 56 bytes of RAM. Other than EEPROM, this RAM has unlimited write cycles.

Is your project using a RTC module? If yes, can you use aRTC module offering some bytes of RAM, like DS1307, maybe?

You can also use a FRAM memory chip. No battery needed. 3.3V, 5V, SPI & I2C versions. No more thru hole parts tho, will need an SMD adapter. SOIC packages are not too bad to hand solder. http://www.digikey.com/products/en/integrated-circuits-ics/memory/774?FV=fff40027%2Cfff80434%2Cffe00306%2C1f140000&mnonly=0&newproducts=0&ColumnSort=1000011&page=1&stock=1&pbfree=0&rohs=0&k=fram+memory&quantity=&ptm=0&fid=0&pageSize=25&pkeyword=fram+memory