How can I reuse physical memory by matching two variables in one place?

I am writing code for a serial communication gateway using MSP430F2619 with Code Composer Studio 6.1.

The gateway has two different modes of operation:

  • Wizard , this is the default mode. He will interrogate the data as a bus master on the RS485 bus, and then send it to another board with a different protocol.

  • Slave in which it responds to the PC application on the serial port.

The program will either be in master or slave mode, since the same RS-485 bus is used in conjunction with a PC and other slave boards.

Instead of using 2 different memory buffers for each mode, I am trying to figure out if the same physical memory can be used by both buffers.

I tried searching the Composer Studio memory overlap function without success.


Essentially, I have this:

char Slaverxbuffer[2048]; char Masterrxbuffer[2048]; 

I want both of these buffers to have shared memory, so I will use 2 KB of memory instead of 4 KB.

I need a way for Slaverxbuffer and Masterrxbuffer use the same memory area, something like:

 char Sharedbuffer[2048]; 
+5
source share
2 answers

You can have only one buffer and share it between the two modes of operation? This will probably be the easiest solution.

Otherwise, in C and C ++, you can accomplish this by indicating that the two buffers are fields of type union . Each of the members in the union has the same base address in memory, and the total size of the union is the size of the largest member.

The code looks something like this:

 union OverlaidBuffer { char Slaverxbuffer[2048]; char Masterrxbuffer[2048]; }; union OverLaidBuffer overlay; /* Code to use the slave buffer */ for(unsigned int index = 0; index < 2048; ++index) { overlay.Slaverxbuffer[index] = some_slave_value(); } /* Code to use the master buffer */ for(unsigned int index = 0; index < 2048; ++index) { overlay.Masterrxbuffer[index] = some_master_value(); } 

Please note that if you use a join, you should always read from the member you recently wrote. Writing to one member and then reading from another is undefined behavior and is likely to lead to errors. For example, the code:

 overlay.Slaverxbuffer[i] = 1; overlay.Masterrxbuffer[i] = 2; x = overlay.Slaverxbuffer[i]; 

may well set x to 1, since this is the "obvious" last value stored in overlay.Slaverxbuffer[i] , although this memory address now contains a value of 2.

Another way to achieve this is to use compiler and linker directives to place the two buffers in specific places in memory. Most built-in tools allow you to do this using the #pragma in your source code. The exact format of the directive will depend on the specific tool chain; Below are a few examples.

Then, in the linker file, add directives to place the character in a specific place:

+9
source

If, as you say, you do not need a union, can you have only one buffer and a separate variable for registering the mode in which you are in?

 char rxBuffer[2048]; bool rxMode; // false = master, true = slave 

That way you can have a buffer in a fixed memory location (defined by the linker) and reference the buffer

+3
source

All Articles