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

[复制链接]
3411|14
 楼主| sunmeat 发表于 2014-11-28 08:18 | 显示全部楼层 |阅读模式
1.MDK工程设置如下
图像 052.png
 楼主| sunmeat 发表于 2014-11-28 08:19 | 显示全部楼层
 楼主| sunmeat 发表于 2014-11-28 08:19 | 显示全部楼层
 楼主| sunmeat 发表于 2014-11-28 08:20 | 显示全部楼层
2.其中stm32debug.ini文件内容为
  1. /******************************************************************************/

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

  3. /******************************************************************************/

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

  5. /******************************************************************************/

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

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

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

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

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

  11. /******************************************************************************/

  12. FUNC void DebugSetup (void) {

  13. // <h> Debug MCU Configuration

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

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

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

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

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

  19. // <0=> Asynchronous

  20. // <1=> Synchronous: TRACEDATA Size 1

  21. // <2=> Synchronous: TRACEDATA Size 2

  22. // <3=> Synchronous: TRACEDATA Size 4

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

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

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

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

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

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

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

  30. // </h>

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

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

  33. }

  34. DebugSetup(); // Debugger Setup
 楼主| sunmeat 发表于 2014-11-28 08:21 | 显示全部楼层
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. }
 楼主| sunmeat 发表于 2014-11-28 08:21 | 显示全部楼层
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. }
 楼主| sunmeat 发表于 2014-11-28 08:21 | 显示全部楼层
4.包含头文件#include <stdio.h>,并调用printf
 楼主| sunmeat 发表于 2014-11-28 08:22 | 显示全部楼层
5在debug中打开输出仿真:View-serial-debug(printf)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

208

主题

2132

帖子

13

粉丝
快速回复 在线客服 返回列表 返回顶部