2019-01-01 15:48:54 +00:00
|
|
|
#include <avr/io.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "usart.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "ringbuffer.h"
|
2019-01-17 18:39:05 +00:00
|
|
|
#include <avr/interrupt.h>
|
2019-01-01 15:48:54 +00:00
|
|
|
|
|
|
|
volatile ringbuffer_t usart_buffer;
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
usart_init(void)
|
|
|
|
{
|
|
|
|
usart_buffer = ringbuffer_new(USART_BUFFER_SIZE);
|
2019-01-17 18:39:05 +00:00
|
|
|
UBRR0H = (unsigned char)(UBRR_VALUE >> 8);
|
|
|
|
UBRR0L = (unsigned char)UBRR_VALUE;
|
|
|
|
UCSR0B = _BV(TXEN0) | _BV(TXCIE0);
|
|
|
|
UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
|
|
|
|
sei();
|
2019-01-01 15:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
usart_putc(char c)
|
|
|
|
{
|
2019-01-17 18:39:05 +00:00
|
|
|
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;
|
2019-01-01 15:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ISR(USART_TX_vect)
|
|
|
|
{
|
|
|
|
if(ringbuffer_nonempty(usart_buffer))
|
|
|
|
{
|
2019-01-17 18:39:05 +00:00
|
|
|
UDR0 = ringbuffer_get_char(usart_buffer);
|
|
|
|
UCSR0A |= _BV(TXC0);
|
2019-01-01 15:48:54 +00:00
|
|
|
}
|
|
|
|
}
|