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

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.

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",

Related

Poetry: how to deal with hardware dependant libraries

I'm trying to configure my poetry pyproject.toml in a way that we can choose between a cpu and a gpu version of the dependencies. The reason for that is that the library torch needs to be installed by specifying the hardware you want to use (see poetry's Instructions for installing PyTorch). My final goal is to be able to install the dependencies on my local computer (Mac with M1 ship) as well as to be able to generate a wheel to send to some distant server and then pip install it (I'm actually using Databricks dbx to do that). Here is the configuration file:
[tool.poetry]
name = "my_project"
version = "0.1.0"
description = ""
authors = ["Me"]
readme = "README.md"
packages = [
{include = "my_project"},
{include = "scripts"}
]
[tool.poetry.dependencies]
python = "3.8.10"
torch = [
{url = "https://download.pytorch.org/whl/cpu/torch-1.12.1-cp38-none-macosx_11_0_arm64.whl", markers="extra == 'cpu'"},
{url = "https://download.pytorch.org/whl/cu102/torch-1.12.1%2Bcu102-cp38-cp38-linux_x86_64.whl", markers="extra == 'gpu'"}
]
[tool.poetry.extras]
cpu=["torch"]
gpu=["torch"]
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
When I run:
poetry install --extras="cpu"
I get the following error:
Because my_project depends on both torch (1.12.1) # https://download.pytorch.org/whl/cpu/torch-1.12.1-cp38-none-macosx_11_0_arm64.whl and torch (1.12.1+cu102) # https://download.pytorch.org/whl/cu102/torch-1.12.1%2Bcu102-cp38-cp38-linux_x86_64.whl, version solving failed.

pyo3 rust module kills Python on import

I'm writing my first Rust-based Python module, and it kills the Python process on import. I've got it down to a pretty minimal example, based loosely on the html-py-ever example (which does run for me without crashing).
I'm running Python 3.8 on an M1 macbook, Python is compiled for arm64.
% python -c "import platform;print(platform.machine())"
arm64
My output, reproducer command using the files pasted below. The install should take care of any python requirements:
(rust) jeremytemp#Jeremy-McGibbons-MacBook-Pro minimal % pip install -e . && python test.py
Obtaining file:///Users/jeremytemp/rust/minimal
Installing collected packages: minimal
Attempting uninstall: minimal
Found existing installation: minimal 0.1.0
Uninstalling minimal-0.1.0:
Successfully uninstalled minimal-0.1.0
Running setup.py develop for minimal
Successfully installed minimal-0.1.0
zsh: killed python test.py
src/lib.rs:
use pyo3::{prelude::*, wrap_pyfunction};
#[pyfunction]
fn foo() -> PyResult<u64>{
let u: u64 = 1;
Ok(u)
}
#[pymodule]
fn minimal(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(foo, m)?)?;
Ok(())
}
Cargo.toml:
[package]
name = "minimal"
version = "0.1.0"
edition = "2021"
[dependencies]
pyo3 = { features = ["extension-module"] }
[lib]
name = "minimal"
crate-type = ["cdylib"]
setup.py:
from setuptools import setup
from setuptools_rust import RustExtension
setup(
rust_extensions=[RustExtension("minimal.minimal")],
)
setup.cfg:
[metadata]
name = minimal
version = 0.1.0
license = MIT
[options]
packages = minimal
zip_safe = False
setup_requires = setuptools-rust >= 0.12.1;
python_requires = >=3.8
include_package_data = True
minimal/__init__.py:
from .minimal import *
test.py:
import minimal
pip freeze output is
(rust) jeremytemp#Jeremy-McGibbons-MacBook-Pro minimal % pip freeze
attrs==21.4.0
beautifulsoup4==4.11.1
certifi==2021.10.8
iniconfig==1.1.1
# Editable Git install with no remote (minimal==0.1.0)
-e /Users/jeremytemp/rust/minimal
packaging==21.3
pluggy==1.0.0
py==1.11.0
pyparsing==3.0.8
pytest==7.1.2
semantic-version==2.9.0
setuptools-rust==1.3.0
soupsieve==2.3.2.post1
tomli==2.0.1
typing_extensions==4.2.0
What am I doing wrong? Are there any steps I can take to get more helpful debugging output than "killed"?
Not a very satisfying answer, but the example executes fine on my windows machine. I'm assuming this is an issue with pyo3 on M1 Macs.

