This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

What is the relationship between UART and printf within retarget?

HI.

I'm trying to understand the relationship between UART and printf within retarget.

as I understand, retarget supports to implement low level function fputc, if I want to use printf().

if I am right, I can't still understand the relationship between printf and UART_STDOUT.

especially, the below example is what I want to share you.

this is the hello.c example code.

//Hello.c

#ifdef CORTEX_M3
#include "CMSDK_CM3.h"
#else
#include "CMSDK_CM4.h"
#endif

#include <stdio.h>
#include "uart_stdout.h"

int main(void)
{ 
  // UART init
  UartStdOutInit();

  printf("Hello world\n");

  printf("** TEST PASSED **\n");

  // End simulation
  UartEndSimulation();

  return 0;
}

and retarget.c

#include <stdio.h>
#include <time.h>
#include <rt_misc.h>
#pragma import(__use_no_semihosting_swi)

extern unsigned char UartGetc(void);
extern unsigned char UartPutc(unsigned char my_ch);
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;


int fputc(int ch, FILE *f) {
  return (UartPutc(ch));
}

int fgetc(FILE *f) {
  return (UartPutc(UartGetc()));
}

int ferror(FILE *f) {
  /* Your implementation of ferror */
  return EOF;
}


void _ttywrch(int ch) {
  UartPutc (ch);
}


void _sys_exit(int return_code) {
label:  goto label;  /* endless loop */
}

I can't understand how does printf() work? Is printf() working as UART function as a fake ? or really works as UART?

Is printf() connected UART function? I can't understand the relationship between UART function of cortex-m3 and printf().

Does printf() differently work such as printf() of ANCI C?

Would you please anyone help me to understand this question to clarify?

Thanks in advance

  • The library printf() is the ANSI one. 

    ANSI printf() takes your string, formats it and prints it "somewhere".  There is then a lot of platform specific code that makes the somewhere happen, ie on a PC taking the string, converting it to characters and displaying it in your console.

    In an embedded device the "somewhere" might be a screen, some LEDs, a Bluetooth socket or a UART.  The retargeting code allows you to take the generic printf() and implement some output that makes sense on your platform. 

    One, fairly common, way to do this is to output the string, one character at a time, via a serial port.  This is what that retarget.c is doing.