This repository is a template for stm32 development with VSCode and CMake in Windows/WSL. Main tools:
- CMake with stm32 toolchain
- stlink
- gcc arm
- gdb
- WSL*
- USB-IP*
*- if using WSL
Execute tools/install_toolchain_win_scoop.bat
. This will install scoop package manager and all required toolchain components.
This toolchain was tested in WSL2 with Ubuntu 22.04. Without deep dive I was unable to make it work in Ubuntu 20.04. Suggested installation:
- (optional) Install Windows Terminal
- Run
wsl --install
If wsl is already installed, it is possible to add new distribution:
-
(optional) Install Windows Terminal
-
Open PowerShell
-
Run
curl (("https://cloud-images.ubuntu.com", "wsl/jammy/current/", "ubuntu-jammy-wsl-amd64-wsl.rootfs.tar.gz") -join "/") ` --output ubuntu-22.04-tar.gz
This will download Ubuntu 22.04 image. You can browse other images if you need a different version.
-
Run
wsl --import <Distribution Name> <Installation Folder> <Ubuntu WSL2 Image Tarball path>
This will add additional distribution to WSL.
Example:wsl --import Ubuntu-stm32 "C:\Users\<Username>\Documents\Ubuntu-stm32" .\ubuntu-22.04-tar.gz
At this point the distributon is added. By default tere is only root user. We need to create new user.
-
Run
wsl -d <Distribution Name>
We've now entered ubuntu shell. -
Run
NEW_USER=<USERNAME>
This will store username for future use.Next, create user and set password:
useradd -m -G sudo -s /bin/bash "$NEW_USER"
passwd "$NEW_USER"
-
Run
tee /etc/wsl.conf <<_EOF [user] default=${NEW_USER} _EOF
This will change created user to be the default when you log in.
-
Run
logout
to exit wsl and then run
wsl --terminate <Distribution Name>
to close wsl.
You can use steps 3-7 to create additional WSL distributions.
First step was done using guide by cloudbytes.dev
- Launch VSCode.
- Install WSL extension.
- Open new WSL window using distro created in the first step.
3.1 (optional) Open terminal in vscode and create folder sctructure. Example:
mkdir repos && cd repos
- Clone this repository with submodules.
git clone --recurse-submodules https://github.com/Dooez/stm32-template
- Open cloned folder in VSCode.
- Install suggested extensions.
- Run
sudo ./install_toolchain.sh $USER
This will install required tools and set up usbip in WSL. - After script finished, reboot WSL.
-
Install USB-IP
-
Open PowerShell
-
Run
usbipd wsl list
This will show list of devices. Find your busid of youe STLink device:BUSID VID:PID DEVICE STATE 1-1 0483:3748 STM32 STLink Not attached
In this case busid is 1-1
-
Run
usbipd wsl attach --busid <busid> -d <Distribution Name>
This will attach STLink to WSL. To check if everyuthing is working run
usbipd wsl list
You should see
BUSID VID:PID DEVICE STATE 1-1 0483:3748 STM32 STLink Attached - Ubuntu-22.04
-
(optinal) Run in WSL
lsusb
And you should see your deviceBus 001 Device 002: ID 0483:3748 STMicroelectronics ST-LINK/V2
You will need to repeat this step when you reboot WSL or host machine
See usbipd wiki for more detailed info.
- After running installation shell script, installing the extensions and relaunching VSCode CMake Tools should ask for kit. If not, you may select kit from bottom panel. If everything is successfully installed, arm-none-eabi will be available, this is the kit you need.
- Open CMakeLists.txt and change MCU model to match your device. After saving CMakeLists.txt CMake Tools extension will run configuration and all dependencies should be downloaded. Read stm32-cmake documentation on how to use it.
fetch_svd(<mcu>)
andupdate_launch_json()
CMake function will download SVD file and updatelaunch.json
fields marked by comment... /* #update this field with CMake */
- Copy
stm32<>xx_hal_conf.h
configured (by STM32CubeMX for example) for your board. - To build use [CMake: build] build task (Ctrl + Shift + B by default) or hotkey (F7 by default). To flash and erase use [ST Flash] and [ST Erase] tasks.
- For debugging use Run and Debug window (Ctrl + Shift + D by default) or hotkey (F5).