RTT APIs and Configurations

API Functions

APIUsage
SEGGER_RTT_ConfigDownBuffer()Configure or add a down buffer by specifying its name, size and flags
SEGGER_RTT_ConfigUpBuffer()Configure or add an up buffer by specifying its name, size and flags
SEGGER_RTT_GetKey()Reads one character from SEGGER RTT buffer 0. Host has previously stored data there
SEGGER_RTT_HasKey()hecks if at least one character for reading is available in SEGGER RTT buffer
SEGGER_RTT_Init()Initializes the RTT Control Block
SEGGER_RTT_printf()Send a formatted string to the host
SEGGER_RTT_Read()Read characters from any RTT down channel which have been previously stored by the host
SEGGER_RTT_SetTerminal()Set the "virtual" terminal to send following data on channel 0
SEGGER_RTT_TerminalOut()Send one string to a specific "virtual" terminal
SEGGER_RTT_WaitKey()Waits until at least one character is available in SEGGER RTT buffer 0. Once a character is available, it is read and returned
SEGGER_RTT_Write()Send data to the host on an RTT channel
SEGGER_RTT_WriteString()Write a 0-terminated string to an up channel via RTT
SEGGER_RTT_GetAvailWriteSpace()Returns the number of bytes available in the ring buffer

SEGGER_RTT_ConfigDownBuffer()

Configure or add a down buffer by specifying its name, size and flags.

Syntax

int SEGGER_RTT_ConfigDownBuffer (unsigned BufferIndex, const char* sName, char* pBuffer, int BufferSize, int Flags);

ParameterMeaning
BufferIndexIndex of the buffer to configure.
Must be lower than SEGGER_RTT_MAX_NUM_DOWN_CHANNELS.
sNamePointer to a 0-terminated string to be displayed as the name of the channel.
pBufferPointer to a buffer used by the channel.
BufferSizeSize of the buffer in Bytes.
FlagsFlags of the channel (blocking or non-blocking).
Flags[1:0]: RTT operating mode.
Flags[23:2]: Reserved for future use.
Flags[31:24]: Used for validity check, must be zero.
Return ValueMeaning
>= 0OK
< 0Error

Example

// Configure down buffer 1
SEGGER_RTT_ConfigDownBuffer(1, "DataIn", &abDataIn[0], sizeof(abDataIn),
                             SEGGER_RTT_MODE_NO_BLOCK_SKIP);

Once a buffer is configured only the flags of the buffer should be changed.

SEGGER_RTT_ConfigUpBuffer()

Configure or add an up buffer by specifying its name, size and flags.

Syntax

int SEGGER_RTT_ConfigUpBuffer (unsigned BufferIndex, const char* sName, char* pBuffer, int BufferSize, int Flags);

ParameterMeaning
BufferIndexIndex of the buffer to configure.
Must be lower than SEGGER_RTT_MAX_NUM_UP_CHANNELS.
sNamePointer to a 0-terminated string to be displayed as the name of the channel.
pBufferPointer to a buffer used by the channel.
BufferSizeSize of the buffer in Bytes.
FlagsFlags of the channel (blocking or non-blocking).
Flags[1:0]: RTT operating mode.
Flags[23:2]: Reserved for future use.
Flags[31:24]: Used for validity check, must be zero.
Return ValueMeaning
>= 0OK
< 0Error

Example

// Configure up buffer 1 to work in blocking mode
SEGGER_RTT_ConfigUpBuffer(1, "DataOut", &abDataOut[0], sizeof(abDataOut),
                           SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL);

Once a buffer is configured only the flags of the buffer should be changed.

SEGGER_RTT_GetKey()

Reads one character from SEGGER RTT buffer 0. Host has previously stored data there.

Syntax

int SEGGER_RTT_GetKey (void);

Return ValueMeaning
>= 0Character which has been read (0 - 255)
< 0No character available (empty buffer)

Example

int c;
c = SEGGER_RTT_GetKey();
if (c == 'q') {
  exit();
}

SEGGER_RTT_HasKey()

Checks if at least one character for reading is available in SEGGER RTT buffer.

Syntax

int SEGGER_RTT_HasKey (void);

