Skip to content

Segger Real time transfer

Segger RTT is a method to use the Segger J-Link to transfer a stream of data over a debugger session. It makes it possible to use printf and similar to output data without using any extra connection, except SWD

More information about Segger RTT is available from Segger

Enable RTT output

To enable RTT, add MIRA_IODEF_RTT(0) to the MIRA_IODEFS() table:

MIRA_IODEFS(
    MIRA_IODEF_NONE,
    MIRA_IODEF_RTT(0),
    MIRA_IODEF_NONE
)

In mira_setup() initialize RTT:

void mira_setup(void) {
    mira_rtt_init();
    /* ... */
}

Blocking mode

RTT is non-blocking by default. Data is dropped if the buffer is full. This may occur if no client is reading, or if the bandwidth is too low for the amount of data.

In some cases, avoiding the loss of data is more important than preserving system performance. Blocking mode is available for that purpose.

For more information, see mira_rtt_set_blocking()

Data input

It is possible to read data from RTT. To read from RTT, use active polling.

To read data, start a process that polls the interface:

PROCESS_THREAD(rtt_reader, ev, data) {
    uint8_t buffer[16];
    int len;

    PROCESS_BEGIN();
    PROCESS_PAUSE();
    for (;;) {
        /* Yield to give other processes access to the CPU */
        PROCESS_PAUSE();

        do {
            /*
             * Continously poll while data is available, to clear te buffer as
             * fast as possible
             */
            len = mira_rtt_read(-1, 0, buffer, sizeof(buffer));
            if (len > 0) {
                /*
                 * Handle buffer here...
                 */
            }
        } while (len > 0);
    }
    PROCESS_END()
}

Defines

Name Value Description
MIRA_IODEF_RTT(_ID) { mira_rtt_read, mira_rtt_write, (_ID) } MIRA_IODEF handler for RTT.

To use RTT as output for mira iodefs, useMIRA_IODEF_RTT(0)MIRA_IODEFS()

Functions

mira_rtt_init

mira_status_t mira_rtt_init(
    void);

Initialize RTT interface.

Return

status of operation

mira_rtt_set_blocking

mira_status_t mira_rtt_set_blocking(
    uint8_t       buf_idx,
    mira_bool_t   blocking);

Set blocking mode.

In blocking mode, mira_rtt_write() will block until enough space is available in output buffer for output.

Parameters

Parameter Description
buf_idx Index of buffer. Only buf_idx=0 is supported.
blocking 0 for non-blocking, non-zero for blocking.

Return

Status of operation.

mira_rtt_read

int mira_rtt_read(
    int           fd,
    uint8_t       buf_idx,
    void*         ptr,
    int           len);

Read data from RTT.

Writes raw data to output buffer, given buf_idx.

Parameters

Parameter Description
fd Unused. Available to fit MIRA_IODEF.
buf_idx Index of buffer. Only buf_idx=0 is supported.
ptr Pointer to where to store input.
len Length of input buffer.

Return

Length of data read and written to ptr. Negative on error.

mira_rtt_write

int mira_rtt_write(
    int           fd,
    uint8_t       buf_idx,
    const void*   ptr,
    int           len);

Write data to RTT.

This method is intended to be used as a MIRA_IODEF output handler.

Writes raw data to output buffer, given buf_idx.

Parameters

Parameter Description
fd Unused. Available to fit MIRA_IODEF.
buf_idx Index of buffer. Only buf_idx=0 is supported.
ptr Pointer to where output is stored.
len Length of output buffer.

Return

Length of data written. Negative on error.