-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
326 lines (291 loc) · 7.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
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
#include <string.h>
#include "LPC2300.h"
#include "interrupt.h"
#include "uart.h"
#include "lcd.h"
#define BUFFER_SIZE 255
/* With this settings we get an exact value for 19200 Baud */
#define PCLK 72000000
#define U0_BPS 19200
#define U1_BPS 4800
#define DIVADDVAL 7
#define MULVAL 8
#define U0_DLVAL ((PCLK*MULVAL/(MULVAL+DIVADDVAL)/U0_BPS+8)>>4)
#define U1_DLVAL ((PCLK*MULVAL/(MULVAL+DIVADDVAL)/U1_BPS+8)>>4)
static volatile struct
{
int rptr;
int wptr;
int count;
BYTE buff[BUFFER_SIZE];
} TxFifo0, RxFifo0;
static volatile int TxRun0;
static volatile struct
{
int rptr;
int wptr;
int count;
BYTE buff[BUFFER_SIZE];
} TxFifo1, RxFifo1;
static volatile int TxRun1;
void Isr_UART0 (void)
{
int d, idx, cnt, iir;
do {
iir = U0IIR; /* Get Interrupt ID*/
if (iir & 1) break; /* Exit if there is no interrupt */
switch (iir & 6) {
case 4: /* Receive FIFO is half filled or timeout occured */
idx = RxFifo0.wptr;
cnt = RxFifo0.count;
while (U0LSR & 0x01) { /* Receive all data in the FIFO */
d = U0RBR;
if (cnt < BUFFER_SIZE) { /* Store data if buffer is not full */
RxFifo0.buff[idx++] = d;
cnt++;
if(idx == BUFFER_SIZE) idx = 0;
}
}
RxFifo0.wptr = idx;
RxFifo0.count = cnt;
break;
case 2: /* Transmisson FIFO empty */
cnt = TxFifo0.count;
if (cnt) {
idx = TxFifo0.rptr;
for (d = 12; d && cnt; d--, cnt--) { /* Store data into FIFO (max 12 chrs) */
U0THR = TxFifo0.buff[idx++];
if(idx == BUFFER_SIZE) idx = 0;
}
TxFifo0.rptr = idx;
TxFifo0.count = cnt;
} else {
TxRun0 = 0; /* When no data in the buffer, clear running flag */
}
break;
default: /* Data error or break detected */
d = U0LSR;
d = U0RBR;
break;
}
} while(1);
}
int uart0_test (void)
{
return RxFifo0.count;
}
BYTE uart0_get (void)
{
return U0get(-1);
}
extern volatile UINT Timer;
BYTE U0get (int mtempo)
{
BYTE d;
int idx;
static int brilho=0x38;
int k,seg;
int tempo, tini;
tini = Timer;
tempo = mtempo / 1000;
/* Wait while Rx buffer is empty */
seg = RTC_SEC;
k=10000;
while (!RxFifo0.count) {
idx = LEBOTAO;
if((mtempo > 0) && (Timer-tini) >= mtempo) return ' ';
switch(idx){
case SW3: brilho += 2; // KB4 incrementa contraste
case SW4: brilho --; // KB5 decrementa contraste
break;
case SW2: if(k==0) return ' ';
break;
case SW1: tempo=5;
break;
default: if(k) k--; break;
}
if(tempo > 0 && seg!=RTC_SEC) {
LCDcomando(0x86);
LCDputchar(tempo+'0');
LCDputchar(' ');
LCDcomando(0x87);
LCDputs(" T=");
if((--tempo) == 0) return ' ';
seg = RTC_SEC;
}
}
U0IER = 0; /* Disable interrupts */
idx = RxFifo0.rptr;
d = RxFifo0.buff[idx]; /* Get a byte from Rx buffer */
RxFifo0.rptr = (idx + 1) % BUFFER_SIZE;
RxFifo0.count--;
U0IER = 0x07; /* Enable interrupt */
return d;
}
void uart0_put (BYTE d)
{
#if 0
while (!(U0LSR & 0x20));
U0THR = d;
#else
int idx, cnt;
/* Wait for buffer ready */
while (TxFifo0.count >= BUFFER_SIZE);
U0IER = 0x05; /* Disable Tx Interrupt */
if (!TxRun0) { /* When not in runnig, trigger transmission */
U0THR = d;
TxRun0 = 1;
} else { /* When transmission is runnig, store the data into the Tx buffer */
cnt = TxFifo0.count;
idx = TxFifo0.wptr;
TxFifo0.buff[idx] = d;
TxFifo0.wptr = (idx + 1) % BUFFER_SIZE;
TxFifo0.count = ++cnt;
}
U0IER = 0x07; /* Enable Tx Interrupt */
#endif
}
void uart0_init (void)
{
U0IER = 0x00;
RegisterVector(UART0_INT, Isr_UART0, PRI_LOWEST, CLASS_IRQ);
/* Set PCLKSEL0 to divide by one: U0PCLK = 72MHz */
PCONP |= 8; /* Turn UART0 on */
PCLKSEL0 = (PCLKSEL0 & (~0xc0)) | 0x40;
/* Attach UART0 unit to I/O pad */
PINSEL0 = (PINSEL0 & (~0xf0)) | 0x50;
/* Initialize UART0 */
U0LCR = 0x83; /* Select divisor latch */
U0DLM = U0_DLVAL >> 8; /* Initialize BRG */
U0DLL = U0_DLVAL & 0xff;
U0FDR = (MULVAL << 4) | DIVADDVAL;
U0LCR = 0x03; /* Set serial format N81 and deselect divisor latch */
U0FCR = 0x87; /* Enable FIFO */
U0TER = 0x80; /* Enable Tansmission */
/* Clear Tx/Rx FIFOs */
TxFifo0.rptr = 0;
TxFifo0.wptr = 0;
TxFifo0.count = 0;
RxFifo0.rptr = 0;
RxFifo0.wptr = 0;
RxFifo0.count = 0;
/* Enable Tx/Rx/Error interrupts */
U0IER = 0x07;
}
/****************************************************************************
* UART1 *
****************************************************************************/
void Isr_UART1 (void)
{
int d, idx, cnt, iir;
do {
iir = U1IIR; /* Get Interrupt ID*/
if (iir & 1) break; /* Exit if there is no interrupt */
switch (iir & 6) {
case 4: /* Receive FIFO is half filled or timeout occured */
idx = RxFifo1.wptr;
cnt = RxFifo1.count;
while (U1LSR & 0x01) { /* Receive all data in the FIFO */
d = U1RBR;
if (cnt < BUFFER_SIZE) { /* Store data if buffer is not full */
RxFifo1.buff[idx++] = d;
cnt++;
if(idx == BUFFER_SIZE) idx = 0;
}
}
RxFifo1.wptr = idx;
RxFifo1.count = cnt;
break;
case 2: /* Transmisson FIFO empty */
cnt = TxFifo1.count;
if (cnt) {
idx = TxFifo1.rptr;
for (d = 12; d && cnt; d--, cnt--) { /* Store data into FIFO (max 12 chrs) */
U1THR = TxFifo1.buff[idx++];
if(idx == BUFFER_SIZE) idx = 0;
}
TxFifo1.rptr = idx;
TxFifo1.count = cnt;
} else {
TxRun1 = 0; /* When no data in the buffer, clear running flag */
}
break;
default: /* Data error or break detected */
d = U1LSR;
d = U1RBR;
break;
}
} while(1);
}
int uart1_test (void)
{
return RxFifo1.count;
}
BYTE uart1_get (void)
{
return U1get(-1);
}
BYTE U1get (int mtempo)
{
BYTE d;
int idx;
while (!RxFifo1.count){};
U1IER = 0; /* Disable interrupts */
idx = RxFifo1.rptr;
d = RxFifo1.buff[idx]; /* Get a byte from Rx buffer */
RxFifo1.rptr = (idx + 1) % BUFFER_SIZE;
RxFifo1.count--;
U1IER = 0x07; /* Enable interrupt */
return d;
}
void uart1_put (BYTE d)
{
#if 0
while (!(U0LSR & 0x20));
U1THR = d;
#else
int idx, cnt;
/* Wait for buffer ready */
while (TxFifo1.count >= BUFFER_SIZE);
U1IER = 0x05; /* Disable Tx Interrupt */
if (!TxRun1) { /* When not in runnig, trigger transmission */
U1THR = d;
TxRun1 = 1;
} else { /* When transmission is runnig, store the data into the Tx buffer */
cnt = TxFifo1.count;
idx = TxFifo1.wptr;
TxFifo1.buff[idx] = d;
TxFifo1.wptr = (idx + 1) % BUFFER_SIZE;
TxFifo1.count = ++cnt;
}
U1IER = 0x07; /* Enable Tx Interrupt */
#endif
}
void uart1_init (void)
{
U1IER = 0x00;
RegisterVector(UART1_INT, Isr_UART1, PRI_LOWEST, CLASS_IRQ);
/* Set PCLKSEL0 to divide by one: U0PCLK = 72MHz */
PCONP |= 0x10; /* Turn UART1 on */
PCLKSEL0 = (PCLKSEL0 & (~0x30)) | 0x100;
/* Attach UART1 unit to I/O pad */
PINSEL0 |= 0x40000000; /* Enable TxD1 P0.15 */
PINSEL1 |= 0x00000001; /* Enable RxD1 P0.16 */
/* Initialize UART0 */
U1LCR = 0x83; /* Select divisor latch */
U1DLM = U1_DLVAL >> 8; /* Initialize BRG */
U1DLL = U1_DLVAL & 0xff;
U1FDR = (MULVAL << 4) | DIVADDVAL;
U1LCR = 0x03; /* Set serial format N81 and deselect divisor latch */
U1FCR = 0x87; /* Enable FIFO */
U1TER = 0x80; /* Enable Tansmission */
/* Clear Tx/Rx FIFOs */
TxFifo1.rptr = 0;
TxFifo1.wptr = 0;
TxFifo1.count = 0;
RxFifo1.rptr = 0;
RxFifo1.wptr = 0;
RxFifo1.count = 0;
/* Enable Tx/Rx/Error interrupts */
U1IER = 0x07;
}