基于 Ubuntu 18.04 Jlink + Vim + Cmake + Makefile 调试 STM32

Ubuntu 18.04 Jlink + Vim + Cmake + Makefile 调试 STM32

  • 一、安装 Jlink
  • 二、设置 Jlink 信息
  • 三、烧录一个简单的 Demo 进行测试
  • 四、使用 Cmake 编译一个最小工程
  • 五、编译一个 STM32 最小工程

一、安装 Jlink

在 SEGGER 官网下载 Jlink Linux版本的驱动,安装即可,不要更新固件框架,当心设备变成板砖。

xiechen@xiechen-Ubuntu:~$ ls /opt/SEGGER/JLink
Devices              JLinkGDBServerExe       JLinkSTM32                 libQtCore.so
Doc                  JLinkGUIServerExe       JLinkSTM32Exe              libQtCore.so.4
ETC                  JLinkLicenseManager     JLinkSWOViewer             libQtCore.so.4.8
GDBServer            JLinkLicenseManagerExe  JLinkSWOViewerCLExe        libQtCore.so.4.8.7
JFlashExe            JLinkRegistration       JLinkSWOViewerExe          libQtGui.so
JFlashLiteExe        JLinkRegistrationExe    JMemExe                    libQtGui.so.4
JFlashSPI_CL         JLinkRemoteServer       JRunExe                    libQtGui.so.4.8
JFlashSPICLExe       JLinkRemoteServerCLExe  JTAGLoadExe                libQtGui.so.4.8.7
JFlashSPIExe         JLinkRemoteServerExe    libjlinkarm.so             Samples
JLinkConfigExe       JLinkRTTClient          libjlinkarm.so.6           ThirdParty
JLinkDevices.xml     JLinkRTTClientExe       libjlinkarm.so.6.80.2      x86
JLinkExe             JLinkRTTLogger          libjlinkarm_x86.so
JLinkGDBServer       JLinkRTTLoggerExe       libjlinkarm_x86.so.6
JLinkGDBServerCLExe  JLinkRTTViewerExe       libjlinkarm_x86.so.6.80.2
xiechen@xiechen-Ubuntu:~$ JLinkExe
SEGGER J-Link Commander V6.80b (Compiled Jun  5 2020 17:44:14)
DLL version V6.80b, compiled Jun  5 2020 17:44:03
Connecting to J-Link via USB...O.K.
Firmware: J-Link ARM V8 compiled Jun 19 2012 11:29:30
Hardware version: V8.00
S/N: 805306163
License(s): RDI,FlashDL,FlashBP,JFlash,GDBFull
VTref=0.000V
Type "connect" to establish a target connection, '?' for help
J-Link>

二、设置 Jlink 信息

xiechen@xiechen-Ubuntu:~$ JLinkExe
SEGGER J-Link Commander V6.80b (Compiled Jun  5 2020 17:44:14)
DLL version V6.80b, compiled Jun  5 2020 17:44:03
Connecting to J-Link via USB...O.K.
Firmware: J-Link V9 compiled Oct 25 2018 11:46:07
Hardware version: V9.20
S/N: -1
License(s): RDI, GDB, FlashDL, FlashBP, JFlash, RDDI
VTref=3.275V
Type "connect" to establish a target connection, '?' for help
J-Link>connect
Please specify device / core. <Default>: STM32F407ZG
Type '?' for selection dialog
Device>/
Please specify target interface:
  J) JTAG (Default)
  S) SWD
  F) FINE
  I) ICSP
  C) C2
  T) cJTAG
TIF>J
Device position in JTAG chain (IRPre,DRPre) <Default>: -1,-1 => Auto-detect
JTAGConf>-1,-1
Specify target interface speed [kHz]. <Default>: 4000 kHz
Speed>4000

输入 connect 开始连接,输入 / 配置 Jlink 信息,输入 J 选择 JTAG 连接方式,然后输入 -1, -1 使用 JTAG 默认设置,通信速率设为默认 4000 khz。

基于 Ubuntu 18.04 Jlink + Vim + Cmake + Makefile 调试 STM32_第1张图片

接着直接设置单片机型号即可。

J-Link>connect
Please specify device / core. <Default>: STM32F407ZG
Type '?' for selection dialog
Device>STM32F407ZG
Device "STM32F407ZG" selected.
Connecting to target via JTAG
TotalIRLen = 9, IRPrint = 0x0011
JTAG chain detection found 2 devices:
 #0 Id: 0x4BA00477, IRLen: 04, CoreSight JTAG-DP
 #1 Id: 0x06413041, IRLen: 05, STM32 Boundary Scan
TotalIRLen = 9, IRPrint = 0x0011
JTAG chain detection found 2 devices:
 #0 Id: 0x4BA00477, IRLen: 04, CoreSight JTAG-DP
 #1 Id: 0x06413041, IRLen: 05, STM32 Boundary Scan
