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

Parents Reply Children
  • Andreas,

    Thankyou for thinking about this.  I have been using the UART example in /peripherals, not the ble one.  I am compiling the example using Segger Embedded Studio which I suspect defines the behaviour of printf in a non-helpful way (at least to me). 

    From the Segger manual:

    https://studio.segger.com/index.htm?https://studio.segger.com/ide_property_code_help.htm

    Sending all output to the SEGGER Embedded Studio virtual terminal

    You can send all output to the SEGGER Embedded Studio virtual terminal by supplying the following implementation of the__putchar function in your code:

    #include <debugio.h>
    
    int __putchar(int ch)
    {
      return debug_putchar(ch);
    }

    This hands off output of the character ch to the low-level debug output routine, debug_putchar.

    Whilst this is an adequate implementation of __putchar, it does consume stack space for an unnecessary nested call and associated register saving. A better way of achieving the same result is to define the low-level symbol for __putchar to be equivalent to the low-level symbol for debug_putchar. To do this, we need to instruct the linker to make the symbols equivalent.

    • Select the project node in the Project Explorer.
    • Display the Properties Window.
    • Enter the text __putchar=debug_putchar into the Linker > Linker Symbol Definitions property of the Linker Options group.

    Maybe this redirection has already been configured in the standard install of SES.  (I am using the most recent version of SES).  I will check and come back

    Any thoughts?

    Rob

  • it does consume stack space for an unnecessary nested call

    Does it?

    The optimiser may well be smart enough to avoid that ...

  • After much checking I founder these helper instructions (from the disassembler window)

    --- SEGGER_THUMB_Startup.s -- 393 --------------------------
    bx lr
    HELPER __getchar
    JUMPTO debug_getchar
    HELPER __putchar
    JUMPTO debug_putchar

    However removing the HELPER lines from thumb_crt0.s and crt0.s made no difference to printf nor putchar() which still direct to the debugger terminal.  Doh!!!

    Point taken on the invisible powers of the optimiser.

    Any ideas on what should I try next?

    Rob

  • Hi Rob.

    How did you exactly set up the project in Segger Embedded Studio, did you follow this tutorial?

    Best regards,

    Andreas

Related