单片机开发IDE环境如KeilMDK,虽然操作简单
, to facilitate debugging. But the code editing style is very old-fashioned, and garbled Chinese symbols are still common. And now the popular vscode editor is very good, free and quite lightweight, the code development experience is very good, and it looks comfortable. Clion IDE has a better experience, but it is not free and has a large size. Stm32cubeIDE is easy to generate project templates and configure, but the automatic completion is still too bad. In short, each has its own advantages and disadvantages.
foreword
Here is an introduction to Vscode to build an IDE for developing and debugging STM32, and share it with friends in need.
Like Zhihui Jun’s Clion-based project, they are all Cmake-based projects. In fact, it is not limited to whether the chip is STM32 or RISC-V. Use openocd online debugging).
Although there is also an EIDE plug-in under Vscode, I personally feel that it is a bit cumbersome, and Cmake is more general. It's not too cool to develop STM32 with Vscode. I hope everyone can develop embedded systems gracefully. The picture is quoted from "Configuring CLion for STM32 Development [Elegant Embedded Development]" by Zhihui Jun
【Note】
For the currently popular RISC-V MCU, as long as there is a cross-compilation toolchain, vscode can also be used for configuration and development. The debugger host computer officially supported by RISC-V is openocd. openocd is the most powerful (no one) open source debugging host computer on the surface, supporting various targets (ARM (M, A series), FPGA, RISC-V, etc.), and various debuggers (Jlink, CMSIS-DAP, FTDI, etc.), supports JTAG and SWD interfaces.
Environmental preparation
Software Environment
- STM32CubeMX (not necessary, the advantage is that it can help you quickly configure, if there is a project template, it can be ignored)
- VScode (install relevant plug-ins cmakeTools, cortexDebug)
- MinGW64 (gcc toolchain in windows environment)
- OpenOCD (or jlinkGdbServer also works)
- gcc-arm-none-eabi (arm's gcc cross-editing toolchain, which needs to be added to the environment variable after installation)
Links to resources used
Download executable files directly from ST official website
- MinGW 【https://sourceforge.net/projects/mingw-w64/files/】
- Cmake 【Download | CMake】
- STM32CubeMX【STM32CubeMX - STM32Cube initialization code generator - STMicroelectronics】
- arm-none-eabi-gcc 【https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads】
- OpenOCD 【https://gnutoolchains.com/arm-eabi/openocd/】
The above CMake, MinGW64, arm-none-eabi-gcc and OpenOCD are necessary software. The installation path is preferably free of Chinese characters and spaces. Configure the environment variables to ensure that these lines of commands can run normally under cmd.
The download speed of foreign websites may be slow, here is the address of the tool chain Baidu network disk I use
链接:https://pan.baidu.com/s/1NCQykQ57Xh6PFe28TU_GGw?pwd=goyj
提取码:goyj
--来自百度网盘超级会员V5的分享
After the environment variable is configured, after restarting to make the environment variable take effect, you can use the following statement to test in the command line:
gcc -v
arm-none-eabi-gcc -v
cmake --version
If there is information output, it is installed.
If you don't want to bother with the construction project and want to experience it as soon as possible, you can use my template directly.
The project source code download link is attached, which can be opened directly with vscode:
https://download.csdn.net/download/qq8864/8785579
Vscode plugin installation
Vscode needs to download and install the following plug-ins, only the red line is needed in the first screenshot:
cmake script content
The cmake organization and compilation rules are based on the CMakeLists.txt file. If you are familiar with CMake, you should find it very convenient and powerful. It’s okay if you’re not familiar with it. Basically, you don’t need to modify anything. You just need to know how to add the path of the source code directory and include folder in this file. This file is rarely changed.
#THIS FILE IS AUTO GENERATED FROM THE TEMPLATE! DO NOT CHANGE!
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.20)
# specify cross compilers and tools
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
# project settings
project(vscodeSTM32Demo C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
#Uncomment for hardware floating point
#add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING)
#add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)
#add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)
#Uncomment for software floating point
#add_compile_options(-mfloat-abi=soft)
add_compile_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork)
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)
# uncomment to mitigate c++17 absolute addresses warnings
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-register")
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
message(STATUS "Maximum optimization for speed")
add_compile_options(-Ofast)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
message(STATUS "Maximum optimization for speed, debug info included")
add_compile_options(-Ofast -g)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
message(STATUS "Maximum optimization for size")
add_compile_options(-Os)
else ()
message(STATUS "Minimal optimization, debug info included")
add_compile_options(-Og -g)
endif ()
add_definitions(-DUSE_HAL_DRIVER -DSTM32F103xB -DUSE_STDPERIPH_DRIVER -DSTM32F10X_HD)
include_directories(./STM32F10x_FWLib/inc ./include)
file(GLOB_RECURSE SOURCES "startup/*.*" STM32F10x_FWLib/src/*.c "./source/*.c")
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F103C8Tx_FLASH.ld)
add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
add_link_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork)
add_link_options(-T ${LINKER_SCRIPT})
add_link_options(-specs=nano.specs -specs=nosys.specs -u _printf_float)
add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
Engineering configuration
STM32CubeMX is actually not necessary, we only need to configure a CMake script and startup *.s file, which can be generated by STM32CubeMX, and the same board can also be copied and used directly. Among them, the STM32F103C8Tx_FLASH.ld file is a link script file, which is very important. Different types of films are different. It tells the compiler the relevant compiled executable code, memory variables, interrupt vectors, and which storage area (.text, . location and layout of rodata, .data, .bss, etc. sections in RAM and ROM).
how to use
The initial project engineering code can be generated using STM32Cubmx configuration, or copied from other places, or you can manually create the directory yourself. The startup_stm32f103xb.s assembly file and linker script file are necessary. The CMakeLists.txt file is also necessary. You can write it yourself or use a written template. There are ready-made template files in this article. With these, it doesn't matter whether you use the HAL library or the standard library, it doesn't matter what library you use.
On the premise that the above software environment and plug-ins are installed, you can directly open the project source folder (the folder where CMakeLists.txt is located) with Vscode. The project configuration of cmake will be automatically recognized, and related items will be displayed at the bottom. As shown in the red line in the figure below:
Select the configuration icon button on the bottom status bar to switch the toolchain, as shown in the following figure:
Compilation process log
[main] Configuring project: vscodeSTM32Demo
[proc] Executing command: "D:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DCMAKE_C_COMPILER:FILEPATH=C:\tools\arm-gcc\bin\arm-none-eabi-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\tools\arm-gcc\bin\arm-none-eabi-g++.exe -SD:/Users/Administrator/Desktop/test3/vscodeSTM32Demo -Bd:/Users/Administrator/Desktop/test3/vscodeSTM32Demo/build -G Ninja
[cmake] Not searching for unused variables given on the command line.
[cmake] -- Maximum optimization for speed, debug info included
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: D:/Users/Administrator/Desktop/test3/vscodeSTM32Demo/build
[main] Building folder: vscodeSTM32Demo
[build] Starting build
[proc] Executing command: "D:\Program Files\CMake\bin\cmake.EXE" --build d:/Users/Administrator/Desktop/test3/vscodeSTM32Demo/build --config RelWithDebInfo --target all --
[build] [14/29 3% :: 0.202] Building C object CMakeFiles/vscodeSTM32Demo.elf.dir/source/main.c.obj
[build] [15/29 6% :: 0.222] Building C object CMakeFiles/vscodeSTM32Demo.elf.dir/STM32F10x_FWLib/src/core_cm3.c.obj
[build] [16/29 10% :: 0.265] Building C object CMakeFiles/vscodeSTM32Demo.elf.dir/source/printf_uart.c.obj
[build] [17/29 13% :: 0.297] Building C object CMakeFiles/vscodeSTM32Demo.elf.dir/STM32F10x_FWLib/src/stm32f10x_bkp.c.obj
[build] [18/29 17% :: 0.310] Building C object CMakeFiles/vscodeSTM32Demo.elf.dir/STM32F10x_FWLib/src/misc.c.obj
[build] [19/29 20% :: 0.325] Building C object CMakeFiles/vscodeSTM32Demo.elf.dir/STM32F10x_FWLib/src/stm32f10x_cec.c.obj
[build] [20/29 24% :: 0.339] Building C object CMakeFiles/vscodeSTM32Demo.elf.dir/STM32F10x_FWLib/src/stm32f10x_dbgmcu.c.obj
[build] [21/29 27% :: 0.355] Building C object CMakeFiles/vscodeSTM32Demo.elf.dir/STM32F10x_FWLib/src/stm32f10x_dac.c.obj
Compilation screenshot:
Burning program & online debugging
Before burning, you must add the emulator configuration file. The following is the burning file of DAP-link . You can modify it according to the parameters of the downloader on hand, and save it in the format of xxxlink.cfg , and put it in the project directory config in the folder of
# choose st-link/j-link/dap-link etc.
source [find interface/cmsis-dap.cfg]
transport select swd
# 0x10000 = 64K Flash Size
# 0x80000 = 512K Flash Size
set FLASH_SIZE 0x80000
source [find target/stm32f1x.cfg]
# download speed = 10MHz
adapter speed 10000
reset_config srst_only
#reset_config none
Create a new folder under the root directory of the project config
, and create a new configuration file in it daplink.cfg
(because I use DapLink as the emulator here), the content of the file is as follows:
# choose st-link/j-link/dap-link etc.
adapter driver cmsis-dap
transport select swd
# 0x10000 = 64K Flash Size
set FLASH_SIZE 0x20000
source [find target/stm32f1x.cfg]
# download speed = 10MHz
adapter speed 10000
If using ST-Link:
# choose st-link/j-link/dap-link etc.
#adapter driver cmsis-dap
#transport select swd
source [find interface/stlink.cfg]
transport select hla_swd
source [find target/stm32f1x.cfg]
# download speed = 10MHz
adapter speed 10000
The first two lines set the type and interface of the emulator, and the following lines specify the Flash size , chip type , download speed , etc.
If you don’t know how to set up your chip, you can refer to a series of configuration files that come with OpenOCD, the path is under the OpenOCD installation share\openocd\scripts
directory
reset_config srst_only
Do not add this sentence in the configuration file , it will cause the download to fail. This sentence is to instruct the system to restart, and deleting it will not affect the download.
ISP serial programming program
If there is no need for in-circuit emulation debugging, this is the end here. The generated hex file can directly use the serial port tool to download the program. FlyMcu tool software is a tool for STM32 chip ISP serial port programming, free of charge, and relatively easy to use, easy to use and convenient. There is a prerequisite for one-key downloading using the serial port, that is, your circuit board needs to have an RS serial port switching circuit, among which DTR and RTS are necessary. The purpose is to prepare for one-key downloading, and set the BOOT0 level to high level. In order to guide the STM chip to the serial port programming program, the principle is to control the change of the boot pin through DTR and RTS to make the chip enter the boot upgrade mode.
Tool download address: MCU online programming network: www.mcuisp.com MCU online programming network
Emulator Downloader
Enter the command openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg -c "program build/VSCodeF4.hex verify reset exit" in the terminal to download the program.
To implement debugging like Keil in VSCode, you need to configure the debugging file and click the debugging button on the left. Choose to create a launch.json file. Select C++(GDB/LLDB), and then select the default configuration.
Select Cortex Debug: OpenOcd
Edit launch.json in the .vscode folder under the project root directory and add the following content (configFiles and svdFile configuration), the executable path needs to be modified to the actual path to generate the elf file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"cwd": "${workspaceRoot}",
"executable": "./bin/executable.elf",
"name": "Debug with OpenOCD",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"configFiles": [
"stlink-v2.cfg",
"stm32f4x.cfg"
],
"searchDir": [],
"runToEntryPoint": "main",
"showDevDebugOutput": "none",
"svdFile": "./STM32F40x.svd"
}
]
}
About OpenOCD
As can be seen from the above IDE steps, OpenOCD is a tool that connects gdb and hardware debugger, and is available in linux, macos, and windows. OpenOCD supports a large number of hardware debuggers, such as stlink and jlink debuggers commonly used by stm32. OpenOCD is an open source debugging software that runs on a PC. It was originally initiated by Dominic Rath when he was still in college (2005). OpenOCD is designed to provide debugging, system programming and boundary scan capabilities for embedded devices.
The SVD file is a single-chip register file. Only by adding this file can you see the value of the register during debugging. The change of the register value can be seen by breaking the point in the program to see the change of the register value. The svd file of the stm32 microcontroller can be found under the chip package installation path of keil. (It can only be found after installing keil and chip package)
Run the debug interface:
This is the end, welcome to communicate if you have any questions! Finally, you can happily use vscode to develop, to help you develop happily!
other resources
Embedded Development: Configuring CLion for STM32 Development_clion stm32_Hua Zi's Blog-CSDN Blog
Configure CLion for STM32 development [standard library] - Programmer Sought
Configure VS Code to develop STM32 [universe & strongest editor] - Electronic Engineering World
STM32CubeMX - STM32Cube initialization code generator - STMicroelectronics
MinGW-w64 - for 32 and 64 bit Windows - Browse Files at SourceForge.net
Configure CLion for STM32 development [elegant の embedded development] bzdww
RISC-V MCU ld link script description_at > flash_Borrowing Landscape Blog-CSDN Blog
RISCV SOC development environment 4 - code debugging (openocd + gdb) - Programmer Sought
RISC-V debugging in simple terms_51CTO blog_dpdk in simple terms