作者:xiaolei05
转自:http://blog.csdn.net/xiaolei05/article/details/8526021
Keil调试技术
嵌入式软件开发中的一个基本需求就是能通过终端来输出调试信息,一般可通过2种方式实现:一种是使用串口线连接板上的UART和PC上的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
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.