Setup Arduino Rust On Wsl

08 Sep 2024

This is a guide to use rust for arduino development on wsl (Windows subsystem for linux). Everything will be setup and available in the wsl terminal. Through this guide we will install the following components:

WSL

  1. Install wsl ubuntu. wsl --install this command this will install a default ubuntu distro. If you want to install a different distro refer to this page
  2. We also need to change the font of the terminal to be able to use neo vim properly.
    1. Download any nerdfont as required by neovim.
    2. Select all the files
    3. Right click and choose install.
  3. Setup window terminal to use this new font.
    1. Open a windows terminal
    2. Ctrl+, to open settings
    3. Clik on open json file at the bottom left corner. (This will open the json settings file in the default text editor)
    4. You can create a new or update the existing ubuntu profile
           "profiles":
           {
               "list": [
                   {
                       "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                       "hidden": false,
                       "name": "Ubuntu",
                       "source": "Windows.Terminal.Wsl",
                       "colorScheme": "Ubuntu-ColorScheme",  
                       "cursorShape": "filledBox",
                       "font": {
                           "face": "Cousine Nerd Font",  <-- specify the installed nerd font here
                           "size": 12
                       }
                   },
               ]
           }
      
    5. restart the terminal and notice the new font.
  4. Install usbipd-win.
    1. Connect the arduino to the pc.
    2. list the usb device with usbipd list
    3. share arduino bus with wsl usbipd bind --busid <busid> You only need to share it once
    4. Open wsl and run this command to attach it usbipd attach --wsl --busid <busid>
    5. you should now see the arduino if you list the usb device in wsl with lsusb

You can also create a shortcut to be able to launch a new windows terminal directly with the Ubuntu profile. To do that simply navigate to C:\Users\{username}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs, right click, new and shortcut. Use wt.exe as target and add -p Ubuntu to start the ubuntu wsl profile. So the resulting shortcut target should be wt.exe -p Ubuntu. Note that you might need to use the full path to wt.exe. To get the full path you can simply run where wt in a windows terminal.

Zsh

Follow this page to install zsh in wsl.

Note: You don’t need to install the font since we already installed nerdfonts in the previous step.

NeoVim

Let’s get Rusty youtube channel as a nice video on how to setup linux for rust development. Follow there step in the wsl terminal.

  1. Install nvim
  2. Install nvcahd. Make sure to install nerdfonts in the windows terminal to do that see WSl setup above.
  3. Install rust analyzer
  4. Install rustaceanvim

Arduino tools

Look at this page to setup rust for arduino. This will install all the necessary dependencies and init a new project with cargo generate.

USB

To make sure what /dev/tty** file to use for ravendude you can follow these steps.

First, make sure that you have installed usbipd-win and that the bus is shared with wsl in visble in wsl with lsusb. To determine what /dev/tty*** unbeffured file to use you can do

❯ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 2341:0043 Arduino SA Uno R3 (CDC ACM)  <-- arduino device.  this is a acm so looking for ttyACM* file
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

If this is the only usb device shared with wsl we can suppose that the file name will be /dev/ttyACM0. But if you want to be sure you can use this command dmesg -H > test.txt. This will output to the test.txt file. Open this file and search for arduino.

[  +0.000002] usb 1-1: Manufacturer: Arduino (www.arduino.cc)
[  +0.000002] usb 1-1: SerialNumber: 75130303036351106150
[  +0.024063] cdc_acm 1-1:1.0: ttyACM0: USB ACM device <-- ttyACM0

You could also use this method:

  1. disconnect the arduino.
  2. list all the tty files to a file ls \dev\tty* > test.txt
  3. connect the arduino
  4. attach the device to wsl (see command above in the wsl section.)
  5. list the tty files to a new tmp file ls \dev\tty* > test2.txt
  6. compare both files:
    comm -1 -3 test.txt test2.txt
     /dev/ttyACM0