硬汉嵌入式论坛

 找回密码
 立即注册
查看: 24803|回复: 13
收起左侧

[客户分享] Keil调试技术 (用 SWO 功能替代 printf)

  [复制链接]

747

主题

1049

回帖

3295

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3295
发表于 2012-12-21 09:03:18 | 显示全部楼层 |阅读模式
Keil调试技术





嵌入式软件开发中的一个基本需求就是能通过终端来输出调试信息,一般可通过2种方式实现:一种是使用串口线连接板上的UARTPC上的COM口,通过PC上的超级终端来查看调试信息;另一种则是采用半主机机制,但有可能不被所用的工具链支持。基于Cortex-M3核的软件调试突破了这样的限制,Cortex-M3内核提供了一个ITM(Instrumentation TraceMacrocell)接口,通过SWV(Serial Wire Viewer)可调试由SWO引脚接收到的ITM数据。ITM实现了32个通用的数据通道,基于这样的实现,CMSIS规定用通道0作为终端来输出调试信息,通道31用于操作系统的输出调试(特权模式访问)。在core_cm3.h中定义了ITM_SendChar()函数,因此可通过调用该函数来重写fputc,以在应用程序中通过printf打印调试信息,并可通过ITM Viewer查看这些调试信息。有了这样的实现,嵌入式软件开发者就可以在不配置串口和使用终端调试软件的情况下输出调试信息,在一定程度上减少了工作量

基本概念:

SWD
The J-Link and J-Trace support ARMs Serial WireDebug (SWD). SWD replaces the 5-pin JTAG port with a clock (SWDCLK)and a single bi-directional data pin (SWDIO), providing all thenormal JTAG debug and testfunctionality.
Pin 13 of SWD:
SWO – Serial Wire Output trace port. (Optional,not required for SWD communication.)

SWO
J-Link can be used with devices that supportSerial Wire Output (SWO). Serial Wire Output (SWO) support meanssupport for a single pin output signal from the core. It iscurrently tested with Cortex-M3 only.

SWV
The Instrumentation Trace Macrocell (ITM) andSerial Wire Output (SWO) can be used to form a Serial Wire Viewer(SWV). The Serial Wire Viewer provides a low cost method ofobtaining information from inside the MCU. The SWO can output tracedata in two output formats, but only one output mechanism is validat any one time. The 2 defined encodings are UART and Manchester.The current J-Link implementation only supports UART encoding.Serial Wire Viewer uses the SWO pin to transmit different packetsfor different types of information. The three sources in theCortex-M3 core which can output information via this pinare:
- Instrumentation Trace Macrocell (ITM) forapplication-driven trace source that supports printf-styledebugging. It supports 32 different channels, which allow it to beused for other purposes such as real-time kernel information aswell.
- Data Watchpoint and Trace (DWT) for real-timevariable monitoring and PC-sampling, which can in turn be used toperiodically output the PC or various CPU-internal counters, whichcan be used to obtain profiling information from thetarget.
- Timestamping. Timestamps are emitted relative topackets.

LPC177x/178x debug support
- A JTAG debug interface is included.
- Serial Wire Debug is included. Serial Wire Debugallows debug operations using only 2 wires, simple trace functionscan be added with a third wire.
- The Embedded Trace Macrocell (ETM) is included.The ETM provides instruction trace capabilities.
- The Data Watchpoint and Trace (DWT) unit isincluded. The DWT allows data address or data value matches to betrace information or trigger other events. The DWT includes 4comparators and counters for certain internal events.
- An Instrumentation Trace Macrocell (ITM) isincluded. Software can write to the ITM in order to send messagesto the trace port.
- The Trace Port Interface Unit (TPIU) isincluded. The TPIU encodes and provides trace information to theoutside world. This can be on the Serial Wire Viewer pin or the4-bit parallel trace port.
- A Flash Patch and Breakpoint (FPB) is included.The FPB can generate hardware breakpoints and remap specificaddresses in code space to SRAM as a temporary method of alteringnon-volatile code. The FPB includes 2 literal comparators and 6instruction comparators.


http://www.keil.com/support/docs/3051.htmhttp://www.keil.com/support/man/do
回复

使用道具 举报

7

主题

39

回帖

60

积分

初级会员

积分
60
发表于 2012-12-27 10:16:53 | 显示全部楼层
謝謝
Debug (printf) Viewer

Home » µVision Windows » Debug (printf) Viewer

The Debug (printf) Viewer window displays data streams that are transmitted sequentially through the ITM Stimulus Port 0. Enable ITM Stimulus Port 0.


Debug Viewer Window

