-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabk.c
845 lines (752 loc) · 27.7 KB
/
abk.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/******************************************************************************
* *
* Program : Anadigm Boot Kernel v3.3 *
* Anadigm, Inc. *
* Copyright © 2005. All Rights Reserved. *
* *
* Author : Dave Lovell *
* *
* Date : 3rd May 2005 *
* *
* Target : AN221K04-DEV Development Board with PIC16F876 *
* *
* Notes : See readme.txt for details of changes in this version. *
* *
******************************************************************************/
#include <16F876.h>
#include "abk.h"
// Configuration bitstreams stored in ROM (for chip detect routines)
const an_Byte an_AN120_PrimaryConfigInfo[] =
{
0xD5,
0xB7,
0x12,
0x0 ,
0x30,
0x2
};
const an_Byte an_AN121_PrimaryConfigInfo[] =
{
0xD5,
0xB7,
0x12,
0x0 ,
0x80,
0x4
};
const an_Byte an_AN220_PrimaryConfigInfo[] =
{
0xD5,
0xB7,
0x22,
0x0 ,
0x30,
0x1
};
const an_Byte an_AN221_PrimaryConfigInfo[] =
{
0xD5,
0xB7,
0x22,
0x0 ,
0x80,
0x3
};
const an_Byte an_ANx2x_PrimaryConfigInfo[] =
{
0xCC,
0x0 ,
0xC ,
0x20, 0x0 , 0x20, 0x4 , 0x0 , 0x2 , 0x0 , 0x7F,
0x0 , 0x8 , 0xFF, 0x1 ,
0x2A,
0xDE,
0x0 ,
0x1 ,
0xF ,
0x2A,
0xCE,
0x1 ,
0x12,
0x1 , 0x48, 0x3F, 0x48, 0x3F, 0x1 , 0x20, 0x0 ,
0xFF, 0x20, 0x0 , 0xFF, 0x0 , 0x0 , 0x48, 0x3F,
0x48, 0x3F,
0x2A,
0xCD,
0x2 ,
0x5 ,
0x3 , 0x0 , 0x0 , 0x0 , 0x3 ,
0x2A,
0xDE,
0x3 ,
0x1 ,
0xA ,
0x2A,
0xCD,
0x4 ,
0x5 ,
0x3 , 0x0 , 0x0 , 0x0 , 0x3 ,
0x2A,
0xDE,
0x5 ,
0x1 ,
0xA ,
0x2A,
0xCD,
0x6 ,
0x5 ,
0x3 , 0x0 , 0x0 , 0x0 , 0x3 ,
0x2A,
0xDE,
0x7 ,
0x1 ,
0xA ,
0x2A,
0xCD,
0x8 ,
0x5 ,
0x3 , 0x0 , 0x0 , 0x0 , 0x3 ,
0x2A,
0x9E,
0x9 ,
0x1 ,
0xA ,
0x2A
};
/******************************************************************************
* *
* Procedure Name : RS_232_GetData *
* *
* Purpose : This function retrieves information from the receive *
* buffer, and returns it to the caller. *
* *
* Inputs : None *
* *
* Outputs : int8 - The first data available in the FIFO buffer. *
* *
******************************************************************************/
int8 RS_232_GetData(void)
{
int8 Result;
while (BufferEmpty); // Wait until a character is available
Result = InputBuf[BufferTail]; // Retrieve the data from the buffer...
BufferTail++; // ...and update the buffer pointers.
if (BufferTail == BufferSize)
BufferTail = 0;
BufferFull = 0;
BufferEmpty = (BufferTail == BufferHead);
return Result;
}
/******************************************************************************
* *
* Procedure Name : interrupt *
* *
* Purpose : This function provides the interrupt handler for the PIC *
* microcontroller. It handles all serial transactions. *
* *
* Inputs : None *
* *
* Outputs : None *
* *
******************************************************************************/
#int_rda
void interrupt(void)
{
if (kbhit())
{
InputBuf[BufferHead] = getc();
BufferHead++;
if (BufferHead == BufferSize)
BufferHead = 0;
BufferFull = (BufferHead == BufferTail);
BufferEmpty = 0;
}
}
/******************************************************************************
* *
* Procedure Name : getHEXNumber *
* *
* Purpose : This function retrieves an ASCII hex value from the *
* serial port and converts it to binary. *
* *
* Inputs : None *
* *
* Outputs : int8 - The binary value that was read as ASCII *
* *
******************************************************************************/
int8 getHEXNumber(void)
{
int8 Result, Result2, input;
// Retrieve the most-significant nibble
input = RS_232_GetData();
if (input >= 'a')
input = (input - ('a' - 'A'));
if (input >= 'A')
Result = ((input - 'A') + 10);
else
Result = input - '0';
Result <<= 4;
// Retrieve the least-significant nibble
input = RS_232_GetData();
if (input >= 'a')
input = (input - ('a' - 'A'));
if (input >= 'A')
Result2 = ((input - 'A') + 10);
else
Result2 = (input - '0');
return (Result + Result2);
}
/******************************************************************************
* *
* Procedure Name : send_byte_to_chip *
* *
* Purpose : This function sends a byte of data to the FPAA. *
* *
* Inputs : int8 data - The data byte to send to the FPAA. *
* *
* Outputs : None *
* *
******************************************************************************/
void send_byte_to_chip(int8 data)
{
int8 bit = 8;
while (bit)
{
if (data & 0x80)
output_high(DIN); //if MSB=1 then set data pin high
output_high(DCLK); //set DCLK pin high
output_low(DCLK); //set DCLK pin low
output_low(DIN); //set data pin low
data <<= 1; //left shift the data by 1 bit
bit--; //dec the value of bit
}
}
/******************************************************************************
* *
* Procedure Name : config_clocks *
* *
* Purpose : This function provides a way to run the configuration *
* clock to the FPAA 16 times. It is needed during software *
* resets of the devices. *
* *
* Inputs : None *
* *
* Outputs : None *
* *
******************************************************************************/
void config_clocks(void)
{
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
}
/******************************************************************************
* *
* Procedure Name : reset_chip *
* *
* Purpose : This function sends a soft reset command to the chip *
* chain. All devices in the chain should be reset if the *
* chipID parameter is 0xFF. Otherwise the specific chip *
* with that chip ID will be reset. *
* *
* Inputs : int8 chipID - The device to be reset. 0xFF will reset all *
* devices. Any other value will reset only the*
* device with a matching ID. *
* *
* Outputs : None *
* *
******************************************************************************/
void reset_chip(int8 chipID)
{
int8 loop;
putc(XOFF);
if (chipID == 0xFF)
{
set_tris_b(port_b_inputs & make_ERR_output); //make PIC drive ERRb
output_low(ERRb); //& drive it low
config_clocks(); //wait 16 clocks
output_high(ERRb); //drive it high
set_tris_b(port_b_inputs & make_ERR_input); //& make PIC sense ERRb again
}
else
{
for (loop = 0; loop < 5; loop++)
send_byte_to_chip(0x00); //dummy bytes
send_byte_to_chip(0xD5);
send_byte_to_chip(chipID);
send_byte_to_chip(0x48); //software reset
}
config_clocks(); //send 32 clocks
config_clocks();
putc(XON);
}
/******************************************************************************
* *
* Procedure Name : configure_chip_from_input *
* *
* Purpose : This function provides a conduit for data coming from the *
* serial port and sends it to the FPAA. *
* *
* Inputs : int8 PrimaryConfig - if == config_chip then starts with *
* reset, if == update_chip then no reset*
* *
* Outputs : None *
* *
******************************************************************************/
void configure_chip_from_input(int8 PrimaryConfig)
{
int16 num_bytes; // The number of bytes to be transferred
int8 data_read; // Data read from the serial port.
Status_flag = OK;
if (PrimaryConfig)
reset_chip(0xFF);
// Retrieve the number of bytes we will be moving.
num_bytes = getHEXNumber();
num_bytes = (num_bytes << 8) + getHEXNumber();
// Move the data as long as we have data to send, and we haven't timed out
while (num_bytes)
{
data_read = getHEXNumber();
if (Status_flag == OK)
{
send_byte_to_chip(data_read);
if (!(input(ERRb)))
Status_flag = CONFIG_ERROR;
}
num_bytes--;
num_bytes--;
}
}
/******************************************************************************
* *
* Procedure Name : readADC *
* *
* Purpose : This function reads one of the Analog-to-Digital *
* converter channels and returns the result. *
* *
* Inputs : int8 channel - The A/D channel to read. *
* *
* Outputs : int8 - The 8-bit result of the channel read. *
* *
******************************************************************************/
int8 readADC(int8 channel)
{
set_adc_channel(channel);
delay_us(10);
return (Read_ADC());
}
/******************************************************************************
* *
* Procedure Name : readSPI *
* *
* Purpose : This function reads the SPI port and returns the result. *
* If there is no activity on the SPI port, it returns 255. *
* *
* Inputs : none *
* *
* Outputs : int8 - The 8-bit result *
* *
******************************************************************************/
int8 readSPI(void)
{
int16 SPIalive = 65535;
int8 SPIdata = 255;
setup_adc_ports(RA0_RA1_ANALOG_RA3_RA2_REF); //make RA5 (SARSync) a digital pin
while (input(SYNCBIT) && SPIalive) //wait for sync to go low or timeout
SPIalive--;
if (SPIalive) //if sync is active then go get reading
{
SPIdata = SPI_read(); //do a dummy read to empty SPI port buffer
while (!spi_data_is_in()); //wait for new data to appear
SPIdata = SPI_read(); //read SPI port
}
setup_adc_ports(ALL_ANALOG); //make RA0-5 all analog inputs again
return SPIdata;
}
/******************************************************************************
* *
* Procedure Name : initialize *
* *
* Purpose : This function sets up the initial configuration of the PIC*
* *
* Inputs : None *
* *
* Outputs : None *
* *
******************************************************************************/
void initialize(void)
{
enable_interrupts(GLOBAL); //enable global interrupts
enable_interrupts(INT_RDA); //enable interrupt on RS232 receive data
port_b_pullups(false); //disable port B pull-ups
//set up connections between PIC & FPAA
set_tris_a(port_a_inputs);
set_tris_b(port_b_inputs);
set_tris_c(port_c_inputs);
output_high(CS2b);
output_low(EXECUTE);
output_low(DCLK);
output_low(DIN);
output_high(CONFIG_OK); //turn on green LED
output_high(CONFIG_ERR); //turn on red LED
// Configure RS-232 port
setup_uart(57600);
// Configure the A2D converter
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
// Configure the SPI Interface
setup_spi(SPI_SLAVE | SPI_H_TO_L); //set up SPI port
delay_ms(200); //wait for power on reset to finish (needs 35ms)
output_low(CONFIG_OK); //turn off green LED
output_low(CONFIG_ERR); //turn off red LED
}
/******************************************************************************
* *
* Procedure Name : TestChipType *
* *
* Purpose : This function tests the FPAA type *
* *
* Inputs : int8 Type - FPAA version to test *
* : int8 SuppressPullup - suppress ACT pull-up flag *
* *
* Outputs : int8 - returns 1 if FPAA type tested +ve *
* *
******************************************************************************/
int8 TestChipType(int8 Type, int8 SuppressPullup)
{
int8 ChipData, Loop;
for (Loop = 0; Loop < 5; Loop++)
send_byte_to_chip(0x00);
for (Loop = 0; Loop < 6; Loop++)
{
switch (Type)
{
case an_AN120:
ChipData = an_AN120_PrimaryConfigInfo[Loop];
break;
case an_AN121:
ChipData = an_AN121_PrimaryConfigInfo[Loop];
break;
case an_AN220:
ChipData = an_AN220_PrimaryConfigInfo[Loop];
break;
case an_AN221:
ChipData = an_AN221_PrimaryConfigInfo[Loop];
break;
default:
break;
}
send_byte_to_chip(ChipData);
if ((ChipData) && (!(input(ERRb))))
{
config_clocks();
return 0;
}
}
if (SuppressPullup)
send_byte_to_chip(0x04);
else
send_byte_to_chip(0x05);
for (Loop = 0; Loop < 99; Loop++)
send_byte_to_chip(an_ANx2x_PrimaryConfigInfo[Loop]);
config_clocks();
return 1;
}
/******************************************************************************
* *
* Procedure Name : ChipsPresent *
* *
* Purpose : This function tests the FPAA type *
* *
* Inputs : none *
* *
* Outputs : int1 - returns 1 if FPAA(s) present, 0 if not *
* *
******************************************************************************/
int1 ChipsPresent(void)
{
const int8 ChipData[] = { 0xD5, 0xB7, 0xFF, 0xFF, 0xFF, 0x00 };
int8 DataPtr;
config_clocks();
//Send illegal data and see if an error occurs. If ERRb goes low, we know there is a chip listening to us.
for (DataPtr = 0; DataPtr < 6; DataPtr++)
{
send_byte_to_chip(ChipData[DataPtr]);
if (!(input(ERRb)))
return 1;
}
return 0;
}
/******************************************************************************
* *
* Procedure Name : DetectChips *
* *
* Purpose : This function returns the types of FPAAs in the system *
* *
* Inputs : none *
* *
* Outputs : none *
* *
******************************************************************************/
void DetectChips(void)
{
int8 TypeLoop, ChipLoop, Abort = 0;
ChipCount = 0;
reset_chip(0xFF);
if (ChipsPresent())
{
while ((ChipCount < MaxChips) && (!(input(ACTIVATE))) && (!Abort))
{
for (TypeLoop = an_AN120; TypeLoop <= an_AN221; TypeLoop++)
{
reset_chip(0xFF);
for (ChipLoop = 0; ChipLoop < ChipCount; ChipLoop++)
TestChipType(ChipType[ChipLoop], 1);
if (TestChipType(TypeLoop, 0))
{
ChipType[ChipCount++] = TypeLoop;
switch (TypeLoop)
{
case an_AN120:
printf("120E04|");
break;
case an_AN121:
printf("121E04|");
break;
case an_AN220:
printf("220E04|");
break;
case an_AN221:
printf("221E04|");
break;
}
break;
}
else
{
if (TypeLoop == an_AN221)
{
printf("UNKNOWN|");
Abort = 1;
}
}
}
}
}
printf("END");
putc(null);
reset_chip(0xFF);
}
/******************************************************************************
* *
* Procedure Name : pinTest *
* *
* Purpose : This function checks the state of the ACTIVATE or ERR pin.*
* The 'P' command must be followed by an 'A' or 'E' to tell *
* the function which pin to test. Then by an 'H' or 'L' for *
* high or low. Finally, the user should do a s/w status *
* check to determine if the quizzed state was correct. *
* *
* Inputs : None *
* *
* Outputs : None *
* *
******************************************************************************/
void pinTest(void)
{
int8 Pin, PinStateAssert;
Pin = RS_232_GetData();
if ((Pin != 'A') && (Pin != 'E'))
{
Status_flag = UNKNOWN_COMMAND;
return;
}
PinStateAssert = RS_232_GetData();
if ((PinStateAssert != 'H') && (PinStateAssert != 'h') &&
(PinStateAssert != 'L') && (PinStateAssert != 'l'))
{
Status_flag = UNKNOWN_COMMAND;
return;
}
switch (Pin)
{
case 'A' :
{
switch (PinStateAssert)
{
case 'H':
case 'h':
if (input(ACTIVATE) != 1)
Status_flag = CONFIG_ERROR;
break;
case 'L':
case 'l':
if (input(ACTIVATE) != 0)
Status_flag = CONFIG_ERROR;
break;
}
}
break;
case 'E' :
{
switch (PinStateAssert)
{
case 'H':
case 'h':
if (input(ERRb) != 1)
Status_flag = CONFIG_ERROR;
break;
case 'L':
case 'l':
if (input(ERRb) != 0)
Status_flag = CONFIG_ERROR;
break;
}
}
break;
}
}
/******************************************************************************
* *
* Procedure Name : main *
* *
* Purpose : This is our main application entry point. It runs the *
* command processor, which accepts commands from the serial *
* port and processes them. *
* *
* Inputs : None *
* *
* Outputs : None *
* *
******************************************************************************/
void main(void)
{
int8 command;
initialize(); // set up inital configuration of the PIC & FPAA
output_high(CTS_to_PC); //deassert clear to send to the PC
while (1)
{
// Check serial port to see if a command has started. Commands start with a "STX" character (0x02)
while (input(RTS_from_PC)); //wait until we get a request to send from the PC
output_low(CTS_to_PC); //assert clear to send to the PC
command = RS_232_GetData();
if (command == STX)
{
command = RS_232_GetData();
if (command != sw_status)
Status_flag = OK;
switch (command)
{
case config_chip: // The command '0' does a full configuration
case update_chip: // The command '1' does a reconfiguration
{
output_low(CONFIG_OK);
output_low(CONFIG_ERR);
output_low(CS2b);
configure_chip_from_input(command == config_chip);
if (Status_flag == OK)
output_high(CONFIG_OK);
else
output_high(CONFIG_ERR);
}
break;
case version_info: // The command '#' gives back the version information on the board.
{
printf("Anadigm AN221K04-DEV BOARD v2.0x\n");
printf("ABK Version: 3.3\nRev Date: 3 May 2005\n");
putc(null);
}
break;
case sw_status: // The command '?' returns the current software status.
{
putc('0' + Status_flag);
}
break;
case read_PIC_ADC: // The command 'V' reads the ADC of the PIC
{
putc(readADC(RS_232_GetData()));
}
break;
case read_PIC_SPI: // The command 'W' reads the SPI port of the PIC
{
putc(readSPI());
}
break;
case reset_chips: // The command 'R' resets the FPAA
{
output_low(CONFIG_OK);
output_low(CONFIG_ERR);
reset_chip(getHEXNumber());
}
break;
case identify_chips: // The command 'T' returns the types of FPAAs in the system
{
output_low(CONFIG_OK);
output_low(CONFIG_ERR);
output_low(CS2b);
DetectChips();
}
break;
case command_set: // The command 'S' returns the set of commands
{
printf("01EPRSTVW?#");
putc(null);
}
break;
case pin_status: // The command 'P' returns the state of the ACTIVATE or ERR pin.
{
pinTest();
}
break;
case execute_pulse: // The command 'E' pulses the EXECUTE pin for 1us with 2 DCLKs
{
output_high(EXECUTE);
output_high(DCLK);
output_low(DCLK);
output_high(DCLK);
output_low(DCLK);
output_low(EXECUTE);
}
break;
default:
{
Status_flag = UNKNOWN_COMMAND;
}
break;
}
command = 0x00;
while (command != ETX) //look for ETX (0x03)
command = RS_232_GetData();
}
output_high(CTS_to_PC); //deassert clear to send to the PC
}
}