47 lines
914 B
C
47 lines
914 B
C
#include <avr/io.h>
|
|
#include <stdlib.h>
|
|
#include "usart.h"
|
|
#include "conf.h"
|
|
#include "ringbuffer.h"
|
|
#include <avr/interrupt.h>
|
|
|
|
volatile ringbuffer_t usart_buffer;
|
|
|
|
|
|
void
|
|
usart_init(void)
|
|
{
|
|
usart_buffer = ringbuffer_new(USART_BUFFER_SIZE);
|
|
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)
|
|
{
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
ISR(USART_TX_vect)
|
|
{
|
|
if(ringbuffer_nonempty(usart_buffer))
|
|
{
|
|
UDR0 = ringbuffer_get_char(usart_buffer);
|
|
UCSR0A |= _BV(TXC0);
|
|
}
|
|
}
|