Shop OBEX P1 Docs P2 Docs Learn Events
Storing memory even after power down — Parallax Forums

Storing memory even after power down

dexxteritydexxterity Posts: 14
edited 2013-08-17 18:27 in Propeller 1
I want to be able to store an array of numbers into EEPROM and have it so even if the power is turned off, when I restore power the array is easily accessable. I think this should be possible, do any of you have ideas on how I can do this? Or maybe a link or explanation on what the spin code would look like. Thanks!

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2013-08-17 12:39
    [h=1]Brownout Detector To Save Variables to EEPROM[/h]
    http://obex.parallax.com/object/12

    best regards Stefan
  • dexxteritydexxterity Posts: 14
    edited 2013-08-17 12:43
    Stefan, I checked this out. This seems to save all current running variables but in the demo it doesn't explain how to read back those variables when power is restored.
  • whickerwhicker Posts: 749
    edited 2013-08-17 12:55
    BrownoutDemo.spin
    {{
    Notes:
        When you "F11 - Load EEPROM", the variables from the DAT section below are used in your program.
        Change these variables at will within your program.
        When you power down or have a power loss, these variables are copied to their respective locations in EEPROM, overwriting
        the values from your original "F11- Load EEPROM".  This is done in the REPEAT loop when the variable Power_Good is cleared to 0.
        If you have a lot of variables to save, you will need a large filter capacitor on your power supply to assure enough time to save them.
    }}
    dat
    ' Configuration Variables to/from EEprom
    ' All vars between Cycles and ConfigVars will be restored by VarRestore
    Variable1   Long      1         ' Variable 1 to be saved in the event of a power loss
    Variable2   Long      2         ' Variable 2 to be saved in the event of a power loss
    Variable3   Long      3         ' Variable 3 to be saved in the event of a power loss
    Variable4   Long      4         ' Variable 4 to be saved in the event of a power loss
    Variable5   Long      5         ' Variable 5 to be saved in the event of a power loss
    Variable6   Long      6         ' Variable 6 to be saved in the event of a power loss
    Variable7   Long      7         ' Variable 7 to be saved in the event of a power loss
    TableEnd
    
    ...
    PUB Start
        eeprom.VarRestore(@Variable1,@TableEnd)                 ' Retrieve the variables from EEPROM
    
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-08-17 13:46
    You can do this also in Forth on a Propeller if the eeprom has empty space. Ideally having an extra 32K of eeprom space is the easiest way to assure this. The .spin binary always uses the first 32k of eeprom for an image, and never bothers with the uper 32k. For a few variables, they can be simply added to the Forth dictionary which revises the .spin binary. In that way, you get both the storge in eeprom and the speed of reading from hub ram during actual use.

    Variables that will change frequently require a bit more consideration that arrays of constants that will not change.

    The drawback with storage in the upper eeprom is that reading via I2C, which is a serial mode is rather slow. I have worked with look up tables of over 1000 items in an eeprom and found a lot of time wasted in that approach. But in Forth I could easily change the whole table independent of the program.

    Putting the same data in hub ram was much better, faster. If you have a small array (let's say less than 50 items), the fastest place to store it is in cog ram. You can do this in Forth on the Propeller, or in PASM. But these won't preserve variables for reboot that have changed from the original binary.

    So you really have three places that you can put an array depending on size and needs for speed, and your need for variables versus constants; but only one if desire it to be unaffected by a reboot, somewhere in the eeprom is best. Several Propeller boards come with 64K eeproms just for this purpose, included the Propeller Quickstart.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-08-17 13:55
    Remember that the EEPROM is loaded into RAM at power up. Knowing that, if you write a variable value to the address in EEPROM that it occupies in RAM, that value will be auto loaded on power-up. Easy-peasy. Pick your favorite I2C object and have at it. You can get the address of a variable using the @ operator.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-08-17 14:15
    JohnnyMac has said it all so much more clearly.

    In the case of Forth on the Propeller, variables and constants, single or as arrays that are included in the dictionary are in that first 32K. Stays in eeprom for reboot, but is quickly useful from hubram after booting.

    Forgive my muddled presentation above.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-08-17 16:05
    Like JonnyMac said, this is relatively easy.

    There is a thread about this in the PEK sticky.

    Keep in mind you don't want to write to EEPROM thousands of times a day. The EEPROM will wear out after a bunch of writes. (I don't recall how many a "bunch" is. It could be a million or 100K. It's listed in the datasheet for the EEPROM.)
  • dexxteritydexxterity Posts: 14
    edited 2013-08-17 17:15
    Duane,
    That link looks like what I need. In the example you linked to how do I enter an address?
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-08-17 18:27
    The @ operator gives you the address of a variable. Using my own 24xx512 object, for example, I could save a long to its auto-load address in the eeprom I would do this:
    eeprom.wr_long(@longVar, longVar)
    


    The first parameter is the address (of a long), the second is the value to write. As I said, easy-peasy.
Sign In or Register to comment.