[SOLVED]BluePill F1038C8 Where has all the memory gone

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
Jimbo13
Posts: 12
Joined: Sun Feb 16, 2020 6:10 pm
Location: UK

[SOLVED]BluePill F1038C8 Where has all the memory gone

Post by Jimbo13 »

For a project using an Arduino ProMini I started to run out of flash and ram so I decided to move over to a BluePill.
Unfortunately this seemed to use an excessive amount of memory making the memory problem just as bad.
To get a comparison I built the Blink programme for the Arduino ProMini and BluePill, the results as follows.
Any suggestions on the results or have I missed something.

Windows 10 - Arduino 1.8.10

Arduino ProMini - Blink LED
Sketch uses 960 bytes (3%) of program storage space. Maximum is 30720 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.

BluePill
Board Generic STM32F1 Series
Board Part No BluePill F1038C8
USART Support Enable (Generic Serial)
USB Support None
USB Speed NA
Optimize Smallest (os-default)
C Runtime Library Newlib Nano (default)

Sketch uses 8512 bytes (12%) of program storage space. Maximum is 65536 bytes.
Global variables use 588 bytes (2%) of dynamic memory, leaving 19892 bytes for local variables. Maximum is 20480 bytes.

Using Visual Micro there is slight improvement.
Program size: 7,428 bytes (used 11% of a 65,536 byte maximum) (47.54 secs)
Minimum Memory Usage: 576 bytes (3% of a 20480 byte maximum)
Last edited by Jimbo13 on Mon Mar 23, 2020 4:59 pm, edited 1 time in total.

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

Re: BluePill F1038C8 Where has all the memory gone

Post by fpiSTM »

The STM32 core is "generic" to support all STM32 series (currently 14). To achieve this it is based on the STM32 HAL and some wrappings which add an overhead mainly when you didn't use them even if the compiler removed some part of the unused code, it still remains some overhead. This is the price to be generic.
So Blink example shows perfectly this.

Anyway, you can customize your build and then decrease this size as explained here:
https://github.com/stm32duino/wiki/wiki ... figuration

You can first disabling the USART Support in the menu and then add a file named hal_conf_extra.h at sketch level with below definition:

Code: Select all

#define HAL_ADC_MODULE_DISABLED
#define HAL_I2C_MODULE_DISABLED
#define HAL_RTC_MODULE_DISABLED
#define HAL_SPI_MODULE_DISABLED
#define HAL_TIM_MODULE_DISABLED
#define HAL_EXTI_MODULE_DISABLED
This will save around 50% of flash size and 99% of RAM size.

You will not reach the same flash size than the Arduino ProMini but as I said it is a generic core supporting all STM32 series so it could not be like a bare metal core supporting only a few number of boards. ;)

stas2z
Posts: 30
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 1

Re: BluePill F1038C8 Where has all the memory gone

Post by stas2z »

Compiled binary for stm32 will always bigger be than avr, its 32 bit and it have much more complex init

ag123
Posts: 301
Joined: Thu Dec 19, 2019 5:30 am
Answers: 1

Re: BluePill F1038C8 Where has all the memory gone

Post by ag123 »

think stm32 is somewhat different from avr, review the user manual rm0008
https://www.st.com/resource/en/referenc ... 171190.pdf
to get a better understanding of it.
a key feature of stm32f103 is the generic usb device, in addition to a large suite of peripherals adc, spi, uart, i2c, timers etc
and like fpiSTM mentioned, you may be able to reduce code size via customization. but literally even stm32f103c8 has 64k flash.
that is normally adequate for most applications and it isn't necessary to be extreme miserly about the flash use
if you need a lot of flash space and you do not want to use external (e.g. spi flash) storage, one way is to get a stm32 board that uses a bigger chip e.g. the rg, ve or zg or even zi series. those has significantly more flash and sram

Jimbo13
Posts: 12
Joined: Sun Feb 16, 2020 6:10 pm
Location: UK

Re: BluePill F1038C8 Where has all the memory gone

Post by Jimbo13 »

OK all, thanks for all that re-assuring advice, I can now move forward knowing that I have not done anything wrong.
I must now got to get to grips with HAL, not sure exactly what it is or how to use it at the moment but I now have plenty of indoor time to find out.

Jimbo13
Posts: 12
Joined: Sun Feb 16, 2020 6:10 pm
Location: UK

Re: [SOLVED]BluePill F1038C8 Where has all the memory gone

Post by Jimbo13 »

Just as follow up information:
I built the Blink programme in Arduino for BluePill, disabling the USART and USB support:
Sketch uses 7429 bytes (11%) of program storage space.
Global variables use 576 bytes (2%) of dynamic memory

Then in "Arduino - Sketch - Add file" I added the hal_conf_extra.h file as fpiSTM suggested (built using Notepad) but remmed out the line "//#define HAL_TIM_MODULE_DISABLED" to allow the Delay to work.
I then got:
Sketch uses 7016 bytes (10%) of program storage space.
Global variables use 228 bytes (1%) of dynamic memory

I then enabled USB support and got:

Sketch uses 16520 bytes (25%) of program storage space.
Global variables use 317 bytes (15%) of dynamic memory

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

Re: [SOLVED]BluePill F1038C8 Where has all the memory gone

Post by fpiSTM »

Delay work without HAL_TIM as it relies on systick not on timer so it can be safely disabled.

Jimbo13
Posts: 12
Joined: Sun Feb 16, 2020 6:10 pm
Location: UK

Re: [SOLVED]BluePill F1038C8 Where has all the memory gone

Post by Jimbo13 »

If I leave that line I get the following errors and just assumed it was that line causing it because it.

In file included from C:\Users\james\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/board.h:8,
from C:\Users\james\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/wiring.h:41,
from C:\Users\james\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/Arduino.h:32,
from sketch\JimsBluePillBlink.ino.cpp:1:
C:\Users\james\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/stm32/analog.h:55:66: error: 'TimerCompareFormat_t' has not been declared
55 | void pwm_start(PinName pin, uint32_t clock_freq, uint32_t value, TimerCompareFormat_t resolution);
| ^~~~~~~~~~~~~~~~~~~~
exit status 1
Error compiling for board Generic STM32F1 series.

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

Re: [SOLVED]BluePill F1038C8 Where has all the memory gone

Post by fpiSTM »

Ah yes there is an issue on the 1.8.0 which is solved in the master:
https://github.com/stm32duino/Arduino_C ... issues/839

sorry, fix will be available in the coming 1.9.0

Jimbo13
Posts: 12
Joined: Sun Feb 16, 2020 6:10 pm
Location: UK

Re: [SOLVED]BluePill F1038C8 Where has all the memory gone

Post by Jimbo13 »

For my project I want to use the things excluded by hal_conf_extra.h
So without hal_conf_extra.h and enabling Serial and USB I get:

Sketch uses 19524 bytes (29%) of program storage space. Maximum is 65536 bytes.
Global variables use 3548 bytes (17%) of dynamic memory, leaving 16932 bytes for local variables. Maximum is 20480 bytes.

Back to my original point, this is a lot of memory to only flash an LED and send simple text on USB before I have even started to add my real programme and assuming I stick to the stated memory size of 65K bytes and not assume there is actually 128k bytes.

Post Reply

Return to “General discussion”