.TH "avr_stdio" 3avr "Fri Nov 24 2023 23:59:10" "Version 2.0.0" "avr-libc" \" -*- nroff -*- .ad l .nh .SH NAME avr_stdio \- : Standard IO facilities .SH SYNOPSIS .br .PP .SS "Functions" .in +1c .ti -1c .RI "\fBFILE\fP * \fBfdevopen\fP (int(*put)(char, \fBFILE\fP *), int(*get)(\fBFILE\fP *))" .br .in -1c .SH "Detailed Description" .PP .PP .nf #include .fi .PP .PP .SS "Introduction to the Standard IO facilities" .PP This file declares the standard IO facilities that are implemented in \fCavr-libc\fP\&. Due to the nature of the underlying hardware, only a limited subset of standard IO is implemented\&. There is no actual file implementation available, so only device IO can be performed\&. Since there's no operating system, the application needs to provide enough details about their devices in order to make them usable by the standard IO facilities\&. .PP Due to space constraints, some functionality has not been implemented at all (like some of the \fCprintf\fP conversions that have been left out)\&. Nevertheless, potential users of this implementation should be warned: the \fCprintf\fP and \fCscanf\fP families of functions, although usually associated with presumably simple things like the famous 'Hello, world!' program, are actually fairly complex which causes their inclusion to eat up a fair amount of code space\&. Also, they are not fast due to the nature of interpreting the format string at run-time\&. Whenever possible, resorting to the (sometimes non-standard) predetermined conversion facilities that are offered by avr-libc will usually cost much less in terms of speed and code size\&. .PP .SS "Tunable options for code size vs\&. feature set" .PP In order to allow programmers a code size vs\&. functionality tradeoff, the function \fBvfprintf()\fP which is the heart of the printf family can be selected in different flavours using linker options\&. See the documentation of \fBvfprintf()\fP for a detailed description\&. The same applies to \fBvfscanf()\fP and the \fCscanf\fP family of functions\&. .PP .SS "Outline of the chosen API" .PP The standard streams \fCstdin\fP, \fCstdout\fP, and \fCstderr\fP are provided, but contrary to the C standard, since avr-libc has no knowledge about applicable devices, these streams are not already pre-initialized at application startup\&. Also, since there is no notion of 'file' whatsoever to avr-libc, there is no function \fCfopen()\fP that could be used to associate a stream to some device\&. (See \fBnote 1\fP\&.) Instead, the function \fC\fBfdevopen()\fP\fP is provided to associate a stream to a device, where the device needs to provide a function to send a character, to receive a character, or both\&. There is no differentiation between 'text' and 'binary' streams inside avr-libc\&. Character \fC\\n\fP is sent literally down to the device's \fCput()\fP function\&. If the device requires a carriage return (\fC\\r\fP) character to be sent before the linefeed, its \fCput()\fP routine must implement this (see \fBnote 2\fP)\&. .PP As an alternative method to \fBfdevopen()\fP, the macro \fBfdev_setup_stream()\fP might be used to setup a user-supplied FILE structure\&. .PP It should be noted that the automatic conversion of a newline character into a carriage return - newline sequence breaks binary transfers\&. If binary transfers are desired, no automatic conversion should be performed, but instead any string that aims to issue a CR-LF sequence must use \fC'\\r\\n'\fP explicitly\&. .PP For convenience, the first call to \fC\fBfdevopen()\fP\fP that opens a stream for reading will cause the resulting stream to be aliased to \fCstdin\fP\&. Likewise, the first call to \fC\fBfdevopen()\fP\fP that opens a stream for writing will cause the resulting stream to be aliased to both, \fCstdout\fP, and \fCstderr\fP\&. Thus, if the open was done with both, read and write intent, all three standard streams will be identical\&. Note that these aliases are indistinguishable from each other, thus calling \fC\fBfclose()\fP\fP on such a stream will also effectively close all of its aliases (\fBnote 3\fP)\&. .PP It is possible to tie additional user data to a stream, using \fBfdev_set_udata()\fP\&. The backend put and get functions can then extract this user data using \fBfdev_get_udata()\fP, and act appropriately\&. For example, a single put function could be used to talk to two different UARTs that way, or the put and get functions could keep internal state between calls there\&. .PP .SS "Format strings in flash ROM" .PP All the \fCprintf\fP and \fCscanf\fP family functions come in two flavours: the standard name, where the format string is expected to be in SRAM, as well as a version with the suffix '_P' where the format string is expected to reside in the flash ROM\&. The macro \fCPSTR\fP (explained in \fB: Program Space Utilities\fP) becomes very handy for declaring these format strings\&. .PP .SS "Running stdio without malloc()" .PP By default, \fBfdevopen()\fP requires malloc()\&. As this is often not desired in the limited environment of a microcontroller, an alternative option is provided to run completely without malloc()\&. .PP The macro \fBfdev_setup_stream()\fP is provided to prepare a user-supplied FILE buffer for operation with stdio\&. .PP .SS "Example" .PP .PP .nf #include static int uart_putchar(char c, FILE *stream); static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); static int uart_putchar(char c, FILE *stream) { if (c == '\\n') uart_putchar('\\r', stream); loop_until_bit_is_set(UCSRA, UDRE); UDR = c; return 0; } int main(void) { init_uart(); stdout = &mystdout; printf("Hello, world!\\n"); return 0; } .fi .PP .PP This example uses the initializer form \fBFDEV_SETUP_STREAM()\fP rather than the function-like \fBfdev_setup_stream()\fP, so all data initialization happens during C start-up\&. .PP If streams initialized that way are no longer needed, they can be destroyed by first calling the macro \fBfdev_close()\fP, and then destroying the object itself\&. No call to \fBfclose()\fP should be issued for these streams\&. While calling \fBfclose()\fP itself is harmless, it will cause an undefined reference to free() and thus cause the linker to link the malloc module into the application\&. .PP .SS "Notes" .PP \fBNote 1:\fP .RS 4 It might have been possible to implement a device abstraction that is compatible with \fCfopen()\fP but since this would have required to parse a string, and to take all the information needed either out of this string, or out of an additional table that would need to be provided by the application, this approach was not taken\&. .RE .PP \fBNote 2:\fP .RS 4 This basically follows the Unix approach: if a device such as a terminal needs special handling, it is in the domain of the terminal device driver to provide this functionality\&. Thus, a simple function suitable as \fCput()\fP for \fC\fBfdevopen()\fP\fP that talks to a UART interface might look like this: .RE .PP .PP .nf int uart_putchar(char c, FILE *stream) { if (c == '\\n') uart_putchar('\\r'); loop_until_bit_is_set(UCSRA, UDRE); UDR = c; return 0; } .fi .PP .PP \fBNote 3:\fP .RS 4 This implementation has been chosen because the cost of maintaining an alias is considerably smaller than the cost of maintaining full copies of each stream\&. Yet, providing an implementation that offers the complete set of standard streams was deemed to be useful\&. Not only that writing \fC\fBprintf()\fP\fP instead of \fCfprintf(mystream, \&.\&.\&.)\fP saves typing work, but since avr-gcc needs to resort to pass all arguments of variadic functions on the stack (as opposed to passing them in registers for functions that take a fixed number of parameters), the ability to pass one parameter less by implying \fCstdin\fP or stdout will also save some execution time\&. .RE .PP .SH "Function Documentation" .PP .SS "\fBFILE\fP * fdevopen (int(*)(char, \fBFILE\fP *) put, int(*)(\fBFILE\fP *) get)" This function is a replacement for \fCfopen()\fP\&. .PP It opens a stream for a device where the actual device implementation needs to be provided by the application\&. If successful, a pointer to the structure for the opened stream is returned\&. Reasons for a possible failure currently include that neither the \fCput\fP nor the \fCget\fP argument have been provided, thus attempting to open a stream with no IO intent at all, or that insufficient dynamic memory is available to establish a new stream\&. .PP If the \fCput\fP function pointer is provided, the stream is opened with write intent\&. The function passed as \fCput\fP shall take two arguments, the first a character to write to the device, and the second a pointer to FILE, and shall return 0 if the output was successful, and a nonzero value if the character could not be sent to the device\&. .PP If the \fCget\fP function pointer is provided, the stream is opened with read intent\&. The function passed as \fCget\fP shall take a pointer to FILE as its single argument, and return one character from the device, passed as an \fCint\fP type\&. If an error occurs when trying to read from the device, it shall return \fC_FDEV_ERR\fP\&. If an end-of-file condition was reached while reading from the device, \fC_FDEV_EOF\fP shall be returned\&. .PP If both functions are provided, the stream is opened with read and write intent\&. .PP The first stream opened with read intent is assigned to \fCstdin\fP, and the first one opened with write intent is assigned to both, \fCstdout\fP and \fCstderr\fP\&. .PP \fBfdevopen()\fP uses \fBcalloc()\fP (und thus malloc()) in order to allocate the storage for the new stream\&. .PP \fBNote\fP .RS 4 If the macro __STDIO_FDEVOPEN_COMPAT_12 is declared before including <\fBstdio\&.h\fP>, a function prototype for \fBfdevopen()\fP will be chosen that is backwards compatible with avr-libc version 1\&.2 and before\&. This is solely intented for providing a simple migration path without the need to immediately change all source code\&. Do not use for new code\&. .RE .PP .SH "Author" .PP Generated automatically by Doxygen for avr-libc from the source code\&.