5.7 VSCode + STM32 two
Compile the project using makefile.
1. Softwarea and Tools
1.1 STM32 development kits
STM32CubeMX download link:https://www.st.com.cn/zh/development-tools/stm32cubemx.html
STM32CubeCLT download link:https://www.st.com.cn/zh/development-tools/stm32cubeclt.html
When using STM32CubeMX, if you encounter issues such as inability to connect to the network, missing fields in .ld files causing compilation errors, please choose to install an older version.This is because the STM32 development kit depends on the jar environment, which needs to match the current system version on your computer.
1.2 VS Code and Extension Plugins
VS Code download link:https://code.visualstudio.com/
Extension Plugins:
C/C++、C/C++ Extension Pack、C/C++ Themes、CMake、CMake Tools、LLVM、STM32 VS Code Extension
1.3 Openocd-esp
Openocd-esp download link:https://github.com/espressif/openocd-esp32/releases
Configuring the System Environment Variable PATH:
C:\Openocd-esp\openocd-esp32-win64-0.12.0-esp32-20240821\openocd-esp32\bin
C:\Openocd-esp\openocd-esp32-win64-0.12.0-esp32-20240821\openocd-esp32\share\openocd\scripts
1.4 gcc-arm-none-eabi
gcc-arm-none-eabi download link:https://developer.arm.com/downloads/-/gnu-rm
Configuring the System Environment Variable PATH:
C:\GNU Arm Embedded Toolchain\10 2021.10\bin
1.5 Download cygwin64
cygwin64 download link:Cygwin Installation
Download software packages : gcc-core、gcc-g++、libgccpp1、gdb、make、cmake
Configuring the System Environment Variable PATH:
C:\cygwin64\bin
C:\cygwin64\sbin
2. Setting Up the Compilation Environment
2.1 Configure Project
【Pinout&Configuration】-> 【SYS】->【Debug】->【 JTAG (5 pins) 】;
【Project Manager】 ->【Project】->【Toolchain/IDE】->Cmake;
2.2【GENERATE CODE】
2.3 Import Project into VSCODE
1.Open Project by VSCODE
2.【Run and Debug】 -> 【 create a launch.json file】-> choose 【Cortex Debug】;
3.Modify "launch.json" and save;
{
"version": "0.2.0",
"configurations": [
{
"name": "Build & launch Microcontroller - PowerDebugger",
"type": "cortex-debug",
"request": "launch",
"servertype": "openocd",
"configFiles": [
"interface/cmsis-dap.cfg",
"target/stm32f1x.cfg", // modify - the setting file of target MCU: xxx.cfg
],
"preLaunchTask": "Build",
"executable": "${workspaceFolder}/build/STM32F103ZET.elf", // modify - generate target file xxx.elf
"runToEntryPoint": "main",
"svdFile": "C:/ST/STM32CubeCLT_1.16.0/STMicroelectronics_CMSIS_SVD/STM32F103.svd", // modify - Description file for target registers: xxx.svd
"cwd": "${workspaceFolder}",
},
{
"name": "Attach to Microcontroller - PowerDebugger",
"type": "cortex-debug",
"request": "attach",
"servertype": "openocd",
"configFiles": [
"interface/cmsis-dap.cfg",
"target/stm32f1x.cfg", // modify - the setting file of target MCU:: xxx.cfg
],
"preLaunchTask": "Build",
"executable": "${workspaceFolder}/build/STM32F103ZET.elf", // modify - generate target file: xxx.elf
"runToEntryPoint": "main",
"svdFile": "C:/ST/STM32CubeCLT_1.16.0/STMicroelectronics_CMSIS_SVD/STM32F103.svd", // modify - Description file for target registers:xxx.svd
"cwd": "${workspaceFolder}",
},
{
"name": "Remote debug (gdb)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/STM32F103ZET.elf", // modify - generate target file: xxx.elf
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/GNU Arm Embedded Toolchain/10 2021.10/bin/arm-none-eabi-gdb.exe", // modify - the path of GDB xxx.exe
"targetArchitecture": "arm",
"preLaunchTask": "Build",
"setupCommands": [
{
"text": "-enable-pretty-printing",
"description": "Enable pretty printing",
"ignoreFailures": true
},
{
"text": "file F:/Users/Desktop/STM32F103ZET/build/STM32F103ZET.elf", // modify - generate target file: xxx.elf
"description": "Select the debug file (.elf) to gdb",
"ignoreFailures": false
},
{
"text": "target remote localhost:3333", // modify - Openocd server IP address and port
"description": "Connect GDB Server",
"ignoreFailures": false
},
{
"text": "monitor reset",
"description": "Reset MCU",
"ignoreFailures": false
},
{
"text": "monitor halt",
"description": "Halt",
"ignoreFailures": false
},
{
"text": "load",
"description": "Download file to MCU",
"ignoreFailures": false
},
],
}
]
}
2.4 Creat tasks.json
【Terminal】 -> 【Configure Tasks..】 ->【Creat tasks.json from a template】-> 【Other】;
2.5 Modify tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Clean",
"type": "shell",
"command": "make clean",
"group": {
"kind": "build",
"isDefault": false
}
},
{
"label": "Build",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}"
},
"command": "make",
"args": [
"-j4"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"label": "Clean & Rebuild",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}"
},
"command": "make",
"args": [
"-j4"
],
"group": {
"kind": "build",
"isDefault": false
},
"dependsOn": "Clean"
},
{
"label": "Erase",
"type": "shell",
"command": "openocd",
"args": [
"-f",
"interface/cmsis-dap.cfg",
"-f",
"target/stm32f1x.cfg", // modify - the setting file of target MCU xxx.cfg
"-c",
"init",
"-c",
"reset halt",
"-c",
"flash erase_address 0x08000000 0x00010000",
"-c",
"reset",
"-c",
"exit"
],
"group": {
"kind": "build",
"isDefault": false
}
},
{
"label": "Erase all",
"type": "shell",
"command": "openocd",
"args": [
"-f",
"interface/cmsis-dap.cfg",
"-f",
"target/stm32f1x.cfg", // modify - the setting file of target MCU: xxx.cfg
"-c",
"init",
"-c",
"reset halt",
"-c",
"flash erase_sector 0 0 last",
"-c",
"reset",
"-c",
"exit"
],
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": false
}
},
{
"label": "Download",
"type": "shell",
"command": "openocd",
"args": [
"-f",
"interface/cmsis-dap.cfg",
"-f",
"target/stm32f1x.cfg", // modify - the setting file of target MCU: xxx.cfg
"-c",
"program build/STM32F103ZET_TEST.elf verify reset", // modify - generate target file: xxx.elf
"-c",
"reset run",
"-c",
"exit"
],
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": false
}
},
{
"label": "Build & Download",
"type": "shell",
"command": "openocd",
"args": [
"-f",
"interface/cmsis-dap.cfg",
"-f",
"target/stm32f1x.cfg", // modify -the setting file of target MCU: xxx.cfg
"-c",
"program build/STM32F103ZET_TEST.elf verify reset", // modify - generate target file: xxx.elf
"-c",
"reset run",
"-c",
"exit"
],
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": false
},
"dependsOn": "Build"
},
{
"label": "Start openocd server",
"type": "shell",
"command": "openocd",
"args": [
"-f",
"interface/cmsis-dap.cfg",
"-f",
"target/stm32f1x.cfg", // modify -the setting file of target MCU: xxx.cfg
"-c",
"gdb_report_data_abort enable",
"-c",
"gdb_port 3333",
"-c",
"tcl_port 6666",
"-c",
"telnet_port 4444"
],
"group": {
"kind": "build",
"isDefault": false
}
},
]
}
3. Build、Debug code
3.1 Terminal -> Run task
Build;
Download;
Erase all;
Start openocd server
3.2 Run and Debug
bend debug:Attach to Microcontroller - PowerDebugger;
Remote debug:Remote debug (gdb)
4. IAP Function
IAP function has two code.
Boot code : It includes the functionality to develop the upgrade program and jump to the app address (e.g., 0x8006000).
App code : You should modify three places.
4.1 csystem_stm32f1xx.c
Path:Core\Src\system_stm32f1xx.csystem_stm32f1xx.c
Open :USER_VECT_TAB_ADDRESS,
Modify:VECT_TAB_OFFSET为0x00006000U
4.2 STM32F103C8Tx_FLASH.ld
Modify:
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 64K
to
FLASH (rx) : ORIGIN = 0x8006000, LENGTH = 40K
4.3 Create a download.cfg file
Add:
set DOWNLOAD_ADDRESS 0x08006000
4.4 "tasks.json Download " add
"-f",
"download.cfg"