KocsisV, I debugged a moment while having breakfast. Heap debugging still works great!
- The "fixed" ram is ~4KB. Screens, buffers, different settings/structures...
- Then, the reserved heap is actually a lot larger than required, it peaked at about 2.1KB.
The options were very constrained at the beginning of development, as the original screen management wasn't dynamic.
I had to sacrifice lots of features until I later made the dynamic loading, while 10KB devices were still having issues with random stuff changing by divine intervention
.
I repeat, the heap issue I mentioned was caused by "syscalls.c" and "sysmem.c" files missing.
Lost a lot of hair until I analyzed the disassembly code and I noticed _sbrk() was returning always a valid pointer, and quickly figured out the issue.
After more optimizations (Which were a lot!) widget space was reduced, then saving was allowed only in boot, main, settings, calibration and reset_confirmation screens, which use at most 356 bytes, keeping the stack under 2KB, almost halving the previous usage.
But to be safe, I allocated all the free ram to it, it doesn't hurt, that ram would be left unused anyways.
In any case, the system allocates all ram at boot to prevent heap fragmentation.
The widgets design is also dynamic, glueing different elements using pointers.
- Screen (52 bytes). The only not dynamically-allocated part of the gui system.
- Points to the next screen, first widget, current widget, customized handling functions...
-Main "widget_t" (28bytes)
- "Content" linked to the widget type. Ex. in the system menu, it's linked to a comboBox_widget_t.
- "next_widget" points for the next widget, only used in non-combo screens, ex. boot screen, reset confirmation...
- comboBox_widget_t (28bytes)
- "first" linked to the first "comboBox_item_t"
- Then each "comboBox_item_t" contains "next" item and so on.
- comboBox_item_t (20bytes)
- Linked to "action_screen", "action" or "editable_widget_t".
- action_screen and action don't use additional space, they hold just integers or a pointer to a function.
- editable_widget_t is linked to an additional widget, so it's also allocated when creating that combo type.
- editable_widget_t (64bytes)
- Content can be can be:
- Editable: "field_int32"(editable integer) or "field_string"(editable string)
- Multioption: Editable integer but the integer is used as index to an array of strings, ex. value 0,1 -> char *opts[]={"OFF","ON"}
As you see, the ram usage is pretty modular, keeping it optimized for each widget configuration, but grows quickly!
Also, each allocation (malloc) adds 8 bytes to handle internal stuff.
SYSTEM menu has only "screen" combo options, doesn't attach editable widgets:
- Main screen widget: 28+8=36
- Combo widget: 28+8=36
- 6x non-editable items= 6*(20+8) = 168
- TOTAL: 240bytes
Now let's go with a larger screen. "SYSTEM"
- Main screen widget: 28+8=36
- Combo widget: 28+8=36
- 15x editable items: 15*((20+8)+(64+8))=1500
- 5x non-editable items= 5*(20+8) = 140
- TOTAL: 1712bytes
IRON is 2KB, and most remaining screens use ~1KB or less.
Nevermind: STM32F101/2/3 don't have USB OTG, so it's a dead end!