KevsRobots Learning Platform
77% Percent Complete
By Kevin McAleer, 3 Minutes
The Raspberry Pi Pico is a popular microcontroller for various projects. This lesson will guide you through setting up Rust for development on the Pico, how to install your Rust programs on the device, techniques for debugging, and walking through a sample program.
To develop Rust programs for the Raspberry Pi Pico, you’ll need to set up your build environment:
cross
tool for cross-compiling: cargo install cross
.rustup target add thumbv6m-none-eabi
After developing your Rust application, you’ll need to compile it for the Pico and upload it:
cargo build --release --target thumbv6m-none-eabi
Convert the compiled program to UF2 format, which is necessary for the Raspberry Pi Pico bootloader.
Push the RESET button on your Pico while holding the BOOTSEL button, release the BOOTSEL after the device is connected. It should mount as a Mass Storage Device.
Copy your .uf2
file to the Raspberry Pi Pico mass storage device.
Debugging microcontroller applications can be challenging. Here are some techniques for debugging Rust programs on the Pico:
println!
debugging to send output to a connected serial terminal.Here’s a simple Rust program that blinks an LED on the Raspberry Pi Pico:
#![no_std]
#![no_main]
use panic_halt as _;
use rp_pico::hal::{prelude::*, Timer};
use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
let mut pac = rp_pico::pac::Peripherals::take().unwrap();
let mut watchdog = Watchdog::new(pac.WATCHDOG);
let clocks = rp_pico::hal::clocks::init_clocks_and_plls(
rp2040::XOSC_CRYSTAL_FREQ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();
let mut delay = Timer::new(pac.TIMER, &mut pac.RESETS);
let sio = Sio::new(pac.SIO);
let pins = rp_pico::Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);
let mut led_pin = pins.led.into_push_pull_output();
loop {
led_pin.set_high().unwrap();
delay.delay_ms(500);
led_pin.set_low().unwrap();
delay.delay_ms(500);
}
}
This program initializes the board’s peripherals, sets up an LED pin, and then blinks the LED on and off every 500 milliseconds.
In this lesson, you’ve learned how to set up a Rust development environment for the Raspberry Pi Pico, how to install Rust programs on the device, strategies for debugging, and you’ve walked through an example program that blinks an LED.
You can use the arrows ← →
on your keyboard to navigate between lessons.