Article

Run Rust on your embedded device from VSCode in one click

Published on 2 min read

    The rust-analyzer plugin for VSCode provides you with a helpful little ▶ Run button above every test or main() function that lets you execute your code right out of the editor. Unfortunately though, this won't work out of the box in embedded projects, since rust-analyzer will invoke cargo run when you click on ▶ Run, and cargo itself does not know how to flash and run applications on embedded targets.

    However, Rust-Analyzer does seamlessly integrate with probe-run, a custom cargo runner for embedded development: Since probe-run is a cargo runner and not a cargo subcommand, all we have to do is modify our settings to use probe-run instead whenever cargo run is called. After that, we can use the ▶ Run button like we would in a native project.

    To show how to set this up, let's configure a project based on cortex-m-quickstart, namely our beginner embedded training code examples.

    First, make sure you've installed Rust-Analyzer and probe-run:

    $ cargo install probe-run
    

    Then, we need to find out if our chip is supported. This also tells us which variant name to use in our configuration later.

    $ probe-run --list-chips
    (..)
            STM32F107VB
            STM32F107VC
    nrf52 series
        Variants:
            nRF52810_xxAA
            nRF52811_xxAA
            nRF52832_xxAA
            nRF52832_xxAB
            nRF52840_xxAA
    nrf51 series
    (..)
    

    Since we want to build our examples for the nRF52840 Development Kit, our variant of choice is nRF52840_xxAA.

    Now, in the .cargo/config or .cargo/config.toml file of our project, we can set probe-run as the default runner to be used when running executables built for our board.

    [target.thumbv7em-none-eabi]
    runner = "probe-run --chip nRF52840_xxAA"
    #         ^^^^^^^^^        ^^^^^^^^^^^^^
    
    [build]
    target = "thumbv7em-none-eabi" # = ARM Cortex-M4
    

    And that's it! Now you can use the Run button as usual:

    Note that this now works for every file in your workspace, omitting the need for multiple custom VSCode tasks:

    ☝️ Note that we've just released probe-run 0.1.2 which adds support for hard-float targets, so we can use thumbv7em-none-eabihf too ✨

    Sponsor this work

    probe-run is a Knurling project and can be funded through GitHub sponsors. Sponsors get early access to defmt and other tools we are building. Thank you to all of the people already sponsoring our work through the Knurling project!