ColdFire RAM allocation

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ColdFire RAM allocation

2,118 Views
LPs1978
Contributor III

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

Message Edited by LPs1978 on 2009-04-23 01:33 PM
Labels (1)
0 Kudos
9 Replies

572 Views
LPs1978
Contributor III

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.

0 Kudos

572 Views
RichTestardi
Senior Contributor II

Sorry I could not help...

 

0 Kudos

572 Views
LPs1978
Contributor III

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

0 Kudos

572 Views
RichTestardi
Senior Contributor II

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

 

 

 

0 Kudos

572 Views
RichTestardi
Senior Contributor II

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:

 

  http://forums.freescale.com/freescale/board/message?board.id=CFCOMM&message.id=4348&query.id=25059#M...

 

-- Rich

 

 

 

0 Kudos

572 Views
RichTestardi
Senior Contributor II

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...

0 Kudos

572 Views
LPs1978
Contributor III

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?

0 Kudos

572 Views
RichTestardi
Senior Contributor II
Yes, that makes sense and the method I told you is I believe the most straightforward way to achieve that.  Note that without the struct, there is no deterministic ordering of the addresses for a and b -- a can be less *or* be can be less, at the compiler and linker's choosing (what you see when you compile and link once may not repeat, or at least is not guaranteed to repeat, when you change the program and compile and link again).
0 Kudos

572 Views
RichTestardi
Senior Contributor II

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

 

 

0 Kudos