STM32 ITM(指令跟踪宏单元)

今天看代码的时候发现了新大陆,他的printf不是用串口发的,搞得我还在研究串口是怎么弄的

#define ITM_STIM_U32 (*(volatile unsigned int*)0xE0000000)    // Stimulus Port Register word acces
#define ITM_STIM_U8  (*(volatile         char*)0xE0000000)    // Stimulus Port Register byte acces
#define ITM_ENA      (*(volatile unsigned int*)0xE0000E00)    // Trace Enable Ports Register
#define ITM_TCR      (*(volatile unsigned int*)0xE0000E80)    // Trace control register

uint8_t SwoPt_EnCheck(void)
{
	if ((ITM_TCR & 1) == 0) {
		return 0;
	}

	if ((ITM_ENA & 1) == 0) {
		return 0;
	}		
	return 1;
}

void SwoPt_sendByte(uint8_t SwoPt_c)
{
	if(SwoPt_EnCheck())
	{
		if(ITM_STIM_U8 & 1)
		{
			ITM_STIM_U8 = SwoPt_c;
		}
	}
}

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  //USART_SendData(EVAL_COM1, (uint8_t) ch);
	SwoPt_sendByte((uint8_t) ch);

  /* Loop until the end of transmission */
  //while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  //{}

  return ch;
}

这个是stm32f407LGT6的代码,贴上就能用

选择调试模式

 

打开窗口

 

 如果出问题

在下面设置成你当前的主频频率

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STM32上使用printf函数输出信息需要进行重定向。通过重定向,可以将printf函数的输出发送到仿真器连接的PC机上的终端窗口。具体实现方法如下: 1. 首先,需要在工程中添加一个文件,命名为retarget.c,并将其加入到工程中。 2. 在retarget.c文件中,需要实现fputc函数,该函数将会被printf函数调用。在fputc函数中,可以利用ITM(Instrumentation Trace Macrocell)机制提供的寄存器,将数据发送给仿真器,然后仿真器将数据发送到PC机上的终端窗口。以下是一个示例的fputc函数的实现: ```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; } ``` 3. 在工程中的main函数中,需要添加以下代码来重定向printf函数的输出: ```c #include <stdio.h> extern void initialise_monitor_handles(void); int main() { initialise_monitor_handles(); // 其他代码 printf("Hello, world!"); // 其他代码 return 0; } ``` 通过以上步骤,就可以在STM32上使用printf函数并将输出重定向到仿真器连接的PC机上的终端窗口了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值