Skip to content

Fix for using LTO in Arduino IDE 2.x to reduce flash usage #215

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: main
Choose a base branch
from

Conversation

maxint-rd
Copy link
Contributor

Fix #204, #179, #25. First only for CH32V003, CH32VM00X, other may follow...

Flash memory fills up quickly, especially on chips like the V003 and V002 that have only 16kB of flash.

As mentioned in this comment, one of the ways to reduce flash usage, is to use Link Time Optimization (LTO). LTO removes code that is never called; potentially saving multiple kilobytes of valuable flash memory. This optimization worked great in version 1.8.19 of the Arduino IDE, but didn't work in any v2.x IDE (see also another comment). Note that LTO can also reduce RAM usage.

After lengthy research (see #204) the main cause appeared to be a known issue in the used GCC compiler/linker, related to how weak functions defined in assembly files can take precedence over regular functions that were supposed to redefine their weak counterparts.

For the CH32 Arduino core this issue caused important interrupt handlers such as the SysTick handler to be removed when using LTO, leaving the microcontroller hanging in the endless loop of the default handler when such an interrupt occurred.

So far the best workaround found to get LTO working again, was to move the assembly implementation of the default handlers from startup_ch32....S to a C implementation in ch32...._misc.c. Note: both assembly and C implementations use just two bytes of flash. For the CH32V003 the code is somewhat smaller than before (even without LTO!). Thanks @dfleck for contributing this solution.

This PR gives priority to fixing LTO on 16kB processors (mainly V003/V002). Others may follow later. Although the fix is fairly straightforward, I prefer to only submit tested changes and don't have all member of the CH32 family in my collection.

See #204 for more information.
Related issues: #179, #25,

…ee issue openwch#204)

[CH32VM00X] Move handlers from startup_ch32v00X.S to ch32v00X_misc.c to fix LTO (see issue openwch#204)
[CH32V00x] Move handlers from startup_ch32v00x.S to ch32v00x_misc.c to fix LTO (see issue openwch#204)

[CH32VM00X] Move handlers from startup_ch32v00X.S to ch32v00X_misc.c to fix LTO (see issue openwch#204)

[CH32VM00X] Move handlers from startup_ch32v00X.S to ch32v00X_misc.c to fix LTO (see issue openwch#204)

Move handlers from startup_ch32v00x.S to ch32v00x_misc.c to fix LTO (see issue openwch#204)
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.

Using LTO in Arduino IDE 2.x to reduce flash usage (working!)
1 participant