Skip to main content
Lyu.1
Senior
August 8, 2025
Solved

External SDRAM scattered loading issue

  • August 8, 2025
  • 1 reply
  • 503 views

Hi,Master:

MCU: stm32h743ii

IDE:keil
Question:
There is a 32M external SDRAM, and the scatter file is as follows:

LR_IROM1 0x08000000 0x00200000 { ; load region size_region
 ER_IROM1 0x08000000 0x00200000 { ; load address = execution address
 *.o (RESET, +First)
 *(InRoot$$Sections)
 *(_drv_set_)
 .ANY (+RO)
 .ANY (+XO)
 }
 
 ...
 
 EXT_SDRAM 0xC0000000 0x2000000 { ; RW data
 *.o(ext_sdram)
 }
}

Suppose there are some variables in the program that are specified to be located in 'ext_sdram' section.

static lv_color_t buff1[1024 * 600] __attribute__((used, section("ext_sdram")));
static lv_color_t buff2[1024 * 600] __attribute__((used, section("ext_sdram")));
...

During the scatter file loading, these variables will be initialized. Therefore, the external SDRAM needs to be initialized before the scatter file loading, as shown below:

Reset_Handler PROC
 EXPORT Reset_Handler [WEAK]
 IMPORT SystemInit
 IMPORT __low_level_init
 IMPORT __main

 LDR R0, =SystemInit
 BLX R0
 LDR R0, =__low_level_init
 BLX R0
 LDR R0, =__main
 BX R0
 ENDP
				 
void __low_level_init(void) 
{
 DIS_INT();
 sys_cache_enable(); 
 HAL_Init(); 
 sys_stm32_clock_init(192, 5, 2, 4); // init all clock 
 delay_init(480);
 sdram_init(); 
}

Problems:

  1. Before the scatter file loading, modifications to global or static variables within __low_level_init may be overwritten after the scatter file loading.
  2. Before the scatter file loading, global or static variables that __low_level_init depends on are not initialized, which may cause abnormal code execution.

I think this approach does not elegantly solve the problem. Is there a more reasonable way to solve it? For example, prevent the scatter file from loading the ext_sdram section, thus eliminating the need to initialize the external SDRAM with __low_level_init before the scatter file loading.
Thank you very much!

Best answer by mƎALLEm

This is not a question linked to STM32 MCU but to MDK-ARM toolchain (Keil). You need to contact them directly in their forum. But again you need to initialize the SDRAM before the scatter loading phase as I said before.

1 reply

mƎALLEm
Technical Moderator
August 21, 2025

Hello,

You need to initialize the SDRAM in system_stm32h7xx.c / SystemInit().

Already implemented natively in the HAL: the SDRAM is initialized if DATA_IN_ExtSDRAM was defined by the user.

The initialization of the SDRAM is done with SystemInit_ExtMemCtl().

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
Lyu.1
Lyu.1Author
Senior
August 22, 2025

Hi, mƎALLEm
Currently, I haven't handled this by adding relevant initialization in the SystemInit() function. Instead, I've added the UNINIT modifier in the scatter file:

EXT_SDRAM 0xC0000000 UNINIT 0x2000000 { ; RW data
 .ANY (ext_sdram)
}

And I've also added attribute modifiers to the variables:

__attribute__((zero_init, section("ext_sdram")))

I've found that after these operations, the EXT_SDRAM won't  scatter-loading during the __main stage, thus eliminating the need for pre-initializing the external SDRAM in advance.
I discovered this approach through my own trials, but I'm not sure whether it's reasonable or not. I hope to get guidance from experts. Thank you very much. 

mƎALLEm
mƎALLEmBest answer
Technical Moderator
August 22, 2025

This is not a question linked to STM32 MCU but to MDK-ARM toolchain (Keil). You need to contact them directly in their forum. But again you need to initialize the SDRAM before the scatter loading phase as I said before.

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."