cancel
Showing results for 
Search instead for 
Did you mean: 

stm32F0 discovery and printf on usart

Rob1
Associate II
Posted on December 13, 2016 at 06:46

Hi all. I'm net to STM micros , so excuse the stupid question.

I've set up USART1 on the stm32F0 discovery board, using the SPL , NOT cube.

If I send data out the usart using the 'USART_SendData' function provided by the peripheral library , the data

is sent out no problem , but if I use printf('dsfs') , nothing is sent.

How do I ensure the printf sends data to the serial port , and not a screen or anywhere else?

I am using Atollic6

Cheers

Neddie

6 REPLIES 6
Goj Immpypi
Associate II
Posted on March 15, 2017 at 04:01

I had a similar problem with STM32F103 (STM32 Smart V2) when following the VisualGDB tutorial here:

https://visualgdb.com/tutorials/arm/stm32/uart/

I was having problems with (what appeared) to be nothing coming out of the printf() , so I added USART_SendData to see if it worked any differently.

     for (;;)      {

         printf('Hello World! ');           

         USART_SendData(USART1, 0x63);           

         GPIO_WriteBit(GPIOC, GPIO_Pin_6, Bit_SET);           

         Delay();           

         GPIO_WriteBit(GPIOC, GPIO_Pin_6, Bit_RESET);           

         Delay();      

      }

curiously I don't see alternating text of 'Hello World! c', or even a string of 'Hello World'. Instead there's a long string of 'c's and then eventually a big burp of 'Hello World's. So I suspect yours is working the same. The printf's are somehow getting buffered, but not actually sent to UART. The USART_SendData immediately appears on the UART, even when single-step debugging. The printf() text does not.

I'm a complete newbie to the STM32, but it appears there's some sort of buffer/flushing issue. Appending '\n\r' to text in printf() does not cause this odd problem and makes things work as one would expect. Note the string of c's appears in the *middle* of one of the Hello World text strings printed.

I'm curious as to the real answer.

Here's the output from the code above:

Port open

ccccccccccccccccccccccccccccccccccccccccccccccccccllo World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello Worldccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello Woccccccccccccccccc

Port closed

*edit: manually reformat the code sample

Posted on March 20, 2017 at 03:22

GNU/GCC is buffering the output.

If you don't want that to happen sprintf() to a buffer of your own, and then send the string buffer to the USART directly

void USART_SendString(USART_TypeDef* USARTx, unsigned char *Buffer)

{

  while(*Buffer)

  {

    while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);

    USART_SendData(USARTx, *Buffer++);

  }    

}
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
m8r-xuulk51
Associate II
Posted on March 20, 2017 at 14:10

Hi,

read this (toinclude syscalls.c)

http://www.openstmorg/tiki-view_forum_thread.php?comments_parentId=1346

Also, as mentioned before, the Output is buffferd until you send a ' ' newline.

Instead, you can turn off the buffering with something like this:

setbuf(stdout, NULL);�?

Posted on March 20, 2017 at 15:45

Really should check TXE before blindly using USART_SendData()

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 20, 2017 at 22:02

this is really helpful, thank you. I wonder how many of those other 'hey, printf is not working for me' folks also did not know this? 🙂

Posted on March 20, 2017 at 23:13

It is hard to know, there are usual rational explanations for things if you understand the mechanics.

A lot of printf() problems I see are with the SWV channel, odd USART output implementations, there are a lot of broken examples provided by ST, and bad examples tend to spread further.

Forums also tend to be highly repetitive.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..