Article count:10827 Read by:13690842

Featured Content
Account Entry

Useful Tips | Teach you how to turn JLink into a serial port debugging assistant

Latest update time:2022-06-21
    Reads:
I don't know how you print debugging information in MCU development. Most of them should use serial port debugging and printing. In most cases, serial port 1 is reserved for debugging and printing when making boards and writing codes.

But what if there is no reserved serial port in actual development? In fact, our downloader can be used for debugging and printing, but many friends don’t know this function. Today, let’s talk about how to use the debugger JLink to print information.

1. JLink Simulator Debugger

There are many downloaders, but I only use JLink, which is small and easy to carry. For MCU developers, the downloaders generally used are JLink and ST-Link. The functions of the two are similar. JLink is from SEEGER, and ST-Link is from ST, and only supports ST series chips. I only use JLink downloader for debugging because it is small in size and has only four wires. It is very convenient to use, YYDS!

J-Link OB ARM emulator debugger

2. Install JLink driver

Download link: https://www.segger.com/downloads/jlink/. After buying the JLink driver, the seller will generally provide the JLink driver program. After the driver is installed, you can download the debugging program.

https://www.segger.com/downloads/jlink/

Of course, if we want to use the RTT function of JLink ( Real Time Transfer, not the RT-Thread operating system ), we 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, you can install it directly. After the installation is complete, you will see the following content in your installation directory:

JLink Installation Directory

3. Transplant RTT

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

The specific directory after decompression is: D:\Software\SEGGER\JLink_V644b\Samples\RTT\SEGGER_RTT_V644b\RTT

Then copy this RTT folder to the project folder where we write the program

Then create a new group in the project and add the two files in the folder . RTT RTT .c

Of course, remember to add the header file path RTT

At this point, the transplantation is basically successful. Isn't it very simple? Just add the RTT source code to the project. There is no need to modify any other operations.

4. RTT Printout

Next you can print the output.

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

int main(void)

    HAL_Init();                     //初始化HAL库    
    Stm32_Clock_Init(336,8,2,7);   //设置时钟,168Mhz
 delay_init(168);                //初始化延时函数
 LED_Init();      //初始化LED 
 
 while(1)
 {
         SEGGER_RTT_printf(0,"zhiguoxin666\r\n");
 }
}

After compiling without errors, connect the downloader

Connect the hardware
Configured in SW mode

Then open the JLink installation directory JLinkRTTViewer.exe

According to the following configuration

Download the code to the microcontroller and you can see that it is printed perfectly.

5. Tips for using RTT

1. RTT buffer size

Sometimes we find that our information cannot be printed out completely, which may be because the buffer is not enough. The default buffer size is 1K bytes. If it is not enough, you can change it to a larger size.

Default 1024 bytes

2. Use of multiple virtual ports

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

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

Write 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, download, and observe the phenomenon:

Window 0
Window 1
Window 2

3. Change the printing character color

RTT supports character display in different colors.

When using it, just add the macro definition of the corresponding color 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, download, and observe the phenomenon:

red
green
blue

4. Use printf redirection

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

Atomic Routines

Redefine fputc function

//重定义fputc函数 
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, download

Conclusion : RTT and USRAT have their own advantages. You should choose according to different situations. If you encounter a display project that does not reserve a serial port for debugging and printing information, you can use this method. Of course, there are many other ways to print and debug. This is just one of them. If you have a better method, please leave a message in the comment area~



Add WeChat and reply " join group"

Invite you to join the technical exchange group!

Domestic chips|Automotive electronics|Internet of Things|New energy|Power supply|Industry|Embedded...

Reply to any content you want to search in the official , such as problem keywords, technical terms, bug codes, etc., and you can easily get relevant professional technical content feedback . Go and try it!


If you want to see our articles more often, you can go to our homepage, click the "three dots" in the upper right corner of the screen, and click "Set as Star".

Welcome to scan the QR code to follow us



Latest articles about

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号