STM32CubeIDE, Flash and SDRAM configuration. - Page 1
Forum section: electronics / general

author: luiHS
author_rank: Frequent Contributor
timestamp: December 11, 2021, 12:43:16 am
content: HiI 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 = 1024KRAM (xrw): ORIGIN = 0x20000000, LENGTH = 128KITCMRAM (xrw): ORIGIN = 0x00000000, LENGTH = 64K}Regards

author: DavidAlfa
author_rank: Super Contributor
timestamp: December 12, 2021, 10:02:30 pm
content: 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-boardAfter generating the HAL code, you'll see the peripheral initializations in main.cSearch for:Code:static void MX_FMC_Init(void)And insert the SRAM init sequence at the end, like this:Code:/* 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 0xD0000000You can add the sdram section to the linker script like this (This example uses 8MB):Code:/* Memories definition */MEMORY{CCMRAM (xrw): ORIGIN = 0x10000000,LENGTH = 64KRAM(xrw): ORIGIN = 0x20000000,LENGTH = 192KFLASH(rx): ORIGIN = 0x8000000,LENGTH = 2048KSDRAM(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:__attribute__((section(".sdram"))) char data;But remember, you can't initialize it:Code:__attribute__((section(".sdram"))) char data = "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

author: luiHS
author_rank: Frequent Contributor
timestamp: December 13, 2021, 01:38:04 pm
content: 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.

author: DavidAlfa
author_rank: Super Contributor
timestamp: December 13, 2021, 05:35:21 pm
content: 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/lwmemMight worth reading:https://www.openstm32.org/forumthread2017

author: luiHS
author_rank: Frequent Contributor
timestamp: December 15, 2021, 03:25:29 pm
content: 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.

author: DavidAlfa
author_rank: Super Contributor
timestamp: December 15, 2021, 04:45:51 pm
content: 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.

author: jnz
author_rank: Frequent Contributor
timestamp: December 15, 2021, 11:11:33 pm
content: Offtopic question that probably everyone here can answer quick without needing a new topic:Is STM32CubeIDE just their old AC6 / System Workbench / Atolic updated?

author: luiHS
author_rank: Frequent Contributor
timestamp: December 16, 2021, 02:43:39 am
content: Quote from: DavidAlfa on December 15, 2021, 04:45:51 pmYes, 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 = 1536KSDRAM2 (rw): ORIGIN = 0xD0180000, LENGTH = 512KSDRAM3 (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)))#endifstatic U8 layer0_fb1 ;This way layer0_fb1 will be in the SDRAM1

author: luiHS
author_rank: Frequent Contributor
timestamp: December 16, 2021, 02:49:41 am
content: Quote from: jnz on December 15, 2021, 11:11:33 pmSTM32CubeIDE 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.

author: DavidAlfa
author_rank: Super Contributor
timestamp: December 16, 2021, 09:30:45 am
content: I see no difference. STM32 cube ide is the following, the same thign evolved.