One of the most most common debugging techniques is “logging”, or creating a history of what happens while code runs. Logging can be especially difficult in embedded systems, which often lack a natural way to log events, like a console. Commonly UARTs are used to log messages via a serial port, but this approach can consume significant run-time that may undermine the timing constraints of the software. It also requires the use of additional pins to support development.
I’ve found it especially difficult to debug applications using Nordic’s Bluetooth Low Energy products (nRF family). Fortunately, due to some helpful hints by fellow Nordic developers (Thanks Jeremy) and great products from Segger, I’ve got a solution that suits my needs fairly well. Segger’s Real-Time Terminal is a perfect solution to the problem. I’ve taken the example’s provided by Segger and made a package from them (easier to configure on multiple projects). I’ve also added some wrapper functions that provide additional data I find helpful when debugging.
The following explains how to setup a logger for the nRF series of chips, but assumes that:
- You are using a Segger J-Link (I’m using the J-Link CortexM)
- You are using Keil MDK v5 or later,which uses Software Packs.
This guide is broken into three distinct phases:
- Installing the Logging Package and updating the Keil’s IDE to easily connect to Segger’s Log Viewer
- Configuring a project to use the Logging functions
- A quick demo of actual use
Installing the Logging Package and updating the Keil
These steps should only need to be performed once.
- Install the latest version of Segger’s J-Link: https://www.segger.com/jlink-software.html?step=1&file=JLink_496a.
- Install the Software Package I created for Logging
- Download Siever.Segger_RTT_Logger.1.0.1.pack. (It works with any system that Segger’s RTT supports, not just Nordic)
- Double click on the package. It should automatically install via Keil’s Package Installer.
- Update Keil’s Tools menu to allow easy access to the Log Viewer (which is the window you will use to view log messages).
- Open Keil.
- Select Tools → Customize Tools Menu... from the menu:
- Add a new entry for the Log Viewer. When done it should look something like:
There are a few things to notice here:
- I typed Log Viewer in the first available slot. This will be the name of the new item on the Tools menu.
- The Command is the JLinkRTTViewer.exe. Verify that the path is correct for your installation.
- Note that Run Independent is checked.
The Tools menu should now have a new item that will be used to launcher the Log Viewer.
Configuring a project to use the Logging functions
You will need to do this for each project on which you want to use these logging functions.
- Add the Pack to the Run-Time Environment. Select Project → Manage → Run-Time Environment...:
- Ensure that the package will be included at Run-Time. Expand it and make sure it is selected:
- The package will need to be enabled via a define. Add the define to the project’s Preprocessor Symbols.
Demo of Use
In order to use the logging functions you will need to add calls to the logging functions to your code. You can use primitive functions provided by Segger, but I choose to use my own wrappers. In order to use my wrappers you will need to include the header file:
and insert calls into your code. I’ve provided three functions that each have printf semantics (and each call Segger’s SEGGER_RTT_vprintf()). My functions are:
- Used for logging run-time errors (shown in red)
- Used for logging warnings (shown in yellow)
- Used for logging general information (shown in white)
Here are some examples in code:
The Log Viewer you will ask how it should connect to a debugger session. When being run with an existing debugging session just indicate it should connect to the debugging session:
Here’s an example of the log generated by the three examples shown above:
- The different warning styles are color coded.
- The log macros I’ve created insert the file name and line number. This does consume additional flash memory, so don’t get too carried away with log messages.
- Currently I have all log messages going to the tab for Terminal 0. It would be easy to update the approach to include different messages to different terminal tabs for different modules.
This work is provided as-is with no warranty or guarantee of any sort. I’m new to Segger’s RTT and haven’t done any review of performance issues. I’m also new to CMSIS Packages and haven’t verified that I’ve followed all appropriate package conventions. At this point this is an alpha version, but any input or suggestions are welcome. The log macros were made for my needs when I developed them and may not suite other’s needs. The formatting used for them could certainly be improved.
The package includes unmodified code provided by Segger at: http://download.segger.com/J-Link/RTT/RTT_Implementation_141217.zip. Code by Segger includes a copyright notice, but they have given me permission to share the package.