forked from drandyhaas/Haasoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialprocessor.v
1486 lines (1438 loc) · 47.1 KB
/
serialprocessor.v
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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// from http:https://www.sparxeng.com/blog/software/communicating-with-your-cyclone-ii-fpga-over-serial-port-part-3-number-crunching
module processor(clk, rxReady, rxData, txBusy, txStart, txData, readdata, get_ext_data, ext_data_ready, wraddress_triggerpoint, rden, rdaddress, ram_output1, ram_output2, ram_output3, ram_output4,
newcomdata,comdata,debug3,debug4,spare,serial_passthrough,master_clock, imthelast,imthefirst,rollingtrigger,trigDebug,
adcdata,adcready,getadcdata,getadcadr,adcvalid,adcreset,adcramdata,writesamp,writeadc,adctestout,
triggerpoint,downsample, screendata,screenwren,screenaddr,screenreset,trigthresh,trigchannels,triggertype,triggertot,
SPIsend,SPIsenddata,delaycounter,carrycounter,usb_siwu,SPIstate,offset,gainsw,do_usb,
i2c_ena,i2c_addr,i2c_rw,i2c_datawr,i2c_datard,i2c_busy,i2c_ackerror, usb_clk60,usb_dataio,usb_txe_busy,usb_wr,
rdadtwo,trigthreshtwo, debug1,debug2,chip_id, highres, use_ext_trig, digital_buffer1, nsmp, outputclk,
phasecounterselect,phaseupdown,phasestep,scanclk,
ext_trig_delay, noselftrig, usb_oe, usb_rd, usb_rxf, usb_pwrsv, clk_rd,
nselftrigcoincidentreq, selftrigtempholdtime, allowsamechancoin,
trigratecounter,trigratecountreset);
input clk;
input[7:0] rxData;
input rxReady;
input txBusy;
output reg txStart;
output reg[7:0] txData;
output reg[7:0] readdata;//first byte we got
output wire spare,debug3,debug4;
reg led1,led2,led3,led4;
reg io1,io2,io3,io4;
output reg get_ext_data;
input ext_data_ready;
parameter ram_width=12;//9 is 512 samples
input wire[ram_width-1:0] wraddress_triggerpoint;
output wire [ram_width-1:0] rdaddress;
output wire [ram_width-1:0] rdadtwo;
reg [ram_width-1:0] rdaddress_slow;
reg [ram_width-1:0] rdadtwo_slow;
reg [ram_width-1:0] rdaddress_fast;
//reg [ram_width-1:0] rdadtwo_fast;//only for lockin stuff
output reg [ram_width-1:0] triggerpoint;
output reg rden=0;
input wire [7:0] ram_output1;
input wire [7:0] ram_output2;
input wire [7:0] ram_output3;
input wire [7:0] ram_output4;
input wire [7:0] digital_buffer1;
output reg serial_passthrough;
output reg [1:0] master_clock;
output reg[7:0] comdata;
output reg newcomdata;
output reg imthelast; // to remember if we're the last one in the chain
output wire imthefirst; // to remember if we're the last one in the chain
output reg rollingtrigger;
input trigDebug;
input [11:0] adcdata;
input adcready;
input adcvalid;
output reg getadcdata;
output reg [4:0] getadcadr;
output reg adcreset;
output reg [10:0] writesamp;
output reg writeadc;
output reg [11:0] adctestout;
output reg [7:0] downsample;
output reg [7:0] screendata;
output reg screenwren=0;
output reg [9:0] screenaddr = 10'd0;
output reg screenreset=0;
output reg [7:0] trigthresh = 8'h80, trigthreshtwo=8'hff; // the normal and high trigger thresholds
output reg [3:0] trigchannels = 4'b1111;
output reg [3:0] triggertype = 4'b0001;//rising edge on, falling edge off, other off
output reg [ram_width:0] triggertot;
output reg [15:0] SPIsenddata;//the bits to send
output reg SPIsend;//start sending
input [7:0] delaycounter;
input [7:0] carrycounter;
input [3:0] SPIstate;
output wire[3:0] offset;
output reg[3:0] gainsw;
reg[3:0] oversamp;
output wire debug1,debug2;
input [63:0] chip_id;
output reg highres=0;
output reg use_ext_trig=0;
output reg outputclk=1;
output reg[4:0] ext_trig_delay=0;
output reg noselftrig=0;
output reg[1:0] nselftrigcoincidentreq=0; // how many additional coincident channels to require for self trigger
output reg[7:0] selftrigtempholdtime=10; // how long to fire a channel for, to use for coincidence (and now the trigger rate counter too)
output reg allowsamechancoin=0; // whether to allow same channel, firing in the past, to count as coincidence
input [31:0] trigratecounter;
output reg trigratecountreset=0;
output reg i2c_ena;
output reg [6:0] i2c_addr;
output reg i2c_rw;
output reg [7:0] i2c_datawr;
input [7:0] i2c_datard;
input i2c_busy;
input i2c_ackerror;
reg [7:0] i2cdata[8];//up to 8 bytes of data to send
reg [3:0] i2c_datacounttosend,i2c_datacount;
reg i2cgo=0;
reg i2cdoread=0;
localparam READ=0, SOLVING=1, WAITING=2, WRITE_EXT1=3, WRITE_EXT2=4, WAIT_ADC1=5, WAIT_ADC2=6, WRITE_BYTE1=7, WRITE_BYTE2=8, READMORE=9,
WRITE1=10, WRITE2=11,SPIWAIT=12,I2CWAIT=13,I2CSEND1=14,I2CSEND2=15,
WRITE_USBFAST_EXT1=16, WRITE_USBFAST_EXT2=17,
LOCKIN1=18,LOCKIN2=19,LOCKIN3=20,LOCKINWRITE1=21,LOCKINWRITE2=22,
WRITE_USB_EXT1=24, WRITE_USB_EXT2=25, WRITE_USB_EXT3=26, WRITE_USB_EXT4=27, WRITE_USB_EXT5=28,
PLLCLOCK=30;
reg[4:0] state,i2cstate;
reg [7:0] myid;
assign imthefirst = (myid==0);
reg [7:0] extradata[10];//to store command extra data, like arguemnts (up to 10 bytes)
reg [ram_width+2:0] SendCount=0;
reg [2:0] blockstosend=4; // will be 4 for normal, but 5 (or more) for sending logic analyzer stuff etc.
reg [15:0] nsamp = 6;
input [11:0] adcramdata;
reg writebyte;//whether we're sending the first or second byte (since it's 12 bits from the Max10 ADC)
reg[7:0] bytesread, byteswanted;
reg thecounterbit, thecounterbitlockin;
reg [7:0] clockbitstowait=5, clockbitstowaitlockin=3; //wait 2^clockbitstowait (8?) ticks before sending each data byte
reg [3:0] sendincrement = 0; //skip 2**sendincrement bytes each time
output reg [ram_width-1:0] nsmp = 0; // samplestosend
reg [7:0] chanforscreen=0;
reg autorearm=0;
integer thecounter=0, timeoutcounter=0, serialdelaytimer=0,serialdelaytimerwait=0;
reg[4:0] serialdelaycounter=0;
reg addonetoextradata=0;
output wire clk_rd;
reg [7:0] usb2counter;
output reg do_usb=0;
input usb_clk60;
output wire [7:0] usb_dataio;
reg [7:0] usb_dataio_slow;
reg [7:0] usb_dataio_fast;
input usb_txe_busy;
reg usb_txe_not_busy;
output reg usb_oe=1;
output reg usb_rd=1;
input usb_rxf;
output reg usb_pwrsv=1;
output wire usb_wr, usb_siwu;
reg usb_wr_slow, usb_siwu_slow;
reg usb_wr_fast, usb_siwu_fast;
reg checkfastusbwriting=0;
//TODO: use memory bits for this, not register space??
reg [5:0] screencolumndata [128]; //all the screen data, 128 columns of (8 rows of 8 dots)
//For writing out data in WRITE1,2
reg[7:0] ioCount, ioCountToSend;
reg[7:0] data[0:15];
//For lockin calculations
reg [7:0] numlockinbytes=16;//number of bytes of info to send for lockin info
integer lockinresult1;
integer lockinresult2;
reg [15:0] lockinnumtoshift = 0;
reg [31:0] chan2mean, chan3mean;
reg calcmeans;
//for clock phase
reg[7:0] pllclock_counter=0;
reg[7:0] scanclk_cycles=0;
output reg[2:0] phasecounterselect; // Dynamic phase shift counter Select. 000:all 001:M 010:C0 011:C1 100:C2 101:C3 110:C4. Registered in the rising edge of scanclk.
output reg phaseupdown=1; // Dynamic phase shift direction; 1:UP, 0:DOWN. Registered in the PLL on the rising edge of scanclk.
output reg phasestep=0;
output reg scanclk=0;
initial begin
state<=READ;
i2cstate<=READ;
usb2state<=USBFAST_IDLE;
myid<=200;
master_clock<=2'b00;//start as my own master
imthelast<=0;//probably not last
rollingtrigger<=1;
triggerpoint<=(2**(ram_width-2));// 1/4 of the screen
downsample<=1;
serial_passthrough<=0;
usb_siwu_slow<=1;
gainsw<=4'b0000;//1 is for 1k resistor (gain 2), 0 is for 100 Ohm resistor (gain .2)
oversamp<=4'b0011;//1 is for _no_ oversampling (and only matters for bits 0 and 1)
led1<=1; //on
led2<=1; //on
led3<=1; //on
led4<=1; //on
io1<=0; //off
io2<=0; //off
io3<=0; //off
io4<=0; //off
end
//set the LEDs to indicate my ID
always @(posedge clk) begin
thecounter<=thecounter+1;
if (thecounter[26]==1'b1 ) begin //flash every few seconds
if (imthelast) begin
led1<=0; led2<=0; led3<=0;//all off
end
else if (myid==0) begin
led1<=0; led2<=0;
end
else begin
led1<=0;
end
end
else begin
if (imthelast) begin
led1<=1; led2<=1; led3<=1;//all on
end
else if (myid==0) begin
led1<=1; led2<=1;
end
else begin
led1<=1;
end
end
if (thecounter[24]==1'b1 ) begin //pulse faster
io3 <= 0;
end
else begin
io3 <= 1;
end
end
reg oldled1,oldled2,oldled3,oldled4,oldio1,oldio2,oldio3,oldio4;
// reg [7:0] PWMoffset0 = 58; //22.7% *256;
// reg [7:0] PWMoffset1 = 58; //22.7% *256;
// reg [7:0] PWMoffset2 = 58; //22.7% *256;
// reg [7:0] PWMoffset3 = 58; //22.7% *256;
// reg [7:0] pwmcounter;
// //For 1k and 0.1uF, freq=1.6kHz
// //For 1k and 1uF, freq=160Hz
// integer PWMratecounter=0;
// integer PWMrate=2;//how fast we count the pwm clock
// always @(posedge clk) begin
// if (PWMratecounter>=PWMrate) begin
// pwmcounter <= pwmcounter + 1'b1; // free-running counter
// PWMratecounter=0;
// end
// else PWMratecounter=PWMratecounter+1;
// end
// assign offset[0] = (PWMoffset0 > pwmcounter); // comparators
// assign offset[1] = (PWMoffset1 > pwmcounter); // comparators
// assign offset[2] = (PWMoffset2 > pwmcounter); // comparators
// assign offset[3] = (PWMoffset3 > pwmcounter); // comparators
always @(posedge clk) begin
usb_txe_not_busy <= ~usb_txe_busy;
case (state)
READ: begin
get_ext_data<=0;
adcreset<=1;
txStart<=0;
getadcdata<=0;
bytesread<=0;
byteswanted<=0;
newcomdata<=0;
SPIsend<=0;
i2cgo=0;
usb_wr_slow<=1;
ioCount = 0;
send_fast_usb2=0;
addonetoextradata=0;
serialdelaycounter=0;
trigratecountreset=0;
if (rxReady) begin
readdata = rxData;
state = SOLVING;
end
if (oldled1!=led1 || oldled2!=led2 || oldled3!=led3 || oldled4!=led4 || oldio1!=io1 || oldio2!=io2 || oldio3!=io3 || oldio4!=io4) begin
oldled1=led1; oldled2=led2; oldled3=led3; oldled4=led4;
oldio1=io1; oldio2=io2; oldio3=io3; oldio4=io4;
//now send to i2c
i2c_datacounttosend=2;//how many bytes of info to send (not counting address)
i2c_addr=8'h21; // the second mcp io expander
i2cdata[0]=8'h12; // port a
i2cdata[1][0]=led1; i2cdata[1][1]=led2; i2cdata[1][2]=led3; i2cdata[1][3]=led4; // set the low 4 bits to be correct for the leds
i2cdata[1][4]=io1; i2cdata[1][5]=io2; i2cdata[1][6]=io3; i2cdata[1][7]=io4; // set the high 4 bits to be correct for the ios
i2cdata[2]=0; // not used for mcp io expanders
if (i2cstate==READ) begin // if it's busy, we'll do nothing, oh well
i2cdoread = 0;
i2cgo=1;
end
end
end
READMORE: begin
newcomdata=0;
if (rxReady) begin
extradata[bytesread] = rxData;
if (addonetoextradata) comdata=rxData+1;//for propogating the board ID
else comdata=rxData;
newcomdata=1; //pass it on
bytesread = bytesread+1;
if (bytesread>=byteswanted) state=SOLVING;
end
end
SOLVING: begin
if (readdata < 10) begin // got character "0-9"
myid=readdata;//remember my ID
if (readdata==0) begin
master_clock=2'b00; //remain my own master
end
else master_clock=2'b01; //now a slave!
comdata=(readdata+1); // give the next one an ID one larger
newcomdata=1; //pass it on
state=READ;
end
else if (readdata > 9 && readdata < 20) begin // got character "10-19"
if (myid==(readdata-10)) begin
//read me out
serial_passthrough=0;
timeoutcounter=0;//start the clock
state=WAITING;
end
else begin
//pass it on, and set serial to "passthrough mode"
serial_passthrough=1;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
end
else if (readdata > 19 && readdata < 30) begin // got character "20-29"
if (myid==(readdata-20)) begin
imthelast=1; // I'm the last one
outputclk=0;
end
else begin
imthelast=0;
outputclk=1;
end
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (readdata > 29 && readdata < 40) begin // got character "30-39"
if (myid==(readdata-30)) begin
//make me active
serial_passthrough=0;
end
else begin
//pass it on, and set serial to "passthrough mode"
serial_passthrough=1;
comdata=readdata;
newcomdata=1; //pass it on
end
state=READ;
end
// 40 is reserved for doing nothing, to check timing
else if (readdata==50) begin
byteswanted=1;//wait for next byte which is the ID to take (replacing 0-9)
comdata=readdata;
newcomdata=1; //pass it on
addonetoextradata=1;// give the next one an ID one larger
if (bytesread<byteswanted) state=READMORE;
else begin
myid=extradata[0];//remember my ID
if (extradata[0]==0) begin
master_clock=2'b00; //remain my own master
end
else master_clock=2'b01; //now a slave!
state=READ;
end
end
else if (readdata==51) begin
byteswanted=1;//wait for next byte which is the board to read out (replacing 10-19)
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
if (myid==extradata[0] || extradata[0]==255) begin // if 255 all boards read out with one command
//read me out
serial_passthrough=0;
timeoutcounter=0;//start the clock
state=WAITING;
end
else begin
//if (myid<extradata[0]) begin
//pass it on, and set serial to "passthrough mode"
serial_passthrough=1;
//end
state=READ;
end
end
end
else if (readdata==52) begin
byteswanted=1;//wait for next byte which is the board id that's the last one (replacing 20-29)
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
if (myid==extradata[0]) begin
imthelast=1; // I'm the last one
outputclk=0;
end
else begin
imthelast=0;
outputclk=1;
end
state=READ;
end
end
else if (readdata==53) begin
byteswanted=1;//wait for next byte which is the board to not set to serial passthrough, but all others go into serial passthough (replacing 30-39)
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
if (myid==extradata[0]) begin
//make me active
serial_passthrough=0;
end
else begin
//if (myid<extradata[0]) begin
//pass it on, and set serial to "passthrough mode"
serial_passthrough=1;
//end
end
state=READ;
end
end
else if (54==readdata) begin
if (imthelast) outputclk = ~outputclk; //tell the last one to toggle outputting the clock on the left
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (55==readdata) begin //adjust clock phases, if I'm the active one
if (serial_passthrough) begin
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else begin
phasecounterselect=3'b000; // all clocks - see https://www.intel.com/content/dam/www/programmable/us/en/pdfs/literature/hb/cyc3/cyc3_ciii51006.pdf table 5-10
//phaseupdown=1'b1; // up
scanclk=1'b0; // start low
phasestep=1'b1; // assert!
pllclock_counter=0;
scanclk_cycles=0;
state=PLLCLOCK;
end
end
else if (readdata==56) begin
byteswanted=1;//wait for next byte which is the ext_trig_delay
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
ext_trig_delay = extradata[0];
state=READ;
end
end
else if (57==readdata) begin // tell them all toggle just taking triggers in towards the right
noselftrig=~noselftrig;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (58==readdata) begin // tell them all toggle fast usb2 (sync 245 fifo)
do_fast_usb=~do_fast_usb;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (59==readdata) begin // tell them all toggle fast usb2 write cross-checking
checkfastusbwriting=~checkfastusbwriting;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (100==readdata) begin
//tell them all to prime the trigger
get_ext_data=1;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (101==readdata) begin
//tell them all to roll the trigger
io1=1;
rollingtrigger=1;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (102==readdata) begin
//tell them all to not roll the trigger
io1=0;
rollingtrigger=0;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (readdata>109 && readdata<120) begin // 110 to 119
if (serial_passthrough) begin
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else begin
if (readdata==119) getadcadr<=17;//send the data from the temp sensor
else getadcadr<=(readdata-110);//send the data from adc, so 110->0 (pin AIN1), 111->1 (pin 6), up to 118->8 (pin 14)
writesamp<=0;
state=WAIT_ADC1;
end
end
else if (readdata==120) begin
byteswanted=2;//wait for next bytes which are the number of samples to read from max10 adc
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
nsamp=256*extradata[0]+extradata[1];
if (nsamp>4095) nsamp=4095; //max of 4096 samples stored in the ram (12 bit writesamp address), and one less is needed (not sure why...)
state=READ;
end
end
else if (readdata==121) begin
byteswanted=2;//wait for next bytes which are the trigger point
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
triggerpoint=256*extradata[0]+extradata[1];
if (triggerpoint>(2**ram_width)-16) triggerpoint=(2**ram_width)-16;
else if (triggerpoint<4) triggerpoint=4;
state=READ;
end
end
else if (readdata==122) begin
byteswanted=2;//wait for next bytes which are the number of samples to send
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
nsmp=256*extradata[0]+extradata[1];
if (triggerpoint>(nsmp-5)) triggerpoint=nsmp/2;
state=READ;
end
end
else if (readdata==123) begin
byteswanted=1;//wait for next byte which is the number of bytes to skip after each send, log2
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
sendincrement=extradata[0];
state=READ;
end
end
else if (readdata==124) begin
comdata=readdata; newcomdata=1; //pass it on
byteswanted=1;//wait for next byte which is the number of samples to skip in the ADC, log2
if (bytesread<byteswanted) state=READMORE;
else begin
if (extradata[0]>22) extradata[0]=22;
downsample=extradata[0];
clockbitstowaitlockin = extradata[0]-2; // TODO - seems to work OK
state=READ;
end
end
else if (readdata==125) begin
comdata=readdata; newcomdata=1; //pass it on
byteswanted=1;//wait for next byte which is the number of clock ticks to wait between sending bytes, log2
if (bytesread<byteswanted) state=READMORE;
else begin
if (extradata[0]>30) extradata[0]=30;
clockbitstowait=extradata[0];
state=READ;
end
end
else if (readdata==126) begin
comdata=readdata; newcomdata=1; //pass it on
byteswanted=1;//wait for next byte which is the channel to draw on the mini-display
if (bytesread<byteswanted) state=READMORE;
else begin
chanforscreen=extradata[0];
state=READ;
end
end
else if (readdata==127) begin
comdata=readdata; newcomdata=1; //pass it on
byteswanted=1;//wait for next byte which is the trigger threshold
if (bytesread<byteswanted) state=READMORE;
else begin
trigthresh=extradata[0];
state=READ;
end
end
else if (readdata==128) begin
comdata=readdata; newcomdata=1; //pass it on
byteswanted=1;//wait for next byte which is the trigger type: rising, falling, either, ...
if (bytesread<byteswanted) state=READMORE;
else begin
triggertype=extradata[0];
state=READ;
end
end
else if (readdata==129) begin
byteswanted=2;//wait for next bytes which are the trigger time over/under threshold required
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
triggertot=256*extradata[0]+extradata[1];
state=READ;
end
end
else if (readdata==130) begin
comdata=readdata; newcomdata=1; //pass it on
byteswanted=1;//wait for next byte which is whether to trigger or not trigger on a given channel
if (bytesread<byteswanted) state=READMORE;
else begin
if (extradata[0]/4 ==myid) begin //I have this channel
trigchannels[extradata[0]%4]=~trigchannels[extradata[0]%4];//invert previous value
end
state=READ;
end
end
else if (readdata==131) begin
byteswanted=2;//wait for next bytes which are the data to send to SPI on the ADCs
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
SPIsenddata[15:8]=extradata[0];//0 (write) and the 7 bit address
SPIsenddata[7:0]=extradata[1];//the bits to write to that address
SPIsenddata[15]=1'b0;//write is 0
SPIsend=1;
state=SPIWAIT;
end
end
else if (readdata==132) begin // send the delaycounter TDC data, if I'm the board being read out
if (serial_passthrough) begin
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else begin
ioCountToSend = 1;
data[0]=delaycounter;
state=WRITE1;
end
end
else if (readdata==133) begin // send the carrycounter TDC data, if I'm the board being read out
if (serial_passthrough) begin
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else begin
ioCountToSend = 1;
data[0]=carrycounter;
state=WRITE1;
end
end
else if (readdata==134) begin
byteswanted=1;//wait for next byte which is the channel
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
if (extradata[0]/4 ==myid) begin //I have this channel
gainsw[extradata[0]%4]=~gainsw[extradata[0]%4];//switch the gain of the channel
end
//now send to i2c
i2c_datacounttosend=2;//how many bytes of info to send (not counting address)
i2c_addr=8'h20; // the first mcp io expander
i2cdata[0]=8'h12; // port a
i2cdata[1][3:0]=gainsw; // set the low 4 bits to be correct for the gain
i2cdata[1][7:4]=oversamp; // set the high 4 bits to be correct for the oversampling
i2cdata[2]=0; // not used for mcp io expanders
if (i2cstate==READ) begin
i2cdoread = 0;
i2cgo=1;
end
state=READ;
end
end
else if (readdata==135) begin
byteswanted=2;//wait for next bytes which are the serialdelaytimerwait
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
serialdelaytimerwait=50*(256*extradata[0]+extradata[1]); // 50 * amount given, so amount given in microseconds (20ns*50=1us) (except for the flipping clockbitstowait, so actually 2 us steps!)
state=READ;
end
end
else if (readdata==136) begin
byteswanted=6;//wait for next bytes which are the stuff to send over i2c
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
//i2c_addr = 7'b0100000;// 0x20 // for all 3 pins of (last 3 digits) to GND of MCP23017
//i2c_addr = 7'b0100001;// 0x21 // for all a pin to VCC of MCP23017
//i2c_addr = 7'b1100000;// 0x60 // for MCP4728
i2c_datacounttosend=extradata[0];//how many bytes of info to send (not counting address)
i2c_addr=extradata[1]; // get address to write to
i2cdata[0]=extradata[2];
i2cdata[1]=extradata[3];
i2cdata[2]=extradata[4];
if ((extradata[5]==myid || extradata[5]==200) && i2cstate==READ) begin // only pay attention if the board is broadcast (id 200) or for my id!
i2cdoread = 0;
i2cgo=1;
end
state=READ;
end
end
else if (137==readdata) begin
//tell them to send over FT232H USB
do_usb=~do_usb;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (readdata==138) begin
byteswanted=2;//wait for next bytes which are the lockinnumtoshift
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
lockinnumtoshift = 256*extradata[0]+extradata[1];
state=READ;
end
end
else if (139==readdata) begin
//tell them to toggle automatic rearm of the trigger
autorearm=~autorearm;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (readdata==140) begin
byteswanted=1;//wait for next byte which is the high trigger threshold (must be below this to trigger)
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
trigthreshtwo = extradata[0];
state=READ;
end
end
else if (readdata==141) begin
byteswanted=1;//wait for next byte which is the channel to toggle oversampling for
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
if (extradata[0]/4 ==myid) begin //I have this channel
oversamp[extradata[0]%4]=~oversamp[extradata[0]%4];//switch the oversampling of the channel
end
//now send to i2c
i2c_datacounttosend=2;//how many bytes of info to send (not counting address)
i2c_addr=8'h20; // the first mcp io expander
i2cdata[0]=8'h12; // port a
i2cdata[1][3:0]=gainsw; // set the low 4 bits to be correct for the gain
i2cdata[1][7:4]=oversamp; // set the high 4 bits to be correct for the oversampling
i2cdata[2]=0; // not used for mcp io expanders
if (i2cstate==READ) begin
i2cdoread = 0;
i2cgo=1;
end
state=READ;
end
end
else if (readdata==142) begin // send the uniqueID
if (serial_passthrough) begin
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else begin
data[0]=chip_id[7+8*0:8*0];
data[1]=chip_id[7+8*1:8*1];
data[2]=chip_id[7+8*2:8*2];
data[3]=chip_id[7+8*3:8*3];
data[4]=chip_id[7+8*4:8*4];
data[5]=chip_id[7+8*5:8*5];
data[6]=chip_id[7+8*6:8*6];
data[7]=chip_id[7+8*7:8*7];
ioCountToSend = 8; // send 8 bytes
state=WRITE1;
end
end
else if (143==readdata) begin
//tell them to toggle highres mode
highres=~highres;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (144==readdata) begin
//tell them to toggle using ext_trig_in
use_ext_trig=~use_ext_trig;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (readdata==145) begin
byteswanted=1;//wait for next byte which is the number of blocks to send out (for fast adc data, digitial logic analyzer data, etc.)
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
blockstosend = extradata[0];
state=READ;
end
end
else if (readdata==146) begin // send out a byte read from i2c
byteswanted=3;//wait for next bytes which are the chip/address to read from and the board id (or all)
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
i2c_addr=extradata[0]; // get chip to read from
i2c_datacounttosend=2;
i2cdata[0]=extradata[1]; // get address to read from
i2cdata[1]=0; // dummy
if ((extradata[2]==myid || extradata[2]==200) && i2cstate==READ) begin // only pay attention if the board is broadcast (id 200) or for my id!
i2cdoread = 1;
i2cgo=1;
//send message with read info (note - it won't be correct yet, since it takes time to read, so check back a little later!)
ioCountToSend = 1;
data[0] = i2c_datard;
state=WRITE1;
end
else state=READ;
end
end
else if (readdata==147) begin // send the firmware version
if (serial_passthrough) begin
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else begin
ioCountToSend = 1;
data[0]=22; // this is the firmware version
state=WRITE1;
end
//reset stuff!
rden=0;
trigthresh = 8'h80;
trigthreshtwo=8'hff;
trigchannels = 4'b1111;
triggertype = 4'b0001;
highres=0;
use_ext_trig=0;
ext_trig_delay=0;
noselftrig=0;
SendCount=0;
blockstosend=4;
nsamp = 6;
clockbitstowait=5;
sendincrement = 0;
nsmp = 0;
chanforscreen=0;
autorearm=0;
do_usb=0;
checkfastusbwriting=0;
rollingtrigger=1;
triggerpoint=(2**(ram_width-2));
downsample=1;
gainsw=4'b0000;
oversamp=4'b0011;
send_fast_usb2=0;
do_fast_usb=0;
usbdonecounterslow=0;
nselftrigcoincidentreq=0;
selftrigtempholdtime=10;
allowsamechancoin=0;
end
else if (readdata==148) begin
byteswanted=1;//wait for next byte which is the number of coincident channels to require for the self trigger
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
nselftrigcoincidentreq = extradata[0];
state=READ;
end
end
else if (readdata==149) begin
byteswanted=1;//wait for next byte which is how long to hold the self trigger active for (for coincidence purposes, and now trigger rate counter deadtime too)
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
selftrigtempholdtime = extradata[0];
state=READ;
end
end
else if (150==readdata) begin
//tell them to toggle allow same channel coincidence
allowsamechancoin=~allowsamechancoin;
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else if (readdata==151) begin // send the number of triggers, and reset
if (serial_passthrough) begin
comdata=readdata;
newcomdata=1; //pass it on
state=READ;
end
else begin
data[0]=trigratecounter[7+8*0:8*0];
data[1]=trigratecounter[7+8*1:8*1];
data[2]=trigratecounter[7+8*2:8*2];
data[3]=trigratecounter[7+8*3:8*3];
ioCountToSend = 4; // send 4 bytes
trigratecountreset=1;
state=WRITE1;
end
end
else state=READ; // if we got some other command, just ignore it
end
SPIWAIT: begin
newcomdata<=0; //set this back, to just send out data once
//wait for SPIstate from oscillo to be nearly done
if (SPIstate==3) begin
state=READ;
end
end
PLLCLOCK: begin // to step the clock phase, you have to toggle scanclk a few times
pllclock_counter=pllclock_counter+1;
if (pllclock_counter[4]) begin
scanclk = ~scanclk;
pllclock_counter=0;
scanclk_cycles=scanclk_cycles+1;
if (scanclk_cycles>5) phasestep=1'b0; // deassert!
if (scanclk_cycles>7) state=READ;
end
end
WAITING: begin
newcomdata<=0; //set this back, to just send out data once
timeoutcounter=timeoutcounter+1;
if (ext_data_ready) begin // can read out
SendCount= 0;
rdaddress_slow = wraddress_triggerpoint - triggerpoint;// - 1;
rdadtwo_slow = rdaddress_slow;
thecounterbit=thecounter[clockbitstowait];
thecounterbitlockin=thecounter[clockbitstowaitlockin];
if (lockinnumtoshift>0) begin
lockinresult1=0;
lockinresult2=0;
chan2mean=0;
chan3mean=0;
calcmeans=1;
state=LOCKIN1;
end
else begin
if (do_usb) begin
if (do_fast_usb) begin
if (checkfastusbwriting) begin
do_usb<=0;
state=WRITE_EXT1;
end
else state=WRITE_USBFAST_EXT1;
end
else state=WRITE_USB_EXT1;
end
else state=WRITE_EXT1;
end
end
if ( timeoutcounter > 100000000 ) begin
state=READ;//timeout!
//ioCountToSend = 1;
//data[0] = 8'hfb;//send message indicating timeout
//state=WRITE1;
end
end
LOCKIN1: begin
rden = 1;
if ( (thecounter[clockbitstowaitlockin]!=thecounterbitlockin) ) begin
case(SendCount[ram_width+1:ram_width]) // we go through the samples 4 times
0: begin
// first time through we calculate the means
chan2mean = chan2mean + ram_output3;