---
title: "STM32-Debug相关(Keil+Jlink/ST-Link)"
author: "lanxinyuchs"
date: "2013-10-11T10:44:15Z"
tags: []
categories: ["行业应用"]
slug: "stm32-debug-keil-jlink-st-link"
draft: false
lastmod: "2013-11-14T10:58:12Z"
id: 1954389
url: "https://lanxinyuchs.iteye.com/blog/1954389"
---

Serial Wire Debug(SWD) is a two pin alternative to JTAG and has about the same capabilities except Boundary Scan is not possible.

 

    ST-Link只支持SWD模式，而ST-Link/V2和Jlink同时支持SWD和JTAG模式.

 

参考STM32F100 reference manual第4版(以下简称RM)

 

RM第644页，关于芯片和SWD/JTAG的接口定义如下：

 ![](http://dl2.iteye.com/upload/attachment/0090/0282/173efb44-543e-346d-96ba-ee53f947a38c.png)

SWD模式下如果不接复位引脚，根据[http://bbs.21ic.com/icview-127364-1-1.html](http://bbs.21ic.com/icview-127364-1-1.html)，需要在MDK仿真器的设置里面不使用硬件复位，而是用system reset或者vect reset，其区别请参考[http://www.keil.com/support/man/docs/ulink2/ulink2\_ctx\_debug.htm](http://www.keil.com/support/man/docs/ulink2/ulink2_ctx_debug.htm)

 

SWD/JTAG在Jlink中的接口定义请参考[http://www.segger.com/interface-description.html](http://www.segger.com/interface-description.html)

 

使用SWD模式时，释放的管脚可以通过软件remap的方式成为GPIO使用

 

RM第645页提到：由于STM32的SWD/JTAG自带内部上拉/下拉，所以不需要再接外部电阻

     It is necessary to ensure that the JTAG input pins are not floating since they are directly 

connected to flip-flops to control the debug mode features.To avoid any uncontrolled IO levels, the device embeds internal pull-ups and pull-downs on the JTAG input pins.

但是RM第650页又提到

    For SWDIO bidirectional management, the line must be pulled-up on the board (100 KΩ

recommended by ARM).

    有点confuse，参考MCBSTM32E的原理图[http://www.keil.com/mcbstm32exl/mcbstm32e-base-board-schematics.pdf](http://www.keil.com/mcbstm32exl/mcbstm32e-base-board-schematics.pdf)，应该是只有SWDIO需要上拉。

      

      Serial Wire Viewer(SWV): A trace capability providing display of reads, writes, exceptions, PC Samples and printf

      Serial Wire Output(SWO): SWV frames usually come out this one pin output

      参考[http://www.opelc.org/html/28/t-5628.html](http://www.opelc.org/html/28/t-5628.html)

      Instrumentation Trace Macrocell(ITM)和Serial Wire Output(SWO)可以用来共同构成一个Serial Wire Viewer(SWV)。SWV提供了一种低成本的从MCU获得信息的方法。这个功能目前只在Cortex-M3上测试通过。它这意味着更快的传输速度，并且不会中断程序的执行。

     

     SWD模式支持Trace功能，而JTAG模式不支持。根据RM第661页，Trace的异步模式只需要TRACESWO引脚的支持，而同步模式需要的引脚较多。The asynchronous mode requires 1 extra pin and is available on all packages. It is only available if using Serial Wire mode (not in JTAG mode).

    注1：在keil里需要勾选Enable trace和添加初始化文件才能使用Trace功能，初始化文件主要是配置Trace的使用模式(同步/异步)，参考[http://www.keil.com/support/man/docs/jlink/jlink\_cortextrace.htm](http://www.keil.com/support/man/docs/jlink/jlink_cortextrace.htm)和针对STM32的[http://www.keil.com/support/man/docs/jlink/jLink\_STM32F10xxx\_SWT.htm](http://www.keil.com/support/man/docs/jlink/jLink_STM32F10xxx_SWT.htm)，ETM Trace需要在异步模式下才能Enable

    注2：ST-Link V2及以上的版本才支持SWV/Trace功能

    

   关于Keil MDK的工程配置，可参考位于\\Keil\\ARM\\Boards目录下的官方例程。关于Keil Debug功能的使用，可参考[http://www.keil.com/appnotes/files/apnt\_230.pdf](http://www.keil.com/appnotes/files/apnt_230.pdf)。根据AN230第10页，使用Logic analyzer功能时，貌似需要先配置SWV。使用逻辑分析仪可以观察的信号是有限制的[http://www.keil.com/support/man/docs/uv4/uv4\_db\_dbg\_logicanalyzer\_restrictions.htm](http://www.keil.com/support/man/docs/uv4/uv4_db_dbg_logicanalyzer_restrictions.htm)，选择范围外的信号会出现"unknown signal"的错误。

 

 参考[http://www.embedinfo.com/wen-list.asp?id=377](http://www.embedinfo.com/wen-list.asp?id=377)