Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move pinStructs array into PROGMEM to save global variables space #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

brewmanz
Copy link

This reduces the global variable space used.
Please check everything, as I'm using only a tiny fraction of FastGPIO in my code.
The generated code seems to be the same.
My test code was

      typedef FastGPIO::Pin<LED_BUILTIN> fastLED;
      for(int ix = 0; ix < aunit::TestRunner::gFailedOrExpiredTestCount; ++ix){ // maybe patch with large-ish number
        fastLED::setInputPulledUp();
        delay(150);
        fastLED::setOutputHigh();
        delay(200);
        fastLED::setInputPulledUp();
        delay(150);
        fastLED::setInput();
        delay(150);
      }

@DavidEGrayson
Copy link
Member

What kind of environment are you using and how are you building your project? I would expect pinStructs to be garbage collected at link time because it is not needed by the code emitted by the compiler.

@brewmanz
Copy link
Author

brewmanz commented Jan 14, 2025

I'm using the Arduino IDE 1.8.20 under Ubuntu 20.04 LTS.
Aha! And I am referencing pinStructs in my sketch.

On compiling for Arduino Nano with my changes, my last 2 lines of compiler output states
Sketch uses 14260 bytes (46%) of program storage space. Maximum is 30720 bytes. Global variables use 568 bytes (27%) of dynamic memory, leaving 1480 bytes for local variables. Maximum is 2048 bytes.
when accessing FastGPIO::pinStructs,
and when NOT accessing FastGPIO::pinStructs it states
Sketch uses 14164 bytes (46%) of program storage space. Maximum is 30720 bytes. Global variables use 568 bytes (27%) of dynamic memory, leaving 1480 bytes for local variables. Maximum is 2048 bytes.
So accessing pinStructs uses extra Flash, but no extra RAM

I then roll back my changes, recompile, and it states
Sketch uses 14260 bytes (46%) of program storage space. Maximum is 30720 bytes. Global variables use 656 bytes (32%) of dynamic memory, leaving 1392 bytes for local variables. Maximum is 2048 bytes.
when accessing FastGPIO::pinStructs,
and when NOT accessing FastGPIO::pinStructs it states
Sketch uses 14164 bytes (46%) of program storage space. Maximum is 30720 bytes. Global variables use 568 bytes (27%) of dynamic memory, leaving 1480 bytes for local variables. Maximum is 2048 bytes.
So accessing pinStructs uses extra Flash, AND extra RAM

One reason for using pinStructs is to dynamically use a variable pin number to enable code like
FastGPIO::pinStructs[someDynamicPin]::setOutputHigh()
which I haven't done yet, but will very soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants