stor. Object
The stor. object provides access to the non-volatile memory (EEPROM), in which your application can store data that must not be lost when the device is switched off.
On the advice of one of our customers, we would like to remind you that, like all other EEPROMs on the market, the EEPROM ICs used in Tibbo devices allow for a limited number of write cycles. As the Wikipedia article on the subject states, the EEPROM "... has a limited life for erasing and reprogramming, now reaching a million operations in modern EEPROMs. In an EEPROM that is frequently reprogrammed while the computer is in use, the life of the EEPROM is an important design consideration."
If you plan to use the stor. object, please carefully consider if the intended mode of EEPROM use will allow the EEPROM to work reliably throughout the entire projected life of your product. For more information, see Prolonging and Estimating the Life of Your EEPROM below.
The stor.size read-only property tells you the amount of EEPROM memory offered by the stor. object. The stor.setdata method is used to write data to the EEPROM, while the stor.getdata method is used to read data from the EEPROM.
Here is a simple example how to save the IP address of your device into the EEPROM:
dim s as string
dim x as byte
s=ddval(net.ip) 'this way it will take less space in the EEPROM (only 4 bytes needed)
x= stor.set(s,0) 'write EEPROM
'check result
if x<>len(s) then
'EEPROM write failure- do something about this!
end if
And here is how you read this data back from the EEPROM:
net.ip=ddstr(stor.get(0,4))
Note that with the stor. object, addresses are counted from 1, not 0. That is, the first memory location has address 1.
Special configuration area
The EEPROM IC of the device is also used to store certain configuration information required by the device. The memory available to your application equals the capacity of the IC minus the size of the special configuration area.
By default, when you access the first byte of the EEPROM, you are actually accessing the first memory location above the special configuration area. One property — stor.base — returns the size of this offset. On startup, stor.base is equal to the size of the special configuration area, so your program can only access the memory above this area.
You can change stor.base and access the configuration area if needed. For example, you can change the MAC address this way — the next time the device boots up, it will start using the newly set address.
Prolonging and Estimating the Life of Your EEPROM
Here are some ideas on prolonging the life of your EEPROM:
- Limit the number of updates you make to the EEPROM's data. For example, if you are counting events, then do not write to the EEPROM every time you increment the count. Instead, keep a counter variable in RAM and implement an EEPROM update policy. For example, you may choose to update the EEPROM once for every 100 increments of the RAM counter, or after x hours of inactivity.
- Before writing data into an EEPROM location, read this location to see if it already contains the data to be written. In EEPROMs, writing the same data into the same location still counts as a write cycle!
- Use a wear-leveling scheme, in which you rotate your storage area through a range of EEPROM addresses. For example, if you have a 32-bit counter, create an array of 32-bit locations and rotate between these locations when incrementing the counter. This one should be taken in conjunction with the next point...
- When placing frequently updated data into the EEPROM, try to keep this data within sector boundaries. Like flash storage, EEPROMs do have sectors. Tibbo devices with 2KB EEPROMs have 16-byte sectors. Devices with 256-byte EEPROMs have 8-byte sectors. When you update the data in a portion of a sector, you are actually wearing off the entire sector! When counting sector boundaries, take into the account the stor.base property — it gives all your writes a fixed offset! For example, on the EM1000 (2KB EEPROM), to write into the third sector of the EEPROM, use stor.setdata(s,4), because stor.base = 29 and 29 + 4 = 33 (i.e., the first location of the third 16-byte sector).
- Use redundancy — keep two or more copies of your EEPROM data and protect the data with checksums. Should one copy fail, there will be another copy to use. This also helps against abrupt power failures. If the power is turned off while an EEPROM write is in progress, you may lose your data. Having a backup copy helps in such situations.
- Consider using our STG library — it will save you a lot of headache in dealing with EEPROM data.
When estimating the life of the EEPROM in your system, consider the following:
- Always take into the account the underlying sector nature of EEPROM ICs. Writing even a single byte into a sector reduces the lifespan of the entire sector.
- Writing a string of several characters constitutes the number of write cycles equal to the number of characters in that string. Executing stor.setdata("ABC",4) counts as three write cycles, not one.
- High ambient temperature reduces the life of EEPROMs.
- All vendor estimates for projected EEPROM life are probabilistic in nature. You will encounter "stronger" and "weaker" specimens with vastly different endurance levels. All in all, it is quite safe to assume that your EEPROM will last for at least 1 million write cycles per sector ... and yet, there is no guarantee of that.