fixed the USART

This commit is contained in:
Daniel Knüttel 2019-01-17 19:39:05 +01:00
parent ac04dfc73f
commit ac2d26bd65

25
usart.c
View File

@ -3,6 +3,7 @@
#include "usart.h" #include "usart.h"
#include "conf.h" #include "conf.h"
#include "ringbuffer.h" #include "ringbuffer.h"
#include <avr/interrupt.h>
volatile ringbuffer_t usart_buffer; volatile ringbuffer_t usart_buffer;
@ -11,16 +12,26 @@ void
usart_init(void) usart_init(void)
{ {
usart_buffer = ringbuffer_new(USART_BUFFER_SIZE); usart_buffer = ringbuffer_new(USART_BUFFER_SIZE);
UBRRH = (unsigned char)(UBRR_VALUE >> 8); UBRR0H = (unsigned char)(UBRR_VALUE >> 8);
UBRRL = (unsigned char)UBRR_VALUE; UBRR0L = (unsigned char)UBRR_VALUE;
UCSRB = _BV(TXEN) | _BV(TXCIE); UCSR0B = _BV(TXEN0) | _BV(TXCIE0);
UCSRC = _BV(USBS) | _BV(UCSZ0) | _BV(UCSZ1); UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
sei();
} }
int int
usart_putc(char c) 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)) if(ringbuffer_nonempty(usart_buffer))
{ {
while(!(UCSRA & UDRE)); UDR0 = ringbuffer_get_char(usart_buffer);
UDR = ringbuffer_get_char(usart_buffer); UCSR0A |= _BV(TXC0);
} }
} }