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,

    Many thanks. I am unsure what to do with the flash_placement.xml.

    I included the *.s files in the build and excluded the old ones:

    But I get the following build errors:

    Building ‘twi_master_using_app_twi_pca10028_sdk12’ from solution ‘twi_master_using_app_twi_pca10028_sdk12’ in configuration ‘nrf51422_xxac’
    1> Assembling ‘ses_nrf51_startup.s’
    2> Assembling ‘thumb_crt0.s’
    1> Linking twi_master_using_app_twi_pca10028_sdk12.elf
    1>
    1> Diagnostics:
    1> remark: increasing alignment of section .vectors from 1 to 2 because it contains executable instructions
    1> error: undefined symbol: __SEGGER_init_copy
    1> error: undefined symbol: __SEGGER_init_zero
    1> error: undefined symbol: __SRAM_segment_end__
    1> error: undefined symbol: __bss_end__
    1> error: undefined symbol: __bss_start__
    1> error: undefined symbol: __ctors_load_start__
    1> error: undefined symbol: __data_end__
    1> error: undefined symbol: __data_load_start__
    1> error: undefined symbol: __data_start__
    1> error: undefined symbol: __dtors_load_start__
    1> error: undefined symbol: __fast_end__
    1> error: undefined symbol: __fast_load_start__
    1> error: undefined symbol: __fast_start__
    1> error: undefined symbol: __rodata_end__
    1> error: undefined symbol: __rodata_load_start__
    1> error: undefined symbol: __rodata_start__
    1> error: undefined symbol: __stack_process_end__
    1> error: undefined symbol: __stack_process_start__
    1> error: undefined symbol: __text_end__
    1> error: undefined symbol: __text_load_start__
    1> error: undefined symbol: __text_start__
    Build failed

    It seems I should keep the Segger_thumb?

    But then I get errors:

    Building ‘twi_master_using_app_twi_pca10028_sdk12’ from solution ‘twi_master_using_app_twi_pca10028_sdk12’ in configuration ‘nrf51422_xxac’
    2> Assembling ‘SEGGER_THUMB_Startup.s’
    1> Linking twi_master_using_app_twi_pca10028_sdk12.elf
    1>
    1> Diagnostics:
    1> error: multiply defined global symbol _start
    1> error: defined by thumb_crt0.o
    1> error: and by SEGGER_THUMB_Startup.o
    1> remark: increasing alignment of section .vectors from 1 to 2 because it contains executable instructions
    1> error: undefined symbol: __SRAM_segment_end__
    Build failed

    Rob

  • As a check I reverted to the original files and the build completed

    Thanks again for helping with this. 

    For my education what is the broad purpose of these start-up files?  They seem to be the first instructions that the processor executes on power-up. 

    Are they in effect the boot file?

    Rob

  • 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()?

    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

     

Related