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) {


// Debug MCU Configuration


// DBG_SLEEP Debug Sleep Mode


// DBG_STOP Debug Stop Mode


// DBG_STANDBY Debug Standby Mode


// TRACE_IOEN Trace I/O Enable 


// TRACE_MODE Trace Mode


// <0=> Asynchronous


// <1=> Synchronous: TRACEDATA Size 1


// <2=> Synchronous: TRACEDATA Size 2


// <3=> Synchronous: TRACEDATA Size 4


// DBG_IWDG_STOP Independant Watchdog Stopped when Core is halted


// DBG_WWDG_STOP Window Watchdog Stopped when Core is halted


// DBG_TIM1_STOP Timer 1 Stopped when Core is halted


// DBG_TIM2_STOP Timer 2 Stopped when Core is halted


// DBG_TIM3_STOP Timer 3 Stopped when Core is halted


// DBG_TIM4_STOP Timer 4 Stopped when Core is halted


// DBG_CAN_STOP CAN Stopped when Core is halted


//



_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

  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

  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 ,并调用printf

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




你可能感兴趣的:(开发工具)