Skip to content

Only include timer0 overflow ISR and initialization when it is actually used. #320

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

matthijskooijman
Copy link
Collaborator

This applies the same trick we used for HardwareSerial to the timer0-related functions. With this PR, if none of delay, millis or micros is used, the timer0 overlow ISR is not included and enabled.

This has two advantages:

  • Sketches that want to define their own ISR are now free to do so.
  • Sketches that do not need any of these timer functions, save some memory.

Note that delayMicroseconds is not included in the above list, since it does not use timer0 but does cycle counting, so can still be used without pulling in the timer0 ISR.

This is implemented by putting the timer0 related code in a separate .c file, which is only pulled in if any of these functions are referenced (this happens because the core is linked through an .a file). To prevent the (new) init_systick() initialization function, which is called from init() in wiring.c to pull in the file, a weak definition of init_systick() is included in wiring.c which is replaced by the real definition from wiring_systick.c if any of the timer functions are used.

Without this PR, compiling the bare minimum sketch gives:

Sketch uses 444 bytes (1%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.

With this PR applied, the result is:

Sketch uses 270 bytes (0%) of program storage space. Maximum is 32256 bytes.
Global variables use 0 bytes (0%) of dynamic memory, leaving 2048 bytes for local variables. Maximum is 2048 bytes.

This PR is the result of some discussion in the #arduino IRC channel between @rlcamp and myself. @rlcamp implemented most of this PR, I reviewed it and made some perfectioning changes.

rlcamp added 2 commits March 19, 2020 21:41
This moves all timer0-related code into a new file wiring_systick.c.
This is a preparation for a next commit, to allow this code to be
included only when linked. The delayMicroseconds function is not
included in this file, since it does not actually use the timer but does
cycle counting (but for consistency, it is moved into its own file
instead).

This commit only moves code and fixes some whitespace in the process, it
does not actually change anything else.
This prevents pulling in wiring_systick.c, thus omitting timer0 setup
and ISR, unless any of delay(), millis(), or micros() are actually used.
@matthijskooijman
Copy link
Collaborator Author

@facchinm, did you guys have some script to do a bulk compile of all examples or something similar? I think it would be nice to see the impact of this change against a number of examples or real-world sketches. I wrote a script for that a long time ago, but that's probably out of date now (should maybe be updated to arduino-cli, but no point in that if there is something out there already).

@facchinm
Copy link
Member

This is very clever indeed, thanks you very much for submitting it! I'll forward this to the cloud team for a mass compile against Create examples.

@matthijskooijman
Copy link
Collaborator Author

Ah, ther's a cloud-based mass compile tool there, nice! Any of that open? And does it allow comparing things (like compilation failures or sketch sizes with/without a PR applied)?

@matthijskooijman
Copy link
Collaborator Author

This is very clever indeed, thanks you very much for submitting it! I'll forward this to the cloud team for a mass compile against Create examples.

@facchinm, any idea about whether this has bee ndone? Would be nice to move this change forward.

@facchinm
Copy link
Member

@facchinm, any idea about whether this has bee ndone? Would be nice to move this change forward.

No ETA yet, sorry 😞

@CLAassistant
Copy link

CLAassistant commented Apr 9, 2021

CLA assistant check
All committers have signed the CLA.

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

Successfully merging this pull request may close these issues.

4 participants