This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to redirect printf to UART when using Segger Embedded Studio

The Segger Compiler is integrated with JLINK debugging software

Printf statements, when compiled, direct to the studio debugger terminal

How can I create code where they are directed to the UART?

Project options allow configuration of printf and scanf format but not its output

I am using the uart example in Nordic SDK 12.3. The only way so far that I have found to 'print' to the UART is using a custom print function:

void uart_printf(const char *fmt, ...)
{
char buf[200], *p;
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
for (p = buf; *p; ++p)
app_uart_put(*p);
va_end(ap);
}

This works but seems laborious;  what is the elegant way?

Many thanks

Rob

  • Hi Rob.

    The startup files are code which are run before main starts, you can call it a bootstrapping of the system, so you are correct in that they are "in effect the boot file".

    northernLight said:

    As a recap of the problem I am trying to solve:  I would like to redirect printf (or stop its current redirection) so that the compiled code operates without Segger Studio, just with a UART connection

    Does stdio.h define the standard output destination for putchar()?

     I don't think you should have to add anything to the code, you should only need the correct configuration in sdk_config.h and include the file retarget.c.

    Can you try with my configuration: sdk_config.h

    I think the vital thing is that you have defined this as 1:

    // <e> NRF_LOG_BACKEND_SERIAL_USES_UART - If enabled data is printed over UART
    //==========================================================
    #ifndef NRF_LOG_BACKEND_SERIAL_USES_UART
    #define NRF_LOG_BACKEND_SERIAL_USES_UART 1
    #endif

    And this as 0:

    // <e> NRF_LOG_BACKEND_SERIAL_USES_RTT - If enabled data is printed using RTT
    //==========================================================
    #ifndef NRF_LOG_BACKEND_SERIAL_USES_RTT
    #define NRF_LOG_BACKEND_SERIAL_USES_RTT 0
    #endif

    As you can see here from the configuration wizard:

    Hope this works for you.

    Best regards,

    Andreas

  • Andreas

    Thankyou for your time on this.

    I believe these only configure NRF_LOG instructions, not direct printf commands

    When using NRF_LOG_INFO commands with RTT set and UART unset I get data to the debug terminal in SES

    But with the same code with RTT unset and UART set I get no output to my serial monitor window

    :-(

    What is the function of retarget.c?  Does it affect the destination for standard output?

    I am using the sdk_config for the peripheral/uart example from SDK 12.3.  I'll try the file you propose.

    How do I incorporate flash_placement.xml in the compiler configuration?

    Many thanks

    Rob

     

  • Hi.

    northernLight said:

    Thankyou for your time on this.

    I believe these only configure NRF_LOG instructions, not direct printf commands

    When using NRF_LOG_INFO commands with RTT set and UART unset I get data to the debug terminal in SES

    But with the same code with RTT unset and UART set I get no output to my serial monitor window

    This is not what happend with the sdk_config.h file I gave you?

    northernLight said:
    What is the function of retarget.c?  Does it affect the destination for standard output?

     It is an implementation of functions in stdio.h.

    northernLight said:
    How do I incorporate flash_placement.xml in the compiler configuration?

     Follow step 7 on this guide.

    Best regards,

    Andreas

Related