-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
274 lines (225 loc) · 5.66 KB
/
uart.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* USART-Init beim ATmegaXX */
/* modified by siro 25.4.2010 to add atmega48 support */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include "uart.h"
#define UART_BAUD_RATE 19200
//#define UART_INTERRUPT
#define UART_RXBUFSIZE 50
#define UART_TXBUFSIZE 50
#if defined(__AVR_ATmega128__)
#define UCSRB UCSR0B
#define UCSRC UCSR0C
#define UDR UDR0
#define UBRRH UBRR0H
#define UBRRL UBRR0L
#define URSEL UMSEL
#define UART_UDRE_VECTOR SIG_UART_DATA
#define UART_RECV_VECTOR SIG_UART_RECV
#elif defined(__AVR_ATmega48__) | defined(__AVR_ATmega168__) | defined(__AVR_ATmega162__) | defined(__AVR_ATmega644__) | defined(__AVR_ATmega644P__)
#define UCSRB UCSR0B
#define UCSRC UCSR0C
#define UDR UDR0
#define UBRRH UBRR0H
#define UBRRL UBRR0L
#define TXEN TXEN0
#define RXEN RXEN0
#define UCSRA UCSR0A
#define UDRIE UDRIE0
#define UCSZ0 UCSZ00
#define RXCIE RXCIE0
#define TXCIE TXCIE0
#define RXC RXC0
#define UDRE UDRE0
#define UART_UDRE_VECTOR USART0_UDRE_vect
#define UART_RECV_VECTOR USART0_RX_vect
#elif defined(__AVR_ATmega8__) | defined(__AVR_ATmega32__)
#define UART_UDRE_VECTOR USART_UDRE_vect
#define UART_RECV_VECTOR USART_RXC_vect
#else
#define UART_UDRE_VECTOR USART_UDRE_vect
#define UART_RECV_VECTOR USART_RXC_vect
#warning Your AVR is not officially supported by the uart-lib, it might not work.
#endif
#ifdef UART_LEDS
#warning UART_LEDS is truned on, which means that PC0 and PC1 will be toggled by receiving/transmitting bytes!!
#endif
#define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) ((F_OSC+(UART_BAUD_RATE)*8L)/((UART_BAUD_RATE)*16L)-1)
#ifdef UART_INTERRUPT
volatile static char rxbuf[UART_RXBUFSIZE];
volatile static char txbuf[UART_TXBUFSIZE];
volatile static char *volatile rxhead, *volatile rxtail;
volatile static char *volatile txhead, *volatile txtail;
ISR(UART_UDRE_VECTOR) {
if ( txhead == txtail ) {
#ifdef UART_LEDS
PORTC ^= 0x01;
#endif
UCSRB &= ~(1 << UDRIE); /* disable data register empty IRQ */
} else {
UDR = *txtail; /* schreibt das Zeichen x auf die Schnittstelle */
if (++txtail == (txbuf + UART_TXBUFSIZE)) txtail = txbuf;
}
}
ISR(UART_RECV_VECTOR) {
int diff;
#ifdef UART_LEDS
PORTC ^= 0x02;
#endif
/* buffer full? */
diff = rxhead - rxtail;
if ( diff < 0 ) diff += UART_RXBUFSIZE;
if (diff < UART_RXBUFSIZE -1) {
// buffer NOT full
*rxhead = UDR;
if (++rxhead == (rxbuf + UART_RXBUFSIZE)) rxhead = rxbuf;
} else {
UDR; //reads the buffer to clear the interrupt condition
}
}
#endif // UART_INTERRUPT
void uart_init() {
#ifdef UART_LEDS
DDRC |= 0x03; // Port C LED outputs
#endif
PORTD |= 0x01; //Pullup an RXD an
UCSRA = 0;
UCSRB = (1<<TXEN) | ( 1 << RXEN); //UART RX und TX einschalten
UCSRC = (3<<UCSZ0); //Asynchron 8N1
UBRRH=(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)>>8);
UBRRL=(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_CPU));
#ifdef UART_INTERRUPT
// init buffers
rxhead = rxtail = rxbuf;
txhead = txtail = txbuf;
// activate rx IRQ
UCSRB |= (1 << RXCIE);
#endif // UART_INTERRUPT
}
#ifdef UART_INTERRUPT
void uart_putc(char c) {
volatile int diff;
/* buffer full? */
do {
diff = txhead - txtail;
if ( diff < 0 ) diff += UART_TXBUFSIZE;
} while ( diff >= UART_TXBUFSIZE -1 );
cli();
*txhead = c;
if (++txhead == (txbuf + UART_TXBUFSIZE)) txhead = txbuf;
UCSRB |= (1 << UDRIE); /* enable data register empty IRQ */
sei();
}
#else // WITHOUT INTERRUPT
void uart_putc(char c) {
while (!(UCSRA & (1<<UDRE))); /* warten bis Senden moeglich */
UDR = c; /* schreibt das Zeichen x auf die Schnittstelle */
}
#endif // UART_INTERRUPT
void uart_putstr(char *str) {
while(*str) {
uart_putc(*str++);
}
}
void uart_putstr_P(PGM_P str) {
char tmp;
while((tmp = pgm_read_byte(str))) {
uart_putc(tmp);
str++;
}
}
#ifdef UART_INTERRUPT
char uart_getc()
{
char val;
while(rxhead==rxtail) ;
val = *rxtail;
if (++rxtail == (rxbuf + UART_RXBUFSIZE)) rxtail = rxbuf;
return val;
}
#else // WITHOUT INTERRUPT
char uart_getc()
{
while (!(UCSRA & (1<<RXC))); // warten bis Zeichen verfuegbar
return UDR; // Zeichen aus UDR zurueckgeben
}
#endif // UART_INTERRUPT
// returns 1 on success
#ifdef UART_INTERRUPT
char uart_getc_nb(char *c)
{
if (rxhead==rxtail) return 0;
*c = *rxtail;
if (++rxtail == (rxbuf + UART_RXBUFSIZE)) rxtail = rxbuf;
return 1;
}
#else // WITHOUT INTERRUPT
char uart_getc_nb(char *c)
{
if (UCSRA & (1<<RXC)) { // Zeichen verfuegbar
*c = UDR;
return 1;
}
return 0;
}
#endif // UART_INTERRUPT
#ifdef UART_HEXDUMP
//hexdump utility
void uart_hexdump(unsigned char *buf, int len)
{
unsigned char x=0;
char sbuf[3];
while(len--){
itoa(*buf++, sbuf, 16);
if (sbuf[1] == 0) uart_putc(' ');
uart_putstr(sbuf);
uart_putc(' ');
if(++x == 16) {
uart_putstr_P(PSTR("\r\n"));
x = 0;
}
}
}
#endif
#ifdef UART_GETLINE
//get one Cariage return terminated line
//echo charakters back on Uart
//returns buffer with zero terminated line on success, 0 pointer otherwise
char * uart_getline(){
static char buffer[UART_LINE_BUFFER_SIZE];
static char * pos = buffer;
char tmp;
while((tmp = uart_getc())){
if(tmp == '\r'){
*pos = 0; //terminate line
pos = buffer; //reset pointer
return buffer; //and return the buffer
}
if((tmp != '\n') && (pos < buffer+UART_LINE_BUFFER_SIZE-1)){ //buffer full?
*pos++ = tmp; //no: write character to buffer
//uart_putc (tmp);
}
}
return 0;
}
#endif
#ifdef UART_INTERRUPT
char uart_rxbuffer_notempty(void)
{
if (rxhead==rxtail)
return 0;
else
return 1;
}
#endif
#ifdef UART_INTERRUPT
#if UART_RXBUFSIZE < 255
uint8_t uart_getrxqueuelength(void)
#else
uint16_t uart_getrxqueuelength(void)
#endif
{
return (rxhead - rxtail);
}
#endif