Binary size
RMK has included many optimizations by default to reduce binary size. But there are still some tricks to reduce the binary size further. If you get a linker error like:
or some errors occur when writing configs to flash, that means your microcontroller's internal flash is not big enough.
For the minimal example, please check out the examples/use_rust/stm32f1 and examples/use_config/stm32f1 examples.
There are several approaches to solve the problem:
Common approaches
Change DEFMT_LOG level
Logging 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:
Enable unstable feature
According to embassy's doc, you can set the following in your .cargo/config.toml
And then compile your project with nightly Rust:
For keyboard.toml users
RMK provides several options that you can use to reduce the binary size:
-
If you don't need storage, you can disable the
storagefeature to save some flash. To disablestoragefeature you need to disable default features ofrmkcrate, and then enable other features you need. -
You can also fully remove
defmtby removingdefmtfeature fromrmkcrate and similar feature gates from all other dependencies. -
If you don't need vial support, you can also disable the
vialfeature by disabling default features ofrmkcrate.
If you're using keyboard.toml, you'll also need to disable storage, defmt, and vial in the toml config:
For Rust code users
Use panic-halt
By 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:
Then in main.rs, use panic-halt instead:
Remove defmt-rtt
You can also remove the entire defmt-rtt logger to save flash.
In this case, you have to implement an empty defmt logger.
Totally remove storage and vial support
You can disable storage and vial feature in Cargo.toml:
And then remove anything no longer needed in main.rs.