I am breaking this out as a separate file to make the content easy to find.
A lot of information is available in the arv-libc source. See:
I have looked at different example including on in the source file stdio.h. The following was tested and worked:
// main.c #include <stdio.h> #include "uart.h" int main(void) { init_uart(); stdout = &uart_output; printf("Hello, world!\n"); while(1); return 0; }
// uart.c #include <avr/io.h> #include <stdio.h> #include "uart.h" #ifndef BAUD #define BAUD 9600 #endif #include <util/setbaud.h> /* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */ FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ); void init_uart(void) { UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE; UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */ UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */ } int uart_putchar(char c, FILE *stream) { if (c == '\n') { uart_putchar('\r', stream); } loop_until_bit_is_set(UCSR0A, UDRE0); UDR0 = c; return 0; } int uart_getchar(FILE *stream) { loop_until_bit_is_set(UCSR0A, RXC0); return UDR0; }
// uart.h int uart_putchar(char c, FILE *stream); int uart_getchar(FILE *stream); void init_uart(void); /* http://www.ermicro.com/blog/?p=325 */ extern FILE uart_output; extern FILE uart_input;