cancel
Showing results for 
Search instead for 
Did you mean: 

how to use ''printf''?

Min-Kyoung Kim
Associate III
Posted on February 09, 2017 at 14:47

I'm using examples of project  'UART_Printf'. 

But I can't use printf. 

I set uart1. and I checked that uart1 tx/rx works. 

I added below code to my source. 

&sharpinclude 'stdio.h'

&sharpifdef __GNUC__

/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf

set to 'Yes') calls __io_putchar() */

&sharpdefine PUTCHAR_PROTOTYPE int __io_putchar(int ch)

&sharpelse

&sharpdefine PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

&sharpendif /* __GNUC__ */

PUTCHAR_PROTOTYPE

{

/* Place your implementation of fputc here */

/* e.g. write a character to the USART1 and Loop until the end of transmission */

HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);

return ch;

}

#uart #printf #stm32f4
10 REPLIES 10
Posted on February 09, 2017 at 15:17

You are using what chip, or what board, with what compiler?

Where do you expect to see this data, and how have you connected up and configured the USART?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Sirma Siang
ST Employee
Posted on February 09, 2017 at 16:10

Hello Kim,

Assuming that you are using Eclipse IDE, your code should be enough, but you have to add Syscalls.c file into your project.

Then considering that cube has been well configured to generate the correct code to initialise the board, you have to connect the demo board to your pc, and open the virtual com port... set it with the correct parameters... and your printf should show up.

As you can see, I made few assuptions....

Could you please detail a bit more your code generation chain, your IDE, the HW on which you want to do this test ?

Kind regards

Sirma

Posted on February 09, 2017 at 22:07

I'm using STM32CubeMX + SWSTM32 to program a stm32f446RE board.

I've found the code 

http://www.st.com/content/st_com/en/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-configurators-and-code-generators/stm32cubemx.html

 of software development tools. 
Posted on February 09, 2017 at 22:09

I already checked the USART is working. I can receive and transmit the data through USART1 to PC. 

But I can't use only 'printf' function. 

when I use 'printf', it doesn't have any reaction. 

I'm using STM32CubeMX + SWSTM32 to program a stm32f446RE board.

Posted on February 09, 2017 at 23:03

The next place to look would be the write functionality implemented in syscalls/newlib code. This would need to call the __io_putchar() functionality provided.

Your options here would be to review the code compiled into the project, and to debug/breakpoint various points, and single-stepping code. Going in via puts() might be simpler than printf()

The Nucleo 446RE would likely be using USART2 for VCP connectivity.

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

I can't find syscalls.c file. should I copy from the project of Nucleo 446RE Examples?

Do you mean that I prefer to use puts() instead of printf()?

Posted on February 10, 2017 at 03:31

I use GNU/GCC directly, I do not use SWSTM32. I have posted numerous GNU/GCC + MAKE examples which provide printf() via SWV and USART.

I mean puts() is easier to debug/trace. There isn't a lot of faffing about with formatting strings to confuse the process, you should be a few function calls away from code that touches the USART with puts(), or putchar().

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 10, 2017 at 03:33

yes copy it from the other example.

Posted on February 10, 2017 at 09:51

Yes, copy syscalls.c from example into your project.

Kind regards

Sirma