Cross Compile Rust Code for Raspberry Pi
1. Goal
Rust code was developed on the Windows PC for an experiment setup to collect and visualize real-time data from an embedded system (RP2040 MCU for example). Data is sent over serial/USB connection (FTDI 232H cable) to the PC. Now it’s time to deploy the hardware in the field for “long-term” data collection.
In this case, PC will be replaced with many instances of Raspberry Pi computers. So the Rust code needs to be cross compiled to run on Pi instead, while Pi is still interfacing with the embedded system over the serial/USB cable.
Here is a collection of notes on how to set up the Pi, and build code on Windows to run on the Pi. Things are changing both in the realms of Pi software, as well as Rust toolset. This is the learning as of March, 2023.
2. Setup Headless Raspberry Pi
Since Pi is only used as a data logger to stream serial data into a local file for storage, we don’t need to attach monitor or keyboard to it.
Over the years, a lot of things become easier for setting up Pi OS software, and run it headless. On the other hand, some other things become more restrictive due to security reasons.
To start, use Raspberry Pi Imager to burn the latest OS image. Click into the advanced option button to set up the following items before hand:
- host name (if you have multiple Pi-s to setup, choose different name for each one)
- enable SSH and login password
- set WIFI information, if available
After Pi is booted from the flashed SD card, SSH into it with the “nnn.local” (host name). Hopefully no need to NMAP out its IP address. The other way to connect to Pi is through an Ethernet cable to PC directly (link).
Now install xrdp and add a new user (security reason), so that Windows remote desktop can access Pi GUI, if needed.
sudo apt-get install xrdp
sudo adduser piguiuser
sudo usermod -aG sudo piguiuser