Pages: [1] 2   Go Down
Author Topic: EEPROMex library - An extension of the standard Arduino EEPROM library  (Read 20071 times)
1 Member and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

For my ongoing clock project, I wanted to persistently store some data after turning off the Arduino. This should be easy since Arduino board has an on-board EEPROM. Unfortunately the Arduino standard EEPROM library only implements functionality for reading and writing a single bytes, which makes the EEPROM quite cumbersome to use.

This is why I wrote the EEPROMex library, an extension of the standard Arduino EEPROM library. It extends the functionality of the original Arduino EEPROM library with:

- Reading, writing to basic types like bytes, longs, ints, floats & doubles
- Reading, writing to single bits
- Reading, writing of any data format (for example structs)
- Reading, writing of arrays of any format
- Updating only changed bytes rather than rewriting all (thus reducing wear)
- (Very) basic memory management
- Debug functionality: limit number of writes. limit writing in memory range
 
You can download the library here: 
  http://thijs.elenbaas.net/downloads/?did=3
 
And find detailed explanation and samples of the functionality here           
   http://thijs.elenbaas.net/2012/07
/extended-eeprom-library-for-arduino


A simple example of reading and writing ints and floats:
Code:
#include <EEPROMex.h>
// Always get the EEPROM in the same order
// This will ensure that each variable gets
// the same adress after every restart
int addressInt   = EEPROM.getAddress(sizeof(int));
int addressFloat = EEPROM.getAddress(sizeof(float));

double IntVarInput    = 1234;
double IntVarOutput   = 0;

double FloatVarInput  = 101.101;
double FloatVarOutput = 0;

void setup()
{
Serial.begin(9600);

// Write an int to EEPROM, and then read it back
EEPROM.writeInt(addressInt, IntVarInput);
IntVarOutput = EEPROM.readInt(addressInt);
Serial.print(IntVarOutput);
Serial.println();

// Write a float to EEPROM, and then read it back
EEPROM.writeFloat(addressFloat, FloatVarInput);
FloatVarOutput = EEPROM.readDouble(addressFloat);
Serial.print(FloatVarOutput);
Serial.println();
}
void loop() {}

Let me know what you find!

[UPDATE: The previous version had some bugfixes. Please download the update version. The new version also comes with support for newer Arduino boards and Teensy boards!]
« Last Edit: December 01, 2013, 03:13:02 pm by mrTee » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Before I start spending more time in this library than needed, some questions:

- Are many of you arduino aficionado's out there are using the EEPROM for storage? If you aren't using it, is that because you don't need it, or because of some lacking functionality?
- Are there people using external SPI connected EEPROM (or even RAM)? Would it be useful to generalize the backend of this library so that it would also work for SPI connected storage?
- Are there people using the very nice flash library by Mikal Hart (http://arduiniana.org/libraries/flash/)? Would it be useful to, in a similar manner, implement object oriented variables that can recall /store themselves from EEPROM?

Please let me know if you find bugs or have suggestions for future improvements.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is also http://arduino.cc/playground/Code/EEPROMWriteAnything available already.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Indeed! In fact, there are a lot of snippets and small libraries around that implement some form of EEPROM reading/writing to extend the standard library. The one you mention is probably one of the most general (and useful?), but there are also

http://arduino.cc/playground/Code/EepromUtil
http://tushev.org/articles/electronics/47-modified-eeprom-library-for-arduino
http://blog.ncode.ca/?p=42

And so on. There is no rocket science in this library, it is meant mostly for convenience. What I figured was lacking was a library that I could use as a drop-in replacement for the EEPROM library, that implemented both the basic types like, int, float, double with a syntax very similar to the standard lib, as well as more complex types like structs and arrays.

Still, this library introduces some functionality that I could not really find elsewhere:

- Updating only changed bytes, instead of blindly rewriting all of them. In practice I am often writing more or less the same data to EEPROM. Updating instead of writing then saves a lot EEPROM wear.

- A maximum on the number of bytes that you can write. In theory you could burn out a memory cell in a few minutes: a write/erase cycle takes approximately 4 ms, so writing 100.000 times to a single cell takes 6 1/2 min. It happened to me that I put an EEPROM write statement within a loop rather than outside, an only after a minute or so running noticed that my Arduino didn't seem to be doing anything useful. So far, it seems that my trusted Arduino survived  smiley.
 
- Basic Memory allocation functionality. It is basically just a counter of the last allocated byte. This is also implemented in newer AVR libraries, but not in the one that ships with the Arduino.

- Targeting single bits. I haven't needed it myself yet, but I found it as a suggestion somewhere, and figured it should be easy enough to add. The idea is that writing single bits can help you use the 1Kb that you have with maximum efficiency.
Logged

Offline Offline
Sr. Member
****
Karma: 4
Posts: 327
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if this tests out,
  it would be  a great improvement / addition to the standard lib.
Logged

Earth (I think)
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

if this tests out, it would be  a great improvement / addition to the standard lib.
I agree. I was fiddling with the builtin EEPROM library, making a way to store events and timestamps, and ran into the problem that timestamps wouldn't store into 1 byte. I eventually wound up packing them in a really odd way with a 12.3 second resolution or some such so I could fit them in 2 bytes--as I recall, the first byte was the week and the second byte was split into the day & time in some strange way. It didn't really work too well. A library like this would have been really helpful.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if this tests out,
  it would be  a great improvement / addition to the standard lib.


Well, that would of course be nice :-) In fact, I would prefer that the Arduino came with a lot more user-created libraries by default (or would provide an easy way of getting to them, perhaps using a package manager), so people would start building on work of others rather that reinventing the wheel every time.

