cancel
Showing results for 
Search instead for 
Did you mean: 

STM32411RE printf

marco3
Associate II
Posted on October 01, 2016 at 20:58

How can I print data acquired from a gyroscope on RealTerm using printf. I'm using a STM32F411RE and I used CubeMX to set registers.  Thanks!

#printf
7 REPLIES 7
Posted on October 02, 2016 at 15:32

The exact method depends on the tool-chain being used.

You need to initialize USART2 and the PA2/PA3 pins that it uses for the VCP connection. Then you need to create a putchar() or chain specific equivalent to output characters to USART2.

This is an example using the SPL and Keil for this board.

http://www.keil.com/forum/61540/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 02, 2016 at 18:37

Hi Marco,

For most development environments, adding a callback function that performs the _write() function, which would normally be provided by an OS, will make printf() work for you.

you can Google _write() for more information.  The function and its parameters look like this:

int _write(int FD, char *buffer, int len){

HAL_UART_Transmit(&huart2, (uint8_t*)buffer, len, 1000);

return len;

}

where the first parameter is a handle for the output device being used, in this case since there is only one output device- the console, you can ignore this.  It then passes a pointer to the formatted string it has prepared to output, and the length of the string.  The function is of type int and expects the number of characters successfully written to be returned at the end of the function.  The above clip fulfills all of these needs and uses a HAL call to output the string to your UART (change the uart reference if you are using a different one).

Also, in some tool chains this function is prefixed with two underscores, rather than one.  The above example works with SW4STM32.  I believe that the EWARM toolchain wants two underscores.  In some environments you will get additional link errors for other OS functions like _sbrk() which may be needed as well, but if you are using CubeMX for your init code I think this will generally not be a problem.

Good luck,

Aaron

marco3
Associate II
Posted on October 03, 2016 at 17:29

thank you! But I have some questions.

Why return len?

Does it work even if my buffer is an array? because it prints just one number instead of three.

Posted on October 03, 2016 at 17:48

It writes bytes, it doesn't understand arrays of numbers, the buffer/length can describe an array of numbers, and it will write it as is. Understand representation of data in memory, and in files.

It returns a length as a success code, just as the standard

http://codewiki.wikidot.com/c:system-calls:write

function would, negative for a failure. Review standard C texts.

If you want to print an array of numbers in ASCII, you'd perhaps use printf() or puts(itoa()) and those would then initiate multiple write(), as you enumerate through the array.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Walid FTITI_O
Senior II
Posted on October 04, 2016 at 14:25

Hi pirotta.marco, 

I recommend the example ''UART_printf'' in the

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef4.html

at this path: STM32Cube_FW_F4_V1.13.0\Projects\STM32F411RE-Nucleo\Examples\UART\UART_Printf

-Hannibal-

marco3
Associate II
Posted on October 04, 2016 at 19:56

thank you everyone. it works!