Arduino IDE - Huge memory usage of STM32

Development environment specific, Arduino, Eclipse, VS2013, Em::Blocks etc
Post Reply
mike
Posts: 1
Joined: Fri May 21, 2021 12:23 pm

Arduino IDE - Huge memory usage of STM32

Post by mike »

Hi!

Recently I just started using the STM32 bluepill board and I was trying to blink the on-board LED. I programmed the board using Arduino IDE but what shock me was the memory usage is so huge. Merely blinking a LED takes up 17% of the program memory space. Below is my code and the output of the IDE. Is this normal? Does anyone knows how to reduce the memory usage?

Code:

Code: Select all

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin PC13 as an output.
  pinMode(PC13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(PC13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(200);              // wait for a second
  digitalWrite(PC13, LOW);    // turn the LED off by making the voltage LOW
  delay(200);              // wait for a second
}
Output:

Code: Select all

Sketch uses 11180 bytes (17%) of program storage space. Maximum is 65536 bytes.
Global variables use 1432 bytes (6%) of dynamic memory, leaving 19048 bytes for local variables. Maximum is 20480 bytes.
stm32flash 0.4

http://stm32flash.googlecode.com/

Using Parser : Raw BINARY
Interface serial_w32: 115200 8E1
Version      : 0x22
Option 1     : 0x00
Option 2     : 0x00
Device ID    : 0x0410 (Medium-density)
- RAM        : 20KiB  (512b reserved by bootloader)
- Flash      : 128KiB (sector size: 4x1024)
- Option RAM : 16b
- System RAM : 2KiB
Write to memory
Erasing memory
Wrote address 0x08002bac (100.00%) Done.

Starting execution at address 0x08000000... done.
Also, does the 128KiB means that the flash memory of the chip is 128KiB? I remember the specification written on the website I bought this board was 64kB.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Arduino IDE - Huge memory usage of STM32

Post by ag123 »

128kb is the flash, the sketch firmware lives here.
blinking a led takes 'lots of storage' as the core bundles lots of features in it such as initializing the io pheriperials gpios, uart, spi, i2c etc at reset.
in addition it initialize the crystal and system clock as well as systick and if you have selected usb-serial, that is bundled into it.
there is also part of the newlib (standard c library used in it)
usb-serial (usb cdc acm) takes quite a bit of storage and makes it possible to Serial.println("hello world"); from the sketch and you can connect to it using a serial terminal over a usb-cable on a com port to view that message. usb-serial is the defacto easiest way to connect to the mcu over usb and is rather fast 1Mbps (mega bits per sec) is achievable. for anything more it depends on usb polling (host pc side) and optimizations at the sketch side (e.g. to transmit in bulk rather than multiple calls separated by delays).
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Arduino IDE - Huge memory usage of STM32

Post by fpiSTM »

Edit: Seems you used Roger's core so below customization is not valid. Only for STM32 core.

You can do some customization depending of your needs:
https://github.com/stm32duino/wiki/wiki ... tes-of-ram

And also redefine some arrays to save space:
https://github.com/stm32duino/wiki/wiki ... efinitions
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Arduino IDE - Huge memory usage of STM32

Post by mrburnette »

mike wrote: Fri May 21, 2021 12:33 pm ...
Merely blinking a LED takes up 17% of the program memory space. Below is my code and the output of the IDE. Is this normal? Does anyone knows how to reduce the memory usage?
...
The concern with embedded programming under C++ is not the code size, but the code size growth.
Always compile a blank sketch (IDE 2..0.0-beta.6):

Code: Select all

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}
And use the ArduinoIDE console output as a base-line:

Code: Select all

Sketch uses 14384 bytes (13%) of program storage space. Maximum is 110592 bytes.
Global variables use 3128 bytes (17%) of dynamic memory, leaving 14280 bytes for local variables. Maximum is 17408 bytes.

--------------------------
Compilation complete.
From the above you can easily see the overhead of C++ programming ... not necessarily "Arduino" specific. There was a time when programmers only used assembler for microcontrollers because C++ has a significant starting-out footprint.

Find out more here: http://www.open-std.org/jtc1/sc22/wg21/docs/TR18015.pdf
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: Arduino IDE - Huge memory usage of STM32

Post by mlundin »

I would say C/C++ as such is not much larger footprint than assembly code, the compilers are more efficient the anyone but expert assembly gurus.
The differences in size is the added code libraries from the core, mathlibs are a fair chunk, gpio and communications, that will make further use of these functions simpler and more efficient.

Now it must be said that the HAL/LL subsystems are coded for portability and stability and not for minimal size or maximal speed.
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: Arduino IDE - Huge memory usage of STM32

Post by fredbox »

The important thing is not to panic. You can do a lot in 64KB.

I have a project that is 1500+ lines in 17 source files.
It uses Nokia LCD (SPI), EEPROM (I2C), DS3231(I2C), PWM, CLI, USB serial, and hardware serial.
It compiles to just 50KB and has been running on a blue pill board for many months.

As long as your program fits in memory and works, consider it a win.
If your program becomes too big to fit in the available space, then you can start optimizing or move to a cpu with more memory.

You can code a blink sketch that takes a few hundred bytes, but not using the Arduino IDE / STM32Duino core.
The book "Beginning STM32 Programming" by Warren Gay has one that is about 700 bytes.
Post Reply

Return to “IDE's”