cancel
Showing results for 
Search instead for 
Did you mean: 

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

Alex Golubev
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

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

View solution in original post

5 REPLIES 5
KnarfB
Principal III

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

Alex Golubev
Associate II

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

Can _write be called without '\ n'?

There are standard C methods making stdout unbuffered like

setvbuf(stdout, NULL, _IONBF, 0);

Though I haven't tested that.

Alex Golubev
Associate II

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?

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