Rust And Pico2

Rust And Pico2

So here’s the simple tutorial to build and flash a Rust application to the Pico 2 microcontroller

Step 1: Environment set-up

  1. Clone the Pico SDK:

     git clone https://github.com/raspberrypi/pico-sdk
    
  2. Set the PICO_SDK_PATH environment variable:

     export PICO_SDK_PATH=/path/to/pico-sdk
    
  3. Clone the picotool:

     git clone https://github.com/raspberrypi/picotool
    
  4. Install dependencies:

    • On Mac:

        brew install libusb pkg-config
      
    • On Linux:

        sudo apt install libusb
      

(Don’t forget to have CMake installed as well!)

  1. Set up picotool for building:

     cd picotool
     mkdir build
     cd build
     cmake ../
    
  2. Build picotool using make:

     make
    

Step 2: Adding targets

adding the targets needed for building for the ARM Cortex M33 cores:

rustup target add thumbv8m.main-none-eabihf

Step 3: Building and Blinking the LED

In this we’ll simply use the rp-hal-rp2350-public library to build an LED blinking example.

  1. Clone the library:

     git clone https://github.com/thejpster/rp-hal-rp2350-public
     cd rp-hal-rp2350-public
    
  2. Build the example for ARM Cortex M33 cores:

     cargo build --example pwm_blink --target thumbv8m.main-none-eabihf --all-features
    

This compiles the pwm_blink example, preparing it to be flashed onto the Pico 2.


Step 4: Connect pico to your laptop and check info

Connected it using a USB cable while holding down the BOOTSEL button, and now it should be detected as a mass storage device.

check device information by running:

picotool info -d

Here’s a sample output from my Pico 2:

Device Information
 type:                 RP2350
 package:              QFN60
 chipid:               0x5fb123495dff26f0
 flash devinfo:        0x0c00
 current cpu:          ARM
 available cpus:       ARM, RISC-V
 default cpu:          ARM
 secure boot:          0
 debug enable:         1
 secure debug enable:  1
 flash size:           4096K

This confirms your Pico 2 is connected correctly and ready to be flashed.


Step 5: Flashing:)

  1. Option 1: Flash Directly using picotool:

     picotool load -t elf ./target/thumbv8m.main-none-eabihf/debug/examples/pwm_blink
    

    Here’s a sample output:

     Family id 'rp2350-arm-s' can be downloaded in absolute space:
       00000000->02000000
     Loading into Flash: [==============================]  100%
    
  2. Option 2: Convert the Binary to a .uf2 File:

     picotool makeuf2 -o output.uf2 ./target/thumbv8m.main-none-eabihf/debug/examples/pwm_blink
    

    After generating the .uf2 file, simply drag and drop it onto your Pico 2’s bootloader drive.


Step 6: Rebooting the Pico 2

Once the application is loaded, reboot the device by running:

picotool reboot

Sample output:

The device was rebooted into application mode.

Now you should see the LED flashing! 🎉


And that’s it! You’ve successfully built and flashed your first Rust application to the Pico 2 microcontroller.