Unknown DP version. Assuming DPv0
Scanning AP map to find all available APs
AP[1]: Stopped AP scan as end of AP map has been reached
AP[0]: AHB-AP (IDR: 0x24770011)
Iterating through AP map to find AHB-AP to use
AP[0]: Core found
AP[0]: AHB-AP ROM base: 0xE00FF000
CPUID register: 0x410FC241. Implementer code: 0x41 (ARM)
Found Cortex-M4 r0p1, Little endian.
FPUnit: 6 code (BP) slots and 2 literal slots
CoreSight components:
ROMTbl[0] @ E00FF000
ROMTbl[0][0]: E000E000, CID: B105E00D, PID: 000BB00C SCS-M7
ROMTbl[0][1]: E0001000, CID: B105E00D, PID: 003BB002 DWT
ROMTbl[0][2]: E0002000, CID: B105E00D, PID: 002BB003 FPB
ROMTbl[0][3]: E0000000, CID: B105E00D, PID: 003BB001 ITM
ROMTbl[0][4]: E0040000, CID: B105900D, PID: 000BB9A1 TPIU
ROMTbl[0][5]: E0041000, CID: B105900D, PID: 000BB925 ETM
Cortex-M4 identified.
J-Link>

三、烧录一个简单的 Demo 进行测试

找一个 LED 闪烁的例程,将里面的 hex 文件复制到系统中,烧录至 ARM 中验证可行性。

J-Link>loadbin Template.hex 0x8000000
Halting CPU for downloading file.
Downloading file [Template.hex]...
Comparing flash   [100%] Done.
Erasing flash     [100%] Done.
Programming flash [100%] Done.
J-Link: Flash download: Bank 0 @ 0x08000000: 1 range affected (16384 bytes)
J-Link: Flash download: Total: 0.447s (Prepare: 0.014s, Compare: 0.001s, Erase: 0.416s, Program & Verify: 0.010s, Restore: 0.003s)
J-Link: Flash download: Program & Verify speed: 1600 KB/s
O.K.

显示烧写成功。

输入 g 来运行程序,发现 LED 正常闪烁,代表程序正常运行了。

输入 h 来挂起程序,发现 LED 停止闪烁,代表程序停止了。

J-Link>g
J-Link>h
PC = 0800052C, CycleCnt = 40000000
R0 = 0050B7A6, R1 = 0050B7A7, R2 = 00000600, R3 = 01000301
R4 = 00000000, R5 = 20000000, R6 = 00000000, R7 = 00000000
R8 = 00000000, R9 = 00000000, R10= 08000574, R11= 00000000
R12= 20000040
SP(R13)= 20000660, MSP= 20000660, PSP= 00000000, R14(LR) = 0800054D
XPSR = 21000000: APSR = nzCvq, EPSR = 01000000, IPSR = 000 (NoException)
CFBP = 04000000, CONTROL = 04, FAULTMASK = 00, BASEPRI = 00, PRIMASK = 00
FPS0 = 00000000, FPS1 = 00000000, FPS2 = 00000000, FPS3 = 00000000
FPS4 = 00000000, FPS5 = 00000000, FPS6 = 00000000, FPS7 = 00000000
FPS8 = 00000000, FPS9 = 00000000, FPS10= 00000000, FPS11= 00000000
FPS12= 00000000, FPS13= 00000000, FPS14= 00000000, FPS15= 00000000
FPS16= 00000000, FPS17= 00000000, FPS18= 00000000, FPS19= 00000000
FPS20= 00000000, FPS21= 00000000, FPS22= 00000000, FPS23= 00000000
FPS24= 00000000, FPS25= 00000000, FPS26= 00000000, FPS27= 00000000
FPS28= 00000000, FPS29= 00000000, FPS30= 00000000, FPS31= 00000000
FPSCR= 03000000
J-Link>

到这里,可以确认 Jlink 在 Ubuntu 上面用起来了。

四、使用 Cmake 编译一个最小工程

文件树如下。

xiechen@xiechen-Ubuntu:~/document/stm32F407$ tree
.
├── autoRun
├── bin
│   └── jerome
├── build
├── CMakeLists.txt
├── include
├── lib
└── src
    └── main.c
5 directories, 4 files
Cmake 内容如下:
CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
PROJECT(STM32F407)
SET(CMAKE_BUILD_TYPE RELEASE)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
INCLUDE_DIRECTORIES(
    ${PROJECT_SOURCE_DIR}/include
)
ADD_EXECUTABLE(
    jerome
    src/main.c
)

主函数内容如下:

#include 
int main()
{
    printf("Hello world\n");
    return 0;
}

Shell 脚本内容如下:

cd build
rm -rvf *
cmake ..
make
cd ../bin
./jerome

运行脚本编译之后,输出预期的内容。

xiechen@xiechen-Ubuntu:~/document/stm32F407$ ./autoRun
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xiechen/document/stm32F407/build
Scanning dependencies of target jerome
[ 50%] Building C object CMakeFiles/jerome.dir/src/main.c.o
[100%] Linking C executable ../bin/jerome
[100%] Built target jerome
Hello world

五、编译一个 STM32 最小工程

编译程序到 ARM 上运行,需要使用交叉工具链。文件解压 之后,将它的库加到环境变量中,更新一下即可。

echo "export PATH=/home/xiechen/software/gcc-arm-none-eabi-8-2018-q4-major/bin:$PATH" >> ~/.bashrc
source ~/.bashrc
`

你可能感兴趣的:(M4,嵌入式)