Hi all,
a simple question for you: is there an option to allocate variable in internal RAM not like stack, but as in flash?
I mean: if I put use this code:
char A;
char B;
The address of A is higher than the address of B.
What I need is to have A with a lower address than B.
Regards
Thanks Rich,
I know all things you wrote.
My question was a curiosity only, to have a copy in FLASH of a RAM values that can be with "incremental address" instead of a "stack like" allocation.
Thanks again.
Sorry I could not help...
Thanks for your answers, but is not my case. I have a lot of variable that has to be stored in FLASH and also I need to read directly from the FLASH during my process.
Than I need to invert with the flash writing function my data, or I have to valvulate offsets in bottom to top way.
Thaks for your support
I'm not sure I understand... You can store read-only variables in flash, and initialize them, with the "const" modifier:
const struct {
char a;
char b;
} g = {
'x',
'y'
};
Again, when you declare independent variables, even in flash, there is no guaranteed relationship between their addresses unless you use a struct.
Now, if you want to *write* to flash variables (i.e., modify them after they are initialized), it's a whole another ball-game... You can see some of that here, but it depends totally on the MCU you are using:
http://forums.freescale.com/freescale/board/message?board.id=CFCOMM&thread.id=6653
Perhaps you can describe in more detail what you are trying to achieve -- I thought you only wanted to control the relative address of RAM variables...
-- Rich
PS and if you define a struct, you can put it in both RAM and flash, and copy between them, like:
struct mystruct {
char a;
char b;
};
const struct mystruct inflash = { ... };
struct mystruct inram;
Then, to copy from flash to RAM is:
memcpy(&inram, &inflash, sizeof(inram));
And to copy from RAM to flash is (using the func in the previously indicated post):
flash_write_words((uint32 *)&inflash, (uint32 *)&inram, sizeof(inflash)/sizeof(uint32));
Note that a) the struct must be a multiple of 4 bytes, and b) the flash_write_words() is highly MCU dependent -- you can see versions for many of the CF V2 and V1 cores in the file I posted.
What MCU are you using? (Once you cross from talking about RAM to flash, it all totally depends on the MCU.)
Also note that for flash updates you make that you really care about, you need to store two copies of the data (since updates are not atomic) and use the old one up to the point that the new one is completely updated, using an algorithm such as the one here:
-- Rich
PPS and flash_write_words() only works if the destination page has already been erased first... And that means all the data in the 2k (or 4k, or 1k, or 512 byte, or whatever, depending on the MCU) block has to be able to be erased... Ordinarily you won't want to use the "const" structure allocation to do this, but you'll use linker file magic to get an aligned chunk of memory, and then point to it explicitly to access the structs, like, "mystructptr = (struct mystruct *)0x10000", and then access thru mystructptr. It all gets a lot more complicated if you're talking about read/write variable updates in flash, but there are lots of posts on the forum about that -- probably the most commonly discussed topic...
Sorry for my not so clear explanation.
I want to declare many variables that have an incremental address as they are declared.
For example, if I declare:
uint32 A;
uint32 B;
The address of these variable will be that the address of B is lower that the address of A.
If A is 0x20000104, than B will be 0x20000100.
What I want to have is the contrary A with address 0x20000100 and B with address 0x20000104
Is more clear now?
Hi,
I'm not sure I completely understand the question, but if you are asking about the relationship of the addresses of side-by-side global (or non-local) RAM variables, that's not strictly specified in C -- the compiler and linker can move them around for packing efficiency (or other) reasons.
With that said, if you have global variables that you want specific addressing relationships between, you can pack them in a struct, like:
static struct {
char a;
char b;
} g;
And then refer to g.a and g.b (rather than simply a and b). In this case, you know g.a will be laid out in RAM right before g.b, subject to all the normal "structure packing" rules regarding field alignment.
-- Rich