网上说的添加一段重新定义输出的代码,然后配置一下debug那种方法我试过了
然后还有一种是配置这里
也试过了还是不行,查来查去就只有这两种方法
而且上面这两种方法在运行时都会卡在 while (ITM_Port32(0) == 0); 这行
然后我自己查一下ai,改了源码:
#include "stm32f10x.h"
#include "core_cm3.h"
#include "stdio.h"
#include "Delay.h"
#define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000 + 4 * n)))
#define DBGMCU_CR (*(volatile uint32_t *)0xE0042004)
#define TRACE_IOEN (1 << 5)
#define TPI_ACPR (*(volatile uint32_t *)0xE0040010)
int fputc(int ch, FILE *f) {
if (CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk) {
while (ITM_Port32(0) == 0);
ITM_Port32(0) = ch;
}
return ch;
}
int main() {
RCC->APB2ENR |= RCC_APB2ENR_AFIOEN | RCC_APB2ENR_IOPBEN;
AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE; // 禁用JTAG
// 启用跟踪引脚
DBGMCU_CR |= TRACE_IOEN;
// 解锁并配置ITM
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
ITM->LAR = 0xC5ACCE55;
ITM->TCR = (1 << 0) | (1 << 3) | (1 << 16);
ITM->TER |= (1 << 0);
// 设置TPIU波特率(假设系统时钟72MHz,目标2MHz)
TPI_ACPR = 35; // 72MHz / (35+1) = 2MHz
while(1) {
printf("hello world\n");
Delay_ms(1000);
}
}
这样不会卡住,但是没有输出,找来找去都没有输出,希望能看到这里的大牛有知道的可以和我说一下,真的很想知道,拜托了