question about EEMEM

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

Hi, this is my first post in this forum.
I've a question about the macro EEMEM.
If i've this code..

#include  

const uint8_t a EEMEM = 1;
const uint8_t b EEMEM = 2;
const uint8_t c EEMEM = 3;
const uint8_t d EEMEM = 4;

When I compile the code, I'm sure that the compiler add these variables in eeprom in the order they are written?
In another word, I'm sure that in eeprom i will find
0x01 0x02 0x03 0x04?

Thanks for the response.
Bye.

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

Maybe. Maybe not. No guarantees except when referring to 'a', you will access 'a' and not b,c,or d, or any other eeprom location.

Maybe describe why you want them in a specific order. It can be done and there can be good reasons to do it. But if no good reason, then no need.

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

Throw them in a struct, which should force the ordering. At the very least the largest variation would be that the compiler will either order them a-d, or d-a, so you should be able to test which ordering the compiler uses and alter your code to suit.

However, that's not a perfect solution, as the compiler *could* change its ordering at a later date (however unlikely).

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

curtvm wrote:
Maybe. Maybe not. No guarantees except when referring to 'a', you will access 'a' and not b,c,or d, or any other eeprom location.

Maybe describe why you want them in a specific order. It can be done and there can be good reasons to do it. But if no good reason, then no need.

as I thought.. :(
I want the data stored in order because I want to run a code like this..

#include 
#include 

struct Tests {
	u08 enable;
	int (*test)(int a, ...);
	char args[];
};

const struct Tests t1 EEMEM = {0, func1, "abc"};
const struct Tests t2 EEMEM = {0, func2, "defghi"};
const struct Tests t3 EEMEM = {0, func3, "lmnopqrst"};

int main (void)
{
	void *T[] = {&t1, &t2, &t3};
	int lenT[] = { sizeof(t1), sizeof(t2), sizeof(t3) };
	struct Tests t;
	for (int i=0; i<3; i++)
	{
		eeprom_busy_wait();
		eeprom_read_block(&t, T[i], lenT[i]);
		t.test(t.desc);
		//some code
	}
}

Now this code don't work because the sizeof function can't return the correct size of the struct. If the data is stored in eeprom sequentially I can calculate the size of first two element by this:

int lenT[] = { t2-t1, t3-t2, xx-t3 };

Is not a good solution but with some improvements it can work..
Another solution is to store in a byte of struct the length of structure, but this length must be calculated by hand.. Not very comfortable but useful..

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

katte wrote:
Another option is to make the size of your structure fixed by moving the variable sized args to another section of memory.
Such as the below (untested) modification of your program.

(Also, I don't see how the stack allocated "struct Tests t;" in you original program would work because struct Tests doesn't have a defined size.)

#include 
#include 

const char[] t1_args EEMEM = "abc";
const char[] t2_args EEMEM = "defghi";
const char[] t3_args EEMEM = "lmnopqrst";

struct Tests {
	u08 enable;
	int (*test)(int a, ...);
	char *args; // May need a qualifier that specifies it to a pointer in EEPROM
};

const struct Tests t1 EEMEM = {0, func1, t1_args};
const struct Tests t2 EEMEM = {0, func2, t2_args};
const struct Tests t3 EEMEM = {0, func3, t3_args};

const struct Tests[] test_array EEMEM = {t1, t2, t3};

int main (void)
{
	struct Tests t;
	for (int i=0; i<3; i++)
	{
		char args[MAX_ARGS_LENGTH];
		eeprom_busy_wait();
		eeprom_read_block(&t, test_array[i], sizeof(struct Tests));
		t.test(t.desc);
                eeprom_read_zero_terminated_string(args, t.args); // you'll need to write this
		//some code
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

An alternate way to make the length of struct Tests constant, but may use more memory, is to set a fixed size of the args field. The degree of memory efficiency is the average length of args versus the maximum length of args. To do this, rather than:

struct Tests { 
   u08 enable; 
   int (*test)(int a, ...); 
   char args[];
}

use

struct Tests { 
   u08 enable; 
   int (*test)(int a, ...); 
   char args[MAXIMUM_ARGS_LENGTH];
}; 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What I think I would do is to make fixed-length structures with a char* instead of the char[]. Name each string and define outside the structure.
[lol--same as kmr. Great minds think alike. ;) ]

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

kmr wrote:
Another option is to make the size of your structure fixed by moving the variable sized args to another section of memory.
Such as the below (untested) modification of your program.

const char[] t1_args EEMEM = "abc";
const char[] t2_args EEMEM = "defghi";
const char[] t3_args EEMEM = "lmnopqrst";

struct Tests {
   u08 enable;
   int (*test)(int a, ...);
   char *args; // May need a qualifier that specifies it to a pointer in EEPROM
};

const struct Tests t1 EEMEM = {0, func1, t1_args};
const struct Tests t2 EEMEM = {0, func2, t2_args};
const struct Tests t3 EEMEM = {0, func3, t3_args}; 

Yeah, great idea..
this is a great solution. Thanks!
Monday I will try this solution.

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

You're very welcome, good luck with it!