mcci-catena / arduino-lmic

LoraWAN-MAC-in-C library, adapted to run under the Arduino environment
https://forum.mcci.io/c/device-software/arduino-lmic/
MIT License
629 stars 207 forks source link

Low memory warning - ways to optimize? #597

Closed KonstantinMastak closed 3 years ago

KonstantinMastak commented 3 years ago

Describe your question or issue

I want to move from matthijskooijman/arduino-lmic to mcci-catena/arduino-lmic for my atmega328p-based project due to LoRaWAN specs compatibility issues.

However, when I try to compile default ttn-otaa example from MCCI-Catena (no changes except "FILLMEIN" contants), I get the following warning:

==== Sketch uses 23884 bytes (74%) of program storage space. Maximum is 32256 bytes. Global variables use 1877 bytes (91%) of dynamic memory, leaving 171 bytes for local variables. Maximum is 2048 bytes. Low memory available, stability problems may occur.

When I compile basically the same ttn-otaa example from matthijskooijman, I get the following:

=== Sketch uses 19986 bytes (61%) of program storage space. Maximum is 32256 bytes. Global variables use 930 bytes (45%) of dynamic memory, leaving 1118 bytes for local variables. Maximum is 2048 bytes.

I have no questions about increased program storage space - this is totally justified. However, increase in global variables usage is dramatic and makes arduino-lmic from mcci-catena unusable on atmega328p in any practical task.

So my question is: is there anything I can do with this doubling of dynamic memory usage like maybe turning off some unnecessary defines, making some small modifications to source code that can move some constants to PROGMEM, etc?

Can you point me to which exact parts of mcci-catena implementation consume twice more dynamic memory in comparison to matthijskooijman implementation?

Environment

This information is all important; it's hard to help without a complete set of answers.

terrillmoore commented 3 years ago

Sorry Konstantin.

It's using more memory primarily because buffer sizes for messages increased to support full-size messages. This was required for compliance. If you chase down the max message sizes, I think you'll find how to shrink those back down. In addition, I suggest you turn off PING and BEACON support; those are only for class B, and class B is not currently working.

KonstantinMastak commented 3 years ago

Terrill, thank you very much for your suggestion!

Turning on DISABLE_PING, DISABLE_BEACONS and turning off LMIC_ENABLE_long_messages resulted in:

Sketch uses 21536 bytes (66%) of program storage space. Maximum is 32256 bytes. Global variables use 1491 bytes (72%) of dynamic memory, leaving 557 bytes for local variables. Maximum is 2048 bytes.

Which is pretty much enough for my needs.

KonstantinMastak commented 3 years ago

The problem is solved, thank you.