cancel
Showing results for 
Search instead for 
Did you mean: 

How to use printf() function with stm32cubeMX + sw4stm32

gae089
Associate II
Posted on September 01, 2015 at 19:13

Hi to every one,

It's the first time that I use the STM32CubeMX + SWSTM32 to program a stm32f401RE board. I have done already some example project but I can't use the printf() function to communicate  with a terminal ( Tera Term) on the PC. Somebody can explain me how I can do it?

Thank you in advance!!
21 REPLIES 21
Nesrine M_O
Lead II
Posted on September 01, 2015 at 19:36

Hi,

I'd highly recommend you to take a look to the project under STM32Cube F4 package:

STM32Cube_FW_F4_V1.8.0\Projects\STM32F401RENucleo\Examples\UART\UART_Printf\SW4STM32

It shows how to retarget the C library printf function to the UART in order to output the printf message on the Hyperterminal using USART2 interface connected to ST-LINK MCU.

-Syrine-

gae089
Associate II
Posted on September 02, 2015 at 12:10

Thank you, I followed your suggestion and now I'm able to use the printf() function.

But I can't print the float numbers; For example:

/************************/

float value=3.5; 

printf(''%f'', value);

/************************/

in the terminal it doesn't write anything;

How can I resolve that?

Thank you again...

Nesrine M_O
Lead II
Posted on September 02, 2015 at 15:12

Open your SW4STM32, go to: project -> properties > C/C+ build > Settings > MCU GCC Linker > Miscellaneous > Linker flags: add this flag: -specs=nano.specs -u _printf_float 

-Syrine-

gae089
Associate II
Posted on September 02, 2015 at 16:59

I added the flag and now when I want to print a float variable the function prints 0.0000000.

For example:

/**********************************/

float value=4.5;

printf(''MyFloat= %f'', value);

/**********************************/

I get ''MyFloat= 0.0000000''. Why?

Thank you so much for your time...

Nesrine M_O
Lead II
Posted on September 03, 2015 at 12:32

Try to add this flag: -specs=nosys.specs

(-specs=nosys.specs -specs=nano.specs -u _printf_float )

to previous options or add the syscalls.c file to your project 

-Syrine-

gae089
Associate II
Posted on September 03, 2015 at 16:21

Done!! But again I get 0.000000 when I use float variables... :(

TarekB-ST
Associate III
Posted on September 03, 2015 at 17:09

Hi,

I think that changing _estack in your linker(*.ld) file can resolve the issue.

_estack should be 8byte aligned, so modify

_estack = 0x20017FFF;    /* end of RAM */

by

_estack = 0x20018000;    /* end of RAM */

Regards

gae089
Associate II
Posted on September 04, 2015 at 11:18

Yesss.. Now it works!!! Thank you so much BochT!! And Thank you to Syrine for his patience...

Regards

yhuang
Associate II
Posted on September 05, 2015 at 04:26

Another forum have mentioned the issue already.

http://www.openstm32.org/forumthread698