VS Code IDE for mbed Projects
Introduction
If you are working with STMicroelectronics’ F0 and L0 family of microcontrollers, you are propably using Keil uVision as your IDE. However, if you try to use fancy stuff like mbed OS, and uTensor (this was actually the reason behind this article) on a relativley higher end microcontrollers, you are most likely going to get hit with the IDE dilemma. There are no modern, kinda proffesional, easy to install and use IDEs that are free! I mean, not really, otherwise, I would not have written this article 😁
In this post, I will detail a step-by-step guide to set up a functional VS Code IDE to work with projects exported from mbed-cli tool, on a Windows 10 environment. In this tutorial I am using STM32L4 Discovery kit IoT node (in mbed, this is known as DISCO_L475VG_IOT01A).
Steps
- Install Visual Studio Code (link).
- Download and install GNU Tools ARM Embedded (link).
- This will be installed in
C:\Program Files (x86)\GNU Tools Arm Embedded
. - Add the
\bin
folder to yourPATH
environment variable (e.g.,C:\Program Files (x86)\GNU Tools ARM Embedded\6 2017-q2-update\bin
). - Download GNU Make Windows Build Tools (link).
- Extract the GNU Make files to a path (e.g.,
C:\Program Files\
). - Add the
\bin
folder to thePATH
environment variable (e.g.,C:\Program Files\GNU MCU Eclipse\Build Tools\2.10-20180103-1919\bin
). - Download and install Python 2.7 (link).
- Make sure you also install
pip
with the Python package. - From command line, install mbed CLI:
pip install mbed-cli
- Now you can export mbed projects to VS Code or any other IDE. To export to VS Code, from the project parent folder run:
mbed export --ide vscode_gcc_arm --target DISCO_L475VG_IOT01A
- Change target to match yours. Run the following command to get a list of supported targets and toolchains:
mbed target -S
- For a complete matrix of supported targets and IDEs, run:
mbed export -S
- Export Makefile from mbed CLI:
mbed export --ide make_gcc_arm --target DISCO_L475VG_IOT01A
- This will create a Makefile required by GNU Make to compile your project.
- Open the project folder with VS Code after exporting from mbed CLI.
- Open folder
.vscode
and edit filelaunch.json
. Change fieldMIDebuggerPath
underwindows
to be:"MIDebuggerPath": "arm-none-eabi-gdb.exe",
- Clear the field
serverStarted
, as this looks for a specific pattern in the output to start the GDB server. It’s causing problems in launching the GDB."serverStarted": "",
- If you get
Make error 87
, this is because the makefile is passing a long command more than Windows API can process. Change the linking command in the makefile to be:$(PROJECT).elf: $(OBJECTS) $(SYS_OBJECTS) $(PROJECT).link_script.ld $(file > $@.in, $(filter %.o, $^)) +@echo "link: $(notdir $@)" $(LD) $(LD_FLAGS) -T $(filter-out %.o, $^) $(LIBRARY_PATHS) --output $@ @$@.in $(LIBRARIES) $(LD_SYS_LIBS)
Instead of:
$(PROJECT).elf: $(OBJECTS) $(SYS_OBJECTS) $(PROJECT).link_script.ld +@echo "link: $(notdir $@)" @$(LD) $(LD_FLAGS) -T $(filter-out %.o, $^) $(LIBRARY_PATHS) --output $@ $(filter %.o, $^) $(LIBRARIES) $(LD_SYS_LIBS)
What this does is that it breaks the command into two sections. (Note: there is a tab at the beginning of the second, third, and fourth lines).
- If your source is C++ and you get errors about C++11 standard, make sure that
-std=c++11
is defined inCPP
andCXX_FLAGS
objects in the makefile. For example:CPP = 'arm-none-eabi-g++' '-std=c++11' '-fno-rtti' '-Wvla' '-c' '-Wall' '-Wextra' '-Wno-unused-parameter' '-Wno-missing-field-initializers' '-fmessage-length=0' '-fno-exceptions' '-fno-builtin' '-ffunction-sections' '-fdata-sections' '-funsigned-char' '-MMD' '-fno-delete-null-pointer-checks' '-fomit-frame-pointer' '-O0' '-g3' '-DMBED_DEBUG' '-DMBED_TRAP_ERRORS_ENABLED=1' '-mcpu=cortex-m4' '-mthumb' '-mfpu=fpv4-sp-d16' '-mfloat-abi=softfp' ... CXX_FLAGS += -std=c++11 CXX_FLAGS += -fno-rtti CXX_FLAGS += -Wvla CXX_FLAGS += -DDEVICE_I2C=1 ...
- So far, we haven’t setup the debugger yet. If you are using ST-LINK, you probably want to use OpenOCD. Other options available are SEGGER’s J-Link but this requires upgrading the ST-LINK to a J-Link firmware.
- Download and extract OpenOCD binaries for Windows somewhere (link). e.g.,
C:\openocd-0.10.0\
. - Add the
\bin
folder to yourPATH
environment variable (e.g.,C:\openocd-0.10.0\bin
). - In
launch.json
, edit the fielddebugServerPath
to be:"debugServerPath": "openocd.exe",
Note: if you don’t add the
\bin
folder to yourPATH
, you should provide the full path to youropenocd.exe
. Make sure you can callopenocd.exe
directly from any command line prompt. - Edit the filed
debugServerArgs
to add parameters to be passed to youropenocd.exe
call. This Tells OpenOCD what board/MCU you are trying to connect to."debugServerArgs": "-f C:\\openocd-0.10.0\\scripts\\board\\stm32l4discovery.cfg -f C:\\openocd-0.10.0\\scripts\\interface\\stlink-v2-1.cfg -c init -c \"reset init\"",
Note that you have to provide the full path to your configuration files. You can check supported boards by browsing the
\board
folders. - In the Debug tab, hit the play button to start debugging.