J-Link RTT Viewer tutorial (with code)

Table of contents

 

Introduction to RTT (Real Time Transfer)

tutorial

Introduction to common APIs

RTT buffer size modification

Use printf to redirect

official routine


Introduction to RTT (Real Time Transfer)

It is cumbersome to use the serial port to print logs in the usual debugging code, and it is often necessary to connect the serial port pins, and the printing speed of the serial port is slow, and the interruption of the serial port may affect the execution efficiency of the code.

SEGGER RTT supports the use of the J-link debugger to output information from the target microcontroller, and can also receive input, and it will not affect the real-time performance of the target processor while interacting at a high speed, which can save the serial port used for printing logs.

SEGGER RTT can be used with any J-Link model and with any target processor that supports background memory access, i.e. Cortex-M and RX targets.

RTT supports two directions, multiple channels, up to the host and down to the target, it can be used for different purposes, giving the user as much freedom as possible. The default implementation uses one channel per direction for printable terminal input and output.

With the J-Link RTT Viewer, it is possible to use a "virtual" terminal, allowing printing to multiple windows (e.g. one for standard output, one for error output, and one for debug output).

463331ac801040f2beebdaf76c02fc74.jpeg

The performance of SEGGER RTT is significantly higher than any other technology used to output data to the host PC. An average line of text can be output in a microsecond or less. Basically equivalent to the time to do a single memcopy(). The speed comparison is done on a STM32F407 Cortex-M4 running at 168 MHz, as shown in the figure below, which does not include the time of the printf() call.

b7abf417c5844ed4bb6a9f16f0cde1c6.jpeg

The maximum speed at which output data can be sent to the host depends on the destination buffer size and the destination interface speed. Even with a small target buffer of 512 bytes, RTT speeds of up to 1 MB/s are possible, compared to 0.5 MB/s for regular J-Link models.

5671225e95c54e03afc89bb4968e4668.jpeg

The buffer for the RTT upstream channel may be relatively small. The minimum required buffer size can be approximated by the amount of data written in one millisecond and the maximum value written in one write operation. If data is sent infrequently, the buffer should have enough space for the data sent in one write. If data is sent more frequently, the buffer size should be sufficient for the maximum amount of data written in one millisecond. The figure below shows the measured amount of data required to send uniformly distributed different amounts of data per 100 us and per 1 ms using a J-Link PRO V4 @ 36 MHz JTAG speed on a SEGGER emPower evaluation board (NXP K66 part) at 168 MHz The minimum buffer size for .

f320fb070fd246a5b948a46918077288.jpeg

b112e14cb301407f8d062d6b6224e9c9.jpeg

The RTT implementation code uses about 500 bytes of ROM and 24 bytes ID + 24 bytes per channel for the control block in RAM. Each channel requires some buffer memory. According to the load of the input/output, the recommended size of the upstream channel is 1 kByte, and the recommended value of the downstream channel is 16 to 32 Byte.

a5e8b03eabfb4ae1be48e2355c454c5c.jpeg

tutorial

1. First install the J-Link software driver: SEGGER - The Embedded Experts - Downloads - J-Link / J-Trace

2. After the installation is complete, open the J-Link installation directory (Start->SEGGR->J-Link RTT Viewer->right-click to open the file location->then continue to right-click to open the file location->this is the installation directory) ,

5b1a5b3080c0472e80c1f0ef1226bfd3.jpeg

Find the following path SEGGER\JLink\Samples\RTT, decompress the compressed package SEGGER_RTT_V770c.zip in the path (different versions, the number after V may be different).

6a8a3bd59e894397aeebeedf855f0c77.jpeg

3. Copy the decompressed file to the code project directory.

4. Create a new RTT folder under the MDK project, copy the decompressed files to the RTT directory, and add these files to the MDK project. Don't forget the header file directory.

195f8db111a54cc29c882dee745fc5e2.jpeg

5. After adding files to the project, include #include "SEGGER_RTT.h" in the file you want to use RTT, and then call SEGGER_RTT_printf() directly, for example, SEGGER_RTT_printf(0,"Hello RTT~"); this is the same as C The format of the printf of the language is similar, that is, a parameter of port 0 is added in front, and the code is compiled after editing without errors and then downloaded.

