【WSL2】WSL2+Win11 Python内使用ST-link & ST-Link/USB连接至WSL2
起由是准备使用pyswd
这个库,实现利用Python通过ST-Link调试STM32,由于电脑中的Python开发环境安装在WSL2内的Ubuntu22.04中,需要实现WSL2可以访问Win11中的ST-Link外设,在此记录下配置过程。
pyswd
库配置和libusb
安装
- 在Ubuntu中,利用pip安装
pyswd
库
参考:pyswd 1.0.0pip install pyswd
- win11中配置ST-Link驱动为
libusb
- 到 https://sourceforge.net/projects/libusb-win32/files/libusb-win32-releases/1.2.6.0/ 下载文件【libusb-win32-bin-1.2.6.0.zip】
- 解压后进入~/bin/,打开程序【inf-wizard.exe】,选中STM32 STLink,生成对应的.inf驱动文件
- 点击Next后,选择合适的路径保存.inf驱动文件
- 然后点击Install Now…,完成驱动安装
如果出现:LIbusb:system policy has been modified to reject unsigned drivers
解决方法:以管理员身份打开【inf-wizard.exe】,再重复上述操作.
WSL2连接至USB Device
如果不将WSL2连接至USB Device,当使用pyswd
获取ST-Link的时候,会报如下错误:NoDeviceFoundException;表示WSL2中的Ubuntu无法连接到Win11中的USB Device。
-
WSL2连接至USB Device的操作过程可参考微软的官方文档进行:Connect USB devices
! WSL2 Kernel version要高于5.10.60.1,可在Powershell中使用
wsl --update
进行更新。 -
操作完成后,在bash中调用
lsusb
,可以看见STLink Device说明链接成功。
-
操作完成后,若在调用
pyswd
出现如下错误:
其主要原因是上述微软官方指南的这段话:
You should see the device you just attached and be able to interact with it using normal Linux tools. Depending on your application, you may need to configure udev rules to allow non-root users to access the device. -
后续可参考这个链接配置udev rules授予权限:udev rule to allow non-root access to libusb devices being ignored/not working #8502
-
Plus:每次断开USB设备后,都要重新在powershell使用
usbipd wsl attach --busid <busid>
进行连接,其中<busid>
替换为对应的设备号,可通过命令usbipd wsl list
查看。
更多的参考:https://github.com/dorssel/usbipd-win/wiki/WSL-support
备注
曾短暂出现了在Ubuntu bash中调用命令sudo service udev status
/sudo service udev start
,没有反应,后续利用命令sudo apt install udev
安装udev后,重新调用上述语句,恢复正常…