Various build size optimizations

This commit is contained in:
daylily
2026-01-10 11:20:58 +08:00
parent 0333c4033e
commit 27cd3a3ea8
7 changed files with 36 additions and 24 deletions
+4
View File
@@ -1,8 +1,12 @@
[target.thumbv7em-none-eabihf] [target.thumbv7em-none-eabihf]
runner = 'probe-rs run --chip STM32F411CEUx' runner = 'probe-rs run --chip STM32F411CEUx'
rustflags = ["-Clinker-plugin-lto"]
[build] [build]
target = "thumbv7em-none-eabihf" target = "thumbv7em-none-eabihf"
[env] [env]
DEFMT_LOG = "debug" DEFMT_LOG = "debug"
[unstable]
build-std = ["compiler_builtins", "core"]
+7 -1
View File
@@ -769,7 +769,6 @@ dependencies = [
"base64", "base64",
"cortex-m", "cortex-m",
"cortex-m-rt", "cortex-m-rt",
"critical-section",
"defmt 1.0.1", "defmt 1.0.1",
"defmt-rtt", "defmt-rtt",
"embassy-executor", "embassy-executor",
@@ -781,6 +780,7 @@ dependencies = [
"embedded-graphics", "embedded-graphics",
"embedded-hal-bus", "embedded-hal-bus",
"epd-waveshare", "epd-waveshare",
"panic-abort",
"panic-probe", "panic-probe",
"postcard", "postcard",
"serde", "serde",
@@ -844,6 +844,12 @@ version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "panic-abort"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e20e6499bbbc412f280b04a42346b356c6fa0753d5fd22b7bd752ff34c778ee"
[[package]] [[package]]
name = "panic-probe" name = "panic-probe"
version = "1.0.0" version = "1.0.0"
+4 -2
View File
@@ -10,7 +10,6 @@ cortex-m = { version = "0.7.7", features = [
"critical-section-single-core", "critical-section-single-core",
] } ] }
cortex-m-rt = "0.7.5" cortex-m-rt = "0.7.5"
critical-section = "1.2.0"
defmt = "1.0.1" defmt = "1.0.1"
defmt-rtt = "1.1.0" defmt-rtt = "1.1.0"
embassy-executor = { version = "0.9.1", features = [ embassy-executor = { version = "0.9.1", features = [
@@ -38,6 +37,7 @@ embassy-usb = { version = "0.5.1", features = ["defmt"] }
embedded-graphics = { version = "0.8.1", features = ["defmt"] } embedded-graphics = { version = "0.8.1", features = ["defmt"] }
embedded-hal-bus = { version = "0.3.0", features = ["async"] } embedded-hal-bus = { version = "0.3.0", features = ["async"] }
epd-waveshare = "0.6.0" epd-waveshare = "0.6.0"
panic-abort = "0.3.2"
panic-probe = { version = "1.0.0", features = ["print-defmt"] } panic-probe = { version = "1.0.0", features = ["print-defmt"] }
postcard = { version = "1.1.3", features = ["defmt"] } postcard = { version = "1.1.3", features = ["defmt"] }
serde = { version = "1.0.228", default-features = false, features = ["derive"] } serde = { version = "1.0.228", default-features = false, features = ["derive"] }
@@ -45,6 +45,8 @@ static_cell = "2.1.1"
[profile.dev] [profile.dev]
opt-level = "s" opt-level = "s"
lto = "fat"
[profile.release] [profile.release]
opt-level = "s" opt-level = "z"
lto = "fat"
+1 -1
View File
@@ -140,7 +140,7 @@ impl App {
async fn handle_get_identification(&mut self) { async fn handle_get_identification(&mut self) {
self.write_response(Response::GetIdentification { self.write_response(Response::GetIdentification {
serial: uid_base64(), serial: uid_base64().await,
model: DeviceType::BWRev1, model: DeviceType::BWRev1,
}) })
.await; .await;
+5 -1
View File
@@ -6,6 +6,7 @@ use crate::{
epd::Epd, epd::Epd,
midi::{make_midi_class, make_usb_builder, usb_task}, midi::{make_midi_class, make_usb_builder, usb_task},
}; };
use defmt_rtt as _; use defmt_rtt as _;
use embassy_executor::{Spawner, main}; use embassy_executor::{Spawner, main};
use embassy_stm32::{ use embassy_stm32::{
@@ -14,6 +15,9 @@ use embassy_stm32::{
time::Hertz, time::Hertz,
usb::{self}, usb::{self},
}; };
#[cfg(not(debug_assertions))]
use panic_abort as _;
#[cfg(debug_assertions)]
use panic_probe as _; use panic_probe as _;
macro_rules! static_ref { macro_rules! static_ref {
@@ -68,7 +72,7 @@ async fn main(spawner: Spawner) {
peri.SPI1, peri.PA4, peri.PA5, peri.PA7, peri.PB10, peri.PB0, peri.PB1, peri.SPI1, peri.PA4, peri.PA5, peri.PA7, peri.PB10, peri.PB0, peri.PB1,
); );
let mut usb_builder = make_usb_builder(peri.USB_OTG_FS, peri.PA12, peri.PA11); let mut usb_builder = make_usb_builder(peri.USB_OTG_FS, peri.PA12, peri.PA11).await;
let midi = make_midi_class(&mut usb_builder); let midi = make_midi_class(&mut usb_builder);
spawner.must_spawn(usb_task(usb_builder)); spawner.must_spawn(usb_task(usb_builder));
+3 -5
View File
@@ -1,4 +1,3 @@
use defmt_rtt as _;
use embassy_executor::task; use embassy_executor::task;
use embassy_stm32::{ use embassy_stm32::{
Peri, Peri,
@@ -6,7 +5,6 @@ use embassy_stm32::{
usb::{self, DmPin, DpPin}, usb::{self, DmPin, DpPin},
}; };
use embassy_usb::{UsbVersion, class::midi::MidiClass}; use embassy_usb::{UsbVersion, class::midi::MidiClass};
use panic_probe as _;
use static_cell::StaticCell; use static_cell::StaticCell;
use crate::{Irqs, uid}; use crate::{Irqs, uid};
@@ -15,19 +13,19 @@ pub type UsbDriver = usb::Driver<'static, USB_OTG_FS>;
pub type UsbBuilder = embassy_usb::Builder<'static, UsbDriver>; pub type UsbBuilder = embassy_usb::Builder<'static, UsbDriver>;
pub type UsbMidi = MidiClass<'static, UsbDriver>; pub type UsbMidi = MidiClass<'static, UsbDriver>;
pub fn make_usb_builder( pub async fn make_usb_builder(
peri: Peri<'static, USB_OTG_FS>, peri: Peri<'static, USB_OTG_FS>,
dp: Peri<'static, impl DpPin<USB_OTG_FS>>, dp: Peri<'static, impl DpPin<USB_OTG_FS>>,
dm: Peri<'static, impl DmPin<USB_OTG_FS>>, dm: Peri<'static, impl DmPin<USB_OTG_FS>>,
) -> UsbBuilder { ) -> UsbBuilder {
let ep_out_buffer = static_ref!([u8; 4096] = [0; 4096]); let ep_out_buffer = static_ref!([u8; 256] = [0; 256]);
let config_descriptor = static_ref!([u8; 256] = [0; 256]); let config_descriptor = static_ref!([u8; 256] = [0; 256]);
let control_buf = static_ref!([u8; 256] = [0; 256]); let control_buf = static_ref!([u8; 256] = [0; 256]);
let mut config = embassy_usb::Config::new(0x1209, 0xc9c9); let mut config = embassy_usb::Config::new(0x1209, 0xc9c9);
config.manufacturer = Some("Project Daylily"); config.manufacturer = Some("Project Daylily");
config.product = Some("Inkclip BW"); config.product = Some("Inkclip BW");
config.serial_number = Some(uid::uid_base64()); config.serial_number = Some(uid::uid_base64().await);
config.bcd_usb = UsbVersion::Two; config.bcd_usb = UsbVersion::Two;
let usb_driver = usb::Driver::new_fs(peri, Irqs, dp, dm, ep_out_buffer, usb::Config::default()); let usb_driver = usb::Driver::new_fs(peri, Irqs, dp, dm, ep_out_buffer, usb::Config::default());
+12 -14
View File
@@ -1,20 +1,18 @@
use base64::prelude::*; use base64::prelude::*;
use embassy_stm32::uid::uid; use embassy_stm32::uid::uid;
use embassy_sync::once_lock::OnceLock;
pub fn uid_base64() -> &'static str { pub async fn uid_base64() -> &'static str {
unsafe { core::str::from_utf8_unchecked(uid_base64_bytes()) } unsafe { core::str::from_utf8_unchecked(uid_base64_bytes().await) }
} }
#[allow(static_mut_refs)] pub async fn uid_base64_bytes() -> &'static [u8; 16] {
pub fn uid_base64_bytes() -> &'static [u8; 16] { static UID_BASE64: OnceLock<[u8; 16]> = OnceLock::new();
static mut UID_BASE64: [u8; 16] = [0; 16]; UID_BASE64.get_or_init(|| {
static mut LOADED: bool = false; let mut b64 = [0u8; 16];
critical_section::with(|_| unsafe { BASE64_STANDARD
if !LOADED { .encode_slice(uid(), &mut b64)
let uid = uid(); .expect("12-byte UID encoded to 16 bytes of base64");
BASE64_STANDARD.encode_slice(uid, &mut UID_BASE64).unwrap(); b64
LOADED = true; })
}
});
unsafe { &*core::ptr::addr_of!(UID_BASE64) }
} }