Skip to main content
Alex Golubev
Associate III
October 21, 2020
Solved

How to call the function "_write (int file, char * ptr, int len)" when calling the function printf ("test")?

  • October 21, 2020
  • 5 replies
  • 1904 views

Hi.

I am using stm32cubeide version 1.2.0.

I am using stm32CubeMx version 5.6.1.

The "int _write (int file, char * ptr, int len)" function is not called when the printf ("test") function is called.

What did you do:

1. Added to Properties ---> c / c ++ Build ---> Settings ---> Linker ---> Miscellaneous ---> -u _printf_float and -u _scanf_float.

2. Added to the main () function, printf ("test");

3. Launched the program

4. Did not receive a call to "_write (int file, char * ptr, int len)" to transfer characters "test".

How can I solve my problem?

Thanks for your time.

This topic has been closed for replies.
Best answer by KnarfB

printf (stdout) is line buffered. printf ("test\n") will ultimately call _write.

5 replies

KnarfB
KnarfBBest answer
Super User
October 21, 2020

printf (stdout) is line buffered. printf ("test\n") will ultimately call _write.

Alex Golubev
Associate III
October 21, 2020

Yes you are right. If you add '\ n' to printf ("test \ n") it works.

Can _write be called without '\ n'?

KnarfB
Super User
October 21, 2020

There are standard C methods making stdout unbuffered like

setvbuf(stdout, NULL, _IONBF, 0);

Though I haven't tested that.

Alex Golubev
Associate III
October 21, 2020

made:

1. Added setvbuf (stdout, NULL, _IONBF, 0); in

void initialise_monitor_handles ()

{

setvbuf (stdout, NULL, _IONBF, 0);

}

2. Registered in main ()

2.1 extern void initialise_monitor_handles (void);

2.2 In the body of the function main (), initialise_monitor_handles ();

I started it without '\ n' printf ("test") ;. Everything is working.

Are there any pitfalls in the implementation?

KnarfB
Super User
October 21, 2020

Perfect.

> Are there any pitfalls in the implementation?

printf implementation uses malloc. You may check that by setting a breakpoint at _sbrk.

The implementation is broken when FreeRTOS is used, see http://www.nadler.com/embedded/newlibAndFreeRTOS.html

Otherwise: good luck

hth

KnarfB