rust on rpi pico
Prerequisites
- Ubuntu 22.04.01
- libudev
- build-essentail (for the c linker of gcc)
Setup
- Install the Rust programming language
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Typing the previous command into the terminal will install the rustup - rust version management tool, cargo - library management and build tool as well as rustc - the rust compiler. Restart the shell for the downloaded programs to become available.
- Install elf2uf2
cargo install elf2uf2-rs
This program enables the user to automatically convert elf binaries to uf2 files which will be uploaded to the RPI pico. The program is part of the pico-sdk.
- Setup the dependencies
The dependencies are declared in the Cargo.toml file. Create a new file called “Cargo.toml” (case sensitive) in the project directory.The minimum setup would look like this:
[dependencies]
rp-pico = "0.4.1" // Hardware abstraction layer for the pico
panic-halt = "0.2.0" // Sends the program into an infinit loop when panicing
The rp-pico crate is part of the rp2040-hal repository.
- Declare the target
The microcontroller on the RPI pico is the RP 2040, based on an ARM Cortex-M0. Because you won’t be running an operating system on the pico e.g. the code will be a “bare-metal” program. You will be adding the following lines to the main.rs file. This means you won’t be using the standard library and there is no main function.
#![no_std]
#![no_main]
You can either specify the target every time when compiling or add the target to the .cargo/config.toml.
cargo build --target thumbv6m-none-eabi
If you forget to declare this, the compiler will throw this error:
error: language item required, but not found: `eh_personality`
If you don’t feel like providing the target command line option every time the option can alternatively be added to the .cargo/config.toml and declare the target there, like this:
[build]
target = "thumbv6m-none-eabi"
- Coding the main.rs
Additional Resources
Trouble Shooting
Problem
The following error occurs if you don’t have all prerequisits installed when installing elf2uf2-rs:
error: failed to run custom build command for
libudev-sys v0.1.4
Caused by:
process didn't exit successfully: /tmp/cargo-installHyFOBU/release/build/libudev-sys-88bc62f58add172f/build-script-build (exit status: 101)
--- stdout
cargo:rerun-if-env-changed=LIBUDEV_NO_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG
cargo:rerun-if-env-changed=LIBUDEV_STATIC
cargo:rerun-if-env-changed=LIBUDEV_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
--- stderr
thread 'main' panicked at 'called Result::unwrap() on an Err value: "Could not run \"pkg-config\" \"--libs\" \"--cflags\" \"libudev\"\nThe pkg-config command could not be found.\n\nMost likely, you need to install a pkg-config package for your OS.\nTry apt install pkg-config, or yum install pkg-config,\nor pkg install pkg-config, or apk add pkgconfig depending on your distribution.\n\nIf you've already installed it, ensure the pkg-config command is one of the\ndirectories in the PATH environment variable.\n\nIf you did not expect this build to link to a pre-installed system library,\nthen check documentation of the libudev-sys crate for an option to\nbuild the library from source, or disable features or dependencies\nthat require pkg-config."', /home/jakob/.cargo/registry/src/github.com-1ecc6299db9ec823/libudev-sys-0.1.4/build.rs:38:41
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish…
error: failed to compile elf2uf2-rs v1.3.7, intermediate artifacts can be found at /tmp/cargo-installHyFOBU
Solution
Install package config with sudo apt install pkg-config.
Enjoy Reading This Article?
Here are some more articles you might like to read next: