-
Notifications
You must be signed in to change notification settings - Fork 1
/
uart2.c
379 lines (343 loc) · 8.15 KB
/
uart2.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*! \file uart2.c \brief Dual UART driver with buffer support. */
//*****************************************************************************
//
// File Name : 'uart2.c'
// Title : Dual UART driver with buffer support
// Author : Pascal Stang - Copyright (C) 2000-2004
// Created : 11/20/2000
// Revised : 07/04/2004
// Version : 1.0
// Target MCU : ATMEL AVR Series
// Editor Tabs : 4
//
// Description : This is a UART driver for AVR-series processors with two
// hardware UARTs such as the mega161 and mega128
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
#include <avr/io.h>
#include <avr/interrupt.h>
#include "buffer.h"
#include "uart2.h"
// UART global variables
// flag variables
volatile u08 uartReadyTx[2];
volatile u08 uartBufferedTx[2];
// receive and transmit buffers
cBuffer uartRxBuffer[2];
cBuffer uartTxBuffer[2];
unsigned short uartRxOverflow[2];
#ifndef UART_BUFFER_EXTERNAL_RAM
// using internal ram,
// automatically allocate space in ram for each buffer
static char uart0RxData[UART0_RX_BUFFER_SIZE];
static char uart0TxData[UART0_TX_BUFFER_SIZE];
static char uart1RxData[UART1_RX_BUFFER_SIZE];
static char uart1TxData[UART1_TX_BUFFER_SIZE];
#endif
typedef void (*voidFuncPtru08)(unsigned char);
volatile static voidFuncPtru08 UartRxFunc[2];
void uartInit(void)
{
// initialize both uarts
uart0Init();
uart1Init();
}
void uart0Init(void)
{
// initialize the buffers
uart0InitBuffers();
// initialize user receive handlers
UartRxFunc[0] = 0;
// enable RxD/TxD and interrupts
outb(UCSR0B, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN));
// set default baud rate
uartSetBaudRate(0, UART0_DEFAULT_BAUD_RATE);
// initialize states
uartReadyTx[0] = TRUE;
uartBufferedTx[0] = FALSE;
// clear overflow count
uartRxOverflow[0] = 0;
// enable interrupts
sei();
}
void uart1Init(void)
{
// initialize the buffers
uart1InitBuffers();
// initialize user receive handlers
UartRxFunc[1] = 0;
// enable RxD/TxD and interrupts
outb(UCSR1B, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN));
// set default baud rate
uartSetBaudRate(1, UART1_DEFAULT_BAUD_RATE);
// initialize states
uartReadyTx[1] = TRUE;
uartBufferedTx[1] = FALSE;
// clear overflow count
uartRxOverflow[1] = 0;
// enable interrupts
sei();
}
void uart0InitBuffers(void)
{
#ifndef UART_BUFFER_EXTERNAL_RAM
// initialize the UART0 buffers
bufferInit(&uartRxBuffer[0], uart0RxData, UART0_RX_BUFFER_SIZE);
bufferInit(&uartTxBuffer[0], uart0TxData, UART0_TX_BUFFER_SIZE);
#else
// initialize the UART0 buffers
bufferInit(&uartRxBuffer[0], (u08*) UART0_RX_BUFFER_ADDR, UART0_RX_BUFFER_SIZE);
bufferInit(&uartTxBuffer[0], (u08*) UART0_TX_BUFFER_ADDR, UART0_TX_BUFFER_SIZE);
#endif
}
void uart1InitBuffers(void)
{
#ifndef UART_BUFFER_EXTERNAL_RAM
// initialize the UART1 buffers
bufferInit(&uartRxBuffer[1], uart1RxData, UART1_RX_BUFFER_SIZE);
bufferInit(&uartTxBuffer[1], uart1TxData, UART1_TX_BUFFER_SIZE);
#else
// initialize the UART1 buffers
bufferInit(&uartRxBuffer[1], (u08*) UART1_RX_BUFFER_ADDR, UART1_RX_BUFFER_SIZE);
bufferInit(&uartTxBuffer[1], (u08*) UART1_TX_BUFFER_ADDR, UART1_TX_BUFFER_SIZE);
#endif
}
void uartSetRxHandler(u08 nUart, void (*rx_func)(unsigned char c))
{
// make sure the uart number is within bounds
if(nUart < 2)
{
// set the receive interrupt to run the supplied user function
UartRxFunc[nUart] = rx_func;
}
}
void uartSetBaudRate(u08 nUart, u32 baudrate)
{
// calculate division factor for requested baud rate, and set it
u16 bauddiv = ((F_CPU+(baudrate*8L))/(baudrate*16L)-1);
if(nUart)
{
outb(UBRR1L, bauddiv);
#ifdef UBRR1H
outb(UBRR1H, bauddiv>>8);
#endif
}
else
{
outb(UBRR0L, bauddiv);
#ifdef UBRR0H
outb(UBRR0H, bauddiv>>8);
#endif
}
}
cBuffer* uartGetRxBuffer(u08 nUart)
{
// return rx buffer pointer
return &uartRxBuffer[nUart];
}
cBuffer* uartGetTxBuffer(u08 nUart)
{
// return tx buffer pointer
return &uartTxBuffer[nUart];
}
void uartSendByte(u08 nUart, u08 txData)
{
// wait for the transmitter to be ready
// while(!uartReadyTx[nUart]);
// send byte
if(nUart)
{
while(!(UCSR1A & (1<<UDRE)));
outb(UDR1, txData);
}
else
{
while(!(UCSR0A & (1<<UDRE)));
outb(UDR0, txData);
}
// set ready state to FALSE
uartReadyTx[nUart] = FALSE;
}
void uart0SendByte(u08 data)
{
// send byte on UART0
uartSendByte(0, data);
}
void uart1SendByte(u08 data)
{
// send byte on UART1
uartSendByte(1, data);
}
int uart0GetByte(void)
{
// get single byte from receive buffer (if available)
u08 c;
if(uartReceiveByte(0,&c))
return c;
else
return -1;
}
int uart1GetByte(void)
{
// get single byte from receive buffer (if available)
u08 c;
if(uartReceiveByte(1,&c))
return c;
else
return -1;
}
u08 uartReceiveByte(u08 nUart, u08* rxData)
{
// make sure we have a receive buffer
if(uartRxBuffer[nUart].size)
{
// make sure we have data
if(uartRxBuffer[nUart].datalength)
{
// get byte from beginning of buffer
*rxData = bufferGetFromFront(&uartRxBuffer[nUart]);
return TRUE;
}
else
return FALSE; // no data
}
else
return FALSE; // no buffer
}
void uartFlushReceiveBuffer(u08 nUart)
{
// flush all data from receive buffer
bufferFlush(&uartRxBuffer[nUart]);
}
u08 uartReceiveBufferIsEmpty(u08 nUart)
{
return (uartRxBuffer[nUart].datalength == 0);
}
void uartAddToTxBuffer(u08 nUart, u08 data)
{
// add data byte to the end of the tx buffer
bufferAddToEnd(&uartTxBuffer[nUart], data);
}
void uart0AddToTxBuffer(u08 data)
{
uartAddToTxBuffer(0,data);
}
void uart1AddToTxBuffer(u08 data)
{
uartAddToTxBuffer(1,data);
}
void uartSendTxBuffer(u08 nUart)
{
// turn on buffered transmit
uartBufferedTx[nUart] = TRUE;
// send the first byte to get things going by interrupts
uartSendByte(nUart, bufferGetFromFront(&uartTxBuffer[nUart]));
}
u08 uartSendBuffer(u08 nUart, char *buffer, u16 nBytes)
{
register u08 first;
register u16 i;
// check if there's space (and that we have any bytes to send at all)
if((uartTxBuffer[nUart].datalength + nBytes < uartTxBuffer[nUart].size) && nBytes)
{
// grab first character
first = *buffer++;
// copy user buffer to uart transmit buffer
for(i = 0; i < nBytes-1; i++)
{
// put data bytes at end of buffer
bufferAddToEnd(&uartTxBuffer[nUart], *buffer++);
}
// send the first byte to get things going by interrupts
uartBufferedTx[nUart] = TRUE;
uartSendByte(nUart, first);
// return success
return TRUE;
}
else
{
// return failure
return FALSE;
}
}
// UART Transmit Complete Interrupt Function
void uartTransmitService(u08 nUart)
{
// check if buffered tx is enabled
if(uartBufferedTx[nUart])
{
// check if there's data left in the buffer
if(uartTxBuffer[nUart].datalength)
{
// send byte from top of buffer
if(nUart)
outb(UDR1, bufferGetFromFront(&uartTxBuffer[1]) );
else
outb(UDR0, bufferGetFromFront(&uartTxBuffer[0]) );
}
else
{
// no data left
uartBufferedTx[nUart] = FALSE;
// return to ready state
uartReadyTx[nUart] = TRUE;
}
}
else
{
// we're using single-byte tx mode
// indicate transmit complete, back to ready
uartReadyTx[nUart] = TRUE;
}
}
// UART Receive Complete Interrupt Function
void uartReceiveService(u08 nUart)
{
u08 c;
// get received char
if(nUart)
c = inb(UDR1);
else
c = inb(UDR0);
// if there's a user function to handle this receive event
if(UartRxFunc[nUart])
{
// call it and pass the received data
UartRxFunc[nUart](c);
}
else
{
// otherwise do default processing
// put received char in buffer
// check if there's space
if( !bufferAddToEnd(&uartRxBuffer[nUart], c) )
{
// no space in buffer
// count overflow
uartRxOverflow[nUart]++;
}
}
}
UART_INTERRUPT_HANDLER(USART0_TX_vect)
{
// service UART0 transmit interrupt
uartTransmitService(0);
}
UART_INTERRUPT_HANDLER(USART1_TX_vect)
{
// service UART1 transmit interrupt
uartTransmitService(1);
}
UART_INTERRUPT_HANDLER(USART0_RX_vect)
{
// service UART0 receive interrupt
uartReceiveService(0);
}
UART_INTERRUPT_HANDLER(USART1_RX_vect)
{
// service UART1 receive interrupt
uartReceiveService(1);
}