Click on “Gua Gua Little Master” above, select “Pin/Star Official Account“
Useful Resources Delivered First Hand!

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!

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.

Of course, to use the RTT feature of JLink (Real Time Transfer, not RT-Thread operating system
), 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:

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

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

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

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


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

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");
}
}


Then open JLinkRTTViewer.exe in the JLink installation directory

Configure as follows

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

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.

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:

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:



3. Change Print Character Color
RTT supports different colors for character display.

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:



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.

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

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!



End
Recommended Articles Click on the blue text to jump

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