cancel
Showing results for 
Search instead for 
Did you mean: 

How to exchange variable values in two programs

Ekim.1
Associate II

Hello, everyone!

I am a user of STM32H753. I want to run two programs within STM32H753 and exchange the values ​​of the variables between these two programs. (Program 1 is running on Flash memory bank 1 sector 0; Program 2 is running on Flash memory bank 2 sector 0.)

For reference, I use STM32CubeIDE 1.3.0 as a compile tool.

In the past, when IAR was used, two variables in the program were placed in the same address in the noinit area, and after changing the compilation tool to STM32CubeIDE, I do not know what to do.

What is the way to exchange variables between the two programs?

1 ACCEPTED SOLUTION

Accepted Solutions
berendi
Principal
  1. Define one common struct that holds all variables. Find out its size in bytes.
  2. Decide which memory are (DTCM/D1/D2/D3) should hold the values.
  3. Find the corresponding memory definition line in the *.ld file, decrease LENGTH to free up some memory space to accomodate the variables.
  4. Declare a pointer to the common struct type, pointing to the area feed up in step 3, i.e. to ORIGIN+LENGTH

It would be possible to create a noinit section analogous to IAR, but the ordering of variables within a section can't be guaranteed.

So, my STM32H743IITX_FLASH.ld contains the following memory definitions

MEMORY
{
  DTCMRAM	(xrw)	: ORIGIN = 0x20000000,	LENGTH = 128K
  ITCMRAM	(xrw)	: ORIGIN = 0x00000000,	LENGTH = 64K
  RAM_D1	(xrw)	: ORIGIN = 0x24000000,	LENGTH = 512K
  RAM_D2	(xrw)	: ORIGIN = 0x30000000,	LENGTH = 288K
  RAM_D3	(xrw)	: ORIGIN = 0x38000000,	LENGTH = 64K
  FLASH	(rx)	: ORIGIN = 0x8000000,	LENGTH = 2048K
  RAM_BACKUP	(xrw)	: ORIGIN = 0x38800000,	LENGTH = 4K
}

I would place a struct not exceeding 1024 bytes at the end of RAM_D1, so I adjust that line

  RAM_D1	(xrw)	: ORIGIN = 0x24000000,	LENGTH = 511K

Then I can declare my struct as

struct common_vars {
	int a, b, c;
	// ...
} *common_area = (void*)(0x24000000 + 511*1024);

and access values through common_area->a etc

View solution in original post

8 REPLIES 8
berendi
Principal
  1. Define one common struct that holds all variables. Find out its size in bytes.
  2. Decide which memory are (DTCM/D1/D2/D3) should hold the values.
  3. Find the corresponding memory definition line in the *.ld file, decrease LENGTH to free up some memory space to accomodate the variables.
  4. Declare a pointer to the common struct type, pointing to the area feed up in step 3, i.e. to ORIGIN+LENGTH

It would be possible to create a noinit section analogous to IAR, but the ordering of variables within a section can't be guaranteed.

So, my STM32H743IITX_FLASH.ld contains the following memory definitions

MEMORY
{
  DTCMRAM	(xrw)	: ORIGIN = 0x20000000,	LENGTH = 128K
  ITCMRAM	(xrw)	: ORIGIN = 0x00000000,	LENGTH = 64K
  RAM_D1	(xrw)	: ORIGIN = 0x24000000,	LENGTH = 512K
  RAM_D2	(xrw)	: ORIGIN = 0x30000000,	LENGTH = 288K
  RAM_D3	(xrw)	: ORIGIN = 0x38000000,	LENGTH = 64K
  FLASH	(rx)	: ORIGIN = 0x8000000,	LENGTH = 2048K
  RAM_BACKUP	(xrw)	: ORIGIN = 0x38800000,	LENGTH = 4K
}

I would place a struct not exceeding 1024 bytes at the end of RAM_D1, so I adjust that line

  RAM_D1	(xrw)	: ORIGIN = 0x24000000,	LENGTH = 511K

Then I can declare my struct as

struct common_vars {
	int a, b, c;
	// ...
} *common_area = (void*)(0x24000000 + 511*1024);

and access values through common_area->a etc

@berendi​ 

Thank you, berendi.

I tried as you said, and my code uttered an error.

For reference, I placed the structure in DTCMRAM.

Next is my .ld file.

MEMORY
{
  DTCMRAM    (xrw)    : ORIGIN = 0x20000000,   LENGTH = 127K
  ITCMRAM    (xrw)    : ORIGIN = 0x00000000,   LENGTH = 64K
  RAM_D1    (xrw)    : ORIGIN = 0x24000000,   LENGTH = 512K
  RAM_D2    (xrw)    : ORIGIN = 0x30000000,   LENGTH = 288K
  RAM_D3    (xrw)    : ORIGIN = 0x38000000,   LENGTH = 64K
  FLASH    (rx)    : ORIGIN = 0x8100000,   LENGTH = 2048K
}

I also made the structure as follows.

struct common_vars {
	uint8_t a;
	uint8_t b;
	uint8_t c;
	uint16_t d;
} *common_area = (void*)(0x20000000 + 127*1024);

This is an error message that occurred to me.

0693W000000W8gdQAC.png

0693W000000W8giQAC.png

I don’t know what’s wrong with you. Do you know what’s wrong?

Where did you define it? In an include file you pull into every file?

Perhaps you need it to be an extern and define the value once.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@Community member​ 

I defined a structure in a single header file.

This header file is included in several .c files that require this header file among the source files in the project.

I think it's a matter of redefinition every time it's included, but I don't know how to solve it.

Separate the type declaration, variable declaration, and variable definition, as you would with any other variable that is used in more than one compilation unit.

@berendi​ 

If you don't mind, can you give me an example?

type definition:

struct common_vars {
	int a, b, c;
	// ...
};

variable declaration:

extern struct common_vars *common_area;

The above goes into a header file. The variable definition goes into a .c source:

struct common_vars *common_area  = (void*)(0x20000000 + 127*1024);

Thank you, berendi!!

Thanks to this, I was able to confirm that my program was working normally.