I am trying to retarget the printf() function for the STM32F411RET microcontroller in the ARM GCC toolchain environment which uses Newlib for the C standard library.
When I search for how to retarget printf(), many people say I need to implement _write() or _write_r(). And it seems both working.
But I still have questions about them:
When I look through the document of Newlib, it says I can implement
write()to output files, but it doesn't look like it is working. It looks like we can implement_write(), but this function is never be mentioned in the document. What happened towrite()? Does an underscore make anything different?In which situation is
_write_r()preferable to_write()? I don't understand the concept of reentrancy in C. Any examples?
writefunction, conforming programs are allowed to define their own function (or variable) namedwritewithout it changing howprintfbehaves. This means thatprintfcan't use a function namedwrite, it has to use a function with a name that conforming programs aren't allowed to use, like say_write.write()will work withopen(); they are standard POSIX type functions. The special files number 0, 1, 2 are for stdin, stdout, stderr (typically). You write the implementation of these for newlib and if the file is >2 then it is some special file which you must index somehow. Ie, theopen()would have specified a desired file to write to and returned a file number >2. USER code must not rely on these values. However, implementers need to define something.printftoputsand in the process break everything. In this case you can either implementputstoo or rename printf to something else.