This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How exactly does the printf to UART work?

Hello, I migrated codes that I believed to be relevant to the UART output to a new project, namely the uart_init function and others, and it worked in main.c, which is where I put the "uart_init" function.

However, it does not work elsewhere i.e. if I use the printf function, the whole program goes into hard fault handler. Also, when I try to "go to definition" printf, I get nothing.

Why is that?

Thank you for looking into the problem.

The code: http://textuploader.com/5nqxs

The problem is that I can get correct results as long as I use printf in the main.c not anywhere else, even by reference.

Parents
  • Hi Karin,

    Printf() is a standard function, and relies on "hook functions" for your selected transport layer (in this case, UART). If you are using Keil, the hook functions called from printf is "fputc" and "fgetc".

    If you do not have these functions implemented, the printf() command does not know where to go, and then goes into a undefined state.

    We have an example implementation of a "retarget" operation in our SDK:

    \components\libraries\uart\retarget.c

    Cheers, Håkon

  • Thank you once again Håkon, I have already included the retarget.c in my project. The problem is, I already can printf output strings, but I can only do so in main.c, not anywhere else. Not any other .c file, which is very, very puzzling.

    Can you please help me... one more time? I have never encountered this before.

Reply Children
No Data
Related