nRF5 SDK v11.0.0
Experimental: UART/RTT logging

The UART/RTT logging library provides functions to output logging information over SEGGER's Real Time Transfer (RTT), UART, or raw UART. The library can be used both to output messages and to read input.

You can decide if you want to output the logging information over RTT, UART, or raw UART, or if you do not want to output it at all. If you choose to not output any information, all logging macros can be left in the code without any cost; they will just be ignored. To enable logging over RTT, define NRF_LOG_USES_RTT=1 in the project. To enable logging over UART, define NRF_LOG_USES_UART=1. To enable logging over raw UART, define NRF_LOG_USES_RAW_UART=1. You can specify only one of these defines, not several at the same time. To disable logging, do not specify either one.

Available protocols

The following protocols are available to output logging information:

RTT

SEGGER's Real Time Transfer (RTT) is a proprietary technology for bidirectional communication that supports J-Link devices and ARM-based microcontrollers. The advantage of using RTT is that it is very efficient; logging only updates a buffer in RAM. RTT does not rely on any other peripherals than the J-Link debugging interface.

UART

UART used to be the default way of outputting information in most SDK examples. It is quick and power-efficient, but it requires dedicated use of the UART peripheral for logging.

Raw UART

Unlike UART, raw UART uses blocking calls. Therefore, it works in all situations, while regular UART stops working if interrupts are turned off (on purpose or because of a failure). Raw UART is slower and less power-efficient than regular UART.

Initializing the library

You must initialize the library before any logging can be done. To do so, use the NRF_LOG_INIT macro. This macro calls the init function for either RTT, UART, or raw UART, depending on which is used.

The following code example shows how to initialize the library:

uint32_t err_code;
// Initialize module
err_code = NRF_LOG_INIT();
if (err_code != NRF_SUCCESS)
{
// Module initialization failed. Take corrective action.
}

Printing log information

To print log messages, use the provided macros. For example:

// Logging a string.
NRF_LOG("Some text\r\n");
// Logging a string and a HEX number with printf.
NRF_LOG_PRINTF("Function returned error code %X\r\n", some_error_code);
// Logging a string and a HEX number with NRF_LOG and NRF_LOG_HEX.
NRF_LOG("Function returned error code ");
NRF_LOG_HEX(some_error_code);
NRF_LOG("\r\n");
// Logging a character.
NRF_LOG_HEX_CHAR(some_char);

NRF_LOG_PRINTF requires more processor time than the other logging functions. Therefore, applications that require logging but need it to interfere as little as possible with the execution, should avoid using printf.

Note that the string being printed should end with "\r\n" for neat formatting on the terminal.

You can append _DEBUG to any of the macro names. The debug macros will check if DEBUG is defined before printing any output. If DEBUG is undefined, the log call is removed.

Appending _ERROR to any of the macro names will print the message to the error stream rather than the output stream.

Reading input

You can use the library to read input from RTT or UART. Always check for available input in the input buffer before reading it. For example:

// Read input if there is any available.
{
uint8_t read_char = NRF_LOG_READ_INPUT();
}

Viewing the output

How to view the output depends on the protocol that is used:

RTT

RTT output can be viewed in the J-Link RTT Viewer, which is available from SEGGER. The viewer is also included in the nRF Tools.

To read or write messages over RTT, connect an nRF5 development board via USB and run the J-Link RTT Viewer. Select the correct target device (for example, nRF51422_xxAC or nRF52) and the target interface "SWD".

segger_rtt_viewer.png
Settings in J-Link RTT Viewer

UART

To view UART output, start a terminal emulator like PuTTY and connect to the used COM port with the following UART settings:

To determine the correct COM port, look for a name similar to "JLink CDC UART Port (COM4)" in the Windows device manager.

putty_uart.png
Settings in PuTTY

Raw UART

To view raw UART output, start a terminal emulator like PuTTY and connect to the used COM port with the following UART settings:

To determine the correct COM port, look for a name similar to "JLink CDC UART Port (COM4)" in the Windows device manager.

putty_raw_uart.png
Settings in PuTTY

Documentation feedback | Developer Zone | Updated