STM32中重定向printf到SWO口

Keil中调试 : 用 SWO 功能替代 printf

引用网址:http://blog.csdn.net/xiaolei05/article/details/8526021

keil官方Guide: http://www.keil.com/support/man/docs/jlink/jlink_trace_itm_viewer.htm

嵌入式软件开发中的一个基本需求就是能通过终端来输出调试信息,一般可通过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

 

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.

GCC工具链下调试:  用 SWO 功能替代 printf

引用自:http://bbs.elecfans.com/forum.php?mod=viewthread&tid=466474&extra=

printf在命令行编程的时候是非常常用的,虽然是个老函数,但是功能强大,经久不衰
51等8位 单片机由于RAM比较小,栈就比较小,跑printf比较吃力,
但是STM32这种32位单片机跑printf就很容易了,而作为一种调试手段,printf十分方便、直观。
比较常见的方法是把printf重定向到串口,不过这需要外接一个串口线,比较麻烦。
其实STM32自带的SWO口是能够异步输出数据的,而且不需要外接什么设备,
ST-LINK/J-Link等带SWO口的调试器都支持。
下面以STM32F4Discovery开发板+GCC为例说明。
根据这里的方法,也可以把printf定位到其他外设。
PS:IAR在编译选项里自带了printf via SWO的功能,就不需要外加设置了。
http://community.silabs.com/t5/Microcontroller-How-to-Guides/SWO-printf-in-IAR/td-p/98257
首先来说说怎么把信息输出到SWO口,一句话搞定。
ITM_SendChar(ch);
这是在core_cm4.h(如果是F1系列的那就是core_cm3.h)中定义的内联函数。
不过不需要特意去include这个头文件,通过#include "stm32f4xx.h"就间接地将core_cm4.h包含进来。
不过说起来,ITM这个东西其实严格来说是Cortex-M提供的一个特性,而不是STM32。
利用这个函数把信息输出到SWO口之后再打开St-Link Utility,
在菜单里找到ST-LINK→Printf via SWO Viewer就会弹出一个窗口,
设置System Clock为单片机内核频率,点Start就能看到输出的信息了。
接下来就是把printf函数输出的字符串重定向过去了。
由于单片机的外设功能是根据需求变化的,编译器不可能确定printf需要用到的外设资源,
于是乎它就干脆留了个接口,也就是_write函数,
当然除了_write函数之外还有_read等其他函数,不过这里我们用不到。
其声明为 int _write(int fd, char* ptr, int len);
关于_write函数,说简单点,就是所有涉及到输出字符串的函数,
比如printf和putchar(),最终都会跑到_write函数,这里fd是文件标识符,说开来就比较复杂了,
这里我们用得到的就只有STDOUT_FILENO跟STDERR_FILENO,
其中前一个是标准输出的文件标识符的预定义变量,后一个是错误输出的文件标识符预定义变量。
第二个变量ptr是需要输出的字符串首地址,len就是输出长度。
当我们调用printf函数后,C运行库会把输入变量转换为最终需要输出的字符串,
然后调用_write函数,将结果输出。我们的工作就是实现一个_write函数。
新建一个_write.c文件,内容如下:
  1. #include <stdio.h>
  2. #include <unistd.h>

  3. #include "stm32f10x.h"

  4. #ifdef _DEBUG

  5. int _write(int fd, char* ptr, int len)
  6. {
  7. if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
  8. {
  9. int i = 0;
  10. while(i<len)
  11. ITM_SendChar(ptr[i++]);
  12. }
  13. return len;
  14. }

  15. #endif
复制代码

加了个#ifdef _DEBUG 的效果是未加 _DEBUG 定义的时候就忽略下面的东西,
因为这东西主要是用在调试阶段,RELEASE版本里面都用不到了,而且多少还是会影响速度。
其他东西就很简单了- -不需要多说明了吧。
直接编译能通过,但是链接会报错,提示无法找到_read之类的一堆函数。
在链接脚本的下面libgcc.a ( * )后面加上libnosys.a ( * ),就不会报错了。
具体原因涉及到Cortex-M3使用的newlib库的实现,就不具体展开了。
好吧好吧,其实我也不知道。
如果想把信息定位到串口,可以直接把ITM_SendChar改成相应的串口函数,
也可以利用DMA,先把数据拷贝到DMA缓冲区,让DMA自动传数据,提高响应速度。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STM32printf函数的输出可以通过重定义fputc函数来实现。fputc函数是一个底层的I/O函数,用于将字符逐个打印输出。在重定义fputc函数时,我们可以设置输出的地方,比如UART通信的输出寄存器、显示屏的显示寄存器,或者是蓝牙、WiFi等。下面是一个使用UART协议输出的例子: ```c // 重定义fputc函数 int fputc(int ch, FILE *f) { while((USART1->SR & 0X40) == 0); // 循环发送,直到发送完毕 USART1->DR = (uint8_t)ch; return ch; } ``` 这样,当我们在代码使用printf函数时,实际上是调用了重定义后的fputc函数来逐个字符地打印输出。需要注意的是,如果使用半主机模式,需要连接SWO线(即JTAG接线),而当目标板脱离仿真器(如jlink/ulink)单独运行时,不能使用半主机模式。因此,在使用printf函数时,需要根据具体的情况选择合适的配置。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [stm32printf函数](https://blog.csdn.net/m0_46392035/article/details/127028010)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [STM32使用printf重定向](https://blog.csdn.net/weixin_48120109/article/details/126474339)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值