30-04-2021



Originally published on GitHub Gists

2020年7月26日更新:增加了Jlink GDB Server的支持,可以使用J-Scope 和 SEGGER RTT了 2020年7月3日更新:改为了使用Cortex-Debug插件来进行调试; 详细研究了ccppproperties.json该如何配置,保证消灭红色波浪. STM32F030R8 - Mainstream Arm Cortex-M0 Value line MCU with 64 Kbytes of Flash memory, 48 MHz CPU, STM32F030R8T6, STM32F030R8T6TR, STMicroelectronics. Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications. Visual Studio Code is free and available on your favorite platform.

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.

Steps

  1. Visual Studio Code

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.

Stm32 Visual Studio Code Free

2. GNU ARM Embedded Toolchain

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

Arch Linux

Install arm-none-eabi-gccarm-none-eabi-gdbarm-none-eabi-newlib.

Debian/Ubuntu

Install gcc-arm-none-eabigdb-multiarchlibnewlib-arm-none-eabi.

Windows

Install the toolchain from arm’s website

3. Make

Linux

Install make from your package manager

Stm32 Blue Pill Visual Studio Code

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.

Stm32 Cmake

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.

5.1. C/C++ Extension

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

5.2. stm32-for-vscode Extension

Same as above, but with this extension.

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.

Visual

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.

Code

Error: libusb_open() failed with LIBUSB_ERROR_ACCESS during upload

Stm32 Visual Studio Code

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:

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:

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.

This is a supporting extension to setup STM32 projects generated from STM CubeMX. It automatically generates a makefile and starts the build process, it also adds debugging, build and flash configurations to the workspace.

It also supports using cpp files, however for this main.c has to ben manualy renamed to main.cpp for it to work.

To use the extension use the cmd/ctrl+shift+p shortcut to open the commands panel and run the Build STM project command. This will setup the project and start the building process.

This project depends on a few dependencies so make sure they are installed before using this extension.

Prerequisites

There are a few command line tools that needs to be added to the PATH for this extension to work. On top of this the Cortex-Debug extension needs to be installed for debugging. Should any of the command line tools not be installed you will get a warning stating which one is missing. The requirements are stated below.

Visual Studio Code Gdb Stm32

  • Make (platform dependent, Windows, OSX: install command line developer tools)
  • OpenOCD: Windows, all platforms

Configuring CubeMX

This extension assumes the project initialised with CubeMX and the option to create a Makefile project under Project Manager->Project->Toolchain/IDE.

Also please leave the default on Copy all used libraries into the project folder and generate seperate .c and .h files for the peripherals.

How to use

Use cmd/ctrl+shift+p to open the show all commands panel and issue the command: Build STM32 project. Please note that external libraries should be placed in the lib folder and source code should be place in the src and inc folders.

Features

Visual Studio Code Stm32 Linux

  • Creating a makefile and building and STM32 project.
  • Adding configurations for debugging, flashing and building in the workspace.
  • Ability to compile it as a C++ project by adding a main.cpp file.
  • Automatic configuration of intellisense.
  • Detection of required tools and creating a warning.

Stm32 Visual Studio Code Download

Disclaimer

This an extension created because I wanted a fast way to build, flash and debug STM32 on OSX in VSCode. This extension is used internally at Bureau Moeilijke Dingen for development. As this might be helpful to others I have allocated time to publish this extension. Should you find any bugs or have feature requests please open an issue on the Github page.

Issues

Cannot find arm-none-eabi-gcc or will not compile on OSX

Due to new access permissions on OSX sometimes when installing the extension has no access and execution rights for the arm compiler. To prevent this we advise using xpm as an installer. Make sure you hae a version of node installed. You can then run the following command in the terminal to install the arm-none-eabi compiler:

$ npx xpm install --global @xpack-dev-tools/arm-none-eabi-gcc@latest

(npx allows you to not install xpm as a global dependency and just run it).After this the arm-non-eabi-gcc files can be found in the following folder where the text LATEST-VERSION-NUMBER should be replaced by the version number you downloaded: ~/Library/xPacks/@xpack-dev-tools/arm-none-eabi-gcc/LATEST-VERSION-NUMBER/.content/bin/arm-none-eabi-gcc

If you input this into you STM32 for VSCode configuration the compilation should work.