Skip to main content
Fede Rico
Associate III
June 15, 2016
Question

No Carrige return when redirect printf() to UART2

  • June 15, 2016
  • 3 replies
  • 1283 views
Posted on June 15, 2016 at 15:41

Hi there,

I'm working on custom board based on STM32F103 and the IDE is Keil. I will use UART2 to print some debug logs. To do this I add the fputc function to my code in order to redirect printf to UART2.

int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
HAL_UART_Transmit(&uart_debug_handler,(uint8_t*)&ch,1,1);
return ch;
}

Everything work but the output on serial port seems to haven't the Carrige Return (CR). There is only the Line Feed (LF). This is an extract of serial output:

I'm Alive: 2269
I'm Alive: 2270
I'm Alive: 2271
I'm Alive: 2272

Any suggestions? Thanks a lot! Federico
    This topic has been closed for replies.

    3 replies

    troy1818
    Associate III
    June 16, 2016
    Posted on June 16, 2016 at 10:45

    Do you send /r ?

    Can we see the printf line ?
    Tesla DeLorean
    Guru
    June 16, 2016
    Posted on June 16, 2016 at 11:21

    If your terminal needs to receive '' '' when you send ' ', then you are going to have to add that functionality to your fputc() function, or change your terminal settings.

    int fputc(int ch, FILE *f)
    {
    /* Place your implementation of fputc here */
    /* e.g. write a character to the USART */
    if ((char)ch == '
    ') // LF -> CR LF
    HAL_UART_Transmit(&uart_debug_handler,(uint8_t*)''
    '',1,1);
    HAL_UART_Transmit(&uart_debug_handler,(uint8_t*)&ch,1,1);
    return ch;
    }

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Fede Rico
    Fede RicoAuthor
    Associate III
    June 16, 2016
    Posted on June 16, 2016 at 18:18

    Thanks clive1!!!

    You solved my bug in printf!

    Thanks!