Author Topic: STM32 project code size problem ( small active load)  (Read 18301 times)

0 Members and 5 Guests are viewing this topic.

Offline Sniper1Topic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: ro
STM32 project code size problem ( small active load)
« on: October 23, 2022, 08:25:18 am »
So i had a STM32 , since i ordered the wrong one and i want to use it if possible, and i was also in need of a small active load (5W+ ) on the heat sink and circuit part i am more or less done designing ( since i mostly copy from Dave s vid and the PWM to analog is the part that concerned me more).
Tangents aside i just set up the pins and such in stm32cubide and the code size exceeded the 16k of my MCU (without any libraries added). I know my code will be relatively small (100-200lines) and i am very surprised that it is so large (also why would one sell such MCUs if u cant run stuff on them?). So after a bit of research i found out how bloated stm32cubide is.

STM32L010K4T6 ( https://www.mouser.co.uk/ProductDetail/511-STM32L010K4T6 )

I want to do this:
Read 3-4 buttons (or a rotary encoder) and based on that increase/decrease PWM duty cycle (that gets filtered to analog) and fed into Dave s op amp+FET constant current circuit. And have the most relevant data live on a I2C LCD. And potentially add code for constant power, constant voltage and constant resistance and maybe potentially a SD card for data logger.
The thing is i know that in Arduino the code would be relatively small even with a LCD lib so i am very confused onto how to proceed. What would a good tool that would reduce the code bloat ? I it a 1 off so multi MCU compatibility is not a concern.
NOTE:
1 i did heat dissipation math and i should be fine up to 6.5W without a fan and with a fan i think i am close to 11W
2 also power supply will be 12V and i am thinking of potentially adding a negative rail with a 555 timer so i have enough swing for the op amp and FET
3 for flashing i have a USB to UART bridge that supports 3v3 logic , a few Arduino and a home made rpi pico (it seems to be working)

PS: regarding the PWM filtering and heat math i am not sure if i should post later here or in another post, the thing is i know i can push it to 5W safely but i am not sure how much further i can actually push it + i know that if i use a slower PWM i get a better resolution but i am not sure about how to filter such wide bands ( atm i only simulated in LTTSpice with 1 frequency and a RC low pass filters and buffers)
« Last Edit: October 23, 2022, 08:29:59 am by Sniper1 »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6926
  • Country: es
Re: STM32 project code size problem ( small active load)
« Reply #1 on: October 23, 2022, 08:28:52 am »
How about compiler optimizations?
Have you set "optimize for size" (Os) ?
Also, try enabling LTO to sabe additional space by adding "-flto" to the compiler and linker arguments.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Sniper1Topic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: ro
Re: STM32 project code size problem ( small active load)
« Reply #2 on: October 23, 2022, 08:30:57 am »
probably not
how do i do this? i am new to STM32.......
 

Offline capt bullshot

  • Super Contributor
  • ***
  • Posts: 3033
  • Country: de
    • Mostly useless stuff, but nice to have: wunderkis.de
Re: STM32 project code size problem ( small active load)
« Reply #3 on: October 23, 2022, 08:52:36 am »
If you're new to STM32, and want to use their tools, I'd recommend you to get a derivative with plenty of Flash and RAM.
E.g. one of the smallish Nucleo-32 boards with a STM32L432 (don't go for a low memory derivative with these boards, they all cost the same).
Once you're familiar with their workflow and tools, you can start to reduce the memory footprint - e.g. by moving from HAL to LL (a setting for code generation within STM32CubeMX), but this will demand some labour from you as HAL and LL have different approaches, and HAL has more working examples than LL.
For the lowest memory footprint, drop that tool at all and write all the low level stuff yourself.
Safety devices hinder evolution
 

Offline Sniper1Topic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: ro
Re: STM32 project code size problem ( small active load)
« Reply #4 on: October 23, 2022, 08:58:29 am »
So after looking into the program i ended up here:
command: gcc
All options :-mcpu=cortex-m0plus -std=gnu11 -g3 -DDEBUG -DUSE_HAL_DRIVER -DSTM32L010x4 -c -I../Core/Inc -I../Drivers/STM32L0xx_HAL_Driver/Inc -I../Drivers/STM32L0xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32L0xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage --specs=nano.specs -mfloat-abi=soft -mthumb


So from what i understand i need to edit command and have it like:
gcc -Os -flto

??
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6926
  • Country: es
Re: STM32 project code size problem ( small active load)
« Reply #5 on: October 23, 2022, 09:00:31 am »
First, ensure you're compiling for Release and not for Debug (Click on the "Hammer" building icon, select Release)
(Edit: You're clearly not with -O0 :))

Then right click on the project -> Properties -> C/C++ Build -> Settings
Ensure the upper tab "Configuration" is set to Release.
Optimization -> choose Optimize for size.

Try compiling now.
Fitting or not, you might want to try this additional setting:

Miscellaneous tab -> Other flags -> Click the adding button (Small icon with green "+") and write "-flto".
Repeat this last step with the miscellaneous tab on the Linker.

Recompile and check the difference in size!

Zip and attach your project if you want it to get checked.
« Last Edit: October 23, 2022, 09:10:14 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Sniper1Topic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: ro
Re: STM32 project code size problem ( small active load)
« Reply #6 on: October 23, 2022, 09:16:21 am »
done

now it is this:

arm-none-eabi-gcc -o "Active_Load.elf" @"objects.list"   -mcpu=cortex-m0plus -T"D:\github-stuff\Progrmable_Active_Loads\Small_load_V1\SW1\STM32L010K4TX_FLASH.ld" --specs=nosys.specs -Wl,-Map="Active_Load.map" -Wl,--gc-sections -static -flto --specs=nano.specs -mfloat-abi=soft -mthumb -Wl,--start-group -lc -lm -Wl,--end-group
e:\st\stm32cubeide_1.8.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.0.202111181127\tools\arm-none-eabi\bin\ld.exe: Active_Load.elf section `._user_heap_stack' will not fit in region `RAM'
e:\st\stm32cubeide_1.8.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.0.202111181127\tools\arm-none-eabi\bin\ld.exe: region `RAM' overflowed by 192 bytes
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:64: Active_Load.elf] Error 1
"make -j8 all" terminated with exit code 2. Build might be incomplete.

so now it is RAM.... Arduino has same amount 2k so again something feels very off here since there is no actual code just pericardial setup
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: STM32 project code size problem ( small active load)
« Reply #7 on: October 23, 2022, 09:22:08 am »
Post mapfile (Active_Load.map).

JW
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6926
  • Country: es
Re: STM32 project code size problem ( small active load)
« Reply #8 on: October 23, 2022, 09:22:59 am »
You might be alllocating a too large buffer somewhere.
Additonally, open CubeMX (Double-click the blue .ioc file), try reducing the heap and stack sizes under "Project Manager".
« Last Edit: October 23, 2022, 09:39:10 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Sniper1Topic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: ro
Re: STM32 project code size problem ( small active load)
« Reply #9 on: October 23, 2022, 09:33:41 am »
there  https://pastebin.com/ivb48bgC 
Also ill try to look at those heap files  it is 0x200 how much should i put 0x100? Also min stack size is 0x400 (what is that ?)
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6926
  • Country: es
Re: STM32 project code size problem ( small active load)
« Reply #10 on: October 23, 2022, 09:38:50 am »
Additonally, open CubeMX (Double-click the blue .ioc file), try reducing the heap and stack sizes under "Project Manager".
Today my messages are getting flooded with [ size] tags automatically, what's wrong with the forum?


Try to find what's using the ram, temporary edit the linker script (STM23xxxx_FLASH.ld) and modify the RAM size.
Code: [Select]
RAM              (xrw) : ORIGIN = 0x20000000, LENGTH = 2KPut 16K for example. Will compile now, and you'll be able to see everything in the Build Analyzer section.
Go to Memory Details, RAM and find out what's using so much.
(Of course, programming that will simply not work)
Once you fix it and get under the 2K limit, put the original size back.

To have an idea of the stack size, open The Static Stack Analyzer window.
Under Call graph, you'll find the worst case scenario (Where a function allocates stack, calls another function, allocates more stack and so on).
Ensure the reserved stack setting in CubeMX meets this, so if you have 400bytes there, you'll be fine with 0x1C0 bytes in CubeMX (448bytes).
Of course, that will change as your code grows, so give it some margin, ex. 0x200 (512bytes).
As you get close to the final code, you might tighten it down.

The heap size is a different thing, that's for the dynamic allocation (malloc and such), often used by some CubeMX functions like the USB library to allocate the buffers.
You can find out the allocated memory with this code
Code: [Select]
#include <malloc.h>
uint32_t getHeapUsage(void){
  return mallinfo().uordblks;
}
Calling getHeapUsage() would return the Heap usage in bytes.
You can call this after HAL initialization.

Can't help further without seeing some code, you might be declaring large arrays, or using the wrong word size for them, can't really tell![/code]
« Last Edit: October 23, 2022, 10:13:21 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Sniper1Topic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: ro
Re: STM32 project code size problem ( small active load)
« Reply #11 on: October 23, 2022, 09:45:43 am »
not sure but it didn't let me attach files, that is why i used paste bin
also a hep of 0x100 is enough ? or rather what does it do and how much can i safely reduce it?
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 6123
  • Country: nl
Re: STM32 project code size problem ( small active load)
« Reply #12 on: October 23, 2022, 09:53:11 am »
To attach files here they should be less then 4MB and have one of the listed extensions.

To add a binary file just add a ".txt" extension after the ".bin" and you can upload it here, provided it is less then the 4MB size limit.

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 6123
  • Country: nl
Re: STM32 project code size problem ( small active load)
« Reply #13 on: October 23, 2022, 09:55:21 am »
Also min stack size is 0x400 (what is that ?)

This reserves 1KB of memory for the call stack used by C. This is where parameters are passed to functions and return addresses are kept.

Offline Sniper1Topic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: ro
Re: STM32 project code size problem ( small active load)
« Reply #14 on: October 23, 2022, 09:55:32 am »
it was about 150k and i made it stuff.map.txt so it was ok in theory but it didnt want to upload
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 6123
  • Country: nl
Re: STM32 project code size problem ( small active load)
« Reply #15 on: October 23, 2022, 09:58:58 am »
That is weird, try to attach a picture to see if it will do it.

Offline Sniper1Topic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: ro
Re: STM32 project code size problem ( small active load)
« Reply #16 on: October 23, 2022, 10:02:17 am »
update:
with a stack of 0x100 it did compile and uses like 96+% of RAM, aparently the largest consumer is user heap stack
i have most of the project on github but the stm32 part is not since i am not sure if uploading all is ok
folders insidde the folder:
.setings
core
debug
drivers
release
what should i upload? also core release and drivers have sub folders , should i upload them too?
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6926
  • Country: es
Re: STM32 project code size problem ( small active load)
« Reply #17 on: October 23, 2022, 10:07:24 am »
CubeMX allocates that Stack by default, for a small project it's usually overkill.

You can attach zip files fine. Just compress it.
There's a very convenient option, Right click ->Export -> Archive file, will create a zip file with the whole project.
But first run Right click -> Clean to wipe all the unnecessary built stuff.

I forgot something: Stack usage analysis won't work if you enable LTO.
« Last Edit: October 23, 2022, 10:18:01 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline cv007

  • Super Contributor
  • ***
  • Posts: 1061
Re: STM32 project code size problem ( small active load)
« Reply #18 on: October 23, 2022, 10:19:18 am »
Your map file shows a stack size of 0x400, heap size of 0x200, bss used 0x2B4, data use 0x0c-
0x400+0x200+0x2B4+0x0C = 0x8C0, 0x8C0 - 0x800 = 0xC0 = the 192 bytes the linker is complaining about.

You just need to adjust the stack/heap sizes to something more appropriate. You could also set both to 0 and forego the linker checking for stack/heap space (via overflowing ram allocation). Making up numbers without evidence is kind of useless anyway (unless they are large and you have the ram to not care).

edit- obviously you need stack space, but you can also monitor data usage (I'll assume the ide has some way to inform, otherwise the toolchain 'size' program can inform). You can also figure out a good number for stack space size that both allows for your stack size usage + some fudge, then set it as a stack size number for the linker to check at compile time to make sure your data does not get into this area. There are various ways to get an idea of the stack space used for your app (and there are ways to make your app panic if that fudge area is breached).
« Last Edit: October 23, 2022, 10:32:52 am by cv007 »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6926
  • Country: es
Re: STM32 project code size problem ( small active load)
« Reply #19 on: October 23, 2022, 10:27:15 am »
Setting stack/heap to 0 won't work.
From my previous experiences, you don't get any type of warning.
If the stack grows too much, it will overwite other memory regions and start a lot of gremlins and funny stuff going in your code.
Suddenly you do Fruits=Apples+Oranges, Eggs turns to -1763, Tomatoes to 0xFFF12 and things like that ::)
Use the heap reported in static stack analuzer call graph!
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline hans

  • Super Contributor
  • ***
  • Posts: 1984
  • Country: 00
Re: STM32 project code size problem ( small active load)
« Reply #20 on: October 23, 2022, 10:29:28 am »
I've used that STM32 before, and the FLASH size is so small that even ST's HAL library is too big too fit.

Yes a minimal program will fit, but if you enable all parts of the HAL it won't.

I think there is a configuration header file for this purpose to enable/disable drivers. It removes the code from compilation and saves code space. For some reason linker settings won't fix this, probably because driver code is called from interrupt vectors and so the compiler still thinks they need to be linked in.

Oh also I recommended to not bother with malloc/heap on that size MCU. Just set heap to 0 and never use malloc. Reduce stack size, which unfortunately is a bit of a 'mystery', but 1kiB seems quite large. You could reduce it till the program doesn't like  it anymore. (Note; there are static analysis tools for this, IIRC GCC also has some, but it's not easy to use/access)
 

Offline Sniper1Topic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: ro
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6926
  • Country: es
Re: STM32 project code size problem ( small active load)
« Reply #22 on: October 23, 2022, 10:34:30 am »
It's the project at Small_load_V1/SW1, right?

Your current Release config uses 59% flash, 72% ram.
Checking the call usage, the highest amount is 252 bytes, so setting the Stack to 0x100 byte (Was 0x200=512bytes) reduces ram to 59%.
Then enabling LTO reduces flash to 43% and slightly increases ram to 64%.

Seems good to me, you have plenty of resources left.
« Last Edit: October 23, 2022, 10:44:12 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4660
  • Country: us
Re: STM32 project code size problem ( small active load)
« Reply #23 on: October 23, 2022, 10:38:02 am »
Quote
I've used that STM32 before, and the FLASH size is so small that even ST's HAL library is too big too fit.
Probably it's too small to use most of the standard C functions, too.  (even with "newlib nano")

While a 16k/2k ARM chip is theoretically as useful as a 16k/2k 8bit microcontroller, pretty much NONE of the libraries are optimized well enough to be useful, unless you're really careful.  :-(

 

Offline Sniper1Topic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: ro
Re: STM32 project code size problem ( small active load)
« Reply #24 on: October 23, 2022, 10:40:17 am »
yes
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

-->