Author Topic: STM32CubeIDE, Flash and SDRAM configuration.  (Read 8027 times)

0 Members and 1 Guest are viewing this topic.

Offline luiHSTopic starter

  • Frequent Contributor
  • **
  • Posts: 592
  • Country: es
STM32CubeIDE, Flash and SDRAM configuration.
« on: December 11, 2021, 12:43:16 am »
Hi
I usually work with NXP and MCUXpresso, but this time I have an application for STM32 and I am using the STM32CubeIDE.

I need to know where the load address of the program is defined in Flash and SDRAM. I need to modify the loading address of the flash to compile the program for a bootloader and I want to disable the SDRAM for some tests.

In MCUXpresso, it is as simple as entering Project Properties> C / C ++ Build> MCU Settings. But on the STM32CubeIDE I can't find anything like this.

Does anyone know where I can configure the load address of the flash and the addresses and size of an external SDRAM ?.

I found the source file of the Linker Script and the address and size of the flash is defined there, nothing is indicated about the 256Mb SDRAM used by the project. In any case I expected to find a more graphical configuration system in the project properties, as with MCUXpresso in MCU Settings.

/ * Specify the memory areas * /
MEMORY
{
FLASH (rx): ORIGIN = 0x08000000, LENGTH = 1024K
RAM (xrw): ORIGIN = 0x20000000, LENGTH = 128K
ITCMRAM (xrw): ORIGIN = 0x00000000, LENGTH = 64K
}


Regards
« Last Edit: December 11, 2021, 01:05:47 am by luiHS »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5907
  • Country: es
Re: STM32CubeIDE, Flash and SDRAM configuration.
« Reply #1 on: December 12, 2021, 10:02:30 pm »
Tell the STM32 model to help you better.
The HAL will only initializes the FMC peripheral, but not the SDRAM itself, you must still manually initialize the sdram with the proper commands.

Use the SDRAM initialization sequence from here:

https://www.eevblog.com/forum/microcontrollers/stm32h7-sdram/
https://community.st.com/s/question/0D50X0000BVo0k6SQB/cannot-get-fmc-sdram-driver-to-work-on-stm32f429-discovery-board

After generating the HAL code, you'll see the peripheral initializations in main.c
Search for:
Code: [Select]
static void MX_FMC_Init(void)And insert the SRAM init sequence at the end, like this:
Code: [Select]
/* USER CODE BEGIN FMC_Init 2 */
  SDRAM_InitSequence();
/* USER CODE END FMC_Init 2 */
After this, the SDRAM will be ready.

The FMC controller maps the SDRAM to the address 0xD0000000
You can add the sdram section to the linker script like this (This example uses 8MB):

Code: [Select]
/* Memories definition */
MEMORY
{
  CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
  RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K
  FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 2048K
  SDRAM (rwx) : ORIGIN = 0xD0000000, LENGTH = 8192K
}