Solana Rust smart contract build error: build failed

I am a beginner Solana/Rust developer.
As my first Solana project, I built the mint NFT contract.
And then I want to deploy the contract.
So to get the compiled output .so file, I did run like: cargo build,
but getting this error:
warning: cc: warning: src/main.rs: linker input file unused because linking not done
warning: ar: /home/rango/my_tasks/mintdropz/solana-nft-contract/target/debug/build/solana-nft-7f2fc6a536921641/out/src/main.o: No such file or directory
error: failed to run custom build command for `solana-nft v0.1.0 (/home/rango/my_tasks/mintdropz/solana-nft-contract)`
Caused by:
process didn't exit successfully: `/home/rango/my_tasks/mintdropz/solana-nft-contract/target/debug/build/solana-nft-cbb36a23539fa380/build-script-build` (exit status: 1)
--- stdout
cargo:rerun-if-changed=src/main.rs
TARGET = Some("x86_64-unknown-linux-gnu")
OPT_LEVEL = Some("0")
HOST = Some("x86_64-unknown-linux-gnu")
CC_x86_64-unknown-linux-gnu = None
CC_x86_64_unknown_linux_gnu = None
HOST_CC = None
CC = None
CFLAGS_x86_64-unknown-linux-gnu = None
CFLAGS_x86_64_unknown_linux_gnu = None
HOST_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")
CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
running: "cc" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-Wall" "-Wextra" "-o" "/home/rango/my_tasks/mintdropz/solana-nft-contract/target/debug/build/solana-nft-7f2fc6a536921641/out/src/main.o" "-c" "src/main.rs"
cargo:warning=cc: warning: src/main.rs: linker input file unused because linking not done
exit status: 0
AR_x86_64-unknown-linux-gnu = None
AR_x86_64_unknown_linux_gnu = None
HOST_AR = None
AR = None
running: "ar" "cq" "/home/rango/my_tasks/mintdropz/solana-nft-contract/target/debug/build/solana-nft-7f2fc6a536921641/out/libhello.a" "/home/rango/my_tasks/mintdropz/solana-nft-contract/target/debug/build/solana-nft-7f2fc6a536921641/out/src/main.o"
cargo:warning=ar: /home/rango/my_tasks/mintdropz/solana-nft-contract/target/debug/build/solana-nft-7f2fc6a536921641/out/src/main.o: No such file or directory
exit status: 1
--- stderr
error occurred: Command "ar" "cq" "/home/rango/my_tasks/mintdropz/solana-nft-contract/target/debug/build/solana-nft-7f2fc6a536921641/out/libhello.a" "/home/rango/my_tasks/mintdropz/solana-nft-contract/target/debug/build/solana-nft-7f2fc6a536921641/out/src/main.o" with args "ar" did not execute successfully (status code exit status: 1).
warning: build failed, waiting for other jobs to finish...
error: build failed
this is build.rs file:
// Example custom build script.
fn main() {
// Tell Cargo that if the given file changes, to rerun this build script.
println!("cargo:rerun-if-changed=src/main.rs");
// Use the `cc` crate to build a C file and statically link it.
cc::Build::new()
.file("src/main.rs")
.compile("hello");
}
this is Cargo.toml file:
[package]
name = "solana-nft"
version = "0.1.0"
edition = "2018"
[dependencies]
solana-client = "1.7.8"
solana-sdk = "1.7.8"
spl-token = { version = "3.2.0", features = [ "no-entrypoint" ] }
rand = "0.8.4"
solana-program = "1.7.11"
spl-token-metadata = "0.0.1"
openssl = { version = "0.10", features = ["vendored"] }
[build-dependencies]
cc = "1.0"
bindgen = "0.53.1"
I am using ubuntu 18.04, cargo 1.56.0 .
What am I doing wrong?
If you are trying to build a Rust Solana Program (i.e. Smart Contract) you only need to cargo build-bpf as Programs get compiled to BPF through LLVM and that is what is deployed on Solana
This seems to be a compatible issue. install the latest version of rustc
Also when u install the solana toolkit:
sh -c "$(curl -sSfL https://release.solana.com/v1.9.5/install)"
Make sure you update your PATH environment variable to include the solana programs:
I think Solana stable version solves your problem.
sh -c "$(curl -sSfL https://release.solana.com/stable/install)"