6. Then click Start->SEGGR->J-Link RTT Viewer, open J-Link RTT Viewer, select your chip model, and click OK. On many devices, the RTT address can be automatically recognized. For devices that cannot be automatically recognized, you need to enter the RTT address yourself. The RTT address is the address of the structure _SEGGER_RTT in the code.

2db0e3ba48da4b959a55a0d7f953e0d7.jpeg

7. Then you can see what we printed.

ca3dc55153a0401da71953cbc3959e50.jpeg

Introduction to common APIs

1.void SEGGER_RTT_Init (void); The RTT initialization function should be placed at the beginning of the program.

2. int SEGGER_RTT_GetKey (void); Get a key character from the RTT terminal.

    int c;
    c = SEGGER_RTT_GetKey();
    if (c == 'q') {
        exit();
    }

3.int SEGGER_RTT_HasKey (void); Check whether there are characters in the buffer

   if (SEGGER_RTT_HasKey()) {
      int c = SEGGER_RTT_GetKey();
   }

4. int SEGGER_RTT_printf (unsigned BufferIndex, const char * sFormat, …); Format the output string, and you can use SEGGER_RTT_printf() to set the font color and background color.

48021837c886490a8945ceb6c313b694.jpeg

SEGGER_RTT_printf(0,RTT_CTRL_BG_WHITE”RTT TEST\r\n”);
SEGGER_RTT_printf(0,RTT_CTRL_TEXT_BLUE”RTT TEST\r\n”);

5. void SEGGER_RTT_SetTerminal(char TerminalId); Set the virtual terminal ID. In the following example, the parameter 0 in SEGGER_RTT_WriteString is the channel number, not the terminal number.

//
// Send a string to terminal 1 which is used as error out.
//
SEGGER_RTT_SetTerminal(1); // Select terminal 1
SEGGER_RTT_WriteString(0, "ERROR: Buffer overflow");
SEGGER_RTT_SetTerminal(0); // Reset to standard terminal

6. int SEGGER_RTT_WaitKey (void); Check whether there are characters in the buffer.

   int c = 0;
    do {
        c = SEGGER_RTT_WaitKey();
    } while (c != 'c');

RTT buffer size modification

Sometimes our information cannot be printed out completely, maybe because the buffer is not enough, the default buffer size is 1K bytes, if it is not enough, it can be changed to a larger size. Modify the value of #define BUFFER_SIZE_UP in the SEGGER_RTT_Conf.h file.

cb974a5c3d8a471fa6875e93d4f4d52a.jpeg

Use printf to redirect

There are many places where printf is used in the project. It will be very convenient if you can directly modify printf to redirect to the RTT component. The method used is to directly use the API provided by RTT to implement fputc.

#include <stdio.h>
int fputc(int ch, FILE *f)
{
    SEGGER_RTT_PutChar(0,ch);
    return ch;
}

official routine

/*********************************************************************
* SEGGER Microcontroller GmbH *
* Solutions for real time microcontroller applications *
**********************************************************************
* *
* (c) 1995 - 2018 SEGGER Microcontroller GmbH *
* *
* www.segger.com Support: [email protected] *
* *
**********************************************************************
----------------------------------------------------------------------
File : RTT.c
Purpose : Simple implementation for output via RTT.
It can be used with any IDE.
---------------------------- END-OF-HEADER ---------------------------
*/
#include "SEGGER_RTT.h"
static void _Delay(int period) {
int i = 100000*period;
do { ; } while (i--);
}
int main(void) {
int Cnt = 0;
SEGGER_RTT_WriteString(0, "Hello World from SEGGER!\n");
do {
SEGGER_RTT_printf(0, "%sCounter: %s%d\n",
RTT_CTRL_TEXT_BRIGHT_WHITE,
RTT_CTRL_TEXT_BRIGHT_GREEN,
Cnt);
if (Cnt > 100) {
SEGGER_RTT_TerminalOut(1, RTT_CTRL_TEXT_BRIGHT_RED"Counter overflow!");
Cnt = 0;
}
_Delay(100);
Cnt++;
} while (1);
return 0;
}
/*************************** End of file ****************************/

 

 

Guess you like

Origin blog.csdn.net/qq_30095921/article/details/129030119