Skip to main content
Ekim.1
Associate III
April 20, 2020
Solved

How to exchange variable values in two programs

  • April 20, 2020
  • 1 reply
  • 2467 views

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?

This topic has been closed for replies.
Best answer by berendi
  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

1 reply

berendi
berendiAnswer
Principal
April 20, 2020
  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

Ekim.1
Ekim.1Author
Associate III
April 21, 2020

@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?

Tesla DeLorean
Guru
April 21, 2020

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 VenmoUp vote any posts that you find helpful, it shows what's working..