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
  • Hi.

    I'm a bit unsure what you mean by this question.

    The UART example in SDK 12.3 (\examples\ble_peripheral\ble_app_uart) have a printf call in main(void) which prints "UART Start!" to UART.

    You can see it here:

    Can you please explain more details?

    Best regards,

    Andreas

  • 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

Reply
  • 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

Children
Related