-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
638 lines (571 loc) · 15.3 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
Copyright (c) 2011, Ascending Technologies GmbH
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
#include "LPC214x.h"
#include "system.h"
#include "main.h"
#include "uart.h"
#include "irq.h"
#include "hardware.h"
#include "gpsmath.h"
#include "ssp.h"
#include "sdk.h"
#include "ublox.h"
#include "asctecCommIntfOnboard.h"
unsigned char packets;
unsigned char DataOutputsPerSecond;
unsigned int uart_cnt;
unsigned char data_requested=0;
extern int ZeroDepth;
unsigned short current_chksum;
unsigned char chksum_to_check=0;
unsigned char chksum_trigger=1;
unsigned char transmission_running=0;
unsigned char transmission1_running=0;
unsigned char trigger_transmission=0;
volatile unsigned char baudrate1_change=0;
unsigned char send_buffer[16];
unsigned char *tx_buff;
unsigned char UART_syncstate=0;
unsigned char UART1_syncstate=0;
unsigned int UART_rxcount=0;
unsigned char *UART_rxptr;
unsigned int UART1_rxcount=0;
unsigned char *UART1_rxptr;
unsigned char UART_CalibDoneFlag = 0;
static volatile unsigned char rb_busy=0;
unsigned char startstring[]={'>','*','>'};
unsigned char stopstring[]={'<','#','<'};
void uart1ISR(void) __irq
{
unsigned char t;
IENABLE;
unsigned iir = U1IIR;
// Handle UART interrupt
switch ((iir >> 1) & 0x7)
{
case 1:
// THRE interrupt
if (ringbuffer1(RBREAD, &t, 1))
{
transmission1_running=1;
UART1WriteChar(t);
}
else
{
transmission1_running=0;
}
break;
case 2:
// RX interrupt
uBloxReceiveHandler(U1RBR);
break;
case 3:
// RLS interrupt
break;
case 6:
// CTI interrupt
break;
}
IDISABLE;
VICVectAddr = 0; /* Acknowledge Interrupt */
}
void uart0ISR(void) __irq
{
unsigned char UART_rxdata;
#ifdef MATLAB
unsigned char t;
#endif
// Read IIR to clear interrupt and find out the cause
IENABLE;
unsigned iir = U0IIR;
// Handle UART interrupt
switch ((iir >> 1) & 0x7)
{
case 1:
#ifdef MATLAB
if (UART_Matlab_fifo(RBREAD, &t, 1))
{
transmission_running=1;
UARTWriteChar(t);
}
else
{
transmission_running=0;
}
#else
if (aciTxRingBufferByteAvailable())
UARTWriteChar(aciTxRingBufferGetNextByte());
#endif
break;
case 2:
// RDA interrupt - put your HL_serial_0 receive state machine here!
UART_rxdata = U0RBR;
#ifdef MATLAB
if (UART_syncstate==0)
{
if (UART_rxdata=='>') UART_syncstate++; else UART_syncstate=0;
}
else if (UART_syncstate==1)
{
if (UART_rxdata=='*') UART_syncstate++; else UART_syncstate=0;
}
else if (UART_syncstate==2)
{
if (UART_rxdata=='>') UART_syncstate++; else UART_syncstate=0;
}
else if (UART_syncstate==3)
{
if (UART_rxdata=='p') //data pending p=flight params
{
UART_syncstate=4;
UART_rxcount = sizeof(matlab_params_tmp);
UART_rxptr = (unsigned char*) &matlab_params_tmp;
}
else if (UART_rxdata=='c') //data pending c=uart ctrl
{
UART_syncstate = 5;
UART_rxcount = 24+2;
UART_rxptr = (unsigned char*) &matlab_uart_tmp;
}
else if (UART_rxdata=='s') //data pending s=save to eeprom
{
UART_syncstate=0;
triggerSaveMatlabParams=1;
}
else if (UART_rxdata=='h') // stop debug transmission
{
UART_syncstate=0;
xbee_send_flag=0;
}
else
UART_syncstate=0;
}
else if (UART_syncstate==4)
{
UART_rxcount--;
*UART_rxptr=UART_rxdata;
UART_rxptr++;
if (UART_rxcount==0)
{
unsigned short crc_comp=0;
UART_syncstate=0;
crc_comp = crc16(&matlab_params_tmp, sizeof(matlab_params_tmp)-4);
if (crc_comp==matlab_params_tmp.crc)
{
memcpy(&matlab_params, &matlab_params_tmp, sizeof(matlab_params));
//parameter_beep=ControllerCyclesPerSecond/10;
}
}
}
else if (UART_syncstate==5)
{
UART_rxcount--;
*UART_rxptr=UART_rxdata;
UART_rxptr++;
if (UART_rxcount==0)
{
unsigned short crc_comp=0;
UART_syncstate=0;
crc_comp = crc16(&matlab_uart_tmp, 24);
if (crc_comp == matlab_uart_tmp.crc)
{
memcpy(&matlab_uart, &matlab_uart_tmp, sizeof(matlab_uart));
xbee_send_flag=1;
}
}
}
else UART_syncstate=0;
#else
aciReceiveHandler(UART_rxdata);
if (UART_syncstate==0)
{
if (UART_rxdata=='>') UART_syncstate++; else UART_syncstate=0;
}
else if (UART_syncstate==1)
{
if (UART_rxdata=='*') UART_syncstate++; else UART_syncstate=0;
}
else if (UART_syncstate==2)
{
if (UART_rxdata=='>') UART_syncstate++; else UART_syncstate=0;
}
else if (UART_syncstate==3)
{
if (UART_rxdata=='b')
{
UART_syncstate=4;
}else
UART_syncstate=0;
//synchronized to start string => receive your data from here
}
else if (UART_syncstate==4)
{
if (UART_rxdata=='o')
{
UART_syncstate=5;
}else if (UART_rxdata=='s')
{
//send sync
UARTWriteChar('S');
UART_syncstate=0;
}
else
UART_syncstate=0;
//synchronized to start string => receive your data from here
}
else if (UART_syncstate==5)
{
if (UART_rxdata=='o')
{
UART_syncstate=6;
}else
UART_syncstate=0;
//synchronized to start string => receive your data from here
}
else if (UART_syncstate==6)
{
if (UART_rxdata=='t')
{
//start LPC bootloader
UART_syncstate=0;
enter_isp();
}
UART_syncstate=0;
//synchronized to start string => receive your data from here
}
else UART_syncstate=0;
#endif
break;
case 3:
// RLS interrupt
break;
case 6:
// CTI interrupt
break;
}
IDISABLE;
VICVectAddr = 0; // Acknowledge Interrupt
}
void UARTInitialize(unsigned int baud)
{
unsigned int divisor = peripheralClockFrequency() / (16 * baud);
//UART0
U0LCR = 0x83; /* 8 bit, 1 stop bit, no parity, enable DLAB */
U0DLL = divisor & 0xFF;
U0DLM = (divisor >> 8) & 0xFF;
U0LCR &= ~0x80; /* Disable DLAB */
U0FCR = 1;
}
void UART1Initialize(unsigned int baud)
{
unsigned int divisor = peripheralClockFrequency() / (16 * baud);
//UART1
U1LCR = 0x83; /* 8 bit, 1 stop bit, no parity, enable DLAB */
U1DLL = divisor & 0xFF;
U1DLM = (divisor >> 8) & 0xFF;
U1LCR &= ~0x80; /* Disable DLAB */
U1FCR = 1;
}
//Write to UART0
void UARTWriteChar(unsigned char ch)
{
while ((U0LSR & 0x20) == 0);
U0THR = ch;
}
//Write to UART1
void UART1WriteChar(unsigned char ch)
{
while ((U1LSR & 0x20) == 0);
U1THR = ch;
}
unsigned char UARTReadChar(void)
{
while ((U0LSR & 0x01) == 0);
return U0RBR;
}
unsigned char UART1ReadChar(void)
{
while ((U1LSR & 0x01) == 0);
return U1RBR;
}
void __putchar(int ch)
{
if (ch == '\n')
UARTWriteChar('\r');
UARTWriteChar(ch);
}
void UART_send(char *buffer, unsigned char length)
{
unsigned char cnt=0;
while (!(U0LSR & 0x20)); //wait until U0THR and U0TSR are both empty
while(length--)
{
U0THR = buffer[cnt++];
if(cnt>15)
{
while (!(U0LSR & 0x20)); //wait until U0THR is empty
}
}
}
void UART1_send(unsigned char *buffer, unsigned char length)
{
unsigned char cnt=0;
while(length--)
{
while (!(U1LSR & 0x20)); //wait until U1THR is empty
U1THR = buffer[cnt++];
}
}
void UART_send_ringbuffer(void)
{
unsigned char t;
if(!transmission_running)
{
if(ringbuffer(RBREAD, &t, 1))
{
transmission_running=1;
UARTWriteChar(t);
}
}
}
void UART1_send_ringbuffer(void)
{
unsigned char t;
if(!transmission1_running)
{
if(ringbuffer1(RBREAD, &t, 1))
{
transmission1_running=1;
UART1WriteChar(t);
}
}
}
void UART_SendPacket(void *data, unsigned short count, unsigned char packetdescriptor) //example to send data packets as on LL_serial_0
{
unsigned short crc;
int state;
state=ringbuffer(RBWRITE, startstring, 3);
state=ringbuffer(RBWRITE, (unsigned char *) &count, 2);
state=ringbuffer(RBWRITE, &packetdescriptor, 1);
state=ringbuffer(RBWRITE, data, count);
crc=crc16(data,count);
state=ringbuffer(RBWRITE, (unsigned char *) &crc, 2);
state=ringbuffer(RBWRITE, stopstring, 3);
UART_send_ringbuffer();
}
//example CRC16 function
unsigned short crc_update (unsigned short crc, unsigned char data)
{
data ^= (crc & 0xff);
data ^= data << 4;
return ((((unsigned short )data << 8) | ((crc>>8)&0xff)) ^ (unsigned char )(data >> 4)
^ ((unsigned short )data << 3));
}
unsigned short crc16(void* data, unsigned short cnt)
{
unsigned short crc=0xff;
unsigned char * ptr=(unsigned char *) data;
int i;
for (i=0;i<cnt;i++)
{
crc=crc_update(crc,*ptr);
ptr++;
}
return crc;
}
// no longer a ringbuffer! - now it's a FIFO
int ringbuffer(unsigned char rw, unsigned char *data, unsigned int count) //returns 1 when write/read was successful, 0 elsewise
{
static volatile unsigned char buffer[RINGBUFFERSIZE];
// static volatile unsigned int pfirst=0, plast=0; //Pointers to first and last to read byte
static volatile unsigned int read_pointer, write_pointer;
static volatile unsigned int content=0;
unsigned int p=0;
unsigned int p2=0;
if(rw==RBWRITE)
{
if(count<RINGBUFFERSIZE-content) //enough space in buffer?
{
while(p<count)
{
buffer[write_pointer++]=data[p++];
}
content+=count;
return(1);
}
}
else if(rw==RBREAD)
{
if(content>=count)
{
while(p2<count)
{
data[p2++]=buffer[read_pointer++];
}
content-=count;
if(!content) //buffer empty
{
write_pointer=0;
read_pointer=0;
}
return(1);
}
}
else if(rw==RBFREE)
{
if(content) return 0;
else return(RINGBUFFERSIZE-11);
}
return(0);
}
int ringbuffer1(unsigned char rw, unsigned char *data, unsigned int count) //returns 1 when write/read was successful, 0 elsewise
{
static volatile unsigned char buffer[RINGBUFFERSIZE];
// static volatile unsigned int pfirst=0, plast=0; //Pointers to first and last to read byte
static volatile unsigned int read_pointer, write_pointer;
static volatile unsigned int content=0;
unsigned int p=0;
unsigned int p2=0;
if(rw==RBWRITE)
{
if(count<RINGBUFFERSIZE-content) //enough space in buffer?
{
while(p<count)
{
buffer[write_pointer++]=data[p++];
}
content+=count;
return(1);
}
}
else if(rw==RBREAD)
{
if(content>=count)
{
while(p2<count)
{
data[p2++]=buffer[read_pointer++];
}
content-=count;
if(!content) //buffer empty
{
write_pointer=0;
read_pointer=0;
}
return(1);
}
}
else if(rw==RBFREE)
{
if(content) return 0;
else return(RINGBUFFERSIZE-11);
}
return(0);
}
#ifdef MATLAB
void UART_Matlab_Initialize(unsigned int baud)
{
unsigned int Divisor=0, MulVal=1, DivAddVal=0;
// Line Control Register
U0LCR = 0x83; /* 8 bit, 1 stop bit, no parity, enable DLAB */
if (baud == 3000000)
{
Divisor = 1;
MulVal = 13;
DivAddVal = 3;
}
else
{
Divisor = peripheralClockFrequency() / (16 * baud);
MulVal = 1;
DivAddVal = 0;
}
// Set Divisor
U0DLL = Divisor & 0xFF;
U0DLM = (Divisor >> 8) & 0xFF;
// Set Fractional
if (MulVal < 1)
MulVal = 1;
U0FDR = ((MulVal & 0xF) << 4) | (DivAddVal & 0xF);
// Disable DLAB
U0LCR &= ~0x80;
// Enable FIFO
U0FCR = 1;
}
void UART_Matlab_send_ringbuffer(void)
{
unsigned char t;
if(!transmission_running)
{
if(UART_Matlab_fifo(RBREAD, &t, 1))
{
transmission_running=1;
UARTWriteChar(t);
}
}
}
void UART_Matlab_SendPacket(void *data, unsigned short count, unsigned char packetdescriptor)
{
unsigned short crc;
int state=0;
crc=crc16(data,count);
// Disable UART Interrupts
U0IER = 0;
state+=UART_Matlab_fifo(RBWRITE, startstring, 3);
state+=UART_Matlab_fifo(RBWRITE, (unsigned char *) &count, 2);
state+=UART_Matlab_fifo(RBWRITE, &packetdescriptor, 1);
state+=UART_Matlab_fifo(RBWRITE, data, count);
state+=UART_Matlab_fifo(RBWRITE, (unsigned char *) &crc, 2);
state+=UART_Matlab_fifo(RBWRITE, stopstring, 3);
UART_Matlab_send_ringbuffer();
// Enable UART Interrupts
U0IER = 3;
}
// no longer a ringbuffer! - now it's a FIFO
int UART_Matlab_fifo(unsigned char rw, unsigned char *data, unsigned int count) //returns 1 when write/read was successful, 0 elsewise
{
static volatile unsigned char buffer[MATLABFIFOSIZE];
static volatile unsigned short i_read=0, i_write=0;
unsigned int i=0;
int return_val=0;
if(rw == RBWRITE){
while (i < count) {
buffer[i_write++] = data[i++];
i_write &= MATLABFIFOSIZE-1;
if (i_write == i_read) {
i_read++;
i_read &= 128-1;
}
}
return_val = 1;
}
else if(rw == RBREAD){
while (i < count && i_write != i_read) {
data[i++] = buffer[i_read++];
i_read &= MATLABFIFOSIZE-1;
}
return_val = i;
}
return(return_val);
}
#endif