Microprocessor and Development Tools - Embedded Control - Communications - Connectivity
   
Low-Cost Dev Kits
Application Kits
RabbitCores
Latest Downloads
Single-Board Computers
Rabbit Support Forums
Training/Events



Support > Ask Larry > December 19th, 2007

Ask Larry

Dear Larry,

I want to preserve some variables between resets and I am afraid I will need to use my flash. Can you provide me with some alternatives?

John L.

Hi John, while it is possible to preserve values in the flash this is almost never the best option. The Rabbit memory model reserves a small section of flash for your use called the “userblock”. You can use this region of memory to store values you want to preserve between resets, but like all flash, the userblock has some limitations.

Flash is much slower than RAM and when the Rabbit writes to the flash, it disables interrupts. That means you need to carefully monitor when you write to flash because you won’t do anything else until that is finished. For example if you are counting each second from 1 to 100 and you attempt to write to flash during that count, your count will be incorrect.

Flash is also a limited resource and degrades with use. We guarantee 10,000 writes to the flash before it fails. That sounds like plenty to most people but when you consider that a computer doesn’t mind doing the same operation once every 100 milliseconds or faster you can see that it could be easy to damage the flash in a matter of minutes if your program writes to the userblock too often.

Here is a very brief example of how to write to the userblock.

//Write to and Read from the Userblock
main()
{
   int a, b;
   a = 1;
   b = 0;

//For information on these functions, 
//select them and press CTRL-H in Dynamic C
writeUserBlock(0, &a, 2);
readUserBlock(&b, 0, 2);

printf("b= %d\n",b);
}

A much better solution is to use the battery-backed SRAM on the Rabbit device. Battery backed-SRAM is very fast and you can write to the SRAM as often as you like without damaging it.

Here is a short example of how to use the battery-backed SRAM.

/****************************************
   1. Run the program.
   2. Unplug the power from the Rabbit.
   3. Plug it back in.
   4. Run the program again.

   The first time the two printed values will not match.
   The second time, they should.

   The keyword 'static' is a way of ensuring that the program 
   does not place the variable on the stack.

   The keyword 'bbram' is a way of ensuring that on a device 
   with more than one RAM that the battery-backed RAM is 
   used to store the variable.
******************************************/
main()
{
   static bbram int our_var;

   printf("From RAM before assignment our_var = %d\n", our_var);
   our_var = 2008;
   printf("From RAM after assignment our_var = %d\n", our_var);
   printf(“Happy New Year!!”);
}

Did you notice that we intentionally did not initialize the variable so that we could read what was already present in memory?

The userblock is a good place to store information that is written once or updated very rarely. I might store a fixed IP address, calibration constants for my Analog-to-Digital converter, or an ID for the device in the userblock. When I do write to the userblock, I want to make certain that I am not trying to do anything else at the same time.

For anything else, the SRAM is ideal. Using the battery-backed SRAM should allow you to reset the device or even turn off the power while still retaining the values you have stored.

Use that SRAM John, and good luck with your project!

- Larry C.

Larry Cicchinelli is Rabbit’s Technical Support Manager. He has 30 years of embedded experience, and is considered one of the foremost authorities on Rabbit products. Larry and his staff offer comprehensive technical support to Rabbit customers.

Submit your questions for Larry via email at AskLarry@rabbit.com

Read more Ask Larry Answers




   Site Map | Privacy Policy | Contact Us | Feedback Copyright © Rabbit All Rights Reserved    A Digi International® Brand  
View Cart | Contact Us
View Cart
Products Solutions Support Company Channel partners Careers Ordering Information