[Q] How to know how much RAM memory microcontroller have at compile time?

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
User avatar
Bakisha
Posts: 59
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 4
Contact:

[Q] How to know how much RAM memory microcontroller have at compile time?

Post by Bakisha »

Hi, i am using some large array, but, to be able to reserve it at compile time, i need information how much there is it. I tried to look for some #define in core's code, but i didn't find any. I'm just trying to write something in Arduino IDE, that can be used on both cores (STM32 ST and Roger's core), and depending on microcontroller selected from menu, to adjust stuff accordingly.

MGeo
Posts: 25
Joined: Thu Dec 19, 2019 10:29 am

Re: [Q] How to know how much RAM memory microcontroller have at compile time?

Post by MGeo »

You can find RAM size in the variant linker scripts, in boards.txt (https://github.com/stm32duino/Arduino_C ... boards.txt), and also in the part datasheet for your given processor.

Finally, when you compile you get a percentage of dynamic memory used in the console pane.
Attachments
Capture.PNG
Capture.PNG (49.12 KiB) Viewed 1871 times

User avatar
Bakisha
Posts: 59
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 4
Contact:

Re: [Q] How to know how much RAM memory microcontroller have at compile time?

Post by Bakisha »

I know size of RAM available, but i want to write a code that can calculate it at compile time, without the need to comment/uncomment lines depending of CPU/core i had selected from menu.
I am using STM32F103C8 (blue pill) and STM32F401CC, and same code can be used for both of them. I ordered STM32F407VET6 board (still on it's way from china), but i'm pritty sure it will work on that one too.

For example:

Code: Select all

#ifdef USE_HAL_DRIVER                       // STM32 ST cores
#define  RAM_SIZE 0x4400                    // STM32F103C8   
//#define RAM_SIZE 0xE800                    // STM32F401CC
#endif

#ifdef __STM32F1__ // STM32F103C8 on Roger's cores. 
#define RAM_SIZE 0x3a00
#endif

uint8_t RAM[RAM_SIZE];
I know i can set values depending on what CPU is used, but since it can run on all variants, i was hoping there is some values that i can use that is already set.

In board.txt there is entry "GenF4.menu.pnum.CoreBoard_F401RC.upload.maximum_data_size=65536" but nothing i can use with #ifdef

victor_pv
Posts: 9
Joined: Wed Dec 18, 2019 7:33 pm

Re: [Q] How to know how much RAM memory microcontroller have at compile time?

Post by victor_pv »

You best bet is probably to use the board name.
You can look in boards.txt for property build.board of the boards you plan to use.
From platform.txt, the board name will be used to create a macro name:
-DARDUINO_{build.board}

That means, for example, for board DISCO_F030R8, there property build.board is:
Disco.menu.pnum.DISCO_F030R8.build.board=DISCO_F030R8

So during compilation there will be a macro with the name:
ARDUINO_DISCO_F030R8

Then your program could use:

Code: Select all

#ifdef USE_HAL_DRIVER                       // STM32 ST cores
  #ifdef ARDUINO_DISCO_F030R8
    #define RAM_SIZE 16384
  #elseif ARDUINO_NUCLEO....
    #define RAM_SIZE 32768
  #endif
#endif
I hope my explanation was clear enough.

User avatar
Bakisha
Posts: 59
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 4
Contact:

Re: [Q] How to know how much RAM memory microcontroller have at compile time?

Post by Bakisha »

victor_pv wrote:
Sun Dec 22, 2019 4:34 am

I hope my explanation was clear enough.
It was very clear and very helpfull. Thank you.

Yes, i could use (for example):

Code: Select all

#ifdef ARDUINO_BLUEPILL_F103C8
#define RAM_SIZE 0x3a00
#endif
But, for my personal use, i edited platform.txt and added new macro for available RAM.
So, it should look like this:

Code: Select all

#ifdef USE_HAL_DRIVER //  STM32 ST cores
#ifdef ARDUINO_SRAM // edited platform.txt
// change in platform.txt
// line
// build.info.flags=-D{build.series} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} -DBOARD_NAME="{build.board}"
// to
// build.info.flags=-D{build.series} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} -DBOARD_NAME="{build.board}" -DARDUINO_SRAM={upload.maximum_data_size}
#define RAM_SIZE  (ARDUINO_SRAM-0x1800)&0xffff // Automaticly set the amount of RAM that can be available to emulator (maximum is 65535 bytes)

#else // normal platform.txt
#define RAM_SIZE 0x3a00  //                Set the amount of RAM that can be available to emulator (leave at least 2000 bytes for locals) (depends of microcontroller's RAM and core used)
#endif
#endif

#ifdef __STM32F1__ // STM32duino (Roger's) core. Only STM32F103Cx supported
#define RAM_SIZE 0x3a00
#endif

Yes, i need to edit it every time i update the core, but for now, i'm ok with it.

Thanks again for help

User avatar
fpiSTM
Posts: 792
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 30
Location: Le Mans
Contact:

Re: [Q] How to know how much RAM memory microcontroller have at compile time?

Post by fpiSTM »

Hi,

You should have a look at this example:
https://github.com/stm32duino/STM32Exam ... istics.ino

Using the linker script definitions _estack and _sdata should do the trick ;)

But during execution not build time.

User avatar
Bakisha
Posts: 59
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 4
Contact:

Re: [Q] How to know how much RAM memory microcontroller have at compile time?

Post by Bakisha »

fpiSTM wrote:
Sun Dec 22, 2019 3:24 pm
But during execution not build time.
That example looked too advanced for my LVL1 coding skills :-)

I tried to implement it and failed. As you mentioned, it can't be used because it is not constant.

Post Reply

Return to “General discussion”