Skip to main content
gae089
Associate
September 1, 2015
Question

How to use printf() function with stm32cubeMX + sw4stm32

  • September 1, 2015
  • 12 replies
  • 7449 views
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!!
This topic has been closed for replies.

12 replies

Nesrine M_O
Associate
September 1, 2015
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-

g. lewis
Associate III
February 21, 2017
Posted on February 21, 2017 at 18:28

Hello Nesrine,

I'd like to use UART to retarget the C library printf and I followed your comment but I'm stuck when I opened the folder STM32Cube_FW_F4_V1.8.0\Projects\STM32F401RENucleo\Examples\UART\UART_Printf\SW4STM32. Here is what it showes:

-Dir: STM32F4xx-Nucleo

-startup_stm32f411xe.s

-syscalls.c

I don't know what to do next. Please help.

Thanks

Sebastian K.
Associate III
February 21, 2017
Posted on February 21, 2017 at 22:18

Hello,

you need to look in the \Src subdirectory, not the \SW4STM32 one - that only contains some files specific to the toolchain, but not the actual source code.

gae089
gae089Author
Associate
September 2, 2015
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
Associate
September 2, 2015
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
gae089Author
Associate
September 2, 2015
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
Associate
September 3, 2015
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
gae089Author
Associate
September 3, 2015
Posted on September 03, 2015 at 16:21

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

TarekB-ST
Associate
September 3, 2015
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
gae089Author
Associate
September 4, 2015
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
September 5, 2015
Posted on September 05, 2015 at 04:26

Another forum have mentioned the issue already.

http://www.openstm32.org/forumthread698

tommi
Visitor II
July 13, 2017
Posted on July 13, 2017 at 19:38

Just to clarify the issue for future users. I know its repetition but having the full solution in one post is very helpful.

Enable the Uart port and pins needed for the application in Cube or manually. Test the port that it works:

HAL_UART_Transmit(&huart1, 'Hello\n', 6, 0xFFFF);

If it works then continue with printf.

First add this to the bottom of your usart.c file generated by Cube. Change &huart1 as required for the UART port that you are using.

/**

  * @brief  Retargets the C library printf function to the USART.

  * @param  None

  * @retval None

  */

PUTCHAR_PROTOTYPE

{

  /* Place your implementation of fputc here */

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

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

  return ch;

}

Next add this to the top of the usart.c file:

#ifdef __GNUC__

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

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

  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

#else

  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif /* __GNUC__ */

Finally, syscalls.c file needs to be copied to your project, this can be placed in the src folder. The file can be copied as is from:

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

Now printf should be working. For making float number print refer to the other posts in this thread.

Tested with F0 discovery.