单片机
返回首页

VSCode+OpenOCD+STM32CubeMX开发与调试STM32单片机环境

2025-08-29 来源:cnblogs

需要的东西

需要的软件:

image.png

VSCode插件

名称备注
C/C++编译代码
Cortex-Debug配合OpenOCD调试与烧录

需要的硬件:

STM32开发板 STLINK(或其他usb转jtag工具)


新建工程需要添加的文件

VSCode配置文件(放在.vscode目录):

launch.json


{

'version': '0.2.0',

'configurations': [

{//本地调试

'name': 'Local Debug',

'cwd': '${workspaceRoot}',

'executable': './build/${workspaceRootFolderName}.elf',

'request': 'launch',

'type': 'cortex-debug',

'preLaunchTask': 'build',

'servertype': 'openocd',

//'device': 'STM32H750VB', //这个不是很重要,写不写应该无所谓

'configFiles': [

'openocd.cfg'

]

},

{ //另一种调试方案,需要用StartOCD单独打开OCD.调试的时候不会进入startup_xxx.s文件,支持通过网络调试

//有一个美中不足之处:需要在装载文件的地方手工指定全路径

'name': 'Remote Debug',

'type': 'cppdbg',

'request': 'launch',

'miDebuggerPath': 'arm-none-eabi-gdb.exe',

'targetArchitecture': 'arm',

'program': '${workspaceFolder}/build/${workspaceRootFolderName}.elf',

'preLaunchTask': 'build',

'setupCommands': [

{

'description': '装载文件',

'text': 'file 'E:/projects/Folder/${workspaceRootFolderName}/build/${workspaceRootFolderName}.elf'',

//'ignoreFailures': true   //忽略异常

},

{

'text': 'target remote localhost:3333'

},

{

'text': 'monitor reset'

},

{

'text': 'monitor halt'

},

{

'text': 'load'

}

],

'launchCompleteCommand': 'None',

'externalConsole': true,

'cwd': '${workspaceFolder}'

}

]

}

tasks.json


{

// See https://go.microsoft.com/fwlink/?LinkId=733558

// for the documentation about the tasks.json format

'version': '2.0.0',

'tasks': [

{

'label': 'build',

'type': 'shell',

'command': 'make -j8'

},

{

'label': 'startocd',

'type': 'shell',

'command': 'cmd StartOCD.bat'

}

]

}

Openocd配置文件(放在工程根目录)

openocd.cfg


# 需要根据jtag工具修改

source [find interface/stlink.cfg]

# 需要根据要调试的芯片修改

source [find target/stm32h7x.cfg]

# use hardware reset, connect under reset

# connect_assert_srst needed if low power mode application running (WFI...)

# reset_config srst_only srst_nogate connect_assert_srst

reset_config none

StartOCD.bat


clear

openocd -f openocd.cfg -c init -c 'reset halt' 

::一些配置以及烧录语句,根据情况使用:

::-c 'flash write_image erase E:/projects/Folder/STM32H7Template/build/STM32H7Template.bin 0x8000000'

::-c 'stm32h7x unlock 0'


进入单片机查看更多内容>>
相关视频
  • 【TI MSPM0 应用实战】智能小车+工业角度编码器+血氧仪+烟雾探测器!硬核参考设计详解!

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

  • 直播回放: Microchip Timberwolf™ 音频处理器在线研讨会

  • 基于灵动MM32W0系列MCU的指夹血氧仪控制及OTA升级应用方案分享

精选电路图
  • 1瓦线性调频增强器

  • 12V 转 28V DC-DC 变换器(基于 LM2585)

  • 红外遥控音量控制

  • LM317过压保护

  • 12V转110V/220V 500W逆变器

  • DS1669数字电位器

    相关电子头条文章