diff --git a/usart.c b/usart.c index c0f0687..b6aaf01 100644 --- a/usart.c +++ b/usart.c @@ -3,6 +3,7 @@ #include "usart.h" #include "conf.h" #include "ringbuffer.h" +#include volatile ringbuffer_t usart_buffer; @@ -11,16 +12,26 @@ void usart_init(void) { usart_buffer = ringbuffer_new(USART_BUFFER_SIZE); - UBRRH = (unsigned char)(UBRR_VALUE >> 8); - UBRRL = (unsigned char)UBRR_VALUE; - UCSRB = _BV(TXEN) | _BV(TXCIE); - UCSRC = _BV(USBS) | _BV(UCSZ0) | _BV(UCSZ1); + UBRR0H = (unsigned char)(UBRR_VALUE >> 8); + UBRR0L = (unsigned char)UBRR_VALUE; + UCSR0B = _BV(TXEN0) | _BV(TXCIE0); + UCSR0C = _BV(UCSZ00) | _BV(UCSZ01); + sei(); } int usart_putc(char c) { - return ringbuffer_put_char(usart_buffer, c); + int result = ringbuffer_put_char(usart_buffer, c); + // If the buffer is already emtpy there is no + // need to wait for the interrupt. + // Also it is likely that the interrupt will never happen. + // (For instance because this is the first byte sent) + if(UCSR0A & _BV(UDRE0)) + { + UDR0 = ringbuffer_get_char(usart_buffer); + } + return result; } @@ -29,7 +40,7 @@ ISR(USART_TX_vect) { if(ringbuffer_nonempty(usart_buffer)) { - while(!(UCSRA & UDRE)); - UDR = ringbuffer_get_char(usart_buffer); + UDR0 = ringbuffer_get_char(usart_buffer); + UCSR0A |= _BV(TXC0); } }