Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

Click on “Gua Gua Little Master” above, select “Pin/Star Official Account

Useful Resources Delivered First Hand!

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

Summary: I wonder how everyone prints debugging information during microcontroller development. Most likely, they use serial debugging to print. In most cases, serial port 1 is usually reserved for debugging printing during board manufacturing and coding.

But what if no serial port is reserved during actual development? Actually, our downloader can be used as a debugging printer; it’s just that many friends don’t know this feature. Today, I will explain how to use the JLink debugger to print information.

1. JLink Emulator Debugger

There are various downloaders available, but I only use JLink, which is compact and portable. For microcontroller developers, the downloaders used are basically JLink and ST-Link. Both have similar functions; JLink is from SEGGER, while ST-Link is from ST and only supports ST series chips. I only use the JLink downloader for debugging because it is small and has only four wires, making it very convenient. YYDS!

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
J-Link OB ARM Emulator Debugger

2. Install JLink Driver

Download link: https://www.segger.com/downloads/jlink/. After purchasing the JLink driver, the seller usually provides the JLink driver program. Once the driver is installed, you can download and debug programs.

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
https://www.segger.com/downloads/jlink/

Of course, to use the RTT feature of JLink (Real Time Transfer, not RT-Thread operating systemStep-by-Step Guide to Using JLink as a Serial Debugging Assistant), you need to download the complete JLink package from the official website. The latest version is V7.52, but other versions are also acceptable. After downloading, simply install it. Once installed, you will see the following content in your installation directory:

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
JLink Installation Directory

3. Porting RTT

Once the installation is complete, it becomes easier. The RTT source package is located in the directory of the JLINK driver we just installed. My directory is: D:\Software\SEGGER\JLink_V644b\Samples\RTT

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

After extracting, the specific directory is: D:\Software\SEGGER\JLink_V644b\Samples\RTT\SEGGER_RTT_V644b\RTT

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

Then copy this RTT folder to the project folder where you are writing the program

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

Next, create a new RTT group in the project and add the two .c files from the RTT folder.

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

Of course, remember to add the header file path for RTT

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

At this point, the porting is basically successful. Isn’t it simple? You just need to add the RTT source code to the project, and there is no need to modify anything else.

4. RTT Printing Output

Next, you can start printing output.

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "SEGGER_RTT.h"

int main(void)
{ 
    HAL_Init();                     // Initialize HAL library    
    Stm32_Clock_Init(336,8,2,7);   // Set clock, 168Mhz
 delay_init(168);                // Initialize delay function
 LED_Init();      // Initialize LED 
 
 while(1)
 {
         SEGGER_RTT_printf(0,"zhiguoxin666\r\n");
 }
}
Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
Connect the hardware properly
Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
Set to SW mode

Then open JLinkRTTViewer.exe in the JLink installation directory

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

Configure as follows

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

Once the code is downloaded to the microcontroller, you will see it printing perfectly.

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

5. RTT Usage Tips

1. RTT Buffer Size

Sometimes we find that our information cannot be printed completely, which may be due to insufficient buffer size. The default buffer size is 1K byte; if it’s not enough, you can increase it a bit.

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
Default 1024 bytes

2. Using Multiple Virtual Ports

RTT supports printing information to different virtual ports. The usage method is as follows.

First, open three virtual ports in the RTT Viewer software:

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

Write the code

while(1)
{
 SEGGER_RTT_SetTerminal(0);
 SEGGER_RTT_printf(0,"zhiguoxin666,SEGGER RTT Terminal 0!\r\n");
 SEGGER_RTT_SetTerminal(1);
 SEGGER_RTT_printf(0,"zhiguoxin666,SEGGER RTT Terminal 1!\r\n");
 SEGGER_RTT_SetTerminal(2);
 SEGGER_RTT_printf(0,"zhiguoxin666,SEGGER RTT Terminal 2!\r\n");
 delay_ms(1000);
}

Compile, link, and download, then observe the phenomenon:

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
Window 0
Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
Window 1
Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
Window 2

3. Change Print Character Color

RTT supports different colors for character display.

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

To use, just add the corresponding color macro definition in front of the string.

while(1)
{
 SEGGER_RTT_SetTerminal(0);
 SEGGER_RTT_printf(0,RTT_CTRL_TEXT_RED"zhiguoxin666,SEGGER RTT Terminal 0!\r\n");
 SEGGER_RTT_SetTerminal(1);
 SEGGER_RTT_printf(0,RTT_CTRL_TEXT_GREEN"zhiguoxin666,SEGGER RTT Terminal 1!\r\n");
 SEGGER_RTT_SetTerminal(2);
 SEGGER_RTT_printf(0,RTT_CTRL_TEXT_BLUE"zhiguoxin666,SEGGER RTT Terminal 2!\r\n");
 delay_ms(1000);
}

Compile, link, and download, then observe the phenomenon:

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
Red
Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
Green
Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
Blue

4. Using printf Redirection

There are many places in the project where printf is used. If printf can be redirected to the RTT component directly, it would be very convenient. The method is to directly use the API provided by RTT to implement fputc.

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
Atomic Example

Redefine the fputc function

// Redefine fputc function 
int fputc(int ch, FILE *f)
{  
    SEGGER_RTT_PutChar(0, ch);
 return ch;
}

Replace the previous code:

while(1)
{
 printf("zhiguoxin666 ,printf SEGGER RTT Terminal!\r\n");
 delay_ms(1000);
}

Compile, link, and download

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

Conclusion: RTT and USART have their advantages and should be chosen based on different situations. If you encounter a display project without a reserved serial port for debugging printing information, you can use this method. Of course, there are many other methods for printing debugging, this is just one of them. If you have better methods, feel free to leave a comment!

Step-by-Step Guide to Using JLink as a Serial Debugging AssistantStep-by-Step Guide to Using JLink as a Serial Debugging Assistant

Step-by-Step Guide to Using JLink as a Serial Debugging Assistant

End

Recommended Articles Click on the blue text to jump

☞【Collect】How to Play the Common ESP82666Step-by-Step Guide to Using JLink as a Serial Debugging Assistant
【Inspiration】Four Years of College for an Ordinary Person
【Useful】PS2 Remote Control Car Based on STM32
【Useful】Step-by-Step Guide to Writing an Android APP
【Useful】Step-by-Step Guide to Writing a Microcontroller Pointer
【Useful】Step-by-Step Guide to Writing a Serial Debugging Assistant

Feel free toforward, comment, like, share! Thank you for your support!

Leave a Comment