Return ValueMeaning
1At least one character is available in the buffer
0No characters are available to be read

Example

if (SEGGER_RTT_HasKey()) {
  int c = SEGGER_RTT_GetKey();
}

SEGGER_RTT_Init()

Initializes the RTT Control Block.

Syntax

void SEGGER_RTT_Init (void);

Should be used in RAM targets, at start of the application.

SEGGER_RTT_printf()

Send a formatted string to the host.

Syntax

int SEGGER_RTT_printf (unsigned BufferIndex, const char * sFormat, ...)

ParameterMeaning
BufferIndexIndex of the up channel to sent the string to
sFormatPointer to format string, followed by arguments for conversion
Return ValueMeaning
>= 0Number of bytes which have been sent.
< 0Error

Example

SEGGER_RTT_printf(0, "SEGGER RTT Sample. Uptime: %.10dms.", /*OS_Time*/ 890912);
// Formatted output on channel 0: SEGGER RTT Sample. Uptime: 890912ms.

Additional Information

  1. Conversion specifications have following syntax:

    %[flags][FieldWidth][.Precision]ConversionSpecifier%

  2. Supported flags:

    | Flag | Meaning | |:---|:---| | - | Left justify within the field width | | + | Always print sign extension for signed conversions | | 0 | Pad with 0 instead of spaces. Ignored when using '-'-flag or precision |

  3. Supported conversion specifiers:

    | Conversion specifier | Meaning | |:---|:---| | c | Print the argument as one char | | d | Print the argument as a signed integer | | u | Print the argument as an unsigned integer | | x | Print the argument as an hexadecimal integer | | s | Print the string pointed to by the argument | | p | Print the argument as an 8-digit hexadecimal integer. (Argument shall be a pointer to void) |

SEGGER_RTT_Read()

Read characters from any RTT down channel which have been previously stored by the host.

Syntax

unsigned SEGGER_RTT_Read (unsigned BufferIndex, char* pBuffer, unsigned BufferSize);

ParameterMeaning
BufferIndexIndex of the down channel to read from
pBufferPointer to a character buffer to store the read characters
BufferSizeNumber of bytes available in the buffer
Return ValueMeaning
>= 0Number of bytes that have been read.
char acIn[4];
unsigned NumBytes = sizeof(acIn);
NumBytes = SEGGER_RTT_Read(0, &acIn[0], NumBytes);
if (NumBytes) {
  AnalyzeInput(acIn);
}

SEGGER_RTT_SetTerminal()

Set the "virtual" terminal to send following data on channel 0.

Syntax

void SEGGER_RTT_SetTerminal(char TerminalId);

ParameterMeaning
TerminalIdId of the virtual terminal (0-9)

Example

// Send a string to terminal 1 which is used as error out.
SEGGER_RTT_SetTerminal(1); // Select terminal 1
SEGGER_RTT_WriteString(0, "ERROR: Buffer overflow");
SEGGER_RTT_SetTerminal(0); // Reset to standard terminal

All following data which is sent via channel 0 will be printed on the set terminal until the next change

SEGGER_RTT_TerminalOut()

Send one string to a specific "virtual" terminal.

Syntax

int SEGGER_RTT_TerminalOut (char TerminalID, const char* s);

ParameterMeaning
TerminalIdId of the virtual terminal (0-9)
sPointer to 0-terminated string to be sent
Return ValueMeaning
>= 0Number of bytes sent to the terminal
< 0Error

Example

// Sent a string to terminal 1 without changing the standard terminal.
SEGGER_RTT_TerminalOut(1, "ERROR: Buffer overflow.");

SEGGER_RTT_TerminalOut does not affect following data which is sent via channel 0.

SEGGER_RTT_Write()

Send data to the host on an RTT channel.

Syntax

unsigned SEGGER_RTT_Write (unsigned BufferIndex, const char* pBuffer, unsigned NumBytes);

ParameterMeaning
BufferIndexIndex of the up channel to send data to
pBufferPointer to data to be sent
NumBytesNumber of bytes to send
Return ValueMeaning
>= 0Number of bytes which have been sent
< 0Error

With SEGGER_RTT_Write() all kinds of data, not only printable ones can be sent.

