Skip to content

Instantly share code, notes, and snippets.

@bonnee
Last active June 14, 2024 02:19
Show Gist options
  • Save bonnee/393c4be25d2e8620d9ec406073940d3a to your computer and use it in GitHub Desktop.
Save bonnee/393c4be25d2e8620d9ec406073940d3a to your computer and use it in GitHub Desktop.
STM32 developement on Visual Studio Code

STM32 developement on Visual Studio Code

This guide will help you install and setup Visual Studio Code for programming and debugging STM32 boards.

I tested this guide under Arch Linux and Ubuntu 18.04. If you get it to work under other setups, please let me know so I will update the steps with more info.

Warning

If you were using STM32CubeIDE or SystemWorkbench before, you need to convert your projects in order for them to work. The conversion procedure is fully reversible.
Up until the time of writing this guide, it is not possible to use STM32CubeIDE and Visual Studio Code on the same project unless some configuration changes are made on CubeIDE. Besides that, it is reccomended that every person that works on a project runs the same working environment.

Installation

1. STM32CubeMX

If you already have CubeMX installed, you can skip this step

Arch Linux

Install stm32cubemx from the AUR. If you don't have access to the AUR or you just don't want to use it, follow the step below.

Inferior operating systems (Windows, Ubuntu, Temple OS...)

Download the .zip from here (you have to sign up to ST's website) and install the right version for your OS.

2. GNU ARM Embedded Toolchain

These are the tools needed to compile and debug the code.

Arch Linux

Install arm-none-eabi-gcc arm-none-eabi-gdb arm-none-eabi-newlib.

Debian/Ubuntu

Install gcc-arm-none-eabi gdb-multiarch libnewlib-arm-none-eabi.

Windows

Install the toolchain from arm's website

3. Make

Linux

Install make from your package manager

Windows

Install make from this this page (updated in 2006 though...). It should be possible to get the current version of make through WSL, but I don't have experience with it.

4. OpenOCD

Open On-Chip Debugger is the software that will take care of uploading the compiled software to the STM32, and during debug, it will open the connection between the computer and the STM32.
If you are on Windows you could probably get OpenOCD installed, but I don't have idea on how to do it. Contact me if you're willing to find a way.

Linux

Install openocd from your package manager

5. Visual Studio Code

The next steps aren't really necessary to get the thing working. You could just use the a terminal and your favourite editor and you would have (almost) all the functionality of the complete setup. Visual Studio Code is just a pretty front-end.

Linux

This page contains the instructions to install the latest version of VSCode on many distributions.

Windows

Download and install the program from the official website.

6. C/C++ Extension

This extension will take care of intellisense, syntax highlighting and more.
Install this extension in Visual Studio Code.

7. stm32-for-vscode

Same as above, but with this extension.

Configuration

1. STM32CubeMX

By default CubeMX generates projects in a format called EWARM. Unfortunately, EWARM is currently not supported by the VSCode extension, but the more generic Makefile structure is. In order to configure CubeMX to support VSCode you have to navigate to Project Manager->Project->Toolchain/IDE and set it to Makefile.

Under the Code generator tab, enable the Copy all used libraries into the project folder option. This step is needed because stm32-for-vscode doesn't support the implicit inclusion of libraries yet.
After doing that, click on GENERATE CODE; You should see the new Makefile project structure being created.

2. Visual Studio

Using the Open folder menu in Visual Studio Code, navigate to your project's root folder and open it. Now open the command palette (Ctrl+Shift+P or F1) and run Build STM32 Project. A terminal should appear and you will see gcc (hopefully) compiling your project.

Usage

Compiling & Flashing

After you call Build STM32 Project for the first time, stm32-for-vscode will create two custom tasks (that can be accessed by typing Ctrl+Shift+B) to build your code and to flash it into the STM32 board.

Debugging

To debug the code just press F5 inside VSCode and the debugger will start automatically. Remember to stop the debugger before flashing new code.

Troubleshooting

Error: libusb_open() failed with LIBUSB_ERROR_ACCESS during upload

This happnes because you don't have permission to open the serial device. In order to fix this you need to add your user to the group that owns the serial interface. To get the serial interface name you can run dmesg | grep tty after plugging the STM32 in. Read the last line, you should see something like:

cdc_acm 2-2:1.2: ttyACM0: USB ACM device

In this example ttyACM0 is the device name.
Now that you know the name you can get the owner by doing ls -l /dev/ttyACM0. The output looks like this:

crw-rw----  1 user   group   4, 166 set 11 08:45 /dev/ttyACM0

group indicates the group name.

Now you just need to add your user to the group by doing sudo usermod -aG group $USER, substituting group with the group name you have discovered above.

You are done. Log out of your account and log back in to apply the change.

@Bill5203
Copy link

Bill5203 commented Aug 9, 2021

Regarding Error:libusb_open() failed ....
Didn't work. It still has the same error.
Mint 20.2

@gitLotos
Copy link

works on xbuntu LTS

@NicNedwob
Copy link

Hey there,
Sorry if this is a silly question, my stm development is a bit rusty. Is writing to the stm32 board via serial supported in this extension or will it require an st-link module?

@bonnee
Copy link
Author

bonnee commented Jul 25, 2022

Hey there, Sorry if this is a silly question, my stm development is a bit rusty. Is writing to the stm32 board via serial supported in this extension or will it require an st-link module?

This tutorial does not cover the tools needed to flash over serial. the vscode extension integrates with openocd, that expects some sort of external programmer.

@aalmeida314
Copy link

Hi, I have a question. How can I run a .cpp project?

CubeMx just create .c projects. I changed main.c for main.cpp and changed the language for C++ in STM32-for-VSCode.config.yalm, but it still doesn't working. I got this error: MAKE.EXE: *** No rule to make target 'Core/Src/main.c', needed by 'build/main.o'. Stop.

@bonnee
Copy link
Author

bonnee commented Oct 26, 2022

I have never tried to run cpp projects on STM 32 so i can't help you with the details. However, the error you are encountering comes from the makefile that is trying to run main.c that you changed. Does the generated CubeMX code include a main.c file even though the selected language is cpp?

@aalmeida314
Copy link

Actually, in CubeMX there isn't an option to generate codes in cpp, just in C. However, in the description of this extension is written: "This extension also supports using cpp files, however for this main.c has to be manually renamed to main.cpp for it to work." I did it, but it doesn't work.

@bonnee
Copy link
Author

bonnee commented Oct 26, 2022

Have you tried contacting the authors of the extension?

@aalmeida314
Copy link

Yes, and I've been waiting for then answer. I've out here because if you knew it, it will enrich the text!

@AlexSartori
Copy link

Thank you kind sir this gist is always helpful to me god bless the cows

@Leafmun-certii
Copy link

OpenOCD can be installed on windows via choco install openocd
Thanks for the guide :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment