avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] printf with AVR-libc


From: Kang Tin LAI
Subject: Re: [avr-gcc-list] printf with AVR-libc
Date: Mon, 02 Feb 2004 00:25:54 +0800
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624 Netscape/7.1

Hi,

I just finished something, attached here for your reference, and hope help:


====================== fdevopen, fprintf,.... ========

        FILE *con;

(1)     serial_init();
(2)     con = fdevopen(serial0_tx, serial0_rx, 0);
(3)     fprintf(con, "\n\nInitialize LCD thread\n");


(1) setup the serial port
(2) open a "file" with fdevopen(), any subsequent "file" io will use
function serial0_tx and serial0_rx for writing and reading respectively.
As name shown, serial0_tx and rx are connected to the UART (see serial port codes followed).
After fdevopen, stdin, stdout and stderr are also opened automatically,
that means:
(3) you may use:  printf("\n\ntesting,...\n"), or
fprintf(stdout, "\n\ntesting\n") instead of above in line (3).



======= serial port codes (modified from atmel app notes) ========
/*
        Setup USART0, 115200, 8n1, Rx interrupt enable.
*/
void serial_init(void)
{
        rxbuf = malloc(USART_RX_BUFFER_SIZE + USART_TX_BUFFER_SIZE);
        txbuf = rxbuf + USART_RX_BUFFER_SIZE;
#if defined __AVR_AT90S8515__ | __AVR_ATmega103__
        UBRR = UBRRCONST;
        UCR = ((1 << RXCIE) | (1 << RXEN) | (1 << TXEN));
#elif defined __AVR_ATmega128__
        UBRR0H = (unsigned char) ((unsigned) UBRRCONST / 256);
        UBRR0L = (unsigned char) (UBRRCONST);
        UCSR0B = ((1 << RXCIE0) | (1 << RXEN0) | (1 << TXEN0));
        UCSR0C = (3 << UCSZ00);   /* Set frame format: 8n1 */
#else
#warning "AVR device type not defined"
#endif
}

/*
        Receive complete interrupt.
*/
#if defined __AVR_AT90S8515__ | __AVR_ATmega103__
__attribute__ ((signal)) void SIG_UART_RECV(void)
#elif defined __AVR_ATmega128__
__attribute__ ((signal)) void SIG_UART0_RECV(void)
#else
#warning "AVR device type not defined"
#endif
{
        unsigned char rxhd;
        unsigned char byte;

        byte = UDR0;
        rxhd = (rxhead + 1) & USART_RX_BUFFER_MASK;
        if (rxhd != rxtail) {
                *(rxbuf + rxhd) = byte;
                rxhead = rxhd;
        }
}

/*
        Transmit data register empty interrupt.
*/
#if defined __AVR_AT90S8515__ | __AVR_ATmega103__
__attribute__ ((signal)) void SIG_UART_DATA(void)
#elif defined __AVR_ATmega128__
__attribute__ ((signal)) void SIG_UART0_DATA(void)
#else
#warning "AVR device type not defined"
#endif
{
        if (txhead != txtail) {
                txtail = (txtail + 1) & USART_TX_BUFFER_MASK;
                UDR0 = *(txbuf + txtail);
        } else {
                UCSR0B &= ~(1 << UDRIE0);
        }
}

int serial0_rx(void)
{
        while (rxhead == rxtail);
        rxtail = (rxtail + 1) & USART_RX_BUFFER_MASK;
        return *(rxbuf + rxtail);
}

int serial0_tx(char byte)
{
        unsigned char txhd;

        if (byte == '\n')
                serial0_tx('\r');
        txhd = (txhead + 1) & USART_TX_BUFFER_MASK;
        if (txhd != txtail) {
                *(txbuf + txhd) = byte;
                UCSR0B |= (1 << UDRIE0);
                txhead = txhd;
        }
        return 0;
}

====================== serial port codes ========

Helix wrote:
Hi,
I am reading avr-libc-user-manual.pdf and I'm wondering if the printf
function print out chars to the UART or not.
I've found the section "5.14 Standard IO facilities" not enough clear and I
don't understand how it works.
Could someone please clarify ?
TIA

--Helix


_______________________________________________
avr-gcc-list mailing list
address@hidden
http://www.avr1.org/mailman/listinfo/avr-gcc-list


--
---
Kang Tin LAI <address@hidden>
-----



reply via email to

[Prev in Thread] Current Thread [Next in Thread]