SEGGER_RTT_WaitKey()

Waits until at least one character is available in SEGGER RTT buffer 0. Once a character is available, it is read and returned.

Syntax

int SEGGER_RTT_WaitKey (void);

Return ValueMeaning
>= 0Character which has been read (0 - 255)

Example

int c = 0;
do {
  c = SEGGER_RTT_WaitKey();
} while (c != 'c');

SEGGER_RTT_WriteString()

Write a 0-terminated string to an up channel via RTT.

Syntax

unsigned SEGGER_RTT_WriteSting (unsigned BufferIndex, const char* s);

ParameterMeaning
BufferIndexIndex of the up channel to send string to
sPointer to 0-terminated string to be sent
Return ValueMeaning
>= 0Number of bytes which have been sent

Example

SEGGER_RTT_WriteString(0, "Hello World from your target.\n");

SEGGER_RTT_GetAvailWriteSpace()

Returns the number of bytes available in the ring buffer.

Syntax

unsigned SEGGER_RTT_GetAvailWriteSpace (unsigned BufferIndex);

ParameterMeaning
BufferIndexIndex of the up channel that should be checked for space
Return ValueMeaning
>= 0Number of bytes that are free in the selected up buffer

Example

unsigned NumBytesFree;
NumBytesFree = SEGGER_RTT_GetAvailWriteSpace(0);

RTT Modes

ModeExplanation
Background modeIt is the fastest mode with a transfer speed of up to ~2MB/s
Legacy background modeLegacy version of Background mode, which does not come with handling on probe firmware side, leading to lower transfer speeds
Stop modePseudo RTT mode. CPU is halted to read data. Introduced for CPUs that do not support Background-Access

RTT Configuration

Define / RoutineMeaning
SEGGER_RTT_MAX_NUM_DOWN_BUFFERSMaximum number of down (to target) channels
SEGGER_RTT_MAX_NUM_UP_BUFFERSMaximum number of up (to host) channels
BUFFER_SIZE_DOWNSize of the buffer for default down channel 0
BUFFER_SIZE_UPSize of the buffer for default up channel 0
SEGGER_RTT_PRINT_BUFFER_SIZESize of the buffer for SEGGER_RTT_printf to bulk-send chars
SEGGER_RTT_LOCK()Locking routine to prevent interrupts and task switches from within an RTT operation
SEGGER_RTT_UNLOCK()Unlocking routine to allow interrupts and task switches after an RTT operation
SEGGER_RTT_IN_RAMIndicate the whole application is in RAM to prevent falsely identifying the RTT Control Block in the init segment by defining as 1

Channel buffer configuration

DefineMeaning
SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULLA call to a writing function will block, if the up buffer is full
SEGGER_RTT_MODE_NO_BLOCK_SKIPIf the up buffer has not enough space to hold all of the incoming data, nothing is written to the buffer
SEGGER_RTT_MODE_NO_BLOCK_TRIMIf the up buffer has not enough space to hold all of the incoming data, the available space is filled up with the incoming data while discarding any excess data
  • SEGGER_RTT_TerminalOut() ensures that implicit terminal switching commands are always sent out, even while using the non-blocking modes.
  • The buffer configuration only effects the target device buffers, not the host buffers.

Color control sequences

ConfigurationMeaning
RTT_CTRL_RESETReset the text color and background color
RTT_CTRLTEXT*Set the text color to one of the following colors
BLACK
RED
GREEN
YELLOW
BLUE
MAGENTA
CYAN
WHITE
BRIGHT_BLACK
BRIGHT_RED
BRIGHT_GREEN
BRIGHT_YELLOW
BRIGHT_BLUE
BRIGHT_MAGENTA
BRIGHT_CYAN
BRIGHT_WHITE
RTT_CTRLBG*Set the background color to one of the following colorss
BLACK
RED
GREEN
YELLOW
BLUE
MAGENTA
CYAN
WHITE
BRIGHT_BLACK
BRIGHT_RED
BRIGHT_GREEN
BRIGHT_YELLOW
BRIGHT_BLUE
BRIGHT_MAGENTA
BRIGHT_CYAN
BRIGHT_WHITE

ย