How to Connect a Microcontroller (MCU) to WSL2: Embedded Development Guide

 


Bridging the Gap: How to Connect an MCU to WSL2 for Embedded Development

The shift toward modern embedded engineering often requires Linux-native toolchains. Whether you are compiling a firmware image using the Zephyr RTOS, deploying an ESP-IDF application, or running customized compiler toolchains, Linux is the environment of choice for high-performance automation.

For Windows developers, WSL2 (Windows Subsystem for Linux 2) offers an incredibly fast, lightweight Linux environment right alongside their Windows desktop. However, embedded developers immediately hit a notorious hardware wall: WSL2 runs inside a lightweight virtual machine, meaning it cannot natively see USB hardware plugged into your Windows host.

If you plug an Arduino, an ESP32 devkit, or an ST-Link debugger into your laptop, Windows captures the COM port, leaving your WSL2 environment blind.

Fortunately, you don't need to dual-boot or spin up bloated virtual machines. By using the open-source usbipd-win project, you can pass raw USB traffic across the virtual network layer directly into Linux. Here is how to configure a bulletproof connection from your MCU to WSL2.

1. Prerequisites

Before we start routing traffic, ensure your system meets these baseline components:

  • Operating System: Windows 11 or Windows 10 (with the Microsoft Store version of WSL).

  • WSL Setup: A running Linux distribution explicitly set to WSL2.

  • Kernel Check: Your WSL kernel version must be 5.10.60.1 or higher. You can check your status and update it from a Windows PowerShell prompt using:

    PowerShell
    wsl --version
    wsl --update
    

2. Step-by-Step Hardware Forwarding Pipeline

Step 1: Install usbipd-win on Windows

Open an Administrator PowerShell window on your Windows host machine and use the Windows Package Manager (winget) to pull down the required architecture:

PowerShell
winget install dorssel.usbipd-win

Note: Once the installation finishes, close your terminal and open a fresh PowerShell window to ensure the system environment paths refresh correctly.

Step 2: Install USB Utilities inside WSL2

Open your Linux terminal (e.g., Ubuntu) and install the tools required to identify incoming USB hardware identifiers:

Bash
sudo apt update
sudo apt install -y usbutils hwdata

Keep this Linux window open; the virtual environment must be active for Windows to map the hardware connection.

Step 3: Identify Your MCU’s Bus ID

Return to your Windows Administrator PowerShell window and query the connected USB hardware topology:

PowerShell
usbipd list

You will see an output grid detailing every USB device attached to the host. Look for your microcontroller or serial chip (e.g., Silicon Labs CP210x, CH340, or STLink):

Plaintext
BUSID  VID:PID    DEVICE                                   STATE
1-3    1a86:7523  USB Serial Single Channel (COM3)         Not shared
4-1    0483:374b  STMicroelectronics STLink dongle         Not shared

Identify your target device and note its BUSID (for example, 1-3).

Step 4: Bind and Share the Device

Before WSL2 can seize control of the USB traffic, you must explicitly allow Windows to share it. Run the following command in the Admin PowerShell prompt using your specific Bus ID:

PowerShell
usbipd bind --busid 1-3

If you run usbipd list again, you will notice the state has shifted from "Not shared" to "Shared". You only need to perform this bind step once per device.

Step 5: Attach the MCU to WSL2

Now, route the shared USB port directly into the active Linux virtual sub-system. You no longer need an administrator prompt for this step:

PowerShell
usbipd attach --wsl --busid 1-3

Once attached, the Windows operating system surrenders control of that specific COM port interface. It is now fully owned by Linux.

3. Practical Example: Verifying and Reading the Serial Interface

Let's verify that our forwarded microcontroller is fully accessible inside Linux and test communication using standard command-line tools.

1. Verify the Hardware Attachment

Switch over to your active WSL2/Linux terminal and list the system's USB bus devices:

Bash
lsusb

Your microcontroller will now appear natively inside the Linux hardware list. To find the exact endpoint your development tools will interact with, query the system message logs:

Bash
dmesg | tail -n 20

Look for lines indicating a successful driver attachment. It will look similar to this:

Plaintext
[ 1024.452103] usb 1-1: Product: USB Serial Device
[ 1024.453412] usb 1-1: cp210x converter now attached to ttyUSB0

This confirms your MCU is mapped directly to the local dev node: /dev/ttyUSB0 (or /dev/ttyACM0 for certain boards).

2. Resolve Permission Restrictions

By default, Linux limits read/write access to dial-out serial devices to root or members of specific hardware groups. To grant your active Linux user permission to flash the MCU without using sudo, add yourself to the dialout group:

Bash
sudo usermod -a -G dialout $USER

Note: You must log out and log back into your WSL instance (or run wsl --shutdown from a Windows prompt) for group permission updates to take effect.

3. Reading the Serial Output Log

Now, you can use standard embedded developer utilities directly inside WSL2. For instance, to monitor serial output messages or debugging print lines tracking at a standard 115200 baud rate, install and launch screen:

Bash
sudo apt install -y screen
screen /dev/ttyUSB0 115200

(To exit the active screen session when finished, press Ctrl+A followed by K)

4. Detaching the Device

When you finish flashing your firmware or recording telemetry profiles, you can release the hardware so Windows can interact with the COM port again. From your Windows PowerShell terminal, run:

PowerShell
usbipd detach --busid 1-3

Conclusion: Full-Throttle Embedded DevOps

By abstracting hardware boundaries with usbipd-win, you gain access to the best of both worlds: the seamless UI capabilities of Windows mixed with the highly deterministic compiling and flashing toolchains of Linux. This setup forms the ideal baseline framework for deploying fast, automated testing loops for your hardware-defined software projects.

USB Device Connection Tutorial for WSL2

This video walks through the precise command-line sequences needed to seamlessly share and bind your external USB development kits into your Linux environment using the usbipd infrastructure.

Comments

Popular Posts