-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUARTStream.cpp
More file actions
86 lines (71 loc) · 1.78 KB
/
UARTStream.cpp
File metadata and controls
86 lines (71 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "UARTStream.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#define INBUFSIZ 64
#define OUTBUFSIZ 16
#define INMASK (INBUFSIZ-1)
#define OUTMASK (OUTBUFSIZ-1)
#define BAUD 115200
#include <util/setbaud.h>
struct UART_INBUFFER {
volatile uint8_t head;
volatile uint8_t tail;
volatile uint8_t data[INBUFSIZ];
};
volatile static struct UART_INBUFFER inbuffer;
struct UART_OUTBUFFER {
volatile uint8_t head;
volatile uint8_t tail;
volatile uint8_t data[OUTBUFSIZ];
};
volatile static struct UART_OUTBUFFER outbuffer;
int uart_putchar(char c, FILE *stream) {
uint8_t next = (outbuffer.head+1) & OUTMASK;
while (next == outbuffer.tail) { /*wdt_reset();*/ }
outbuffer.data[outbuffer.head] = c;
// Now there definitely is data in the buffer.
outbuffer.head = next;
UCSR0B |= (1 << UDRIE0);
UCSR0A |= (1 << TXC0);
return c;
}
ISR(USART_RX_vect) {
uint8_t next = (inbuffer.head+1) & INMASK;
if (next != inbuffer.tail) {
inbuffer.data[inbuffer.head] = UDR0;
inbuffer.head = next;
}
}
ISR(USART_UDRE_vect) {
if (outbuffer.head == outbuffer.tail) {
UCSR0B &= ~(1<<UDRIE0);
} else {
uint8_t data = outbuffer.data[outbuffer.tail];
outbuffer.tail = (outbuffer.tail+1) & OUTMASK;
UDR0 = data;
}
}
int uart_getchar(FILE *stream) {
if (inbuffer.head == inbuffer.tail)
return -1;
int result = inbuffer.data[inbuffer.tail];
inbuffer.tail = (inbuffer.tail+1) & INMASK;
return result;
}
size_t uart_available() {
return (inbuffer.head-inbuffer.tail) & INMASK;
}
size_t uart_txSpace() {
return (inbuffer.tail-inbuffer.head - 1) & INMASK;
}
void uart_waitFlushed() {
while(outbuffer.head != outbuffer.tail)
;
}
void uart_init() {
UBRR0 = UBRR_VALUE;
UCSR0A = (USE_2X<<1);
UCSR0B = (1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0);
UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);
}