How to develop STM32 with VScode? part 1/2
Introduction
Basic theory
Firstly, you need to create your code. You can do it in every text editor e.g. Notepad,
Word, Eclipse or VScode.
When you
have the text (code) written in C or C++ you need to compile it -> translate a
source code into machine code, bytecode which will be understandable for the
microcontroller. The compiler looks for errors and optimises
the final binary code (those .bin files made with zeros and ones). At this
point, the compiler needs to know the target platform to compile source code
into a suitable format.
Next, your
understandable (for microcontroller) code has to be transferred from your
desktop into a microcontroller. Here comes the programmer and flashing software:
A microcontroller’s programmer acts as an interface between the PC and the target controller. The API/software of the programmer reads data from the hex file stored on the PC and feeds it into the controller’s memory. It needs to have suitable software but you don’t have to bother with that, just use a programmer from a Nucleo (this top part of a board) or any other ST-LINK programmer (I recommend those small USB dongles). Your job (as PC) is transferring this .bin file to the programmer and now you need some kind of flashing software. There are many of them (STM32 ST-LINK Utility, OpenOCD…) but for now, we will be using openocd.
Every Nucleo board has an ST-Link programmer |
ST-Link dongle |
And this is all you need to flash your idea into embedded systems!
But sometimes (probably even often) you need to check your program at runtime, set breakpoints, see variables, find errors, etc. That's when you need a DEBUGGER. And yes, one debugger is on the programmer, but that's only one part. In addition, you need a debugger on your computer and for convenience, some software to display and interact with your code (usually IDEs have such built-in, but for VScode we need an extension such as Cortex-Debug).
Let's make it work!
Key
elements:
- Environment
for code editing: Visual Studio Code
- Building
(within compiling): CMake with Make/Ninja
- Flashing and Debugging: OpenOCD, Cortex-Debug, arm-none-eabi-gdb
1st step - VS code:
Download VScode: linkIn VScode
add extensions: C/C++, CMake Tools, Cortex-Debug:
Warning:
2nd step - new folders:
Create folders
somewhere on your computer for the next steps:
For
example:
C:\tools\OpenOCD
C:\tools\CMake
C:\tools\ Arm GNU Toolchain
3rd step - Arm GNU Toolchain:
Download and install Arm GNU Toolchain: link
Choose your location |
Remember to add path to environment variables |
Now you have and can use arm-none-eabi-gdb which is a GNU debugger for ARM processors.
4th step - OpenOCD:
This supports flashing and debugging a wide variety of platforms
5th step - CMake:
Remember to add path to the environmental variables |
What is CMake?
6th step - Ninja or Make:
Uncheck "... support for the graphical user interface" |
7th step - edit environment variables:
The next
important step is to set up all environment variables and add all new tools to
the PATH. In this step, we will show our system where it can find some useful
tools so we can use them in many places and programs.
Firstly, open “Edit environment variables” and click “Environment variables”.
If you’ve created similar folders, you should have similar paths (some of these paths can be saved as system variables - you can leave them there or move everything to one place):
To simplify further usage, I will delete the path to Arm Toolchain (C:\tools\Arm GNU Toolchain\10 2021.10\bin) and create my own variable (ARMGCC_DIR):
Next, you can change the Path variable and instead of full path C:\tools\Arm GNU Toolchain\10 2021.10\bin use %ARMGCC_DIR% :
This is not necessary (you can always use a full path) but can be useful.
8th step - Checking everything:
And check if everything works (write openocd.exe):
Then in the bin folder in your ARM GNU Toolchain directory (C:\tools\Arm GNU Toolchain\10 2021.10\bin) turn on the cmd window:
It can happen that you’re missing Python 2.7. If so - download and install it (link). After that, everything should be working properly.
Also, you can check if everything is in the right place:
or just check the versions of installed programs:
9th step - Setup Visual Studio Code and configure everything:
Now, is the perfect time to open a project. You can create a new one if you can (how to do this in the next post). For now, we don’t want to configure everything from scratch, so it is better to clone my project from GitHub (if you don’t know how to do this check the next chapter).
If everything is installed it is time to choose the compiler. You can either click on the bottom bar or press Ctr+Shift+P (open Command Palette) and type: “CMake: Scan for Kits”:
10th step - Configure CMake and build your program:
11th step - flashing program into your device:
If error:
If not, you need to download either the STM32CubeProgrammer or the ST-LINK Utility: link. Download and install one of these programs:
After that, your computer should recognize the STLink device. Now, you should be able to flash your
program without any problem.
12th step - Debugging:
* Problem occurs
when your User Name contains non-ASCII letters. There is no matter where you
installed VScode itself .vscode file always will be created C:\Users\your_user_name\.vscode.
And inside it is a folder for all extensions C:\Users\your_user_name\.vscode\extensions. The Cortex Debug extension will crash when the path contains any non-ASCII letter. Moreover, there is no possibility to move the .vscode folder. At least the directory of
extensions can be changed:
Go into the environmental variables and add the new variable VSCODE_EXTENSIONS with the path to the folder where you want to save all
extensions:
How to use Git(Hub) with VScode?
First of all download and install the Git: link
I suggest creating a next folder: C:\tools\GitHub
Probably, if you use the installer as it is, Git will install on the default path C:\Program Files\Git. It is not a problem but if you want to specify a directory you need to open the cmd window in the folder where you’ve got git installer and type git_installer_name.exe /DIR=“your\path\to\GitHub”:
Now you can
open the command window anywhere and use git:
Next, you
create a folder for your project somewhere in your computer. Go there, turn on the cmd window, and type: git clone http://github.com/your/repository/link:
Usually,
this is enough but sometimes you need to authenticate that you have got access
to the repository.
At default, you clone the whole Repo with all branches but if you want to operate only on specified you can type: git clone --branch <branch-name> --single-branch <remote-repo-url>:
More about cloning: link.
In the next part of this post, I will explain how to create a new project for any STM32 MCU.
Comments
Post a Comment