/* Sections */
SECTIONS
{
/* external SDRAM */
.sdram (NOLOAD) :
{
. = ALIGN(4);
*(.sdram .sdram.*)
} > SDRAM
...

The C initialization routine (which sets all your variables to the default values) runs before the user main.
So you have a problem: at that early stage, only internal ram and flash are available.
The peripherals are yet to be configured, so there's no SDRAM area to access.
Thus, you must use NOLOAD, to tell the linker that the SDRAM area shouldn't be initialized.


Then, in your code, declare the sdram variables like this:
Code: [Select]
__attribute__((section(".sdram"))) char data[128];
But remember, you can't initialize it:
Code: [Select]
__attribute__((section(".sdram"))) char data[128] = "This is a test";
This is not for HAL, but gives some hints.
Check how it wipes the SDRAM (Fill with with 0s) after initialization. That way you'll only have to initialize those variables not being 0.
https://github.com/floppes/stm32doom/blob/master/src/sdram.c
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: luiHS

Offline luiHSTopic starter

  • Frequent Contributor
  • **
  • Posts: 592
  • Country: es
Re: STM32CubeIDE, Flash and SDRAM configuration.
« Reply #2 on: December 13, 2021, 01:38:04 pm »
 
Thanks for the information. It is for a STM32H747, I am working on one of the example sources that are available in the ST libraries.

Everything is already configured, but since I have to compile the program to load it with a booloader, I needed to modify the load address in flash. And for some tests I would like to disable the SDRAM that is configured.

So from what I understand there is no wizard in the IDE to configure the flash, RAM and external SDRAM, everything has to be created manually in the source code.

The strange thing is that in the source code we are using, nothing of the external SDRAM is defined in the Linker Script, there is only the Flash and the internal RAM.
« Last Edit: December 13, 2021, 01:42:51 pm by luiHS »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5907
  • Country: es
Re: STM32CubeIDE, Flash and SDRAM configuration.
« Reply #3 on: December 13, 2021, 05:35:21 pm »
I hope you know this already, the mapping address depends on the SDRAM bank configured in FMC (Which stm32 CS pin is connected to the sdram).
It's 0xC0000000 for bank 1, 0xD0000000 for bank 2.
F7 FMC might be different, so ensure to check the reference manual, "FMC memory banks" section.

-I might be very wrong here-
I think it should be safe to move the heap to the sdram, as HAL init doesn't make any use of malloc before FMC initialization, but you must again add the NOLOAD option and manually wipe the memory after sdram inititalization.

As long as your bootloader only uses the internal SRAM, you shouldn't worry.
You can always disable the FMC by calling "HAL_FMC_MspDeInit", but you must ensure there's no heap allocated, and no further allocations are done, or you'll get a Hard fault.

Ideally, it would be great to be able to move the heap to the external sdram at runtime, after it's initialized, but I  haven't found a way to do this.
Perhabs a custom memory manager like lwmem allows that:
https://github.com/MaJerle/lwmem

Might worth reading:
https://www.openstm32.org/forumthread2017
« Last Edit: December 13, 2021, 05:54:20 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: luiHS

Offline luiHSTopic starter

  • Frequent Contributor
  • **
  • Posts: 592
  • Country: es
Re: STM32CubeIDE, Flash and SDRAM configuration.
« Reply #4 on: December 15, 2021, 03:25:29 pm »

Thank you for all the info.

I'm just not very clear yet in which cases the SDRAM needs to be configured in the Linker Script and when not. For example, in the source that I am using that comes in the package for the H7, the SDRAM is used but it does not appear at all in the Linker Script.

I also asked ST technical support and they refer me to example sources for H7 in which the SDRAM is defined in the Linker Script, but they do not tell me why in other example sources of the same package the SDRAM is not defined in the Linker Script although the program does use it.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5907
  • Country: es
Re: STM32CubeIDE, Flash and SDRAM configuration.
« Reply #5 on: December 15, 2021, 04:45:51 pm »
Well, add it to the linker script to easily allocate variables in that memory map.
Otherwise, you'll have to do it manually, using pointers, calculating every offset, ensure nothing overlaps... unless you're using the SDRAM for only 1-2 huge buffers, it'll be a mess.
For other uses, ex. driving the LCD, you don't really need to declare anything.
If you're using the SDRAM for data and also the LCD buffer, I'd recommend to split the SDRAM section to provide isolation.
« Last Edit: December 15, 2021, 04:50:35 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline jnz

  • Frequent Contributor
  • **
  • Posts: 593
Re: STM32CubeIDE, Flash and SDRAM configuration.
« Reply #6 on: December 15, 2021, 11:11:33 pm »
Offtopic question that probably everyone here can answer quick without needing a new topic:

Is STM32CubeIDE just their old AC6 / System Workbench / Atolic updated?
 

Offline luiHSTopic starter

  • Frequent Contributor
  • **
  • Posts: 592
  • Country: es
Re: STM32CubeIDE, Flash and SDRAM configuration.
« Reply #7 on: December 16, 2021, 02:43:39 am »
Well, add it to the linker script to easily allocate variables in that memory map.
Otherwise, you'll have to do it manually, using pointers, calculating every offset, ensure nothing overlaps... unless you're using the SDRAM for only 1-2 huge buffers, it'll be a mess.
For other uses, ex. driving the LCD, you don't really need to declare anything.
If you're using the SDRAM for data and also the LCD buffer, I'd recommend to split the SDRAM section to provide isolation.

Yes, in my case it is used as a double buffer for a TFT screen with a program that plays videos. For variables I think the program uses the internal RAM of the microcontroller.

In the ST support they also answered me the same:

This is only needed if you want to force location of some buffer at in SDRAM when you link your binary.
For example in the ld file:
...
SDRAM1 (rw): ORIGIN = 0xD0000000, LENGTH = 1536K
SDRAM2 (rw): ORIGIN = 0xD0180000, LENGTH = 512K
SDRAM3 (rw): ORIGIN = 0xD0200000, LENGTH = 6144K
...
.framebuffer (NOLOAD): {* (. framebuffer)}> SDRAM1
....

And in the c code:
#if defined (__GNUC__)
__attribute __ ((section (". framebuffer"))) __attribute__ ((aligned (32)))
#elif defined (__ICCARM__)
#pragma data_alignment = 32
#pragma location = "framebuffer"
#elif defined (__CC_ARM)
__attribute __ ((section (". framebuffer"), zero_init)) __attribute__ ((aligned (32)))
#endif
static U8 layer0_fb1 [FRAMEBUFFER_SIZE];

This way layer0_fb1 will be in the SDRAM1

 

Offline luiHSTopic starter

  • Frequent Contributor
  • **
  • Posts: 592
  • Country: es
Re: STM32CubeIDE, Flash and SDRAM configuration.
« Reply #8 on: December 16, 2021, 02:49:41 am »
Offtopic question that probably everyone here can answer quick without needing a new topic:

Is STM32CubeIDE just their old AC6 / System Workbench / Atolic updated?

STM32CubeIDE seems like a mix of everything that has been used as an IDE for ST's microcontrollers.

The System workbench was a plugin for Eclipse, Atollic was a commercial product that ST bought to release it and give it free, and finally there would be Cube, a program that began as an independent assistant to configure the microcontroller and was later integrated into the IDE.

STM32CubeIDE has come out of the mix of everything.
 
The following users thanked this post: jnz

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5907
  • Country: es
Re: STM32CubeIDE, Flash and SDRAM configuration.
« Reply #9 on: December 16, 2021, 09:30:45 am »
I see no difference. STM32 cube ide is the following, the same thign evolved.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: jnz


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf