rustembeddedrust-cargoraspberry-pi-picorp2040

Run code with cargo run on Raspberry Pi Pico using elf2uf2-rs


I try to run Rust code on a Raspberry Pi Pico. A simple "blink" example application is successfully (as it seems) built using:

cargo build --release --target=thumbv6m-none-eabi

I have installed elf2uf2-rs with:

cargo install elf2uf2-rs

And I then try to run the blink application on the Raspberry Pi Pico using:

cargo run --release --target=thumbv6m-none-eabi

but it fails with this message, where "rp2" is the name of my binary:

target/thumbv6m-none-eabi/release/rp2: cannot execute binary file

Any suggestions on what can be wrong?

This is my Cargo.toml:

[package]
name = "rp2"
version = "0.1.0"
edition = "2021"

[dependencies]
rp2040-hal = "0.3.0"
cortex-m = "0.7.2"
embedded-hal = { version = "0.2.5", features = ["unproven"] }
eh1_0_alpha = { version="=1.0.0-alpha.6", package="embedded-hal", optional=true }
embedded-time = "0.12.0"
panic-halt = "0.2.0"
rp2040-boot2 = "0.2.0"
cortex-m-rt = "0.7"
rp-pico = "0.2.0"

[dev-dependencies]
cortex-m-rt = "0.7"
panic-halt = "0.2.0"
rp2040-boot2 = "0.2.0"

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

Update

I now added a file .cargo/config.toml with this content:

# Choose a default "cargo run" tool.
# probe-run is recommended if you have a debugger
# elf2uf2-rs loads firmware over USB when the rp2040 is in boot mode
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# runner = "probe-run --chip RP2040"
runner = "elf2uf2-rs -d"

rustflags = [
    "-C", "linker=flip-link",
    "-C", "link-arg=--nmagic",
    "-C", "link-arg=-Tlink.x",
    "-C", "link-arg=-Tdefmt.x",

    # Code-size optimizations.
    #   trap unreachable can save a lot of space, but requires nightly compiler.
    #   uncomment the next line if you wish to enable it
    # "-Z", "trap-unreachable=no",
    "-C", "inline-threshold=5",
    "-C", "no-vectorize-loops",
]

[build]
target = "thumbv6m-none-eabi"

and a memory.x file with this content:

MEMORY {
    BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
    FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100
    RAM   : ORIGIN = 0x20000000, LENGTH = 256K
}

EXTERN(BOOT2_FIRMWARE)

SECTIONS {
    /* ### Boot loader */
    .boot2 ORIGIN(BOOT2) :
    {
        KEEP(*(.boot2));
    } > BOOT2
} INSERT BEFORE .text;

but then the cargo build --release command fails with this error:

error: linking with `rust-lld` failed: exit status: 1
...
= note: rust-lld: error: cannot find linker script defmt.x

I use a MacBook with M1 Apple Silicon chip, this might be a related issue rust-lld problem under AArch64 system.


Solution

  • I have now got the code running on the Raspberry Pi Pico.

    The first problem was that I hadn't created the .cargo/config.toml file that contains the instruction to "run" with elf2uf2:

    [target.'cfg(all(target_arch = "arm", target_os = "none"))']
    runner = "elf2uf2-rs -d"
    

    The other problem was that my .cargo/config.toml also contained this reference to defmt that I didn't have on my system, so when commenting out this line (under rustflags), the code compiled with cargo build --release and it was run on the Raspberry Pi Pico with cargo run --release:

    # "-C", "link-arg=-Tdefmt.x",