To use the Debug (printf) Viewer for tracing:

   1. Add ITM Port register definitions to your source code.

    #define ITM_Port8(n)    (*((volatile unsigned char *)(0xE0000000+4*n)))
    #define ITM_Port16(n)   (*((volatile unsigned short*)(0xE0000000+4*n)))
    #define ITM_Port32(n)   (*((volatile unsigned long *)(0xE0000000+4*n)))

    #define DEMCR           (*((volatile unsigned long *)(0xE000EDFC)))
    #define TRCENA          0x01000000

   2. Add an fputc function to your source code that writes to the ITM Port 0 register. The fputc function enables printf to output messages.

    struct __FILE { int handle; /* Add whatever you need here */ };
    FILE __stdout;
    FILE __stdin;

    int fputc(int ch, FILE *f) {
      if (DEMCR & TRCENA) {
        while (ITM_Port32(0) == 0);
        ITM_Port8(0) = ch;
      }
      return(ch);
    }

    3.Add your debugging trace messages to your source code using printf.

    printf("AD value = 0x%04X\\r\\n", AD_value);

    4.Set the ITM Port 0 to capture the information. Clear the Port 7..0 privilege bit to access ITM Port 0 from User mode.

    ITM Stimulus Port 0
    Open the View - Serial Windows - Debug (printf) Viewer window.

Note

    ITM Stimulus Ports can be monitored in the Instruction Trace Window, where ITM Port 0 is shown as well.
    Consult Configure Cortex-M Target of the MDK-Primer for information on how to retarget the output.
回复

使用道具 举报

27

主题

272

回帖

353

积分

高级会员

积分
353
发表于 2018-4-13 13:28:26 | 显示全部楼层
mark 一下 ,早知道 我就 不用这么 辛苦了  ,当初 就是  swo 没接
回复

使用道具 举报

0

主题

22

回帖

22

积分

新手上路

积分
22
发表于 2018-4-13 19:22:04 | 显示全部楼层
mark 一下,学习了
回复

使用道具 举报

7

主题

69

回帖

90

积分

初级会员

积分
90
发表于 2018-4-14 20:29:53 | 显示全部楼层
这个没有RTT好,要额外的线
回复

使用道具 举报

98

主题

340

回帖

634

积分

金牌会员

积分
634
发表于 2019-5-11 17:52:29 | 显示全部楼层
近几日更换了几次KEIL版本后,发现之前移植好的SWO printf功能竟然一直卡死在while (ITM_Port32(0) == 0);这个地方(而断电重启不仿真的时候程序可以正常运行),所以怀疑是MDK设置问题,找了半天,设置没发现问题,最后暂时在程序初始化中手动加了一条代码SET_BIT(DBGMCU->CR, DBGMCU_CR_TRACE_IOEN);后正常使用了,有遇到这种现象的多多交流。
1.png 4.png 2.png 3.png

回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106660
QQ
发表于 2019-5-13 01:49:30 | 显示全部楼层
云琴箫龙 发表于 2019-5-11 17:52
近几日更换了几次KEIL版本后,发现之前移植好的SWO printf功能竟然一直卡死在while (ITM_Port32(0) == 0); ...

推荐直接使用MDK5,RTE环境里面直接集成了SWO和Event Recorder重定向,使用方便。
回复

使用道具 举报

10

主题

140

回帖

170

积分

初级会员

积分
170
发表于 2019-5-13 01:51:52 | 显示全部楼层
eric2013 发表于 2019-5-13 01:49
推荐直接使用MDK5,RTE环境里面直接集成了SWO和Event Recorder重定向,使用方便。

为家人和自己保重身体!
回复

使用道具 举报

98

主题

340

回帖

634

积分

金牌会员

积分
634
发表于 2019-5-13 09:08:30 | 显示全部楼层
eric2013 发表于 2019-5-13 01:49
推荐直接使用MDK5,RTE环境里面直接集成了SWO和Event Recorder重定向,使用方便。

目前用的5.18版本没有Event Recorder这个功能,上次MDK升级版本,遇到各种奇怪的现象,导致项目上用的工程代码都无法正常编译,因此还暂时用这个版本。
回复

使用道具 举报

1

主题

9

回帖

12

积分

新手上路

积分
12
发表于 2019-5-21 09:29:25 | 显示全部楼层
peterlao 发表于 2018-4-14 20:29
这个没有RTT好,要额外的线

朋友 您说的RTT是什么  以前没听说过
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106660
QQ
发表于 2019-5-21 11:10:12 | 显示全部楼层
huyu268897 发表于 2019-5-21 09:29
朋友 您说的RTT是什么  以前没听说过

【专题教程第5期】工程调试利器RTT实时数据传输组件,替代串口调试,速度飞快,可以在中断和多任务中随意调用
http://www.armbbs.cn/forum.php?m ... 6177&fromuid=58
(出处: 安富莱电子论坛)
回复

使用道具 举报

0

主题

3

回帖

3

积分

新手上路

积分
3
发表于 2019-5-26 19:03:57 | 显示全部楼层
谢谢楼主好资料!
回复

使用道具 举报

0

主题

124

回帖

124

积分

初级会员

积分
124
发表于 2019-6-6 20:33:56 | 显示全部楼层
这个打印的速率是多少?
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106660
QQ
发表于 2019-6-7 12:10:02 | 显示全部楼层
Edmund1964 发表于 2019-6-6 20:33
这个打印的速率是多少?

SWO比较慢,现在流行SEGGER RTT和Event Recoder
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-29 06:45 , Processed in 0.213710 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表