好记性不如铅笔头

ARM, 操作系统

ARM学习随手笔记:使用ITM方式启用printf

作者最近在学习keil和ARM,哎,只能感叹自己老了,这里简单的记录下学习笔记吧。

CONTENTS

参考网址

【 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.kui0072a/ulink2_trace_itm_viewer.htm
【 http://www.cnblogs.com/afeibfp/archive/2013/01/12/2857262.html

最简单实例

以最简单的M3工程为例,mainfunc.c文件原始内容为:

int main(void)
{
		while(1)
			;
}

修改后的内容为:

#include <stdio.h>

#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

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);
}

int main(void)
{
		printf("Hello World\n");
		while(1)
			;
}

编译OK后,仍然使用simulator方式进行模拟,打开Debug Viewer就可以看到输出了。如下图:

发表评论

9 + 17 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据