SEGGER RTT (2)
SEGGER RTT (2)
Introduction
The article introduce how to use SEGGER-RTT in WIN32. I guess you will be amazing how east it is to setup RTT.
I did my experiment over my NRF51822 board with a on-board JLINK. If you don’t have a JLINK, I recommend to invest one. SEGGER also provides inexpensive JLINK-EDU at 2400NTD (roughly 80USD). I think that’s quite affortable and worth of buying one.
This article is written under WIN32 because SEGGER only provides RTT viewer utility in WIN32. I will show how to use RTT in Linux/ Mac in SEGGER RTT (3), which is the final form of logging service (I’m 100% satisfied with).
RTT Source Code
In SEGGER driver installation folder, you could find a zipped file
/c/Program Files (x86)/SEGGER/JLink_V50i/Samples/SEGGER_RTT_V510i.zip
Uncompressed the file, and copy following files to your MCU project
RTT/SEGGER_RTT.c
RTT/SEGGER_RTT.h
RTT/SEGGER_RTT_Conf.h
RTT/SEGGER_RTT_printf.c
In NORDIC’s SDK (I’m using nRF51_SDK_10.0.0_dc26b5e), the RTT library is also included.
$(NRF51_SDK)/components/drivers_ext/segger_rtt/
Compile RTT Source Code
The RTT source code is quite easy to compile. I’ve compile them against GCC & KEIL-C. What you need to do is adding these files to your project and make compilation pass.
In the library, SEGGER RTT provided a simplified debugging function.
int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...);
Invoke the function in MCU’s application:
unsigned int buf_idx = 0;
while(1) {
unsigned int i;
for(i=0; i<256; i++) {
SEGGER_RTT_printf(buf_idx, "Hello World %d\n", i);
nrf_delay_ms(100);
}
}
Host-Side Application
Open RTT Viewer in Windows. RTT viewer needs JLINK connection to work, so it needs to know following parameters
- Connection type, typically USB
- SWD speed
- MCU type
Then the J-Link RTT Viewer appeared with desired information !
JLINK TELNET SERVER CAPABILITY
JLINK.exe created a TELNET server at port 19021 which dumps the RTT message. The capability enables us to embed RTT in other PC application. Here’s short introduction to use this capability and I will extend this idea in next article.
Step.1 Run JLink in Command Line:
Run following command in terminal window. I put following command in Makefile. These parameters are the same one fed to JLink RTT Viewer. It will show a JLink console below.
"/c/Program Files (x86)/SEGGER/JLink_V510i/JLink.exe" -device nrf51822 -if swd -speed 4000 -autoconnect 1
Step.2 Use TELNET client to connect JLINK-RTT
I’m using Windows 10 which doesn’t have telnet client by default. Run “cmd” window in Administrator and execute following command.
dism /online /Enable-Feature /FeatureName:TelnetClient
Then run following command in command window, which generates the same result as JLINK RTT Viewer:
telnet localhost:19021
Step.3 Use Python Telnet Library
To embed RTT within another application, telnet solution in (2) is not optimal. Python provides telnet library which could do exactly the same thing as (2). In code below, we could do further post-processing in PC-side. For example: log to file, or draw some real-time plotting.
import sys
import telnetlib
import os
def main():
os.system('taskkill -im jlink.exe')
os.system(r'start "" "c:\Program Files (x86)\SEGGER\JLink_V510i\JLink.exe" -device nrf51822 -if swd -speed 4000 -autoconnect 1')
rtt = telnetlib.Telnet('localhost', 19021)
while(True):
data = rtt.read_until('\n')
print(data), # one may do further processing here
return 0
if __name__ == '__main__':
sys.exit(main())
Reference
[1]: SEGGER’s RTT introduction website, https://www.segger.com/jlink-rtt.html
[2]: Debugging with Real Time Terminal, https://devzone.nordicsemi.com/tutorials/6/
[3]: Python’s telnetlib, https://docs.python.org/2/library/telnetlib.html
[4]: SEGGER-RTT (1), http://lihgong.blogspot.com/2016/04/segger-rtt-1.html
留言