/* USART-Init beim ATmegaXX */ #include #include #include #include "config.h" #include "uart.h" #ifdef ATMEGA128 #define UCSRB UCSR0B #define UCSRC UCSR0C #define UDR UDR0 #define UBRRH UBRR0H #define UBRRL UBRR0L #define URSEL UMSEL #endif #define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) ((F_OSC)/((UART_BAUD_RATE)*16L)) #ifdef UART_INTERRUPT volatile static char rxbuf[UART_RXBUFSIZE]; volatile static char txbuf[UART_TXBUFSIZE]; volatile static char *volatile rxhead, *volatile rxtail; volatile static char *volatile txhead, *volatile txtail; ISR(USART_UDRE_vect) { #ifdef UART_LEDS PORTC ^= 0x01; #endif if ( txhead == txtail ) { UCSRB &= ~(1 << UDRIE); /* disable data register empty IRQ */ } else { UDR = *txtail; /* schreibt das Zeichen x auf die Schnittstelle */ if (++txtail == (txbuf + UART_TXBUFSIZE)) txtail = txbuf; } } ISR(USART_RXC_vect) { int diff; #ifdef UART_LEDS PORTC ^= 0x02; #endif /* buffer full? */ diff = rxhead - rxtail; if ( diff < 0 ) diff += UART_RXBUFSIZE; if (diff < UART_RXBUFSIZE -1) { // buffer NOT full *rxhead = UDR; if (++rxhead == (rxbuf + UART_RXBUFSIZE)) rxhead = rxbuf; } else { UDR; //reads the buffer to clear the interrupt condition } } #endif // UART_INTERRUPT void uart_init() { PORTD |= 0x01; //Pullup an RXD an UCSRA = 0; UCSRB |= (1<>8); // UBRRL=(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)); UBRRL = 8; #ifdef UART_INTERRUPT // init buffers rxhead = rxtail = rxbuf; txhead = txtail = txbuf; // activate rx IRQ UCSRB |= (1 << RXCIE); #endif // UART_INTERRUPT } #ifdef UART_INTERRUPT void uart_putc(char c) { volatile int diff; /* buffer full? */ do { diff = txhead - txtail; if ( diff < 0 ) diff += UART_TXBUFSIZE; } while ( diff >= UART_TXBUFSIZE -1 ); cli(); *txhead = c; if (++txhead == (txbuf + UART_TXBUFSIZE)) txhead = txbuf; UCSRB |= (1 << UDRIE); /* enable data register empty IRQ */ sei(); } #else // WITHOUT INTERRUPT void uart_putc(char c) { while (!(UCSRA & (1<