From 115b515e7eeff097f374032241d2727aca16257e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20J=C3=A4ger?= Date: Mon, 3 Aug 2026 00:43:55 +0200 Subject: [PATCH 1/2] feat: create rmk-boot.x linkerscript RMK can use to automagically partition flash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pascal Jäger --- .cargo/config.toml | 5 ++ .github/workflows/release.yml | 1 + Cargo.lock | 36 ++++----- Makefile.toml | 88 +++++++++++++++++---- Readme.org | 120 ++++++++++++++++++---------- build.rs | 143 +++++++++++++++++++++++----------- 6 files changed, 274 insertions(+), 119 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..d217f9d --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,5 @@ +[target.thumbv6m-none-eabi] +runner = "probe-rs run --chip RP2040" + +[target.thumbv7em-none-eabihf] +runner = "probe-rs run --chip nRF52840_xxAA" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 27f7e1e..3095906 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -29,6 +29,7 @@ jobs: files: | *.uf2 *.hex + *.x rmk-boot-nrf52840.elf env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 16abc28..34b110c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,24 +83,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "rmk-boot" -version = "0.1.0" -dependencies = [ - "cortex-m", - "cortex-m-rt", - "embassy-boot", - "embassy-boot-rp", - "embassy-embedded-hal", - "embassy-nrf", - "embassy-rp", - "embassy-sync", - "embassy-time", - "embassy-usb", - "embassy-usb-dfu", - "embedded-storage", -] - [[package]] name = "bytemuck" version = "1.25.0" @@ -1022,6 +1004,24 @@ version = "0.8.53" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47b34b781b31e5d73e9fbc8689c70551fd1ade9a19e3e28cfec8580a79290cc4" +[[package]] +name = "rmk-boot" +version = "0.1.0" +dependencies = [ + "cortex-m", + "cortex-m-rt", + "embassy-boot", + "embassy-boot-rp", + "embassy-embedded-hal", + "embassy-nrf", + "embassy-rp", + "embassy-sync", + "embassy-time", + "embassy-usb", + "embassy-usb-dfu", + "embedded-storage", +] + [[package]] name = "rp-pac" version = "7.0.0" diff --git a/Makefile.toml b/Makefile.toml index e58e273..e143b83 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -8,18 +8,82 @@ install_crate = { crate_name = "flip-link", binary = "flip-link", test_arg = ["- command = "cargo" args = ["build", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-2mb"] +[tasks.copy-x-2mb] +dependencies = ["build-2mb"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv6m-none-eabi --features rp2040-2mb +fi +cp rmk-boot.x rmk-boot-rp2040-2mb.x +''' +] + [tasks.build-4mb] command = "cargo" args = ["build", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-4mb"] +[tasks.copy-x-4mb] +dependencies = ["build-4mb"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv6m-none-eabi --features rp2040-4mb +fi +cp rmk-boot.x rmk-boot-rp2040-4mb.x +''' +] + [tasks.build-8mb] command = "cargo" args = ["build", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-8mb"] +[tasks.copy-x-8mb] +dependencies = ["build-8mb"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv6m-none-eabi --features rp2040-8mb +fi +cp rmk-boot.x rmk-boot-rp2040-8mb.x +''' +] + [tasks.build-16mb] command = "cargo" args = ["build", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-16mb"] +[tasks.copy-x-16mb] +dependencies = ["build-16mb"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv6m-none-eabi --features rp2040-16mb +fi +cp rmk-boot.x rmk-boot-rp2040-16mb.x +''' +] + +[tasks.build-nrf] +command = "cargo" +args = ["build", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52840"] + +[tasks.copy-x-nrf] +dependencies = ["build-nrf"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv7em-none-eabihf --features nrf52840 +fi +cp rmk-boot.x rmk-boot-nrf52840.x +''' +] + [tasks.objcopy-2mb] dependencies = ["install-llvm-tools", "build-2mb"] command = "cargo" @@ -40,37 +104,33 @@ dependencies = ["install-llvm-tools", "build-16mb"] command = "cargo" args = ["objcopy", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-16mb", "--", "-O", "ihex", "rmk-boot-rp2040-16mb.hex"] +[tasks.objcopy-nrf] +dependencies = ["install-llvm-tools", "build-nrf"] +command = "cargo" +args = ["objcopy", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52840", "--", "-O", "ihex", "rmk-boot-nrf52840.hex"] + [tasks.uf2-2mb] -dependencies = ["objcopy-2mb"] +dependencies = ["copy-x-2mb", "objcopy-2mb"] command = "cargo" args = ["hex-to-uf2", "--input-path", "rmk-boot-rp2040-2mb.hex", "--output-path", "rmk-boot-rp2040-2mb.uf2", "--family", "rp2040"] [tasks.uf2-4mb] -dependencies = ["objcopy-4mb"] +dependencies = ["copy-x-4mb", "objcopy-4mb"] command = "cargo" args = ["hex-to-uf2", "--input-path", "rmk-boot-rp2040-4mb.hex", "--output-path", "rmk-boot-rp2040-4mb.uf2", "--family", "rp2040"] [tasks.uf2-8mb] -dependencies = ["objcopy-8mb"] +dependencies = ["copy-x-8mb", "objcopy-8mb"] command = "cargo" args = ["hex-to-uf2", "--input-path", "rmk-boot-rp2040-8mb.hex", "--output-path", "rmk-boot-rp2040-8mb.uf2", "--family", "rp2040"] [tasks.uf2-16mb] -dependencies = ["objcopy-16mb"] +dependencies = ["copy-x-16mb", "objcopy-16mb"] command = "cargo" args = ["hex-to-uf2", "--input-path", "rmk-boot-rp2040-16mb.hex", "--output-path", "rmk-boot-rp2040-16mb.uf2", "--family", "rp2040"] -[tasks.build-nrf] -command = "cargo" -args = ["build", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52840"] - -[tasks.objcopy-nrf] -dependencies = ["install-llvm-tools", "build-nrf"] -command = "cargo" -args = ["objcopy", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52840", "--", "-O", "ihex", "rmk-boot-nrf52840.hex"] - [tasks.uf2-nrf] -dependencies = ["objcopy-nrf"] +dependencies = ["copy-x-nrf", "objcopy-nrf"] command = "cargo" args = ["hex-to-uf2", "--input-path", "rmk-boot-nrf52840.hex", "--output-path", "rmk-boot-nrf52840.uf2", "--family", "nrf52840"] diff --git a/Readme.org b/Readme.org index c4951d5..d5732c3 100644 --- a/Readme.org +++ b/Readme.org @@ -10,6 +10,12 @@ Combine it with RMK's =dfu_rp= or =dfu_nrf= feature — after the initial flash you never need to press BOOTSEL again; all subsequent firmware updates happen over USB via =dfu-util=. +rmk-boot ships a linker script called **=rmk-boot.x=** alongside its firmware +binaries. This file is the single source of truth for the flash partition +layout — RMK reads partition offsets from embedded linker symbols at runtime +via =init_flash_from_linkerscript()=. No manual address calculation, no +matching =memory.x= needed. + * Features - **Dual-slot firmware** — flash is split into ACTIVE and DFU partitions; @@ -23,6 +29,8 @@ over USB via =dfu-util=. - **Flash-size variants** — pre-configured for 2 MB, 4 MB, 8 MB and 16 MB (RP2040) and 1 MB (nRF52840) flash chips. - **UF2 builds** — =cargo make uf2-* = generates ready-to-flash =*.uf2= files. +- **rmk-boot.x** — each build also produces a matching linker script that RMK + consumes at link time for zero-config flash layout. - **Panic handler** — on bootloader panic, blinks SOS in Morse code on the LED so you know something went wrong inside the bootloader itself (as distinct from a firmware panic, which is handled by the app's own handler). @@ -45,14 +53,16 @@ cargo run --release --target thumbv6m-none-eabi --features rp2040-2mb cargo run --release --target thumbv7em-none-eabihf --features nrf52840 #+END_SRC -** Generate a UF2 (for drag-and-drop flashing) +** Generate a UF2 (for drag-and-drop flashing) and rmk-boot.x + +Each =cargo make uf2-*= target also produces a matching =rmk-boot.x= linker script: #+BEGIN_SRC sh -cargo make uf2-2mb # → rmk-boot-rp2040-2mb.uf2 -cargo make uf2-4mb # → rmk-boot-rp2040-4mb.uf2 -cargo make uf2-8mb # → rmk-boot-rp2040-8mb.uf2 -cargo make uf2-16mb # → rmk-boot-rp2040-16mb.uf2 -cargo make uf2-nrf # → rmk-boot-nrf52840.uf2 +cargo make uf2-2mb # → rmk-boot-rp2040-2mb.uf2 + rmk-boot-rp2040-2mb.x +cargo make uf2-4mb # → rmk-boot-rp2040-4mb.uf2 + rmk-boot-rp2040-4mb.x +cargo make uf2-8mb # → rmk-boot-rp2040-8mb.uf2 + rmk-boot-rp2040-8mb.x +cargo make uf2-16mb # → rmk-boot-rp2040-16mb.uf2 + rmk-boot-rp2040-16mb.x +cargo make uf2-nrf # → rmk-boot-nrf52840.uf2 + rmk-boot-nrf52840.x #+END_SRC Copy the =*.uf2= to the =RPI-RP2= mass-storage device that appears when you @@ -73,15 +83,16 @@ to LED (P0_15 by default) starts breathing slowly to show that DFU mode is activ * Flash-size variants -Exactly one feature must be enabled at build time: +Exactly one feature must be enabled at build time. Storage defaults to 32 KB +(8 sectors × 4 K). To change it, edit =STORAGE_SIZE= in =build.rs= and rebuild. -| Feature | Flash size | ACTIVE | DFU | ACTIVE start | DFU start | -|---------------+---------------+--------+--------+--------------+------------| -| =rp2040-2mb= | 2 MB | 944 K | 948 K | 0x10007000 | 0x100F3000 | -| =rp2040-4mb= | 4 MB | 1968 K | 1972 K | 0x10007000 | 0x101F3000 | -| =rp2040-8mb= | 8 MB | 4016 K | 4020 K | 0x10007000 | 0x103F3000 | -| =rp2040-16mb= | 16 MB | 8112 K | 8116 K | 0x10007000 | 0x104F3000 | -| =nrf52840= | 1 MB | 432 K | 436 K | 0x00007000 | 0x00073000 | +| Feature | Flash size | ACTIVE | DFU | ACTIVE start | DFU start | +|---------------+---------------+---------+---------+--------------+------------| +| =rp2040-2mb= | 2 MB | 992 K | 996 K | 0x10007000 | 0x10100000 | +| =rp2040-4mb= | 4 MB | 2016 K | 2020 K | 0x10007000 | 0x101FF000 | +| =rp2040-8mb= | 8 MB | 4064 K | 4068 K | 0x10007000 | 0x103FF000 | +| =rp2040-16mb= | 16 MB | 8160 K | 8164 K | 0x10007000 | 0x107FF000 | +| =nrf52840= | 1 MB | 480 K | 484 K | 0x00007000 | 0x0007B000 | The fixed regions for RP2040 (identical for all variants): @@ -102,14 +113,38 @@ Fixed regions for nRF52840: The 2 MB variant works on boards with 4 MB, 8 MB or 16 MB flash too — it simply leaves the extra space unused. You must switch to a larger variant -only if your RMK firmware exceeds 944 KB. +only if your RMK firmware exceeds 992 KB. * Integration with RMK -rmk-boot's partition layout is the default that RMK's =[dfu]= -configuration expects. When you compile your RMK firmware with the =dfu_rp= or -=dfu_nrf= feature, the DFU USB interface is registered automatically and the -flash partitioning matches rmk-boot's =memory.x=. +RMK integrates with rmk-boot through the **=rmk-boot.x=** linker script. The +file is the single source of truth — it contains both the flash memory layout +(=MEMORY=) for the linker and DFU partition symbols (=__rmk_boot_*=) that RMK +reads at runtime via =init_flash_from_linkerscript()=. + +To use rmk-boot with RMK: + +1. Download the matching =rmk-boot-*.uf2= and =rmk-boot-*.x= from the + [[https://github.com/rmk-rs/rmk-boot/releases][GitHub releases]]. +2. Rename the =.x= file to =rmk-boot.x= and place it next to your =Cargo.toml=. +3. Enable =dfu_rp= or =dfu_nrf= in RMK and add a =[dfu]= section to + =keyboard.toml= (only =led= / =unlock_keys= / =page_size= are needed — + partition offsets come from rmk-boot.x). + +RMK's =init_flash_from_linkerscript()= reads these linker symbols from +=rmk-boot.x= at runtime (all values are flash-relative offsets): + +| Symbol | Description | +|-----------------------------+--------------------------| +| =__rmk_boot_state_offset= | Boot state start | +| =__rmk_boot_state_size= | Boot state size | +| =__rmk_boot_dfu_offset= | DFU download slot start | +| =__rmk_boot_dfu_size= | DFU download slot size | +| =__rmk_boot_storage_offset= | Storage partition start | +| =__rmk_boot_storage_size= | Storage partition size | + +If you build a custom embassy-boot bootloader, define these same symbols in +your own linker script — RMK will pick them up automatically. After the initial flash (bootloader + RMK firmware), all subsequent updates can be done over USB: @@ -243,28 +278,28 @@ PWM peripheral and releases the GPIO pin so the firmware can reclaim it. * Partition layout detail -The build script (=build.rs=) generates =memory.x= at compile time based on the -selected feature. The formula for each variant: +The build script (=build.rs=) generates both =memory.x= (for the bootloader's +own linking) and =rmk-boot.x= (for the firmware's linking) at compile time +based on the selected feature. Both share the same computed values. -- ACTIVE offset: =0x10007000= (fixed, after embassy-boot) -- RP2040 ACTIVE size: =(flash_size - 28K (BOOT2 + bootloader + state) - - STORAGE_SIZE - 4K (1 page)) / 2= -- nRF52840 ACTIVE size: =(flash_size - 28K (bootloader + state) - STORAGE_SIZE - - 4K (1 page)) / 2= +The formula for each variant (=STORAGE_SIZE= defaults to 32 K): + +- ACTIVE offset: =0x10007000= (RP2040) / =0x00007000= (nRF52840) +- ACTIVE size: =(flash_size - 28K - STORAGE_SIZE - 4K) / 2 - DFU offset: =ACTIVE offset + ACTIVE size= -- DFU size: =active_size + 4 K= (one extra page for embassy-boot's swap - algorithm) +- DFU size: =ACTIVE size + 4 K= (one extra page for embassy-boot's swap algorithm) -The linker symbols that embassy-boot reads: +The =rmk-boot.x= file exposes these values as linker symbols that RMK's +=init_flash_from_linkerscript()= reads at runtime (all flash-relative offsets): -| Symbol | Value | -|----------------------------+-------| -| =__bootloader_state_start= | state offset relative to BOOT2 base | -| =__bootloader_state_end= | end of state partition | -| =__bootloader_active_start= | ACTIVE offset relative to BOOT2 base | -| =__bootloader_active_end= | end of ACTIVE partition | -| =__bootloader_dfu_start= | DFU offset relative to BOOT2 base | -| =__bootloader_dfu_end= | end of DFU partition | +| Symbol | Description | +|----------------------------+--------------------------| +| =__rmk_boot_state_offset= | Boot state start | +| =__rmk_boot_state_size= | Boot state size | +| =__rmk_boot_dfu_offset= | DFU download slot start | +| =__rmk_boot_dfu_size= | DFU download slot size | +| =__rmk_boot_storage_offset= | Storage partition start | +| =__rmk_boot_storage_size= | Storage partition size | * Testing the DFU→ACTIVE copy manually (for developers) @@ -275,7 +310,7 @@ cargo build arm-none-eabi-objcopy -O binary target/thumbv6m-none-eabi/debug/firmware firmware.bin # 2. Write it to the DFU partition -probe-rs download --chip RP2040 --binary-format bin --base-address 0x10087000 firmware.bin +probe-rs download --chip RP2040 --binary-format bin --base-address 0x100FF000 firmware.bin # 3. Set the SWAP magic in the state partition python3 -c "open('state_swap.bin', 'wb').write(b'\xF0' + b'\xFF'*4095)" @@ -284,10 +319,10 @@ python3 -c "open('state_swap.bin', 'wb').write(b'\xF0' + b'\xFF'*4095)" probe-rs download --chip RP2040 --binary-format bin --base-address 0x10006000 state_swap.bin # 5. Power-cycle the board -# → LED goes solid for 1 s (forward swap in progress) -# → LED fades (copy in progress) -# → 5 quick blinks (copy done) -# → new firmware boots +# -> LED goes solid for 1 s (forward swap in progress) +# -> LED fades (copy in progress) +# -> 5 quick blinks (copy done) +# -> new firmware boots #+END_SRC * Notes @@ -299,4 +334,3 @@ probe-rs download --chip RP2040 --binary-format bin --base-address 0x10006000 st - If you use the =Pico W= board, the on-board CYW43 wireless LED is not connected to a normal GPIO — pick a different pin such as =GPIO 16= (the default) for an external LED. - diff --git a/build.rs b/build.rs index d576b76..92dc4a2 100644 --- a/build.rs +++ b/build.rs @@ -1,45 +1,43 @@ use std::env; use std::fs; -use std::path::PathBuf; +use std::path::Path; fn main() { - let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + let out_dir = env::var("OUT_DIR").unwrap(); + let out = Path::new(&out_dir); let is_rp2040 = env::var("CARGO_FEATURE_RP2040").is_ok(); let is_nrf52840 = env::var("CARGO_FEATURE_NRF52840").is_ok(); const PAGE_SIZE: usize = 4 * 1024; - - fn fmt_size(bytes: usize) -> String { - if bytes % (1024 * 1024) == 0 { - format!("{}M", bytes / (1024 * 1024)) - } else if bytes % 1024 == 0 { - format!("{}K", bytes / 1024) - } else { - bytes.to_string() - } - } + const STORAGE_SIZE: usize = 32 * 1024; if is_rp2040 { - let flash_size = if env::var("CARGO_FEATURE_RP2040_2MB").is_ok() { - 2 * 1024 * 1024 + let (variant_label, flash_size) = if env::var("CARGO_FEATURE_RP2040_2MB").is_ok() { + ("RP2040 2 MB", 2 * 1024 * 1024) } else if env::var("CARGO_FEATURE_RP2040_4MB").is_ok() { - 4 * 1024 * 1024 + ("RP2040 4 MB", 4 * 1024 * 1024) } else if env::var("CARGO_FEATURE_RP2040_8MB").is_ok() { - 8 * 1024 * 1024 + ("RP2040 8 MB", 8 * 1024 * 1024) } else if env::var("CARGO_FEATURE_RP2040_16MB").is_ok() { - 16 * 1024 * 1024 + ("RP2040 16 MB", 16 * 1024 * 1024) } else { panic!("No RP2040 flash size feature enabled"); }; - let flash_base = 0x1000_0000u32; - const STORAGE_SIZE: usize = 128 * 1024; let remaining = flash_size - 28 * 1024 - STORAGE_SIZE; let active_size = (remaining - PAGE_SIZE) / 2; let dfu_size = active_size + PAGE_SIZE; - let active_offset = flash_base + 0x7000; - let dfu_offset = active_offset + active_size as u32; + // Absolute XIP addresses + let abs_active_offset = 0x1000_7000u32; + let abs_dfu_offset = abs_active_offset + active_size as u32; + let abs_state_offset = 0x1000_6000u32; + // Flash-relative offsets (for DFU symbols) + let flash_base = 0x1000_0000u32; + let rel_state_offset = abs_state_offset - flash_base; + let rel_dfu_offset = abs_dfu_offset - flash_base; + let rel_dfu_size = dfu_size as u32; + let rel_storage_offset = abs_dfu_offset - flash_base + dfu_size as u32; let memory_x = format!( "\ @@ -48,8 +46,8 @@ MEMORY BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100 FLASH : ORIGIN = 0x10000100, LENGTH = 24K - 0x100 BOOTLOADER_STATE : ORIGIN = 0x10006000, LENGTH = 4K - ACTIVE : ORIGIN = 0x{:08X}, LENGTH = {} - DFU : ORIGIN = 0x{:08X}, LENGTH = {} + ACTIVE : ORIGIN = 0x{abs_active_offset:08X}, LENGTH = {active_size} + DFU : ORIGIN = 0x{abs_dfu_offset:08X}, LENGTH = {dfu_size} RAM : ORIGIN = 0x20000000, LENGTH = 256K }} @@ -62,12 +60,10 @@ __bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE) - ORIGIN(BOOT2); __bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(BOOT2); __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOT2); -", - active_offset, fmt_size(active_size), - dfu_offset, fmt_size(dfu_size), +" ); - fs::write(out.join("memory.x"), memory_x).unwrap(); + fs::write(out.join("memory.x"), &memory_x).unwrap(); println!("cargo:rustc-link-search={}", out.display()); println!("cargo:rustc-link-arg-bins=-Tlink.x"); println!("cargo:rustc-link-arg-bins=-Tlink-rp.x"); @@ -76,27 +72,40 @@ __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOT2); println!("cargo:rerun-if-env-changed=CARGO_FEATURE_RP2040_4MB"); println!("cargo:rerun-if-env-changed=CARGO_FEATURE_RP2040_8MB"); println!("cargo:rerun-if-env-changed=CARGO_FEATURE_RP2040_16MB"); + + let rmk_boot_x = build_rmk_boot_x( + variant_label, + abs_active_offset, + active_size as u32, + rel_state_offset, + 0x1000, + rel_dfu_offset, + rel_dfu_size, + rel_storage_offset, + STORAGE_SIZE as u32, + ); + let project_root = Path::new(env!("CARGO_MANIFEST_DIR")); + fs::write(project_root.join("rmk-boot.x"), &rmk_boot_x).unwrap(); } else if is_nrf52840 { let flash_size = 1024 * 1024; let bootloader_size = 24 * 1024; let state_size = 4 * 1024; - let storage_size = 128 * 1024; - let remaining = flash_size - bootloader_size - state_size - storage_size; + let remaining = flash_size - bootloader_size - state_size - STORAGE_SIZE; let active_size = (remaining - PAGE_SIZE) / 2; let dfu_size = active_size + PAGE_SIZE; - let state_offset = bootloader_size as u32; - let active_offset = (bootloader_size + state_size) as u32; - let dfu_offset = active_offset + active_size as u32; + let abs_state_offset = bootloader_size as u32; + let abs_active_offset = (bootloader_size + state_size) as u32; + let abs_dfu_offset = abs_active_offset + active_size as u32; let memory_x = format!( "\ MEMORY {{ FLASH : ORIGIN = 0x00000000, LENGTH = {bootloader_size} - BOOTLOADER_STATE : ORIGIN = 0x{state_offset:08X}, LENGTH = {state_size} - ACTIVE : ORIGIN = 0x{active_offset:08X}, LENGTH = {active_size} - DFU : ORIGIN = 0x{dfu_offset:08X}, LENGTH = {dfu_size} + BOOTLOADER_STATE : ORIGIN = 0x{abs_state_offset:08X}, LENGTH = {state_size} + ACTIVE : ORIGIN = 0x{abs_active_offset:08X}, LENGTH = {active_size} + DFU : ORIGIN = 0x{abs_dfu_offset:08X}, LENGTH = {dfu_size} RAM : ORIGIN = 0x20000000, LENGTH = 256K }} @@ -109,22 +118,68 @@ __bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE); __bootloader_dfu_start = ORIGIN(DFU); __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU); -", - bootloader_size = bootloader_size, - state_offset = state_offset, - state_size = state_size, - active_offset = active_offset, - active_size = fmt_size(active_size), - dfu_offset = dfu_offset, - dfu_size = fmt_size(dfu_size), +" ); - fs::write(out.join("memory.x"), memory_x).unwrap(); + fs::write(out.join("memory.x"), &memory_x).unwrap(); println!("cargo:rustc-link-search={}", out.display()); println!("cargo:rustc-link-arg-bins=-Tlink.x"); + + let rmk_boot_x = build_rmk_boot_x( + "nRF52840", + abs_active_offset, + active_size as u32, + abs_state_offset, + state_size as u32, + abs_dfu_offset, + dfu_size as u32, + abs_dfu_offset + dfu_size as u32, + STORAGE_SIZE as u32, // nRF flash_base is 0, so absolute = relative + ); + let project_root = Path::new(env!("CARGO_MANIFEST_DIR")); + fs::write(project_root.join("rmk-boot.x"), &rmk_boot_x).unwrap(); } else { panic!("No platform feature enabled (rp2040 or nrf52840)"); } println!("cargo:rerun-if-changed=build.rs"); } + +fn build_rmk_boot_x( + variant: &str, + active_offset: u32, + active_size: u32, + state_offset: u32, + state_size: u32, + dfu_offset: u32, + dfu_size: u32, + storage_offset: u32, + storage_size: u32, +) -> String { + format!( + "\ +/* rmk-boot linker script for {variant} — generated by rmk-boot/build.rs + * + * Provides both the MEMORY layout (absolute XIP addresses) and + * flash-relative DFU symbols consumed by init_flash_from_linkerscript(). + * + * If your board has a different flash size, replace this file with the + * matching variant from the rmk-boot releases: + * https://github.com/rmk-rs/rmk-boot/releases + */ + +MEMORY {{ + FLASH : ORIGIN = 0x{active_offset:08X}, LENGTH = {active_size} /* ACTIVE region */ + RAM : ORIGIN = 0x20000000, LENGTH = 256K /* SRAM */ +}} + +/* DFU partition symbols — offsets relative to flash start */ +__rmk_boot_state_offset = 0x{state_offset:X}; +__rmk_boot_state_size = 0x{state_size:X}; +__rmk_boot_dfu_offset = 0x{dfu_offset:X}; +__rmk_boot_dfu_size = 0x{dfu_size:X}; +__rmk_boot_storage_offset = 0x{storage_offset:X}; +__rmk_boot_storage_size = 0x{storage_size:X}; +" + ) +} From b9c4a61552723e4b2ea2aba547750d38df759e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20J=C3=A4ger?= Date: Mon, 3 Aug 2026 14:42:46 +0200 Subject: [PATCH 2/2] feat: nrf52833, noswap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pascal Jäger --- .github/workflows/release.yml | 14 ++- Cargo.toml | 7 +- Makefile.toml | 200 ++++++++++++++++++++++++++++++++-- Readme.org | 85 +++++++++------ build.rs | 118 ++++++++++++++++++-- src/dfu.rs | 4 +- src/led_pwm.rs | 12 +- src/main.rs | 21 ++-- src/nrf52840.rs | 127 ++++++++++++--------- src/rp2040.rs | 125 +++++++++++++-------- 10 files changed, 541 insertions(+), 172 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3095906..b6d11c5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -20,8 +20,15 @@ jobs: - name: Build all UF2 variants run: cargo make uf2-all - - name: Copy nRF ELF - run: cp target/thumbv7em-none-eabihf/release/rmk-boot rmk-boot-nrf52840.elf + - name: Copy nRF ELFs + run: | + cp target/thumbv7em-none-eabihf/release/rmk-boot rmk-boot-nrf52840.elf + cargo build --release --target thumbv7em-none-eabihf --features nrf52833 + cp target/thumbv7em-none-eabihf/release/rmk-boot rmk-boot-nrf52833.elf + cargo build --release --target thumbv7em-none-eabihf --features nrf52840,noswap + cp target/thumbv7em-none-eabihf/release/rmk-boot rmk-boot-nrf52840-noswap.elf + cargo build --release --target thumbv7em-none-eabihf --features nrf52833,noswap + cp target/thumbv7em-none-eabihf/release/rmk-boot rmk-boot-nrf52833-noswap.elf - name: Upload release assets uses: softprops/action-gh-release@v2 @@ -31,5 +38,8 @@ jobs: *.hex *.x rmk-boot-nrf52840.elf + rmk-boot-nrf52833.elf + rmk-boot-nrf52840-noswap.elf + rmk-boot-nrf52833-noswap.elf env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 57758ae..4cbd001 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,9 +25,8 @@ embassy-usb-dfu = { version = "0.3", default-features = false, features = [ "cortex-m", ], optional = true } -# nRF52840 +# nRF52 embassy-nrf = { version = "0.10", features = [ - "nrf52840", "time-driver-rtc1", "unstable-pac", ], optional = true } @@ -39,7 +38,9 @@ rp2040-2mb = ["rp2040"] rp2040-4mb = ["rp2040"] rp2040-8mb = ["rp2040"] rp2040-16mb = ["rp2040"] -nrf52840 = ["embassy-nrf", "cortex-m/critical-section-single-core", "embassy-usb", "embassy-usb-dfu"] +nrf52840 = ["embassy-nrf", "embassy-nrf/nrf52840", "cortex-m/critical-section-single-core", "embassy-usb", "embassy-usb-dfu"] +nrf52833 = ["embassy-nrf", "embassy-nrf/nrf52833", "cortex-m/critical-section-single-core", "embassy-usb", "embassy-usb-dfu"] +noswap = [] [profile.release] opt-level = "z" diff --git a/Makefile.toml b/Makefile.toml index e143b83..f3859c7 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -68,12 +68,12 @@ cp rmk-boot.x rmk-boot-rp2040-16mb.x ''' ] -[tasks.build-nrf] +[tasks.build-nrf52840] command = "cargo" args = ["build", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52840"] -[tasks.copy-x-nrf] -dependencies = ["build-nrf"] +[tasks.copy-x-nrf52840] +dependencies = ["build-nrf52840"] script = [ ''' if [ ! -f rmk-boot.x ]; then @@ -104,8 +104,8 @@ dependencies = ["install-llvm-tools", "build-16mb"] command = "cargo" args = ["objcopy", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-16mb", "--", "-O", "ihex", "rmk-boot-rp2040-16mb.hex"] -[tasks.objcopy-nrf] -dependencies = ["install-llvm-tools", "build-nrf"] +[tasks.objcopy-nrf52840] +dependencies = ["install-llvm-tools", "build-nrf52840"] command = "cargo" args = ["objcopy", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52840", "--", "-O", "ihex", "rmk-boot-nrf52840.hex"] @@ -129,10 +129,194 @@ dependencies = ["copy-x-16mb", "objcopy-16mb"] command = "cargo" args = ["hex-to-uf2", "--input-path", "rmk-boot-rp2040-16mb.hex", "--output-path", "rmk-boot-rp2040-16mb.uf2", "--family", "rp2040"] -[tasks.uf2-nrf] -dependencies = ["copy-x-nrf", "objcopy-nrf"] +[tasks.build-nrf52833] +command = "cargo" +args = ["build", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52833"] + +[tasks.copy-x-nrf52833] +dependencies = ["build-nrf52833"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv7em-none-eabihf --features nrf52833 +fi +cp rmk-boot.x rmk-boot-nrf52833.x +''' +] + +[tasks.objcopy-nrf52833] +dependencies = ["install-llvm-tools", "build-nrf52833"] +command = "cargo" +args = ["objcopy", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52833", "--", "-O", "ihex", "rmk-boot-nrf52833.hex"] + +[tasks.uf2-nrf52833] +dependencies = ["copy-x-nrf52833", "objcopy-nrf52833"] +command = "cargo" +args = ["hex-to-uf2", "--input-path", "rmk-boot-nrf52833.hex", "--output-path", "rmk-boot-nrf52833.uf2", "--family", "0x621E937A"] + +[tasks.uf2-nrf52840] +dependencies = ["copy-x-nrf52840", "objcopy-nrf52840"] command = "cargo" args = ["hex-to-uf2", "--input-path", "rmk-boot-nrf52840.hex", "--output-path", "rmk-boot-nrf52840.uf2", "--family", "nrf52840"] [tasks.uf2-all] -dependencies = ["uf2-2mb", "uf2-4mb", "uf2-8mb", "uf2-16mb", "uf2-nrf"] +dependencies = ["uf2-2mb", "uf2-4mb", "uf2-8mb", "uf2-16mb", "uf2-nrf52840", "uf2-nrf52833", "uf2-2mb-noswap", "uf2-4mb-noswap", "uf2-8mb-noswap", "uf2-16mb-noswap", "uf2-nrf52840-noswap", "uf2-nrf52833-noswap"] + +# ── noswap variants ── + +[tasks.build-2mb-noswap] +command = "cargo" +args = ["build", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-2mb,noswap"] + +[tasks.copy-x-2mb-noswap] +dependencies = ["build-2mb-noswap"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv6m-none-eabi --features rp2040-2mb,noswap +fi +cp rmk-boot.x rmk-boot-rp2040-2mb-noswap.x +''' +] + +[tasks.objcopy-2mb-noswap] +dependencies = ["install-llvm-tools", "build-2mb-noswap"] +command = "cargo" +args = ["objcopy", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-2mb,noswap", "--", "-O", "ihex", "rmk-boot-rp2040-2mb-noswap.hex"] + +[tasks.uf2-2mb-noswap] +dependencies = ["copy-x-2mb-noswap", "objcopy-2mb-noswap"] +command = "cargo" +args = ["hex-to-uf2", "--input-path", "rmk-boot-rp2040-2mb-noswap.hex", "--output-path", "rmk-boot-rp2040-2mb-noswap.uf2", "--family", "rp2040"] + +[tasks.build-4mb-noswap] +command = "cargo" +args = ["build", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-4mb,noswap"] + +[tasks.copy-x-4mb-noswap] +dependencies = ["build-4mb-noswap"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv6m-none-eabi --features rp2040-4mb,noswap +fi +cp rmk-boot.x rmk-boot-rp2040-4mb-noswap.x +''' +] + +[tasks.objcopy-4mb-noswap] +dependencies = ["install-llvm-tools", "build-4mb-noswap"] +command = "cargo" +args = ["objcopy", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-4mb,noswap", "--", "-O", "ihex", "rmk-boot-rp2040-4mb-noswap.hex"] + +[tasks.uf2-4mb-noswap] +dependencies = ["copy-x-4mb-noswap", "objcopy-4mb-noswap"] +command = "cargo" +args = ["hex-to-uf2", "--input-path", "rmk-boot-rp2040-4mb-noswap.hex", "--output-path", "rmk-boot-rp2040-4mb-noswap.uf2", "--family", "rp2040"] + +[tasks.build-8mb-noswap] +command = "cargo" +args = ["build", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-8mb,noswap"] + +[tasks.copy-x-8mb-noswap] +dependencies = ["build-8mb-noswap"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv6m-none-eabi --features rp2040-8mb,noswap +fi +cp rmk-boot.x rmk-boot-rp2040-8mb-noswap.x +''' +] + +[tasks.objcopy-8mb-noswap] +dependencies = ["install-llvm-tools", "build-8mb-noswap"] +command = "cargo" +args = ["objcopy", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-8mb,noswap", "--", "-O", "ihex", "rmk-boot-rp2040-8mb-noswap.hex"] + +[tasks.uf2-8mb-noswap] +dependencies = ["copy-x-8mb-noswap", "objcopy-8mb-noswap"] +command = "cargo" +args = ["hex-to-uf2", "--input-path", "rmk-boot-rp2040-8mb-noswap.hex", "--output-path", "rmk-boot-rp2040-8mb-noswap.uf2", "--family", "rp2040"] + +[tasks.build-16mb-noswap] +command = "cargo" +args = ["build", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-16mb,noswap"] + +[tasks.copy-x-16mb-noswap] +dependencies = ["build-16mb-noswap"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv6m-none-eabi --features rp2040-16mb,noswap +fi +cp rmk-boot.x rmk-boot-rp2040-16mb-noswap.x +''' +] + +[tasks.objcopy-16mb-noswap] +dependencies = ["install-llvm-tools", "build-16mb-noswap"] +command = "cargo" +args = ["objcopy", "--release", "--target", "thumbv6m-none-eabi", "--features", "rp2040-16mb,noswap", "--", "-O", "ihex", "rmk-boot-rp2040-16mb-noswap.hex"] + +[tasks.uf2-16mb-noswap] +dependencies = ["copy-x-16mb-noswap", "objcopy-16mb-noswap"] +command = "cargo" +args = ["hex-to-uf2", "--input-path", "rmk-boot-rp2040-16mb-noswap.hex", "--output-path", "rmk-boot-rp2040-16mb-noswap.uf2", "--family", "rp2040"] + +[tasks.build-nrf52840-noswap] +command = "cargo" +args = ["build", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52840,noswap"] + +[tasks.copy-x-nrf52840-noswap] +dependencies = ["build-nrf52840-noswap"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv7em-none-eabihf --features nrf52840,noswap +fi +cp rmk-boot.x rmk-boot-nrf52840-noswap.x +''' +] + +[tasks.objcopy-nrf52840-noswap] +dependencies = ["install-llvm-tools", "build-nrf52840-noswap"] +command = "cargo" +args = ["objcopy", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52840,noswap", "--", "-O", "ihex", "rmk-boot-nrf52840-noswap.hex"] + +[tasks.uf2-nrf52840-noswap] +dependencies = ["copy-x-nrf52840-noswap", "objcopy-nrf52840-noswap"] +command = "cargo" +args = ["hex-to-uf2", "--input-path", "rmk-boot-nrf52840-noswap.hex", "--output-path", "rmk-boot-nrf52840-noswap.uf2", "--family", "nrf52840"] + +[tasks.build-nrf52833-noswap] +command = "cargo" +args = ["build", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52833,noswap"] + +[tasks.copy-x-nrf52833-noswap] +dependencies = ["build-nrf52833-noswap"] +script = [ +''' +if [ ! -f rmk-boot.x ]; then + touch build.rs + cargo build --release --target thumbv7em-none-eabihf --features nrf52833,noswap +fi +cp rmk-boot.x rmk-boot-nrf52833-noswap.x +''' +] + +[tasks.objcopy-nrf52833-noswap] +dependencies = ["install-llvm-tools", "build-nrf52833-noswap"] +command = "cargo" +args = ["objcopy", "--release", "--target", "thumbv7em-none-eabihf", "--features", "nrf52833,noswap", "--", "-O", "ihex", "rmk-boot-nrf52833-noswap.hex"] + +[tasks.uf2-nrf52833-noswap] +dependencies = ["copy-x-nrf52833-noswap", "objcopy-nrf52833-noswap"] +command = "cargo" +args = ["hex-to-uf2", "--input-path", "rmk-boot-nrf52833-noswap.hex", "--output-path", "rmk-boot-nrf52833-noswap.uf2", "--family", "0x621E937A"] diff --git a/Readme.org b/Readme.org index d5732c3..835c117 100644 --- a/Readme.org +++ b/Readme.org @@ -1,8 +1,8 @@ #+TITLE: rmk-boot -#+SUBTITLE: A pre-built embassy-boot bootloader for RP2040 and nRF52840 +#+SUBTITLE: A pre-built embassy-boot bootloader for RP2040 and nRF52840 / nRF52833 A ready-to-use [[https://github.com/embassy-rs/embassy/tree/main/embassy-boot][embassy-boot]] bootloader for the Raspberry Pi RP2040 and -NRF52840. It sits at the beginning of flash, handles dual-slot firmware +nRF52840 / nRF52833. It sits at the beginning of flash, handles dual-slot firmware switching (active / DFU) with automatic rollback on failure, and signals its state via a PWM-driven LED. @@ -24,10 +24,15 @@ matching =memory.x= needed. - **Power-loss safe** — the swap operation is crash-recoverable; a partially written ACTIVE slot is detected and rolled back. - **LED** — a single GPIO LED to signal bootloader states -- **USB DFU via double-tap (nRF52840 only)** — two NRST resets within ~500 ms +- **USB DFU via double-tap (nRF52 only)** — two NRST resets within ~500 ms enter DFU mode (similar to Adafruit bootloader) - **Flash-size variants** — pre-configured for 2 MB, 4 MB, 8 MB and 16 MB - (RP2040) and 1 MB (nRF52840) flash chips. + (RP2040), 1 MB (nRF52840), and 512 KB (nRF52833) flash chips. +- **noswap mode** — build with =noswap= feature to disable the swap mechanism + entirely. The bootloader always boots directly into ACTIVE (no state checks, + no DFU→ACTIVE copy). Double-tap DFU still works and writes directly to + ACTIVE instead of a separate DFU slot. Ideal for smaller flash chips where + RMK is too large for a dual-slot layout. - **UF2 builds** — =cargo make uf2-* = generates ready-to-flash =*.uf2= files. - **rmk-boot.x** — each build also produces a matching linker script that RMK consumes at link time for zero-config flash layout. @@ -39,9 +44,9 @@ matching =memory.x= needed. ** Build -Needs a target toolchain, install with =rustup target add thumbv6m-none-eabi= (RP2040) / =rustup target add thumbv7em-none-eabihf= (NRF52840) +Needs a target toolchain, install with =rustup target add thumbv6m-none-eabi= (RP2040) / =rustup target add thumbv7em-none-eabihf= (nRF52840 / nRF52833) #+BEGIN_SRC sh -cargo make build-2mb # or build-4mb / build-8mb / build-16mb / build-nrf +cargo make build-2mb # or build-4mb / build-8mb / build-16mb / build-nrf52840 / build-nrf52833 #+END_SRC ** Flash via probe-rs @@ -49,8 +54,10 @@ cargo make build-2mb # or build-4mb / build-8mb / build-16mb / build-nrf #+BEGIN_SRC sh # RP2040 cargo run --release --target thumbv6m-none-eabi --features rp2040-2mb -# NRF52840 +# nRF52840 cargo run --release --target thumbv7em-none-eabihf --features nrf52840 +# nRF52833 +cargo run --release --target thumbv7em-none-eabihf --features nrf52833 #+END_SRC ** Generate a UF2 (for drag-and-drop flashing) and rmk-boot.x @@ -62,24 +69,35 @@ cargo make uf2-2mb # → rmk-boot-rp2040-2mb.uf2 + rmk-boot-rp2040-2mb.x cargo make uf2-4mb # → rmk-boot-rp2040-4mb.uf2 + rmk-boot-rp2040-4mb.x cargo make uf2-8mb # → rmk-boot-rp2040-8mb.uf2 + rmk-boot-rp2040-8mb.x cargo make uf2-16mb # → rmk-boot-rp2040-16mb.uf2 + rmk-boot-rp2040-16mb.x -cargo make uf2-nrf # → rmk-boot-nrf52840.uf2 + rmk-boot-nrf52840.x +cargo make uf2-nrf52840 # → rmk-boot-nrf52840.uf2 + rmk-boot-nrf52840.x +cargo make uf2-nrf52833 # → rmk-boot-nrf52833.uf2 + rmk-boot-nrf52833.x +#+END_SRC + +All variants also have =*-noswap= counterparts (e.g. =uf2-nrf52833-noswap=) that +skip the DFU→ACTIVE swap and boot directly into ACTIVE. +Build all at once: +#+BEGIN_SRC sh +cargo make uf2-all #+END_SRC Copy the =*.uf2= to the =RPI-RP2= mass-storage device that appears when you hold BOOTSEL while plugging in USB. -For nRF52840 this only works if it has the Adafruit UF2 bootloader installed. -**BE AWARE THAT RMK-BOOT OVERWRITES THAT BOOTLOADER ON THE NRF52840!** +For nRF52 this only works if the chip has the Adafruit UF2 bootloader installed. +**BE AWARE THAT RMK-BOOT OVERWRITES THAT BOOTLOADER ON THE NRF52!** -* Entering DFU mode (nRF52840) +* Entering DFU mode (nRF52) -Because the nRF52840 does not have a built in bootloader in ROM, flashing a firmware +Because the nRF52840 / nRF52833 does not have a built in bootloader in ROM, flashing a firmware that does not implement DFU flashing or does not boot, can make flashing impossible. (if no debugger is available) Therefore the bootloader provides the possibility to flash firmware via DFU. The bootloader implements the **double-tap** convention known from the Adafruit -bootloader for the nrf52840. Double tap NRST (reset pin) to GND to enter DFU mode, -to LED (P0_15 by default) starts breathing slowly to show that DFU mode is active. +bootloader for the nRF52. Double tap NRST (reset pin) to GND to enter DFU mode, +the LED (P0_15 by default) starts breathing slowly to show that DFU mode is active. + +With the =noswap= feature, DFU mode writes new firmware directly to the ACTIVE +partition instead of a separate DFU slot. * Flash-size variants @@ -93,6 +111,7 @@ Exactly one feature must be enabled at build time. Storage defaults to 32 KB | =rp2040-8mb= | 8 MB | 4064 K | 4068 K | 0x10007000 | 0x103FF000 | | =rp2040-16mb= | 16 MB | 8160 K | 8164 K | 0x10007000 | 0x107FF000 | | =nrf52840= | 1 MB | 480 K | 484 K | 0x00007000 | 0x0007B000 | +| =nrf52833= | 512 KB | 224 K | 228 K | 0x00007000 | 0x0003F000 | The fixed regions for RP2040 (identical for all variants): @@ -102,7 +121,7 @@ The fixed regions for RP2040 (identical for all variants): | Bootloader code | 0x10000100 | ~24 K | | Boot state | 0x10006000 | 4 K | -Fixed regions for nRF52840: +Fixed regions for nRF52840 / nRF52833: | Region | Start | Size | |--------------------------+------------+------| @@ -164,18 +183,18 @@ No BOOTSEL button needed. The bootloader drives a single LED via hardware PWM: - RP2040: GPIO 25 (=PWM_SLICE0=, channel B), defined in =src/rp2040.rs= -- nRF52840: P0.15 (=PWM0=, channel 0), defined in =src/nrf52840.rs= +- nRF52840 / nRF52833: P0.15 (=PWM0=, channel 0), defined in =src/nrf52840.rs= See [[Changing the LED pin]] below for how to adapt them. | Pattern | Meaning | |----------------------------------------+---------| -| 2 short blinks (≈2 Hz) | Normal boot — bootloader ran and jumped to ACTIVE | -| 1 s solid on | Bootloader detected a pending DFU→ACTIVE swap and is about to copy | -| Fast breathing (300 ms period) | DFU→ACTIVE copy in progress | -| 3 short blinks (50 ms) | Previous forward swap completed but the new app did not call =mark_booted()= — reverting to the old ACTIVE | -| 5 short blinks (50 ms) | Successful DFU→ACTIVE copy; about to jump | -| Slow breathing (3 s period) | USB DFU mode active (nRF52840 only) — waiting for =dfu-util= | +| 2 short blinks (≈2 Hz) | Normal boot — bootloader ran and jumped to ACTIVE. With =noswap= this is the only LED pattern on boot. | +| 1 s solid on | Bootloader detected a pending DFU→ACTIVE swap and is about to copy (not present with =noswap=) | +| Fast breathing (300 ms period) | DFU→ACTIVE copy in progress (not present with =noswap=) | +| 3 short blinks (50 ms) | Previous forward swap completed but the new app did not call =mark_booted()= — reverting to the old ACTIVE (not present with =noswap=) | +| 5 short blinks (50 ms) | Successful DFU→ACTIVE copy; about to jump (not present with =noswap=) | +| Slow breathing (3 s period) | USB DFU mode active (nRF52 only) — waiting for =dfu-util= | | SOS (... --- ...), repeating | Bootloader itself panicked (e.g. flash read error, invalid state partition) | If the LED stays dark the bootloader either isn't running (no bootloader @@ -206,10 +225,10 @@ wrong channel — swap =a= / =_b= or pick a different slice. For PWM slice / channel to PIN mapping see: https://rp2040.implrust.com/pwm/pwm-in-rp2040.html#mapping-of-pwm-channels-to-gpio-pins -*** nRF52840 +*** nRF52 In =src/nrf52840.rs=, change the pin in =SimplePwm::new_1ch()=. Unlike the -RP2040, the nRF52840 has /no fixed PWM pin mapping/: any GPIO can be routed +RP2040, the nRF52 has /no fixed PWM pin mapping/: any GPIO can be routed to any PWM output channel via the PSEL register (=embassy-nrf= handles this internally). The pin doesn't need to match any specific PWM instance. @@ -234,27 +253,29 @@ already in use — just pass =p.PWM1= instead of =p.PWM0=. #+BEGIN_SRC sh # RP2040 cargo run --release --target thumbv6m-none-eabi --features rp2040-2mb -# NRF52840 +# nRF52840 cargo run --release --target thumbv7em-none-eabihf --features nrf52840 +# nRF52833 +cargo run --release --target thumbv7em-none-eabihf --features nrf52833 #+END_SRC *** First time (UF2) -WHEN USING AN NRF52840 WITH THE ADAFRUIT UF2 BOOTLOADER, THIS WILL OVERWRITE +WHEN USING AN NRF52840 / NRF52833 WITH THE ADAFRUIT UF2 BOOTLOADER, THIS WILL OVERWRITE THE UF2 BOOTLOADER! On RP2040 the UF2 bootloader is in ROM, so nothing can happen to it. -1. Hold BOOTSEL, plug in USB, release. (For nRF52840 connect RESET to GND +1. Hold BOOTSEL, plug in USB, release. (For nRF52 connect RESET to GND twice within 500 ms — but note that rmk-boot replaces the Adafruit bootloader, so after the first flash you need probe-rs or dfu-util.) 2. A mass-storage device =RPI-RP2= / =NICENANO= appears. -3. Copy =rmk-boot-rp2040-SIZE.uf2= / =rmk-boot-nrf52840.uf2= onto it. +3. Copy =rmk-boot-rp2040-SIZE.uf2= / =rmk-boot-nrf52840.uf2= / =rmk-boot-nrf52833.uf2= onto it. 4. The board reboots and the bootloader is active. *** Updating the bootloader itself Repeat one of the methods above. A new rmk-boot build replaces the old -one at flash address =0x100= (RP2040) / =0x0= (nRF52840). The existing ACTIVE +one at flash address =0x100= (RP2040) / =0x0= (nRF52). The existing ACTIVE and DFU partitions are preserved as long as the flash-size variant stays the same. @@ -266,12 +287,12 @@ The bootloader is split into five source files: |-------------------+---------| | =src/main.rs= | Feature checks, shared constants, entry point, SysTick handler, panic handler | | =src/rp2040.rs= | RP2040 bootloader — =#[cfg(feature = "rp2040")]=, compiled only for RP2040 | -| =src/nrf52840.rs= | nRF52840 bootloader — =#[cfg(feature = "nrf52840")]=, compiled only for nRF52840 | -| =src/dfu.rs= | nRF52840 USB DFU — =#[cfg(feature = "nrf52840")]=, =block_on()= runtime, USB DFU stack | +| =src/nrf52840.rs= | nRF52 bootloader — =#[cfg(any(feature = "nrf52840", feature = "nrf52833"))]= | +| =src/dfu.rs= | nRF52 USB DFU — =#[cfg(any(feature = "nrf52840", feature = "nrf52833"))]=, =block_on()= runtime, USB DFU stack | | =src/led_pwm.rs= | Cross-platform PWM LED singleton — breathing =tick()= (SysTick), hard on/off =set_raw()=, =deinit()= cleanup | =led_pwm.rs= is platform-agnostic; each platform module constructs the correct -PWM peripheral (=Pwm<'static>= for RP2040, =SimplePwm<'static>= for nRF52840) +PWM peripheral (=Pwm<'static>= for RP2040, =SimplePwm<'static>= for nRF52) and passes it into =led_pwm::init()=. Before jumping to firmware, both call =led_pwm::deinit()= which drops the PWM device — this disables the hardware PWM peripheral and releases the GPIO pin so the firmware can reclaim it. diff --git a/build.rs b/build.rs index 92dc4a2..52676ce 100644 --- a/build.rs +++ b/build.rs @@ -8,6 +8,12 @@ fn main() { let is_rp2040 = env::var("CARGO_FEATURE_RP2040").is_ok(); let is_nrf52840 = env::var("CARGO_FEATURE_NRF52840").is_ok(); + let is_nrf52833 = env::var("CARGO_FEATURE_NRF52833").is_ok(); + let is_noswap = env::var("CARGO_FEATURE_NOSWAP").is_ok(); + + if is_nrf52840 && is_nrf52833 { + panic!("nrf52840 and nrf52833 are mutually exclusive"); + } const PAGE_SIZE: usize = 4 * 1024; const STORAGE_SIZE: usize = 32 * 1024; @@ -26,8 +32,11 @@ fn main() { }; let remaining = flash_size - 28 * 1024 - STORAGE_SIZE; - let active_size = (remaining - PAGE_SIZE) / 2; - let dfu_size = active_size + PAGE_SIZE; + let (active_size, dfu_size) = if is_noswap { + (remaining, 0) + } else { + ((remaining - PAGE_SIZE) / 2, (remaining - PAGE_SIZE) / 2 + PAGE_SIZE) + }; // Absolute XIP addresses let abs_active_offset = 0x1000_7000u32; let abs_dfu_offset = abs_active_offset + active_size as u32; @@ -72,6 +81,7 @@ __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOT2); println!("cargo:rerun-if-env-changed=CARGO_FEATURE_RP2040_4MB"); println!("cargo:rerun-if-env-changed=CARGO_FEATURE_RP2040_8MB"); println!("cargo:rerun-if-env-changed=CARGO_FEATURE_RP2040_16MB"); + println!("cargo:rerun-if-env-changed=CARGO_FEATURE_NOSWAP"); let rmk_boot_x = build_rmk_boot_x( variant_label, @@ -79,8 +89,8 @@ __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOT2); active_size as u32, rel_state_offset, 0x1000, - rel_dfu_offset, - rel_dfu_size, + if is_noswap { 0 } else { rel_dfu_offset }, + if is_noswap { 0 } else { rel_dfu_size }, rel_storage_offset, STORAGE_SIZE as u32, ); @@ -91,13 +101,28 @@ __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOT2); let bootloader_size = 24 * 1024; let state_size = 4 * 1024; let remaining = flash_size - bootloader_size - state_size - STORAGE_SIZE; - let active_size = (remaining - PAGE_SIZE) / 2; - let dfu_size = active_size + PAGE_SIZE; + let (active_size, dfu_size) = if is_noswap { + (remaining, 0) + } else { + ((remaining - PAGE_SIZE) / 2, (remaining - PAGE_SIZE) / 2 + PAGE_SIZE) + }; let abs_state_offset = bootloader_size as u32; let abs_active_offset = (bootloader_size + state_size) as u32; let abs_dfu_offset = abs_active_offset + active_size as u32; + let (dfu_start_sym, dfu_end_sym) = if is_noswap { + ( + format!("__bootloader_dfu_start = ORIGIN(ACTIVE);"), + format!("__bootloader_dfu_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE);"), + ) + } else { + ( + format!("__bootloader_dfu_start = ORIGIN(DFU);"), + format!("__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU);"), + ) + }; + let memory_x = format!( "\ MEMORY @@ -116,14 +141,15 @@ __bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) __bootloader_active_start = ORIGIN(ACTIVE); __bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE); -__bootloader_dfu_start = ORIGIN(DFU); -__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU); +{dfu_start_sym} +{dfu_end_sym} " ); fs::write(out.join("memory.x"), &memory_x).unwrap(); println!("cargo:rustc-link-search={}", out.display()); println!("cargo:rustc-link-arg-bins=-Tlink.x"); + println!("cargo:rerun-if-env-changed=CARGO_FEATURE_NOSWAP"); let rmk_boot_x = build_rmk_boot_x( "nRF52840", @@ -131,15 +157,83 @@ __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU); active_size as u32, abs_state_offset, state_size as u32, - abs_dfu_offset, - dfu_size as u32, + if is_noswap { 0 } else { abs_dfu_offset }, + if is_noswap { 0 } else { dfu_size as u32 }, + abs_dfu_offset + dfu_size as u32, + STORAGE_SIZE as u32, + ); + let project_root = Path::new(env!("CARGO_MANIFEST_DIR")); + fs::write(project_root.join("rmk-boot.x"), &rmk_boot_x).unwrap(); + } else if is_nrf52833 { + let flash_size = 512 * 1024; + let bootloader_size = 24 * 1024; + let state_size = 4 * 1024; + let remaining = flash_size - bootloader_size - state_size - STORAGE_SIZE; + let (active_size, dfu_size) = if is_noswap { + (remaining, 0) + } else { + ((remaining - PAGE_SIZE) / 2, (remaining - PAGE_SIZE) / 2 + PAGE_SIZE) + }; + + let abs_state_offset = bootloader_size as u32; + let abs_active_offset = (bootloader_size + state_size) as u32; + let abs_dfu_offset = abs_active_offset + active_size as u32; + + let (dfu_start_sym, dfu_end_sym) = if is_noswap { + ( + format!("__bootloader_dfu_start = ORIGIN(ACTIVE);"), + format!("__bootloader_dfu_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE);"), + ) + } else { + ( + format!("__bootloader_dfu_start = ORIGIN(DFU);"), + format!("__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU);"), + ) + }; + + let memory_x = format!( + "\ +MEMORY +{{ + FLASH : ORIGIN = 0x00000000, LENGTH = {bootloader_size} + BOOTLOADER_STATE : ORIGIN = 0x{abs_state_offset:08X}, LENGTH = {state_size} + ACTIVE : ORIGIN = 0x{abs_active_offset:08X}, LENGTH = {active_size} + DFU : ORIGIN = 0x{abs_dfu_offset:08X}, LENGTH = {dfu_size} + + RAM : ORIGIN = 0x20000000, LENGTH = 256K +}} + +__bootloader_state_start = ORIGIN(BOOTLOADER_STATE); +__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE); + +__bootloader_active_start = ORIGIN(ACTIVE); +__bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE); + +{dfu_start_sym} +{dfu_end_sym} +" + ); + + fs::write(out.join("memory.x"), &memory_x).unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + println!("cargo:rustc-link-arg-bins=-Tlink.x"); + println!("cargo:rerun-if-env-changed=CARGO_FEATURE_NOSWAP"); + + let rmk_boot_x = build_rmk_boot_x( + "nRF52833", + abs_active_offset, + active_size as u32, + abs_state_offset, + state_size as u32, + if is_noswap { 0 } else { abs_dfu_offset }, + if is_noswap { 0 } else { dfu_size as u32 }, abs_dfu_offset + dfu_size as u32, - STORAGE_SIZE as u32, // nRF flash_base is 0, so absolute = relative + STORAGE_SIZE as u32, ); let project_root = Path::new(env!("CARGO_MANIFEST_DIR")); fs::write(project_root.join("rmk-boot.x"), &rmk_boot_x).unwrap(); } else { - panic!("No platform feature enabled (rp2040 or nrf52840)"); + panic!("No platform feature enabled (rp2040 or nrf52840 or nrf52833)"); } println!("cargo:rerun-if-changed=build.rs"); diff --git a/src/dfu.rs b/src/dfu.rs index 87e61d7..6847686 100644 --- a/src/dfu.rs +++ b/src/dfu.rs @@ -8,9 +8,9 @@ use embassy_sync::blocking_mutex::Mutex; use embassy_nrf::nvmc::Nvmc; // --------------------------------------------------------------------------- -// Interrupt binding – nRF52840 +// Interrupt binding – nRF52 // --------------------------------------------------------------------------- -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] embassy_nrf::bind_interrupts! { pub(crate) struct DfuIrqs { USBD => embassy_nrf::usb::InterruptHandler; diff --git a/src/led_pwm.rs b/src/led_pwm.rs index dfd0cef..f6a3d01 100644 --- a/src/led_pwm.rs +++ b/src/led_pwm.rs @@ -6,16 +6,16 @@ use embassy_sync::blocking_mutex::Mutex; static MS: Mutex> = Mutex::new(Cell::new(0)); static PERIOD: Mutex> = Mutex::new(Cell::new(0)); -// ── nRF52840 ── -#[cfg(feature = "nrf52840")] +// ── nRF52 ── +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] use embassy_nrf::pwm::{DutyCycle, SimplePwm}; -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] type PwmDev = SimplePwm<'static>; -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] static PWM: Mutex>> = Mutex::new(RefCell::new(None)); -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] fn set_hw_duty(duty: u16) { PWM.lock(|c| { if let Some(ref mut pwm) = *c.borrow_mut() { @@ -83,7 +83,7 @@ pub fn set_raw(on: bool) { /// Set PWM duty cycle directly (0-255). /// Use this from a main loop instead of relying on SysTick. -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] pub fn set_duty(duty: u8) { set_hw_duty(duty as u16); } diff --git a/src/main.rs b/src/main.rs index 9260030..b8ecb93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,11 @@ // --------------------------------------------------------------------------- // Feature-exclusion checks // --------------------------------------------------------------------------- -#[cfg(all(feature = "rp2040", feature = "nrf52840"))] -compile_error!("rp2040 and nrf52840 features are mutually exclusive"); +#[cfg(all(feature = "rp2040", any(feature = "nrf52840", feature = "nrf52833")))] +compile_error!("rp2040 and nRF52 features are mutually exclusive"); + +#[cfg(all(feature = "nrf52840", feature = "nrf52833"))] +compile_error!("nrf52840 and nrf52833 features are mutually exclusive"); #[cfg(feature = "rp2040")] const _: () = { @@ -37,9 +40,9 @@ const _: () = { // --------------------------------------------------------------------------- #[cfg(feature = "rp2040")] mod rp2040; -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] mod nrf52840; -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] mod dfu; mod led_pwm; @@ -64,9 +67,9 @@ const PAGE_SIZE: usize = 4096; #[cfg(feature = "rp2040")] const WRITE_SIZE: usize = 1; -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] const PAGE_SIZE: usize = 4096; -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] const WRITE_SIZE: usize = 4; const STATE_ERASE_VALUE: u8 = 0xFF; @@ -86,9 +89,9 @@ const LETTER_GAP_MS: u64 = 450; const WORD_GAP_MS: u64 = 1050; const SWAP_BREATHE_MS: u32 = 300; -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] const DFU_BREATHE_MS: u32 = 3000; -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] const DTAP_SIGNAL_MS: u64 = 500; // --------------------------------------------------------------------------- @@ -99,7 +102,7 @@ fn platform_run() -> ! { rp2040::run() } -#[cfg(feature = "nrf52840")] +#[cfg(any(feature = "nrf52840", feature = "nrf52833"))] fn platform_run() -> ! { nrf52840::run() } diff --git a/src/nrf52840.rs b/src/nrf52840.rs index 5e68a34..0d2b5be 100644 --- a/src/nrf52840.rs +++ b/src/nrf52840.rs @@ -1,4 +1,4 @@ -use embassy_boot::{BootLoader, BootLoaderConfig, State}; +use embassy_boot::BootLoaderConfig; use embassy_nrf::nvmc::Nvmc; use embassy_nrf::pwm::{Prescaler, SimpleConfig, SimplePwm}; @@ -55,32 +55,12 @@ pub fn run() -> ! { } // Phase 2 — normal boot flow - let mut config = + let config = BootLoaderConfig::from_linkerfile_blocking(&flash_mutex, &flash_mutex, &flash_mutex); let active_offset = config.active.offset(); - let mut state_word = [0u8; WRITE_SIZE]; - config.state.read(0, &mut state_word).unwrap(); - let current_state = State::from(&state_word[..]); - - if current_state == State::Swap { - let page_count = config.active.capacity() / PAGE_SIZE; - let progress = current_progress(&mut config.state); - let is_swapped = progress >= page_count * 2; - - if !is_swapped { - led_pwm::set_raw(true); - block_for(Duration::from_millis(PRE_SWAP_MS)); - led_pwm::set_raw(false); - } else { - for _ in 0..PRE_REVERT_COUNT { - led_pwm::set_raw(true); - block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); - led_pwm::set_raw(false); - block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); - } - } - } else { + #[cfg(feature = "noswap")] + { block_for(Duration::from_millis(HB_HALF_MS)); for _ in 0..HB_CYCLES { led_pwm::set_raw(true); @@ -88,40 +68,87 @@ pub fn run() -> ! { led_pwm::set_raw(false); block_for(Duration::from_millis(HB_HALF_MS)); } - } - if current_state == State::Swap { - let page_count = config.active.capacity() / PAGE_SIZE; - let progress = current_progress(&mut config.state); - let is_swapped = progress >= page_count * 2; - if !is_swapped { - led_pwm::start(SWAP_BREATHE_MS); + let mut syst = unsafe { cortex_m::Peripherals::steal() }.SYST; + syst.disable_interrupt(); + syst.disable_counter(); + led_pwm::deinit(); + + unsafe { + let vector_table = active_offset as *const u32; + cortex_m::asm::bootload(vector_table) } } - let mut page = [0u8; PAGE_SIZE]; - let mut bl = BootLoader::new(config); - let state = bl.prepare_boot(&mut page).unwrap_or(State::Boot); + #[cfg(not(feature = "noswap"))] + { + use embassy_boot::{BootLoader, State}; + let mut config = config; // make mutable for prepare_boot - led_pwm::stop(); + let mut state_word = [0u8; WRITE_SIZE]; + config.state.read(0, &mut state_word).unwrap(); + let current_state = State::from(&state_word[..]); - if state == State::Swap { - for _ in 0..POST_SWAP_COUNT { - led_pwm::set_raw(true); - block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); - led_pwm::set_raw(false); - block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); + if current_state == State::Swap { + let page_count = config.active.capacity() / PAGE_SIZE; + let progress = current_progress(&mut config.state); + let is_swapped = progress >= page_count * 2; + + if !is_swapped { + led_pwm::set_raw(true); + block_for(Duration::from_millis(PRE_SWAP_MS)); + led_pwm::set_raw(false); + } else { + for _ in 0..PRE_REVERT_COUNT { + led_pwm::set_raw(true); + block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); + led_pwm::set_raw(false); + block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); + } + } + } else { + block_for(Duration::from_millis(HB_HALF_MS)); + for _ in 0..HB_CYCLES { + led_pwm::set_raw(true); + block_for(Duration::from_millis(HB_HALF_MS)); + led_pwm::set_raw(false); + block_for(Duration::from_millis(HB_HALF_MS)); + } } - } - // Disable SysTick and PWM before handing over to firmware - let mut syst = unsafe { cortex_m::Peripherals::steal() }.SYST; - syst.disable_interrupt(); - syst.disable_counter(); - led_pwm::deinit(); + if current_state == State::Swap { + let page_count = config.active.capacity() / PAGE_SIZE; + let progress = current_progress(&mut config.state); + let is_swapped = progress >= page_count * 2; + if !is_swapped { + led_pwm::start(SWAP_BREATHE_MS); + } + } + + let mut page = [0u8; PAGE_SIZE]; + let mut bl = BootLoader::new(config); + let state = bl.prepare_boot(&mut page).unwrap_or(State::Boot); + + led_pwm::stop(); - unsafe { - let vector_table = active_offset as *const u32; - cortex_m::asm::bootload(vector_table) + if state == State::Swap { + for _ in 0..POST_SWAP_COUNT { + led_pwm::set_raw(true); + block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); + led_pwm::set_raw(false); + block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); + } + } + + // Disable SysTick and PWM before handing over to firmware + let mut syst = unsafe { cortex_m::Peripherals::steal() }.SYST; + syst.disable_interrupt(); + syst.disable_counter(); + led_pwm::deinit(); + + unsafe { + let vector_table = active_offset as *const u32; + cortex_m::asm::bootload(vector_table) + } } } diff --git a/src/rp2040.rs b/src/rp2040.rs index e1d3025..9b30119 100644 --- a/src/rp2040.rs +++ b/src/rp2040.rs @@ -1,4 +1,4 @@ -use embassy_boot_rp::{BootLoader, BootLoaderConfig, State}; +use embassy_boot_rp::BootLoaderConfig; use embassy_rp::flash::{Blocking, Flash}; use embassy_rp::pwm::{Config as PwmConfig, Pwm}; @@ -37,32 +37,12 @@ pub fn run() -> ! { let flash = Flash::<_, Blocking, FLASH_SIZE>::new_blocking(p.FLASH); let flash_mutex = Mutex::new(RefCell::new(flash)); - let mut config = + let config = BootLoaderConfig::from_linkerfile_blocking(&flash_mutex, &flash_mutex, &flash_mutex); let active_offset = config.active.offset(); - let mut state_word = [0u8; WRITE_SIZE]; - config.state.read(0, &mut state_word).unwrap(); - let current_state = State::from(&state_word[..]); - - if current_state == State::Swap { - let page_count = config.active.capacity() / PAGE_SIZE; - let progress = current_progress(&mut config.state); - let is_swapped = progress >= page_count * 2; - - if !is_swapped { - led_pwm::set_raw(true); - block_for(Duration::from_millis(PRE_SWAP_MS)); - led_pwm::set_raw(false); - } else { - for _ in 0..PRE_REVERT_COUNT { - led_pwm::set_raw(true); - block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); - led_pwm::set_raw(false); - block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); - } - } - } else { + #[cfg(feature = "noswap")] + { block_for(Duration::from_millis(HB_HALF_MS)); for _ in 0..HB_CYCLES { led_pwm::set_raw(true); @@ -70,38 +50,87 @@ pub fn run() -> ! { led_pwm::set_raw(false); block_for(Duration::from_millis(HB_HALF_MS)); } - } - if current_state == State::Swap { - let page_count = config.active.capacity() / PAGE_SIZE; - let progress = current_progress(&mut config.state); - let is_swapped = progress >= page_count * 2; - if !is_swapped { - led_pwm::start(SWAP_BREATHE_MS); + let mut syst = unsafe { cortex_m::Peripherals::steal() }.SYST; + syst.disable_interrupt(); + syst.disable_counter(); + led_pwm::deinit(); + + unsafe { + let vector_table = + (embassy_rp::flash::FLASH_BASE as u32 + active_offset) as *const u32; + cortex_m::asm::bootload(vector_table) } } - let bl: BootLoader = BootLoader::prepare(config); + #[cfg(not(feature = "noswap"))] + { + use embassy_boot_rp::{BootLoader, State}; + let mut config = config; // make mutable for prepare_boot - led_pwm::stop(); + let mut state_word = [0u8; WRITE_SIZE]; + config.state.read(0, &mut state_word).unwrap(); + let current_state = State::from(&state_word[..]); - if bl.state == State::Swap { - for _ in 0..POST_SWAP_COUNT { - led_pwm::set_raw(true); - block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); - led_pwm::set_raw(false); - block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); + if current_state == State::Swap { + let page_count = config.active.capacity() / PAGE_SIZE; + let progress = current_progress(&mut config.state); + let is_swapped = progress >= page_count * 2; + + if !is_swapped { + led_pwm::set_raw(true); + block_for(Duration::from_millis(PRE_SWAP_MS)); + led_pwm::set_raw(false); + } else { + for _ in 0..PRE_REVERT_COUNT { + led_pwm::set_raw(true); + block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); + led_pwm::set_raw(false); + block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); + } + } + } else { + block_for(Duration::from_millis(HB_HALF_MS)); + for _ in 0..HB_CYCLES { + led_pwm::set_raw(true); + block_for(Duration::from_millis(HB_HALF_MS)); + led_pwm::set_raw(false); + block_for(Duration::from_millis(HB_HALF_MS)); + } } - } - // Disable SysTick and PWM before handing over to firmware - let mut syst = unsafe { cortex_m::Peripherals::steal() }.SYST; - syst.disable_interrupt(); - syst.disable_counter(); - led_pwm::deinit(); + if current_state == State::Swap { + let page_count = config.active.capacity() / PAGE_SIZE; + let progress = current_progress(&mut config.state); + let is_swapped = progress >= page_count * 2; + if !is_swapped { + led_pwm::start(SWAP_BREATHE_MS); + } + } + + let bl: BootLoader = BootLoader::prepare(config); + + led_pwm::stop(); - unsafe { - let vector_table = (embassy_rp::flash::FLASH_BASE as u32 + active_offset) as *const u32; - cortex_m::asm::bootload(vector_table) + if bl.state == State::Swap { + for _ in 0..POST_SWAP_COUNT { + led_pwm::set_raw(true); + block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); + led_pwm::set_raw(false); + block_for(Duration::from_millis(PRE_REVERT_BLINK_MS)); + } + } + + // Disable SysTick and PWM before handing over to firmware + let mut syst = unsafe { cortex_m::Peripherals::steal() }.SYST; + syst.disable_interrupt(); + syst.disable_counter(); + led_pwm::deinit(); + + unsafe { + let vector_table = + (embassy_rp::flash::FLASH_BASE as u32 + active_offset) as *const u32; + cortex_m::asm::bootload(vector_table) + } } }