2011-10-09 00:42:27 +02:00
|
|
|
/* Based on Atmel Application Note AVR 306 */
|
|
|
|
|
2011-10-05 22:41:19 +02:00
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#ifndef BAUD
|
|
|
|
#define BAUD 9600
|
|
|
|
#endif
|
|
|
|
#include <util/setbaud.h>
|
|
|
|
|
|
|
|
#ifndef UART_RX_BUFFER_SIZE
|
|
|
|
#define UART_RX_BUFFER_SIZE 64
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef UART_TX_BUFFER_SIZE
|
|
|
|
#define UART_TX_BUFFER_SIZE 64
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct tx_ring {
|
2011-10-05 22:43:17 +02:00
|
|
|
int buffer[UART_TX_BUFFER_SIZE];
|
|
|
|
int start;
|
|
|
|
int end;
|
2011-10-05 22:41:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct rx_ring {
|
2011-10-05 22:43:17 +02:00
|
|
|
int buffer[UART_RX_BUFFER_SIZE];
|
|
|
|
int start;
|
|
|
|
int end;
|
2011-10-05 22:41:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct tx_ring tx_buffer;
|
|
|
|
static struct rx_ring rx_buffer;
|
|
|
|
|
|
|
|
/* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */
|
|
|
|
|
|
|
|
void uart_init(void) {
|
2011-10-09 00:42:27 +02:00
|
|
|
|
|
|
|
tx_buffer.start = 0;
|
|
|
|
tx_buffer.end = 0;
|
|
|
|
|
|
|
|
rx_buffer.start = 0;
|
|
|
|
rx_buffer.end = 0;
|
|
|
|
|
2011-10-05 22:41:19 +02:00
|
|
|
UBRR0H = UBRRH_VALUE;
|
|
|
|
UBRR0L = UBRRL_VALUE;
|
|
|
|
|
|
|
|
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
|
|
|
|
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
|
|
|
|
|
|
|
|
sei();
|
|
|
|
}
|
|
|
|
|
|
|
|
int uart_putchar(char c, FILE *stream) {
|
|
|
|
if (c == '\n') {
|
|
|
|
uart_putchar('\r', stream);
|
|
|
|
}
|
2011-10-05 22:43:17 +02:00
|
|
|
|
|
|
|
int write_pointer = (tx_buffer.end + 1) % UART_TX_BUFFER_SIZE;
|
|
|
|
|
|
|
|
if (write_pointer != tx_buffer.start){
|
|
|
|
tx_buffer.buffer[tx_buffer.end] = c;
|
|
|
|
tx_buffer.end = write_pointer;
|
|
|
|
|
|
|
|
/* Data available. Enable the transmit interrupt for serial port 0. */
|
|
|
|
UCSR0B |= _BV(UDRIE0);
|
|
|
|
}
|
|
|
|
|
2011-10-05 22:41:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uart_getchar(FILE *stream) {
|
|
|
|
int read_pointer = (rx_buffer.start + 1) % UART_RX_BUFFER_SIZE;
|
2011-10-05 22:43:17 +02:00
|
|
|
|
|
|
|
rx_buffer.start = read_pointer;
|
|
|
|
return rx_buffer.buffer[read_pointer];
|
2011-10-05 22:41:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ISR(USART_RX_vect) {
|
2011-10-05 22:43:17 +02:00
|
|
|
int write_pointer = (rx_buffer.end + 1) % UART_RX_BUFFER_SIZE;
|
2011-10-05 22:41:19 +02:00
|
|
|
|
|
|
|
/* Add next byte to ringbuffer if it has space available. */
|
2011-10-05 22:43:17 +02:00
|
|
|
if (write_pointer != rx_buffer.start){
|
|
|
|
rx_buffer.buffer[rx_buffer.end] = UDR0;
|
|
|
|
rx_buffer.end = write_pointer;
|
|
|
|
}
|
2011-10-05 22:41:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ISR(USART_UDRE_vect){
|
2011-10-05 22:43:17 +02:00
|
|
|
int read_pointer = (tx_buffer.start + 1) % UART_TX_BUFFER_SIZE;
|
|
|
|
|
2011-10-09 00:42:27 +02:00
|
|
|
/* Transmit next byte if data available in ringbuffer. */
|
2011-10-05 22:43:17 +02:00
|
|
|
if (read_pointer != tx_buffer.end) {
|
|
|
|
UDR0 = tx_buffer.buffer[read_pointer];
|
|
|
|
tx_buffer.start = read_pointer;
|
|
|
|
} else {
|
|
|
|
/* Nothing to send. Disable the transmit interrupt for serial port 0. */
|
|
|
|
UCSR0B &= ~_BV(UDRIE0);
|
|
|
|
}
|
2011-10-05 22:41:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|