打印

MDK+硬件仿真器实现debugprintf()【终于找到这个教程了】

[复制链接]
2968|14
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
sunmeat|  楼主 | 2014-11-28 08:18 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
1.MDK工程设置如下

沙发
sunmeat|  楼主 | 2014-11-28 08:19 | 只看该作者

使用特权

评论回复
板凳
sunmeat|  楼主 | 2014-11-28 08:19 | 只看该作者

使用特权

评论回复
地板
sunmeat|  楼主 | 2014-11-28 08:20 | 只看该作者
2.其中stm32debug.ini文件内容为
/******************************************************************************/

/* STM32DBG.INI: STM32 Debugger Initialization File */

/******************************************************************************/

// <<< Use Configuration Wizard in Context Menu >>> //

/******************************************************************************/

/* This file is part of the uVision/ARM development tools. */

/* Copyright (c) 2005-2007 Keil Software. All rights reserved. */

/* This software may only be used under the terms of a valid, current, */

/* end user licence from KEIL for a compatible version of KEIL software */

/* development tools. Nothing else gives you the right to use this software. */

/******************************************************************************/

FUNC void DebugSetup (void) {

// <h> Debug MCU Configuration

// <o1.0> DBG_SLEEP <i> Debug Sleep Mode

// <o1.1> DBG_STOP <i> Debug Stop Mode

// <o1.2> DBG_STANDBY <i> Debug Standby Mode

// <o1.5> TRACE_IOEN <i> Trace I/O Enable

// <o1.6..7> TRACE_MODE <i> Trace Mode

// <0=> Asynchronous

// <1=> Synchronous: TRACEDATA Size 1

// <2=> Synchronous: TRACEDATA Size 2

// <3=> Synchronous: TRACEDATA Size 4

// <o1.8> DBG_IWDG_STOP <i> Independant Watchdog Stopped when Core is halted

// <o1.9> DBG_WWDG_STOP <i> Window Watchdog Stopped when Core is halted

// <o1.10> DBG_TIM1_STOP <i> Timer 1 Stopped when Core is halted

// <o1.11> DBG_TIM2_STOP <i> Timer 2 Stopped when Core is halted

// <o1.12> DBG_TIM3_STOP <i> Timer 3 Stopped when Core is halted

// <o1.13> DBG_TIM4_STOP <i> Timer 4 Stopped when Core is halted

// <o1.14> DBG_CAN_STOP <i> CAN Stopped when Core is halted

// </h>

_WDWORD(0xE0042004, 0x00000027); // DBGMCU_CR

_WDWORD(0xE000ED08, 0x20000000); // Setup Vector Table Offset Register

}

DebugSetup(); // Debugger Setup

使用特权

评论回复
5
sunmeat|  楼主 | 2014-11-28 08:21 | 只看该作者
3.项目工程加载retarget.c或者debugprint.c,其中retarget.c内容如下
#include <stdio.h>

#include "stm32f10x.h"

#pragma import(__use_no_semihosting_swi)

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

    FILE __stdout;

    FILE __stdin;

int fputc(int ch, FILE *f)

{

    return ITM_SendChar(ch);

}

volatile int32_t ITM_RxBuffer;

int fgetc(FILE *f)

{

  while (ITM_CheckChar() != 1) __NOP();

  return (ITM_ReceiveChar());

}

int ferror(FILE *f)

{

    /* Your implementation of ferror */

    return EOF;

}

void _ttywrch(int c)

{

    fputc(c, 0);

}

int __backspace()

{

    return 0;

}

void _sys_exit(int return_code)

{

label:

    goto label; /* endless loop */

}

使用特权

评论回复
6
sunmeat|  楼主 | 2014-11-28 08:21 | 只看该作者
debugprint.c内容如下:
#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);

}

使用特权

评论回复
7
sunmeat|  楼主 | 2014-11-28 08:21 | 只看该作者
4.包含头文件#include <stdio.h>,并调用printf

使用特权

评论回复
8
sunmeat|  楼主 | 2014-11-28 08:22 | 只看该作者
5在debug中打开输出仿真:View-serial-debug(printf)

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

208

主题

2132

帖子

13

粉丝