MDK下仿真实现printf功能

1.MDK工程设置如下


stm32dbg,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



3.项目工程加载retarget.c或者debugprint.c,其中retarget.c内容如下

  1. #include <stdio.h>

  2. #include "stm32f10x.h"

  3. #pragma import(__use_no_semihosting_swi)

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

  5.     FILE __stdout;

  6.     FILE __stdin;

  7. int fputc(int ch, FILE *f) 

  8. {

  9.     return ITM_SendChar(ch);

  10. }

  11. volatile int32_t ITM_RxBuffer;

  12. int fgetc(FILE *f)

  13. {

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

  15.   return (ITM_ReceiveChar());

  16. }

  17. int ferror(FILE *f)

  18. {

  19.     /* Your implementation of ferror */

  20.     return EOF;

  21. }

  22. void _ttywrch(int c)

  23. {

  24.     fputc(c, 0);

  25. }

  26. int __backspace()

  27. {

  28.     return 0;

  29. }

  30. void _sys_exit(int return_code)

  31. {

  32. label:

  33.     goto label; /* endless loop */

  34. }
debugprint.c内容如下:
  1. #include <stdio.h>

  2. #define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))

  3. #define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n)))

  4. #define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000+4*n)))

  5. #define DEMCR (*((volatile unsigned long *)(0xE000EDFC)))

  6. #define TRCENA 0x01000000

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

  8.     FILE __stdout;

  9.     FILE __stdin;

  10. int fputc(int ch, FILE *f) 

  11. {

  12.     if (DEMCR & TRCENA) 

  13.     {

  14.         while (ITM_Port32(0) == 0);

  15.         ITM_Port8(0) = ch;

  16.     }

  17.     return(ch);

  18. }


4.包含头文件#include <stdio.h>,并调用printf

5在debug中打开输出仿真:View-serial-debug(printf)




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值