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

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

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

跳至解决方案
6,605 次查看
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 项奖励
回复
1 解答
6,550 次查看
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. 

在原帖中查看解决方案

8 回复数
6,551 次查看
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. 

1,722 次查看
ve3id
Contributor III

I have been trying to follow the procedures outlined here to do the same thing, except for UART4 which uses convenient pins PTC14 and PTC15 on the FRDM-K64F boards. I also went in to PORTC_PCR14 and 15 and set them to 0x0303 to enable the UART to those pins with pullups.

I am trying to do this after getting deep into an already-configured project and don't want to to back and create a new project using the config tool to redirect printf and putchar to the serial port. This is now to use for outputting debugging strings, but later I may want to use it as a utility serial output.

The software runs (it previously outputted correctly to the console window) but noting is seen on UART4.

Have I missed something?

Any ideas how I can do this with an existing project?

cheers,

Nigel

 

 

 

0 项奖励
回复
6,592 次查看
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 );
}

 

 

6,582 次查看
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 项奖励
回复
6,579 次查看
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 项奖励
回复
6,574 次查看
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 项奖励
回复
6,597 次查看
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello,

Please tell me which chip do you used? 

 

Regards,

Alice

0 项奖励
回复
6,584 次查看
m4l490n
Contributor V

@Alice_Yang I'm using a MK10DN512VLQ10

0 项奖励
回复