Skip to content

Commit

Permalink
[Core] Allow usage of AVR minimal printf library
Browse files Browse the repository at this point in the history
Enable linking of AVR's libc minimal printf implementation which
can shave ~400 bytes. Usually we use the xprintf implementation
but keyboards that use s(n)printf automatically pull in the AVR
libc implementation, which is ~900 bytes heavy.

Co-authored-by: Sergey Vlasov <sigprof@gmail.com>
  • Loading branch information
KarlK90 and sigprof committed Feb 8, 2022
1 parent a86d6ff commit ad96f25
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/squeezing_avr.md
Expand Up @@ -30,6 +30,19 @@ MAGIC_ENABLE = no
These features are enabled by default, but may not be needed. Double check to make sure, though.
Largest in size is "magic" -- the QMK magic keycodes -- which control things like NKRO toggling, GUI and ALT/CTRL swapping, etc. Disabling it will disable those functions.

If you use `sprintf` or `snprintf` functions you can save around ~400 Bytes by enabling this option.
```make
AVR_USE_MINIMAL_PRINTF = yes
```

This will include smaller implementations from AVRs libc into your Firmware. They are [not fully featured](https://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#gaa3b98c0d17b35642c0f3e4649092b9f1), for instance zero padding and field width specifiers are not supported. So if you use `sprintf` or `snprintf` like this:
```c
sprintf(wpm_str, "%03d", get_current_wpm());
snprintf(keylog_str, sizeof(keylog_str), "%dx%d, k%2d : %c");
```
you will still need the standard implementation.
## `config.h` Settings
If you've done all of that, and you don't want to disable features like RGB, Audio, OLEDs, etc, there are some additional options that you can add to your config.h that can help.
Expand Down
9 changes: 9 additions & 0 deletions platforms/avr/platform.mk
Expand Up @@ -30,6 +30,15 @@ CXXFLAGS += -fno-exceptions -std=c++11

LDFLAGS +=-Wl,--gc-sections

# Use AVR's libc minimal printf implementation which has less features
# and thus can shave ~400 bytes. Usually we use the xprintf
# implementation but keyboards that use s(n)printf automatically
# pull in the AVR libc implementation, which is ~900 bytes heavy.
AVR_USE_MINIMAL_PRINTF ?= no
ifeq ($(strip $(AVR_USE_MINIMAL_PRINTF)), yes)
LDFLAGS += -Wl,--whole-archive -lprintf_min -Wl,--no-whole-archive
endif

OPT_DEFS += -DF_CPU=$(F_CPU)UL

MCUFLAGS = -mmcu=$(MCU)
Expand Down

0 comments on commit ad96f25

Please sign in to comment.