-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
250 lines (212 loc) · 5.56 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
#include <avr/interrupt.h>
#include <avr/io.h>
#include <stdio.h>
#include <string.h>
#include "uart.h"
// UART0 - This one connects to the USB
/* Buffers */
#define UART0_RX_BUFFER_SIZE 32 /* 2,4,8,16,32,64,128 or 256 bytes */
#define UART0_TX_BUFFER_SIZE 32
#define UART0_RX_BUFFER_MASK (UART0_RX_BUFFER_SIZE - 1)
#define UART0_TX_BUFFER_MASK (UART0_RX_BUFFER_SIZE - 1)
/* Static Variables */
static unsigned char UART0_RxBuf[UART0_RX_BUFFER_SIZE];
static volatile unsigned char UART0_RxHead;
static volatile unsigned char UART0_RxTail;
static unsigned char UART0_TxBuf[UART0_TX_BUFFER_SIZE];
static volatile unsigned char UART0_TxHead;
static volatile unsigned char UART0_TxTail;
// UART1 - This one connects to the HEADER
/* Buffers */
#define UART1_RX_BUFFER_SIZE 32 /* 2,4,8,16,32,64,128 or 256 bytes */
#define UART1_TX_BUFFER_SIZE 32
#define UART1_RX_BUFFER_MASK (UART1_RX_BUFFER_SIZE - 1)
#define UART1_TX_BUFFER_MASK (UART1_RX_BUFFER_SIZE - 1)
/* Static Variables */
static unsigned char UART1_RxBuf[UART1_RX_BUFFER_SIZE];
static volatile unsigned char UART1_RxHead;
static volatile unsigned char UART1_RxTail;
static unsigned char UART1_TxBuf[UART1_TX_BUFFER_SIZE];
static volatile unsigned char UART1_TxHead;
static volatile unsigned char UART1_TxTail;
void uart_init(baud_t ubrr0_usb,baud_t ubrr1_header)
{
//UART0
if(ubrr0_usb != DISABLED)
{
UBRR0H = (unsigned char)(ubrr0_usb>>8);
UBRR0L = (unsigned char)ubrr0_usb;
UCSR0C = (3 << UCSZ0); // 8 bit transfer
UCSR0B |= (1 << TXEN0)|(1 << RXEN0)|(1 << RXCIE0);
UART0_RxHead = 0;
UART0_RxTail = 0;
UART0_TxHead = 0;
UART0_TxTail = 0;
}
//UART1
if(ubrr1_header != DISABLED)
{
UBRR1H = (unsigned char)(ubrr1_header>>8);
UBRR1L = (unsigned char)ubrr1_header;
UCSR1C = (3 << UCSZ0); // 8 bit transfer
UCSR1B |= (1 << TXEN1)|(1 << RXEN1)|(1 << RXCIE1);
UART1_RxHead = 0;
UART1_RxTail = 0;
UART1_TxHead = 0;
UART1_TxTail = 0;
}
}
void uart_transmit(uart_channel_t channel, unsigned char c)
{
unsigned char tmphead;
switch (channel)
{
case UART0_USB:
/* Calculate buffer index */
tmphead = (UART0_TxHead + 1) & UART0_TX_BUFFER_MASK;
/* Wait for free space in buffer */
while (tmphead == UART0_TxTail);
/* Store data in buffer */
UART0_TxBuf[tmphead] = c;
/* Store new index */
UART0_TxHead = tmphead;
/* Enable UDRE interrupt */
UCSR0B |= (1<<UDRIE0);
break;
case UART1_HEADER:
/* Calculate buffer index */
tmphead = (UART1_TxHead + 1) & UART1_TX_BUFFER_MASK;
/* Wait for free space in buffer */
while (tmphead == UART1_TxTail);
/* Store data in buffer */
UART1_TxBuf[tmphead] = c;
/* Store new index */
UART1_TxHead = tmphead;
/* Enable UDRE interrupt */
UCSR1B |= (1<<UDRIE1);
break;
case UART_BOTH:
uart_transmit(UART0_USB,c);
uart_transmit(UART1_HEADER,c);
break;
default:
break;
}
}
void uart_transmit_str(uart_channel_t channel, unsigned char * data)
{
for (size_t byte = 0; byte < strlen(data); byte++)
{
uart_transmit(channel,data[byte]);
}
}
unsigned char uart_receive(uart_channel_t channel)
{
unsigned char tmptail;
switch (channel)
{
case UART0_USB:
/* Wait for incoming data */
if(UART0_RxHead == UART0_RxTail) break;
/* Calculate buffer index */
tmptail = (UART0_RxTail + 1) & UART0_RX_BUFFER_MASK;
/* Store new index */
UART0_RxTail = tmptail;
/* Return data */
return UART0_RxBuf[tmptail];
case UART1_HEADER:
/* Wait for incoming data */
if(UART1_RxHead == UART1_RxTail) break;
/* Calculate buffer index */
tmptail = (UART1_RxTail + 1) & UART1_RX_BUFFER_MASK;
/* Store new index */
UART1_RxTail = tmptail;
/* Return data */
return UART1_RxBuf[tmptail];
case UART_BOTH:
/* Not implemented for UART_BOTH*/
default:
return '0';
}
return '0';
}
int uart_has_new_message(uart_channel_t channel)
{
switch (channel)
{
case UART0_USB:
return (UART0_RxHead-UART0_RxTail);
case UART1_HEADER:
return (UART1_RxHead-UART1_RxTail);
case UART_BOTH:
default:
break;
}
return 0;
}
ISR(USART0_RX_vect)
{
unsigned char data;
unsigned char tmphead;
/* Read the received data */
data = UDR0;
/* Calculate buffer index */
tmphead = (UART0_RxHead + 1) & UART0_RX_BUFFER_MASK;
/* Store new index */
UART0_RxHead = tmphead;
if (tmphead == UART0_RxTail) {
/* ERROR! Receive buffer overflow */
}
/* Store received data in buffer */
UART0_RxBuf[tmphead] = data;
}
ISR(USART0_UDRE_vect)
{
unsigned char tmptail;
/* Check if all data is transmitted */
if (UART0_TxHead != UART0_TxTail)
{
/* Calculate buffer index */
tmptail = ( UART0_TxTail + 1 ) & UART0_TX_BUFFER_MASK;
/* Store new index */
UART0_TxTail = tmptail;
/* Start transmission */
UDR0 = UART0_TxBuf[tmptail];
} else {
/* Disable UDRE interrupt */
UCSR0B &= ~(1<<UDRIE0);
}
}
ISR(USART1_RX_vect)
{
unsigned char data;
unsigned char tmphead;
/* Read the received data */
data = UDR1;
/* Calculate buffer index */
tmphead = (UART1_RxHead + 1) & UART1_RX_BUFFER_MASK;
/* Store new index */
UART1_RxHead = tmphead;
if (tmphead == UART1_RxTail) {
/* ERROR! Receive buffer overflow */
}
/* Store received data in buffer */
UART1_RxBuf[tmphead] = data;
}
ISR(USART1_UDRE_vect)
{
unsigned char tmptail;
/* Check if all data is transmitted */
if (UART1_TxHead != UART1_TxTail)
{
/* Calculate buffer index */
tmptail = ( UART1_TxTail + 1 ) & UART1_TX_BUFFER_MASK;
/* Store new index */
UART1_TxTail = tmptail;
/* Start transmission */
UDR1 = UART1_TxBuf[tmptail];
} else {
/* Disable UDRE interrupt */
UCSR1B &= ~(1<<UDRIE1);
}
}