Skip to content

Initialize lazily only on stm32 - #184

Draft
jandelgado wants to merge 1 commit into
masterfrom
initialize_lazily_only_on_stm32
Draft

Initialize lazily only on stm32#184
jandelgado wants to merge 1 commit into
masterfrom
initialize_lazily_only_on_stm32

Conversation

@jandelgado

@jandelgado jandelgado commented Aug 1, 2026

Copy link
Copy Markdown
Owner

The Arduino-based HAL currently lazily initializes the GPIO by calling pinMode on the first call to analogWrite. This lazy initialization was introduced because STM32 needs special treatment here (see #18): STM32duino forbids touching hardware from a global constructor. The cost for all other platforms is an extra flag (byte) and a branch on every analogWrite call, even though only STM32 needs it.

This PR makes lazy initialization opt-in via a kLazyInit_ template parameter (default false), moving init back into the constructor for every platform except STM32, which sets kLazyInit_ = true in jled.h. No public API is changed, the default behavior for non-STM32 platforms now costs zero extra storage/runtime.

@coveralls

coveralls commented Aug 1, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 96.755% (+0.03%) from 96.727% — initialize_lazily_only_on_stm32 into master

@jandelgado
jandelgado changed the base branch from master to move_signal_inversion_to_hal August 1, 2026 16:11
@jandelgado
jandelgado force-pushed the move_signal_inversion_to_hal branch 5 times, most recently from 075747c to e9fef89 Compare August 2, 2026 16:34
@jandelgado
jandelgado force-pushed the initialize_lazily_only_on_stm32 branch from 98c4212 to b530689 Compare August 3, 2026 06:28
@jandelgado
jandelgado force-pushed the move_signal_inversion_to_hal branch from e9fef89 to 7f64f68 Compare August 3, 2026 12:36
@jandelgado
jandelgado force-pushed the initialize_lazily_only_on_stm32 branch from b530689 to d3549b3 Compare August 3, 2026 12:37
@jandelgado
jandelgado force-pushed the move_signal_inversion_to_hal branch from 7f64f68 to 7961ad3 Compare August 3, 2026 14:27
@jandelgado
jandelgado force-pushed the initialize_lazily_only_on_stm32 branch from d3549b3 to 6c48c1d Compare August 3, 2026 14:27
Base automatically changed from move_signal_inversion_to_hal to master August 3, 2026 14:46
…M32 needs it

Deferring pinMode()/analogWriteResolution() to the first analogWrite() call
was only ever needed on STM32duino, which forbids touching hardware from a
global constructor. All other Arduino-compatible platforms paid for the
"already set up" flag and its branch on every analogWrite() call for no
reason.

ArduinoHal gains a kLazyInit_ template parameter (default false = eager init
in the constructor). The flag is stored via a private EBO base that is empty
when kLazyInit_ is false, so the eager path has zero extra storage and the
branch is resolved at compile time via tag dispatch. jled.h sets kLazyInit_
= true only for ARDUINO_ARCH_STM32.
@jandelgado
jandelgado force-pushed the initialize_lazily_only_on_stm32 branch from 6c48c1d to 171f10b Compare August 3, 2026 15:02
@jandelgado jandelgado mentioned this pull request Aug 3, 2026
26 tasks
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