Stupid C programmer Questions - EEPROM variables
mindrobots
Posts: 6,506
Sorry, I've been getting slammed at work and haven;t had much time to play. I did come up with a couple questions tonight while pondering things. As always, thsi could all just be blamed on my lack of C programming knowledge.
1) How would one go about creating a non-volatile variable in EEPROM that can survive across a reset/power-off. In SPIN, this is relatively easy, grab your EEPROM I2C Object, agree with yourself on a safe place above the 32K line to store your non-volatile data and have at it. So, how can this be done in C and where are the safe places in the EEPROM for non-volatile variables in the various memory models?
2) Is there any indication the program can use to determine if it is being executed for the first time after a load versus a reset? I could see where this could be a useful feature if it didn't exist.
Thanks for being tolerant!
1) How would one go about creating a non-volatile variable in EEPROM that can survive across a reset/power-off. In SPIN, this is relatively easy, grab your EEPROM I2C Object, agree with yourself on a safe place above the 32K line to store your non-volatile data and have at it. So, how can this be done in C and where are the safe places in the EEPROM for non-volatile variables in the various memory models?
2) Is there any indication the program can use to determine if it is being executed for the first time after a load versus a reset? I could see where this could be a useful feature if it didn't exist.
Thanks for being tolerant!
Comments
Hmmm, good question. I can't think of any method off-hand, but perhaps the loader leaves some traces of itself behind. Perhaps David Betz (the author of the loader) will chime in here... he might have an idea.
Eric
Hi mindrobots,
the first idea I have is to reserve 2 variables in non volatile memory
for example the vars have the value 0xAA and the complement 0x55
if program starts this 2 vars are read, and if the value and the complement do not match
the probability to run first time is high and the program write it.
else ( the vars match ) you have an indicator for the program, it is not first runtime.
Reinhard
Edit: As I do a closer look at your first question, I guess you have this solution in mind .
For #2, I need to do more testing but last night it seemed uninitialized variables were set to 0 for me after each reset. I wasn't expecting persistence but thought the 0 was a nice touch.
These seem to be a couple often done embedded controller features that should be easy to do for a programmer.
Persistent variables in EEPROM are a bit beyond the scope of the compiler, though. I think a library is the right way to do this, since different boards will have different layouts of non-volatile memory.
Eric
(Global, non-static variables are also initialised to 0, but global variables are Bad.)
-Tor
Initialized global variable data will be saved to EEPROM when you program with propeller-load -e. Of course, that's write once and read many (read on each boot-up). An I2C driver will be necessary to modify EEPROM. It is not clear whether we can rely on the address of a global variable for modifying EEPROM variables, but it should work for LMM programs.
I hadn't a specific application in mind. I thought the application code may need/want different initialization routines depending if it was an initial start up or a start up after a reset. If variables can be updated in EEPROM, then a zero value for a reset counter could be used as a flag for initial serial load and a non-zero value wold indicate an EEPROM reload. Success with solving #1 through EEPROM R/W libraries and being able to find variables in EEPROM from a running would solve the load source indicator of #2 for the most part.
As soon as I get some free time, I'll start working on the EEPROM libraries. (and by way of that, an i2C library, too!)
Thanks, everyone for all the info and guidance.
I haven't had a need for EEPROM variables in any applications but it does seem to be a forum question that comes up every month or so. Probably something to take to the PropGCC committee to discuss the need and priority. There are more important things to work on but this is something we've been telling folks how to do in SPIN (and is doable in other languages also) and can be done by other processors.
I wouldn't think you'd want the runtime variable copies in EEPROM for the reasons you've mentioned but a way to set aside a reserved area for non-volatile user space if needed. The program could just update this space as the dynamic variables changed or a save event occurred. Maybe something in propeller-load.cfg where a space could be set aside if needed in a special definition block? The programmer could set up their own definition block as needed. I don't know, I'm just winging it now.