How do I "connect" printf with a specific UART?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How do I "connect" printf with a specific UART?

Jump to solution
3,275 Views
m4l490n
Contributor V

When creating a project, I can see the option for redirecting printf/scanf to UART:

m4l490n_1-1611090438199.png

 

But I can't see any option neither in the Peripherals Configurator nor in the Pins Configurator to "connect" the UART in my board with printf (UART3 in my case).

Is there any document or resource I can check out to know how to do this?

0 Kudos
1 Solution
3,220 Views
m4l490n
Contributor V

For anyone landing here, this was the solution in my particular case. It is just a matter of adding the following call to void BOARD_InitDebugConsole(void) since it is generated empty.

void BOARD_InitDebugConsole(void)
{
    /* The user initialization should be placed here */
    DbgConsole_Init((uint32_t)UART3, 115200, DEBUG_CONSOLE_DEVICE_TYPE_UART, CLOCK_GetFreq(kCLOCK_BusClk));
}

 

In my case is UART3 @ 115200 but you can substitute those for your particular needs. 

View solution in original post

7 Replies
3,221 Views
m4l490n
Contributor V

For anyone landing here, this was the solution in my particular case. It is just a matter of adding the following call to void BOARD_InitDebugConsole(void) since it is generated empty.

void BOARD_InitDebugConsole(void)
{
    /* The user initialization should be placed here */
    DbgConsole_Init((uint32_t)UART3, 115200, DEBUG_CONSOLE_DEVICE_TYPE_UART, CLOCK_GetFreq(kCLOCK_BusClk));
}

 

In my case is UART3 @ 115200 but you can substitute those for your particular needs. 

3,262 Views
bobpaddock
Senior Contributor III

If using the Config Tool:

In the Debug Console component, pick Custom near the top right.

Then select Serial port UART.

Then in UART configuration pick Peripheral, for me it was LPUART2.
It will be different for you.

Do not install a UART driver for this port as it will conflict with this Debug Console selection (I found the hard way).

Then over in the Pin Config Tool try to remap the pins to the UART that is being used.
I never did get this to work, I could never get the Pin Tool synced with the new Config Tool UART selection.  The system just kept arguing with me that there was a conflict between different UART pins.
My solution was to start with an empty project then  tediously assign all ~50 pins from their unused state.

If not using Config Tool, and using Newlib low level functions for _write() and _read() need to be written that map them to your actual hardware. printf() eventually reaches this low level. These are my debug_X functions.  Sorry I can't help with Redlib, never used it.

 

/** @(#)syscalls.c         <25-Sep-2013 12:17:43 bpaddock>
 *  \date Last Time-stamp: <15-Apr-2016 11:45:41 bob p>
 *
 *  \file syscalls.c
 *  \brief  Low level memory and I/O functions for newlib
 *
 *  Minor changes from Andrew Payne original.
 *  Copyright (c) 2012-2013 Andrew Payne <andy@payne.org>
 */

#include <sys/stat.h> /* struct stat and S_IFCHR*/

#include "compiler.h"

#include "debug.h"
#include "fault_soft.h"

int _close(   int const fd );
int _close(   int const fd ) { UNUSED( fd ); return -1; }

int _isatty( int const  fd );
int _isatty( int const  fd ) { UNUSED( fd ); return  1; }

int _open( const char *name, int const flags, int const mode );
int _open( const char *name, int const flags, int const mode ) { UNUSED( name ); UNUSED( flags ); UNUSED( mode ); return( -1 ); }

int _fstat( int const fd, struct stat *st );
int _fstat( int const fd, struct stat *st )
{
  UNUSED( fd );

  st->st_mode = S_IFCHR;                  /* Character device */

  return( 0 );
}

/*
 * Read "len" of char to "ptr" from file id "fd"
 * Return number of char read.
 */
int _write( int const fd, char const *p, int const len );
int _write( int const fd, char const *p, int const len )
{
    switch( fd )
      {
      case 1:        return debug_write(    p, len); /* stdout */
      case 2:        return debug_write_err(p, len); /* stderr */
      default:       return( -1 );
      }
}

/*
 * Read "len" of char to "ptr" from file id "fd"
 * Return number of char read.
 */
int _read( int const fd,  char *p, int const len );
int _read( int const fd,  char *p, int const len )
{
  UNUSED( fd );

  return debug_read(p, len);
}

/* ------------------------------------------------------------------------------------
 * _sbrk(len) -- Allocate space on the heap
 */
extern char __heap_start[];             /* Defined by the linker script */
static char *heap_end = __heap_start;

char *_sbrk(int const incr);
char *_sbrk(int const incr)
{
  heap_end += incr;                             /* \todo  check for collisions with the stack */

  return( (heap_end - incr) );
}

/* Signal handler (fault): */
void _kill( int const pid, int const sig )  __attribute__((noreturn));
void _kill( int const pid, int const sig )
{
  UNUSED( pid );
  UNUSED( sig );

  fault_soft_handler( 0xFEUL );
}

 

 

3,252 Views
m4l490n
Contributor V

@bobpaddock Thanks for helping! I appreciate it.

I'm using the config tool but I think I'll go with the non-config tool solution because I can't see any Debug Console Component. Perhaps you could please add a screenshot of where to find it and what it looks like.

0 Kudos
3,249 Views
bobpaddock
Senior Contributor III

In the Config Toll, at least the one I'm look at in 11.3, on the left under the Components tab is Utilities.

DebugConsole is found there.

 

0 Kudos
3,244 Views
m4l490n
Contributor V

These are the only components I get when selecting the plus on the "Peripheral drivers (Device specific)" on the "Components" tab in the "Peripherals Config Tool".

m4l490n_0-1611178170267.png

I don't think it would be on the pin config or the clock config. Maybe is the MCU I'm using the reason I don't see it.

0 Kudos
3,267 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello,

Please tell me which chip do you used? 

 

Regards,

Alice

0 Kudos
3,254 Views
m4l490n
Contributor V

@Alice_Yang I'm using a MK10DN512VLQ10

0 Kudos