But for now, I would appreciate it very much if people would testdrive the library and see what is missing and if bugs pop up. I put the library on the Playground here, and am in the process of writing an library on top of EEPROMex that implements variables that can recall /store themselves from EEPROM.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any chance of getting this to work with the 24LC256 I2C eeprom?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@mdn15: Not without some real work. I have thought about this: the class could call different EEPROM writers/readers, which would make the underlying storage transparent to the user. However, I have not had many request for this (not many people seem to use external EEPROMs) so it is not high on my priority list.

What was higher on the list was to fix a small mistake in capitalization (a capital mistake?), which would give build errors on case-sensitive environments.  This has now been fixed.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great - I just stumbled across the limitations of the standard arduino EEPROM library, and this will be real helpful

Thanks for sharing!

tip: make a note about memory used if you include this library, this is always an issue on the 168/328p based boards
tip: most RTC ds1302 modules sold on ebay include a i2c EEPROM - supporting external EEPROMs and in particular this module with a simple example would be a great idea. Most vendors don't even seem to know it is on their module (let alone the optional ds18b20!)
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13702
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any chance of getting this to work with the 24LC256 I2C eeprom?
seen this - http://playground.arduino.cc/Main/LibraryForI2CEEPROM - one?

wrappers for other datatypes are fairly simple, something like this

Code:
void writeFloat(unsigned int address, float value)
{
  writeBlock(address, &value , sizeof(float));
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Sweden
Offline Offline
Sr. Member
****
Karma: 11
Posts: 450
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is a great idea to fix the Arduino EEPROM library but why not use the excellent support already available in AVR?

The compiler supports marking a variable as stored in EEPROM with the attribute EEMEM. This allows symbolic handling of EEPROM addresses instead of the manual method forced by the Arduino EEPROM library. Also there are already all the needed functions for different data size access in AVR EEPROM, http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html
Code:
#include <avr/eeprom.h>

int ex EEMEM;
...
eeprom_write_word(&ex, 42);
...
int x = (int) eeprom_read_word(&ex);
The really bad new is that the Arduino build does not support the initialization of EEPROM. This is not uploaded with the sketch. Otherwise you could initialize the EEPROM variables directly.
Code:
int ex EEMEM = 42;
Using EEPROM such 24LC256 requires access through an I2C/TWI driver. There are a few around.
Cheers!
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13702
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@kowalski

How does the program know/map the location of the variables when using EEMEM?
Or does it just start at address 0 etc?

But if I reorder my vars, does it reorder the mem addresses?

(I know I can write a few sketches to find out)
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it just sequentially stores them
if you change the order in which you 'declare' the variables you mess it up
you can set a starting address

look at the example 'EEPROMEx' - run it and examine the output - it more or less explains itself

not specifically noted - but you can hard code the address - or use a variable of your own

this is just a convenient way to store variables without overlap or empty gaps

I haven't looked into the other examples - they seem to use another (smarter?) scheme to store variables

if you want - I'd be happy to share a code snippet of what I have so far

*edit* oops sorry - misread and thought you were talking about the EEPROMEx library
« Last Edit: March 25, 2013, 04:10:24 pm by FigZ » Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13702
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
if you change the order in which you 'declare' the variables you mess it up
What I expected, thanks for the explanation
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1] 2   Go Up
Jump to: