Embedded development notes — debug component segger_ RTT

Time:2021-6-14

1、 Preface

In the process of embedded development, we often print out some debugging information to debug parameters and find problems. Usually, I use the chip’s serial port hardware device with serial port assistant software to debug. But the PCB hardware design of this project does not reserve serial port debugging interface, so it is not convenient to use serial port debugging. After searching for information, we found thatSeggerProvides a very convenient way to debug – ownJ-LinkHardware coordinationJ-Link RTT ViewerThe software is debugged for information input and output.

2、 Component addition

To use this debugging component, you need to add segger provided by segger_ RTT component code, which is located in the j-link software installation directory, and provides detailed instructions in the user manual.

Component source code location:

User manual location:

RTT components are described in Chapter 16 of the manual

Copy the source code of the component to the project directory, and add relevant files and paths to the project

Finally, it is included in the file that needs to print debugging information#include "SEGGER_RTT.h"The component can be used with the header file.

3、 Component application

The API functions provided by components are explained in the manual. Examples are as follows:

Output test:

int a = 3;
SEGGER_RTT_TerminalOut(0,RTT_CTRL_BG_BLACK""RTT_CTRL_TEXT_BRIGHT_GREEN"SEGGER_RTT_TerminalOut 0\r\n");
SEGGER_RTT_TerminalOut(1,RTT_CTRL_BG_BLUE""RTT_CTRL_TEXT_BRIGHT_YELLOW"SEGGER_RTT_TerminalOut 1\r\n");
SEGGER_RTT_SetTerminal(2);
SEGGER_RTT_printf(0,RTT_CTRL_BG_WHITE""RTT_CTRL_TEXT_BRIGHT_BLACK"SEGGER_RTT_printf 2\r\n");
SEGGER_RTT_SetTerminal(3);
SEGGER_RTT_printf(0,"SEGGER_RTT_printf %d\r\n", a);

openJ-Link RTT ViewerSoftware

The printing results of RTT viewer are as follows:

Input test:

int a;

while(1)
{
    if ((a = SEGGER_RTT_WaitKey()) > 0) 
    {
        SEGGER_RTT_SetTerminal(0);
        SEGGER_RTT_printf(0, "SEGGER_RTT_GetKey = %c\r\n", a);
    }
}

The printing results of RTT viewer are as follows:

4、 Extended application

After the above analysis of segger_ The use of RTT, found that it is very convenient, most of the debugging can replace the serial debugging, butSEGGER_RTT_printf()Function cannot print floating point numbers. The manual lists the supported conversion specifiers for this function as follows:

Conversion specifier Meaning
c Print the argument as one char
d Print the argument as a signed integer
u Print the argument as an unsigned integer
x Print the argument as an hexadecimal integer
s Print the string pointed to by the argument
p Print the argument as an 8-digit hexadecimal integer. (Argument shall be a pointer to void.)

There are no floating-point numbersfOptions. But you can use segger_ RTT output function itself modifies a printf function, so that you can use the complete printf function.

Add your own modified printf function as follows:

/*********************************************************************

Next, test floating point printing

double fa = 0.1f;
double fb = 2.0f;

while(1)
{
    fa += 0.0001f;
    fb -= 0.0002f;
    rtt_printf("floating test:\tfa = %f, fb = %f\r\n", fa, fb);
    delay(0x0fffffff);
}