Rust .exe fails to open on Windows

I'm building an app with rust. Using --cargo run --release successfully compiles the app and runs it, bringing up the GUI window for the app. However, when I manually open target/release/MyApp.exe, nothing happens. Checked when myapp.exe was last modifies shows that running --cargo run --release is updating the app.
I'm on windows 10 so I added "x86_64-pc-windows-msvc" as the build target.
[package]
name = "MuTexAlpha"
version = "0.1.0"
authors = ["Webb Hinton <wyhinton189#gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
fltk = "0.15.1"
serde = "1.0.118"
texture-synthesis = "0.8.0"
rand = "0.7.3"
num_cpus = "1.13.0"
uuid = { version = "0.8", features = ["serde", "v4"] }
image = "0.23.12"
glob = "0.3.0"
dyn-clone = "1.0.4"
colored = "2.0.0"
arboard = "1.1.0"
id_tree = "1.7.0"
id_tree_layout = "2.0.1"
typetag = "0.1"
serde_json = "1.0.61"
snafu = "0.6.10"
palette = "0.5.0"
indicatif = "0.14.0"
[build]
target = "x86_64-pc-windows-msvc"
Isn't the .exe in release the same program being executed with --cargo run --release?
I have some file dependencies in my app, might this be the cause of the issue? (compiling the app gives no errors, however)
Could this be something specifically related to windows?
Might just be a shot in the dark, but are you opening the exe using Windows Explorer?
Open up a cmd.exe window and try running it from there.

Cargo can't parse the Cargo.toml for url version 0.5.7

I encountered a problem when running cargo build:
/usr/local/bin/cargo build --color=always
error: unable to get packages from source
Caused by:
failed to parse manifest at `/home/lzc/.multirust/toolchains/stable/cargo/registry/src/github.com-1ecc6299db9ec823/url-0.5.7/Cargo.toml`
Caused by:
could not parse input as TOML
Caused by:
expected newline, found an identifier at line 14
I found this issue on GitHub, but it didn't solve my problem.
This is my project Cargo.toml:
[dependencies]
hyper = "0.7.2"
rustc-serialize = "0.3"
websocket = "0.15.1"
And my rustc and cargo version:
➜ ~ cargo -V
cargo 0.18.0 (fe7b0cdcf 2017-04-24)
➜ ~ rustc -V
rustc 1.17.0 (56124baa9 2017-04-24)
And here is the file Cargo complains about(/home/lzc/.multirust/toolchains/stable/cargo/registry/src/github.com-1ecc6299db9ec823/url-0.5.7/Cargo.toml):
[package]
name = "url"
version = "0.5.7"
authors = [ "Simon Sapin <simon.sapin#exyr.org>" ]
description = "URL library for Rust, based on the WHATWG URL Standard"
documentation = "http://servo.github.io/rust-url/url/index.html"
repository = "https://github.com/servo/rust-url"
readme = "README.md"
keywords = ["url", "parser"]
license = "MIT/Apache-2.0"
[[test]] name = "format" #<- line 14
[[test]] name = "form_urlencoded"
[[test]] name = "idna"
[[test]] name = "punycode"
[[test]] name = "tests"
[[test]]
name = "wpt"
harness = false
[dev-dependencies]
rustc-test = "0.1"
[features]
query_encoding = ["encoding"]
serde_serialization = ["serde"]
heap_size = ["heapsize", "heapsize_plugin"]
[dependencies.heapsize]
version = ">=0.1.1, <0.4"
optional = true
[dependencies.heapsize_plugin]
version = "0.1.0"
optional = true
[dependencies.encoding]
version = "0.2"
optional = true
[dependencies.serde]
version = ">=0.6.1, <0.8"
optional = true
[dependencies]
uuid = "0.1.17"
rustc-serialize = "0.3"
unicode-bidi = "0.2.3"
unicode-normalization = "0.1.2"
matches = "0.1"
As listed in the url crate's GitHub issue, it previously used a form of TOML that was actually invalid. Newer versions of Cargo no longer parse that invalid form.
Nothing in your shown dependency list requires url version 0.5.7. url version 0.5.10 has been released, so perform a cargo update to switch to it. Note that 0.5.10 was published on Aug 21, 2016, so it's almost a year old at this point.

Resources