Thursday, April 11, 2024

Connect USB device to WSL

Connect USB device to WSL

Connect USB device to WSL

1 Introduction

This article will guide you through the steps required to connect a USB device to Linux running on Windows Subsystem for Linux (WSL).

2 Prerequisites

  • Install WSL and ensure it is updated to the latest version.

For installation guidance, refer to How to install Linux on Windows with WSL

  • Install a Linux distribution configured to WSL 2.


Refer to Set WSL version to 1 or 2

C:\>wsl --set-version ubuntu 2
For information on key differences with WSL 2 please visit https://aka.ms/wsl2
Conversion in progress, this may take a few minutes....
The operation completed successfully.

Ensure "Virtual Machine Platform" optional component is enabled.

C:\> wsl --install --no-distribution
  • Ensure virtualization is enabled in the BIOS.

For more information, refer to Enabling Virtualization in your PC BIOS

  • Install the USBIPD-WIN project.

Visit the usbipd-win project releases page.

Download and run the usbipd-win_x.msi installer.

3 Attach a USB device

  • Ensure a WSL command line is open.
C:\> wsl ~
  • List all the USB devices
C:\> usbipd list

You will see a list similar to this:

Connected:
BUSID  VID:PID    DEVICE                                                        STATE
1-2    0408:5347  HP HD Camera, HP IR Camera                                    Not shared
1-4    0483:3748  STM32 STLink                                                  Not Shared
1-10   8087:0026  Intel(R) Wireless Bluetooth(R)                                Not shared
3-1    0bda:8153  Realtek USB GbE Family Controller #3                          Not shared
3-2    04d9:2011  USB Input Device                                              Not shared
3-5    03f0:644a  HP Universal USB-C Multiport Hub                              Not shared
... ...
  • Share the USB device to be attached to WSL.

This command requires administrator privileges. Select the bus ID of the device accordingly.

C:\> usbipd bind --busid 1-4

After binding, the device's state will change to "Shared".

C:\> usbipd list
Connected:
BUSID  VID:PID    DEVICE                                                        STATE
... ...
1-4    0483:3748  STM32 STLink                                                  Shared
... ...
  • Attach the USB device
C:\> usbipd attach --wsl --busid 1-4

You will receive a confirmation message like this:

usbipd: info: Using WSL distribution 'Ubuntu' to attach; the device will be available in all WSL 2 distributions.
usbipd: info: Using IP address 172.18.64.1 to reach the host.

After the device is attached, it can be seen in WSL.

~ > lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 0483:3748 STMicroelectronics ST-LINK/V2
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

The following step will demonstrate flashing an STM32 discovery board with an OpenOCD for Linux image built from Buildroot.

~/buildroot > sudo board/stmicroelectronics/stm32f429-disco/flash.sh output stm32f429discovery
Open On-Chip Debugger 0.12.0
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Info : clock speed 2000 kHz
Info : STLINK V2J44S0 (API v2) VID:PID 0483:3748
Info : Target voltage: 2.872408
Info : [stm32f4x.cpu] Cortex-M4 r0p1 processor detected
Info : [stm32f4x.cpu] target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for stm32f4x.cpu on 3333
Info : Listening on port 3333 for gdb connections
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
[stm32f4x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08000520 msp: 0x2002bffc
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : device id = 0x10036419
Info : flash size = 2048 KiB
Info : Dual Bank 2048 kiB STM32F42x/43x/469/479 found
flash 'stm32f2x' found at 0x08000000

#0 : stm32f2x at 0x08000000, size 0x00200000, buswidth 0, chipwidth 0
        #  0: 0x00000000 (0x4000 16kB) not protected
        #  1: 0x00004000 (0x4000 16kB) not protected
        #  2: 0x00008000 (0x4000 16kB) not protected
        #  3: 0x0000c000 (0x4000 16kB) not protected
        #  4: 0x00010000 (0x10000 64kB) not protected
        #  5: 0x00020000 (0x20000 128kB) not protected
        #  6: 0x00040000 (0x20000 128kB) not protected
        #  7: 0x00060000 (0x20000 128kB) not protected
        #  8: 0x00080000 (0x20000 128kB) not protected
        #  9: 0x000a0000 (0x20000 128kB) not protected
        # 10: 0x000c0000 (0x20000 128kB) not protected
        # 11: 0x000e0000 (0x20000 128kB) not protected
        # 12: 0x00100000 (0x4000 16kB) not protected
        # 13: 0x00104000 (0x4000 16kB) not protected
        # 14: 0x00108000 (0x4000 16kB) not protected
        # 15: 0x0010c000 (0x4000 16kB) not protected
        # 16: 0x00110000 (0x10000 64kB) not protected
        # 17: 0x00120000 (0x20000 128kB) not protected
        # 18: 0x00140000 (0x20000 128kB) not protected
        # 19: 0x00160000 (0x20000 128kB) not protected
        # 20: 0x00180000 (0x20000 128kB) not protected
        # 21: 0x001a0000 (0x20000 128kB) not protected
        # 22: 0x001c0000 (0x20000 128kB) not protected
        # 23: 0x001e0000 (0x20000 128kB) not protected
STM32F4xx - Rev: Y

auto erase enabled
wrote 16384 bytes from file output/images/stm32f429i-disco.bin in 0.621520s (25.743 KiB/s)

auto erase enabled
wrote 32768 bytes from file output/images/stm32f429-disco.dtb in 1.110691s (28.811 KiB/s)

auto erase enabled
wrote 1785856 bytes from file output/images/xipImage in 40.799717s (42.745 KiB/s)

Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
shutdown command invoked

Author: Winfred Lu

Created: 2024-04-12 Fri 10:40

Validate

No comments: