RMK has included many optimizations by default to of binary size. But there are still some tricks to reduce the binary size more. If you got linker error like:
or some errors occur when writing configs to flash, that means that your microcontroller's internal flash is not big enough.
There are several approaches to solve the problem:
DEFMT_LOG levelLogging is quite useful when debugging the firmware, but it requires a lot of flash. You can change the default logging level to error at .cargo/config.toml, to print only error messages and save flash:
panic-haltBy default, RMK uses panic-probe to print error messages if panic occurs. But panic-probe actually takes lots of flash because the panic call can not be optimized. The solution is to use panic-halt instead of panic-probe:
The in main.rs, use panic-halt instead:
defmt-rttYou can also remove the entire defmt-rtt logger to save flash.
In this case, you have to implement an empty defmt logger.
According to embassy's doc, you can set the following in your .cargo/config.toml
And then compile your project with nightly Rust:
This config will reduce about 4-6kb of binary size furthermore.
After applying all above approaches, total binary size of stm32h7 example can be reduced from about 93KB to 54KB, which means the binary size decreases about 42%!
storage feature and defmt featureIf you don't need storage, you can disable the storage feature to save some flash. To disable storage feature you need to disable default features of rmk crate, and then enable features you need manually.
You can also fully remove defmt by removing defmt feature from rmk crate and similar feature gates from all other dependencies.
If you re using keyboard.toml, you'll also need to disable the storage or defmt in toml config: