Goal is to build Arduino STM32 projects, without the Arduino IDE being installed.
Setup a project by cloning this skeleton project. This sets up a fully configured VSCode project.
Edit the Blink.cpp in the sources folder to your needs. You can add any number of sources files.
Hit F5, the source should be compiled (first time, the Arduino core is also compiled), the compiled firmware is uploaded to the device.
The debugger starts, stopping at the main entry, ready for executing setup and loop.
There are tasks for build (make
on the terminal), flash (make flash
on the terminal) and clean (make clean
on the terminal).
The compiled firmware is in the bin folder, the objects in the obj folder.
During coding, intellisense assists:
Debugger at a breakpoint, showing global and local vars. Also a variable is watched:
For quick and dirty debugging semihosting can be used.
Windows 10
Visual Studio Code
Git for Windows
There are several tools needed for building projects. Most tools do not need to be installed with an installer, but can simply be unzipped.
When done, the toolchain tree should look like this:
D:\Dev-Tools
| - Arduino_Core_STM32
| - CMSIS_5
| - gcc-arm-8
| - MinGW-W64
| - openocd-0.10.0
Download the zip from Github -> Arduino_Core_STM32
Unpack it under the root of the toolchain, and rename the folder to Arduino_Core_STM32, removing the master at the end.
Download the zip from Github -> CMSIS_5
Unpack it under the root of the toolchain.
Download the zip from developer.arm.com
Unpack under the root of the toolchain, and rename the folder to gcc-arm-8.
Edit your path (windows r --> env) and add the bin dir of the toolchain.
Download the zip from sourceforge
Unpack, and move the folder under the dev tools.
Edit your path (windows r --> env) and add the bin dir of the MingGw-X64.
MingGW-X64 is used for make. For convenience, just copy mingw32-make.exe to make.exe (220kB).
Download prebuilt windows binaries from here
Unpack, and move the folder under the dev tools.
VSCode needs two extensions:
- C/C++ for Visual Studio Code
- Cortex Debug
For intellisense to work, c_cpp_properties.json
in the .vscode folder needs to be configured with the right settings. Use the same settings for family, proc and board as in the makefile.
settings.json
needs to be configured with the path to stutil.
Clone the skeleton from github:
git clone https://github.com/JBerg60/Arduino-VSCode.git
Rename the folder to something useful, F030-Blink seems sensible. Open this folder in VSCode.
The repo is configured for a STM32F103C8, a BluePill board on sale for a few dollar on Aliexpress.
Lets say we want to change to a STM32F030F4 board, that is even cheaper as the BluePill.
Edit the Makefile:
FAMILY = STM32F0xx
BOARD = DEMO_F030F4
PROC = STM32F030x6
ARM = cortex-m0
Edit the sketch, the LED on this board is on PA4.
Run the build task(or use make
in a bash terminal), build should be without any errors.
note: for intellisense to work, these settings also need to be configured in c_cpp_properties.json
.
Semihosting can send printf commands (and some others) directly over the ST-Link interface to the Output window of visual Studio Code.
There is no need to setup a UART and a serial monitor. This is a huge advantage. On the other hand, semihosting is not veri fast.
To use semihosting following needs to be done:
- add
LDFLAGS += --specs=lrdimon.specs -lrdimon
to the linker flags. - add
extern "C" void initialise_monitor_handles(void);
to the beginning of the sketch. - add a call to
initialise_monitor_handles();
to the setup function. Launch.json is already configured to use semihosting. Only thetarget
and thedevice
need to be set.
make -> build the system (shortcut ctrl-shift b)
make clean -> clean the system (shortcut ctrl-shift c)
make info -> get info on the connected device (shortcut ctrl-shift i).
make flash -> flash the firmware to the device (shortcut ctrl-shift f).
If necessary, the firmware is build before flashing.
F5 starts the debugger in Visual Studio Code
Ctrl-F5 stops the debugger.
- John Berg - Maintainer - NetbaseNext
See also the list of contributors who participated in this project.
MIT License.
- Stackoverflow