最近在Mac下使用Makefile进行STM32的开发,之前一直使用打串口Log的方式进行代码调试,每次进行调试都需要在特定位置放置Log代码。这种方式调试程序效率低,进行单步跟踪调试困难。下面是使用gdb进行STM32的代码调试,使用的是st-link工具。
使用gdb命令进行在线调试
步骤1 在gcc固件编译的时候添加-g参数
步骤2 在终端中开启st-link的st-util服务
$ st-util
st-util 1.3.0
2017-05-09T13:15:52 INFO /tmp/stlink-20170129-5444-qnsky1/stlink-1.3.0/src/common.c: Loading device parameters....
2017-05-09T13:15:52 INFO /tmp/stlink-20170129-5444-qnsky1/stlink-1.3.0/src/common.c: Device connected is: F1 Medium-density device, id 0x20036410
2017-05-09T13:15:52 INFO /tmp/stlink-20170129-5444-qnsky1/stlink-1.3.0/src/common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x20000 bytes (128 KiB) in pages of 1024 bytes
2017-05-09T13:15:52 INFO /tmp/stlink-20170129-5444-qnsky1/stlink-1.3.0/src/gdbserver/gdb-server.c: Chip ID is 00000410, Core ID is 1ba01477.
2017-05-09T13:15:52 INFO /tmp/stlink-20170129-5444-qnsky1/stlink-1.3.0/src/gdbserver/gdb-server.c: Listening at *:4242...
默认端口4242,可以通过-p参数设置端口。
步骤3 开启程序调试
$ arm-none-eabi-gdb main.elf
GNU gdb (GNU Tools for ARM Embedded Processors) 7.10.1.20160923-cvs
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-apple-darwin10 --target=arm-none-eabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
---Type <return> to continue, or q <return> to quit---
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from stm32f10x_makefile_template.elf...done.
(gdb)
启动gdb调试,main.elf为make后生成的文件。按照上面的命令按Enter进入调试。接下来就可以使用gdb命令进行调试了。gdb的调试请参考100个gdb小技巧
配置Clion的IDE使用gdb进行在线调试
步骤1 在gcc固件编译的时候添加-g参数
步骤2 在终端中开启st-link的st-util服务
$ st-util
st-util 1.3.0
2017-05-09T13:15:52 INFO /tmp/stlink-20170129-5444-qnsky1/stlink-1.3.0/src/common.c: Loading device parameters....
2017-05-09T13:15:52 INFO /tmp/stlink-20170129-5444-qnsky1/stlink-1.3.0/src/common.c: Device connected is: F1 Medium-density device, id 0x20036410
2017-05-09T13:15:52 INFO /tmp/stlink-20170129-5444-qnsky1/stlink-1.3.0/src/common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x20000 bytes (128 KiB) in pages of 1024 bytes
2017-05-09T13:15:52 INFO /tmp/stlink-20170129-5444-qnsky1/stlink-1.3.0/src/gdbserver/gdb-server.c: Chip ID is 00000410, Core ID is 1ba01477.
2017-05-09T13:15:52 INFO /tmp/stlink-20170129-5444-qnsky1/stlink-1.3.0/src/gdbserver/gdb-server.c: Listening at *:4242...
默认端口4242,可以通过-p参数设置端口。
步骤3 配置Clion的在线调试
3.1 添加配置
3.2 添加GDB Remote Debug
3.3 编辑配置
1 为交叉编译的gdb程序
2 为target remote要连接的端口,即为步骤2启动的服务端口
3 为make生成的.elf文件
4 点击OK确认
步骤4 开始在线调试
4.1 在main函数中添加断点
4.2 使用刚才创建的在线调试配置进行调试
4.3 调试界面
至此,大家可以进行IDE界面的调试了。
基于STM32F10x的Makefile模版可以参考
https://github.com/freelamb/stm32f10x_makefile_template