SMT32L082KZ RAM optimization - looking for tips/tricks

Post here first, or if you can't find a relevant section!
Post Reply
MeneerJacco
Posts: 3
Joined: Fri Nov 13, 2020 3:31 pm
Location: The Netherlands

SMT32L082KZ RAM optimization - looking for tips/tricks

Post by MeneerJacco »

Hello,

I am currently working with a SIMCom NB-IoT modem that communicates over UART to my STM32L082KZ.
When processing large downlink messages (chunks of 512bytes at a time) I run into the issue that at some point my code seems to change variables and send empty strings out (AT commands) which is kinda weird. Am using a vector of strings to store the seperate parameters I find in the AT command responses, which I empty after each response ;) .

Am thinking (but I am no expert) that RAM could be the issue here and overflow somehow causes my variables to change (I have additional components and libraries to handle in this code, so my RAM does not have that much headroom anymore).

I was wondering if there are general tips/tricks for RAM optimization - STM32duino specific or in general - so I can free up some space to make it work.

Any tips/tricks would be appreciated :) .
AndrewBCN
Posts: 105
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 1
Location: Strasbourg, France

Re: SMT32L082KZ RAM optimization - looking for tips/tricks

Post by AndrewBCN »

This is not really an STM32-specific question, so I would suggest you do a quick search with your favorite search engine w/ the following keywords:

* "arduino optimize RAM usage"

* "embedded program save ram space"

etc. That will turn up dozens of links with tips and tricks for saving RAM in your Arduino / STM32 projects.

Now specifically in your case: if the program compiles without any warnings but fails at runtime, then what is happening is that your program is exceeding the available heap space (the heap is where your program stores data during runtime) due to dynamic memory allocation or some other reason.

So to get a better grasp of the problem, I would suggest also searching for:

* "heap space exceeded"

* "dynamic memory allocation"

Then it's just a matter of reading, and applying your newly learned knowledge to solve your problem.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: SMT32L082KZ RAM optimization - looking for tips/tricks

Post by fpiSTM »

Share your code. Then we could probably give time tips.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: SMT32L082KZ RAM optimization - looking for tips/tricks

Post by ag123 »

it seemed that mcu has 20k sram
https://www.st.com/en/microcontrollers- ... 082kz.html
one way is you could get an mcu with more sram, it is easy to run out of sram with just 20k of it
to reduce ram use use as little global variables as is possible, no buffers nothing, this may severely compromise the app.
so one way is you can try to figure out if there is something you don't need and maybe you can comment out or set defines to disable those codes that initialize buffers. you could use place things on stacks so that they get unwound when the function returns.
and don't bother to try things like freertos if you only have 20k sram, freetos (and the likes) allocates stacks for each 'thread' (vtasks) and it fragment that 20k sram so that you have much less for everything
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: SMT32L082KZ RAM optimization - looking for tips/tricks

Post by mrburnette »

ag123 wrote: Sat Jun 12, 2021 2:06 pm it seemed that mcu has 20k sram
https://www.st.com/en/microcontrollers- ... 082kz.html
one way is you could get an mcu with more sram, it is easy to run out of sram with just 20k of it
...
Yes - IF you are certain of a memory overrun. But, I do not think that is proven and Arduino code with 3rd party libs are difficult to characterize. This is often a problem with dynamic memory allocation.

May I suggest you pull all of the libraries (.h & .cpp) files into separate ArduinoIDE tabs. Note you must edit #includes to look in the shetch directory instead of the Arduino library.

Create a pen & paper sketch of program flow and attempt to determine where the issue(s) are located. By using techniques shown here you be able to understand your program better.

It does require a bit of effort to understand how the compiler/linker puts things together and how the runtime manages the RAM.

Image
Credit: https://learn.adafruit.com/memories-of- ... ree-memory

Also, YouTube has a few enlightening videos.

Ray
Post Reply

Return to “General discussion”