Make sure to install the correct drivers for the stlink programmer.
⚠️ If you are on Windows, you may want to reboot the system after installing drivers or even PlatformIO
- OS: Windows 10 Pro 20H2 build 19042.1052
- VSCode: v1.53.2
- PlatformIO: Core v5.1.1
- Board: Nucleo-STM32L476RG
- Programmer: Mini ST-LinkV2
The programmer looks like a small USB stick and was bought from az-delivery
Open the extension page, click on Platforms > Embedded and look for STM32Cube
From PIO home click Projects > + Create New Project
Set the project name and the target board (e.g. Nucleo L476RG)
You can also set an installation directory if it's the first time using the extension
Make sure to set the framework to STM32Cube
The extension will generate a directory for the project and a configuration file platformio.ini.
Other settings can be added following the platform specific guide https://platformio.org/platforms/ststm32/boards
Add some code to the folder src
Check out main.c
and main.h
in this Gist
Or the linked GitHub repo
Read the pinout of the programmer and the board and connect 4 pins on the SWD connector:
- RST
- SWCLK
- SWDIO
- GND
- 3.3V
Keep the ST-link jumpers on the board
Connect the usb cable to power up the board
Now verify that the programmer is correctly recognised by PlatformIO:
Open the device tab from the PIO extension and check the device list.
If the programmer is not present in the list you can try:
- Rebooting the system
- Reinstalling the ST-Link drivers (and rebooting the system)
- Reinstalling PlatformIO (First close VSCode then delete the .platformio folder, usually in %userprofile% on windows)
Building
First of all check that the code builds correctly.
You can use one of the following methods:
- Open the PlatformIO extension and click on Project Tasks > nucleo_l476rg > General > Build
- Open the command palette (CTRL + P) and search for PlatformIO: Build
- Click the checkmark ✔️ symbol on the status bar below
If VSCode asks to configure a build task, relaunch the IDE then try to build the project.
Make sure to open the project from PatformIO!
Uploading
Specify the protocol in platformio.ini
upload_protocol = stlink
To be able to upload your code, you may have to configure openocd config files
The files are located under %userprofile%/.platformio/packages/tool-openocd/scripts
In particular for this combination of board + mini usb programmer, it has been necessary to edit the file
boards/st_nucleo_l4.cfg
...
# use hardware reset
# reset_config srst_only srst_nogate
reset_config trst_only
You can then upload your code on the board using one of the following methods:
- Open the PlatformIO extension and click on Project Tasks > nucleo_l476rg > General > Upload
- Open the command palette (CTRL + P) and search for PlatformIO: Upload
- Click the arrow ➡️ symbol on the status bar below
Add the the following line of code to platformio.ini
debug_tool = stlink
Now you can start debugging your code in multiple ways:
- Open the PlatformIO extension and click on Quick Access > Debug > Start Debugging
- Open the command palette (CTRL + P) and search for PlatformIO: Start Debugging
VSCode will reupload the code on the board and open the debugging view.