create dynamic arrays in EEPROM

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

Is it possible to create dynamic arrays in EEPROM so that the space of EEPROM can be allocated at runtime?

Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, why not?
But don't expect that you can use malloc().

You have to write your own memory allocation functions. However, can you give us a more detailed description of what you're trying to do? Otherwise it would be hard to help you.

Regards
Sebastian

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also, keep in mind that EEPROM is guaranteed for only 100,000 write cycles. When most people talk about "dynamic arrays", they intend to use them (i.e., write to them) often. With the application you have in mind, will you never write to this array more than 100,000 times in any one spot over the life of your box?

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The procedure looks like this:

calculating number of bytes needed;
if (number_of_bytes > bytes_in_EEPROM)
	return ERROR;
else
	allocating number_of_bytes;

Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

senmeis wrote:
The procedure looks like this:
calculating number of bytes needed;
if (number_of_bytes > bytes_in_EEPROM)
	return ERROR;
else
	allocating number_of_bytes;

:? Yes, that's the (very) basic concept, but there is so much more you will need to know to make this work correctly.

First, please read Dean's fine tutorial on EEPROM and how to use it: https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=38417 I beg, implore, request, and require you to read this tutorial. DO IT NOW!

Next, you will need to create an EEPROM memory "manager" similar to the malloc() and free() functions. How complex it is depends on how you intend to use your EEPROM-allocated memory. The real issue is how do you deal with the memory space you've free'ed -- is it reused? If so, how?

Finally, if you are using gcc and avr-libc (which I assume you are because of the forum you chose to post in), you will need to work on array access routines. Reading and writing to EEPROM is not as simple as declaring the variable and accessing it. If you've read the turorial above (again, I plead with you to do so), you will know the proper way to read and write the EEPROM.

Again I will warn you about potentially "over-using" the EEPROM memory. Atmel guarantees on 100,000 successful writes to EEPROM. How many times over the life of your product will you attempt to write to the same location in EEPROM? If it is over 100,000 times, you will very likely have a problem.

I have a usage counter in my project that stores two different counters: time the box is up and time the laser is on. Those two values (32-bit variables, in seconds) are stored in EEPROM every minute. If the box is left on 24 hours a day, 7 days a week (which is very likely), I will reach 100,000 writes in about 69 days. Since I didn't want that, I used a circular buffer 64 entries long -- that increases my lifetime to over 12 years, a much more acceptable answer.

Hope this helps!

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I have read this article. Could you please give me an example on how to make an dynamic array? My case does not require many times of writing EEPROM, so it is no problem.

Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you need several dynamic EEPROM arrays?
If you need only one, then the code is simple.

If you have several dynamic arrays and they are always allocated/free simultanous then the implementatin shouldn't be difficult, too.

But if you need to allocate/free each array independant, then you will face problems like memory fragmentation.

You see, we need more information about your project to be able to help.

Regards
Sebastian

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

My case is the easiest one. I need only one array.

Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Then why do you need a "dynamic" array? I should think that a statically allocated array ought to be sufficient - maybe with an "in use" flag or something equivalent inside each entry to indicate whether it is available for new data to be inserted.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because the used size can vary according to the demands and I don't want to allocate all the EEPROM space.

Thanks