[Q] How to know how much RAM memory microcontroller have at compile time?
[Q] How to know how much RAM memory microcontroller have at compile time?
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.
Re: [Q] How to know how much RAM memory microcontroller have at compile time?
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.
Finally, when you compile you get a percentage of dynamic memory used in the console pane.
- Attachments
-
- Capture.PNG (49.12 KiB) Viewed 10137 times
Re: [Q] How to know how much RAM memory microcontroller have at compile time?
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:
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
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];
In board.txt there is entry "GenF4.menu.pnum.CoreBoard_F401RC.upload.maximum_data_size=65536" but nothing i can use with #ifdef
Re: [Q] How to know how much RAM memory microcontroller have at compile time?
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:
I hope my explanation was clear enough.
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
Re: [Q] How to know how much RAM memory microcontroller have at compile time?
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
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
Thanks again for help
Re: [Q] How to know how much RAM memory microcontroller have at compile time?
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.
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.
Re: [Q] How to know how much RAM memory microcontroller have at compile 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.