1、使用JLINK SEGGER RTT打印调试信息
很简单,首先从Jlink官网上下载RTT代码,然后根据开发平台将五个文件添加到自己工程中去,并且在主程序工程中添加SEGGER_RTT.h文件,这里展示的平台有GCC,IAR,KEIL三种平台
之后可以使用SEGGER_RTT_printf(0,”Test%d\r\n”,++u32Counter); 打印调试信息,在JLINK安装目录中打开J-Link RTT Viewer,就可以看到调试信息
2、RTT输入检测
首先在程序中添加SEGGER_RTT_Read(0,rtt_buf,sizeof(rtt_buf));
其次在J-Link RTT Viewer控制台的Input里面选择Sending/Send on Enter,然后End of Line选择None
这样就可以通过下方输入框输入信息,按Enter发送
这样,程序里面接收数组rtt_buf就是输入框所输入的信息
初始化:
`/初始化函数不用也可以,用了会导致没有下载工具连接时,内存满了会导致程序复位/“
SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL);
RTT输入获取函数,并且获取有效数据,调试程序
void DebugRTT(void)
{ BYTE idata,data;
static char RTT_buffer[20];
idata=SEGGER_RTT_Read(0, RTT_buffer, 20);
if(idata)
{
printf(“\r\nidata : 0x%02X”,idata);
printf(“\r\n “);
SEGGER_RTT_Write(0, RTT_buffer, 20);
if(idata>2)
{
for(BYTE i=0;i<(idata-1);i+=2)
{
data=Asc1Bin(RTT_buffer[i])*0x10+Asc1Bin(RTT_buffer[i+1]);
DebugRttMag(data);
}
}
}
}
void DebugRttMag(BYTE data)
{
u8 i;
static u16 MagLen,MagLenMax;
static u8 ComMagStatus;
static u8 checksum;
static u8 MsgPackage[256];
//data=Asc1Bin(*buffer)*0x10+Asc1Bin(*(buffer+1));
printf("\r\nRTT : 0x%02X ",data);
switch (ComMagStatus)
{
case 0:
if (data == 0xA0)
{
ComMagStatus = 1;
checksum = 0;
//printf("\r\n0x%02X ",data);
}
else ComMagStatus = 0;
break;
case 1:
{
ComMagStatus = 2;
MagLenMax = data;
MagLen = 0;
checksum += data;
//printf("\r\n0x%02X ",data);
}
break;
case 2:
if(MagLen < MagLenMax)
{
MsgPackage[MagLen] = data;
checksum += data;
MagLen ++;
}
else
{
MsgPackage[MagLen] = data;
if(data == 0xAF)/ecksum
{
#if 1
printf("\r\n[RTT >>>>>>> MCU]:");
for(i=0;i>>>>>> MCU]:--->ERROR:");
for(i=0;i<(MagLen+1);i++)
{
printf("0x%02X ",MsgPackage[i]);
}
printf("\n");
#endif
}
ComMagStatus = 0;
}
break;
default:
ComMagStatus = 0;
break;
}
}
BYTE Asc1Bin(BYTE asc)
{
if(asc>=’0’ && asc <=’9’) return (asc - ‘0’);
if(asc>=’a’ && asc <=’f’) return (asc - ‘a’ + 0x0a);
if(asc>=’A’ && asc <=’F’) return (asc - ‘A’ + 0x0a);
return 0;
}
降频打印实时数据:
void printfl(unsigned CMD, const char * sFormat, …)
{
va_list ParamList;
BYTE div,remainder;
//memset(CMDflag,0XFF,sizeof(CMDflag));// 计时设置
if(!CMD)
{
div=CMD/16;
remainder=CMD%16;
//printf(“\r\n>>>>>>0x%02X”,CMD);
//printf(“\r\n>>>>>>0x%02X”,div);
//printf(“\r\n>>>>>>0x%02X”,remainder);
if(CMDflag[div]&(1<>>>>>0x%08X",CMDflag[div]);
CMDflag[div]=CMDflag[div]&(~(1<>>>>>0x%08X",CMDflag[div]);
va_start(ParamList, sFormat);
SEGGER_RTT_vprintf(0, sFormat, &ParamList);
}
}
else
{
va_start(ParamList, sFormat);
SEGGER_RTT_vprintf(0, sFormat, &ParamList);
}
}
“`
定义160个打印标记://MAIN_EXTERN WORD CMDflag[10];(按位标记)
在定时器里设置打印条件:
//memset(CMDflag,0XFF,sizeof(CMDflag));// 计时设置