cancel
Showing results for 
Search instead for 
Did you mean: 

No Carrige return when redirect printf() to UART2

Fede Rico
Associate III
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
3 REPLIES 3
troy1818
Senior
Posted on June 16, 2016 at 10:45

Do you send /r ?

Can we see the printf line ?
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 Up vote any posts that you find helpful, it shows what's working..
Fede Rico
Associate III
Posted on June 16, 2016 at 18:18

Thanks clive1!!!

You solved my bug in printf!

Thanks!