forked from drandyhaas/Haasoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialprocessor.v.bak
641 lines (603 loc) · 19.7 KB
/
serialprocessor.v.bak
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
// 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,led1,led2,led3,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,ledbase,SPIstate,offset,gainsw,led4,
i2c_ena,i2c_addr,i2c_rw,i2c_datawr,i2c_datard,i2c_busy,i2c_ackerror);
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 reg led1,led2,led3,led4,ledbase;
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 reg [ram_width-1:0] rdaddress;
output reg [ram_width-1:0] triggerpoint;
output reg rden;
input wire [7:0] ram_output1;
input wire [7:0] ram_output2;
input wire [7:0] ram_output3;
input wire [7:0] ram_output4;
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 [11:0] writesamp;//max of 4096 samples
output reg writeadc;
output reg [11:0] adctestout;
output reg [4: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;
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;
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] i2cdata1;
reg [7:0] i2cdata2;
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;
integer state;
reg [7:0] myid;
reg [7:0] extradata[3:0];//to store command extra data, like arguemnts
reg [ram_width+1:0] SendCount;
integer 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)
integer bytesread, byteswanted;
reg thecounterbit;
integer clockbitstowait = 5; //wait 2^clockbitstowait (8?) ticks before sending each data byte
reg [3:0] sendincrement = 0; //skip 2**sendincrement bytes each time
reg [ram_width-1:0] samplestosend = 0;
reg [7:0] chanforscreen=0;
reg canreadout;
//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
localparam LEN = 1;//number of bytes to write out
localparam LENMAX = LEN - 1;
integer ioCount;
reg[7:0] data[0:LENMAX];
initial begin
state<=READ;
myid<=200;
canreadout<=0;
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;
ledbase<=1;
gainsw[0]<=0;//1 is for 1k resistor (gain 2), 0 is for 100 Ohm resistor (gain .2)
gainsw[1]<=0;
gainsw[2]<=0;
gainsw[3]<=0;
end
assign imthefirst = (myid==0);
integer thecounter;
integer theoldcounter;
//set the LEDs to indicate my ID
always @(posedge clk) begin
thecounter<=thecounter+1;
if (state==READ) led4<=1;
else led4<=0;
if ( imthelast & thecounter[26]==1'b1 ) begin //flash every few seconds
led1<=0; led2<=0; led3<=0;//all on
end
else if (txStart) begin
//if (trigDebug) begin
led1<=0; led2<=0; led3<=0;//all on
end
else if (myid==0) begin
led1<=1; led2<=1; led3<=1;//all off
end
else if (myid==1) begin
led1<=0; led2<=1; led3<=1;//binary 1
end
else if (myid==2) begin
led1<=1; led2<=0; led3<=1;//binary 2
end
else if (myid==3) begin
led1<=0; led2<=0; led3<=1;//binary 3
end
else if (myid==4) begin
led1<=1; led2<=1; led3<=0;//binary 4
end
else begin
led1<=1; led2<=1; led3<=1;
end
end
//reg [7:0] PWMoffset = 23; //9.1% *256;
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;
always @(posedge clk) begin
pwmcounter <= pwmcounter + 1'b1; // free-running counter
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
if (get_ext_data) canreadout=0;
if (ext_data_ready) canreadout=1;
end
always @(posedge clk) begin
case (state)
READ: begin
get_ext_data<=0;
adcreset<=1;
txStart<=0;
getadcdata<=0;
bytesread<=0;
byteswanted<=0;
newcomdata<=0;
SPIsend<=0;
i2c_ena<=0;
if (rxReady) begin
readdata = rxData;
state = SOLVING;
end
end
READMORE: begin
newcomdata=0;
if (rxReady) begin
extradata[bytesread] = rxData;
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;
theoldcounter=thecounter;//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)) imthelast=1; // I'm the last one
else imthelast=0;
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
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
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]+1*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]+1*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
samplestosend=256*extradata[0]+1*extradata[1];
if (triggerpoint>(samplestosend-5)) triggerpoint=samplestosend/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]>30) extradata[0]=30;
downsample=extradata[0];
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]+1*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) trigchannels[extradata[0]]=~trigchannels[extradata[0]];//invert previous value
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
ioCount = 0;
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
ioCount = 0;
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
gainsw[extradata[0]]=~gainsw[extradata[0]];//switch the gain of the channel
state=READ;
end
end
else if (readdata==135) begin
byteswanted=2;//wait for next bytes which are the channel and PWM
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
if (extradata[0]==0) PWMoffset0 = extradata[1];//set the PWM value for the channel (fraction of 256 of which to be on for, *3.3V)
if (extradata[0]==1) PWMoffset1 = extradata[1];
if (extradata[0]==2) PWMoffset2 = extradata[1];
if (extradata[0]==3) PWMoffset3 = extradata[1];
state=READ;
end
end
else if (readdata==136) begin
byteswanted=2;//wait for next byte which is the stuff to send over i2c
comdata=readdata;
newcomdata=1; //pass it on
if (bytesread<byteswanted) state=READMORE;
else begin
//send over i2c!
i2cdata1=extradata[0];
i2cdata2=extradata[1];
state=I2CWAIT;
end
end
else state=READ; // if we got some other command, just ignore it
end
SPIWAIT: begin
//wait for SPIstate from oscillo to be nearly done
if (SPIstate==3) begin
state=READ;
end
end
WAITING: begin
if (canreadout) begin
SendCount = 0;
rdaddress = wraddress_triggerpoint - triggerpoint;// - 1;
thecounterbit=thecounter[clockbitstowait];
state=WRITE_EXT1;
end
if ( (thecounter-theoldcounter) > 100000000 ) begin
state=READ;//timeout!
end
end
WRITE_EXT1: begin
rden = 1;
if(!txBusy && (thecounter[clockbitstowait]!=thecounterbit)) begin // wait a few clock cycles??
//rotate through the 4 outputs
if (SendCount[ram_width+1:ram_width]==0) txData=ram_output1;
if (SendCount[ram_width+1:ram_width]==1) txData=ram_output2;
if (SendCount[ram_width+1:ram_width]==2) txData=ram_output3;
if (SendCount[ram_width+1:ram_width]==3) txData=ram_output4;
txStart = 1;
SendCount = SendCount + (2**sendincrement);
rdaddress = rdaddress + (2**sendincrement);
if (samplestosend>0 && SendCount[ram_width-1:0]>=samplestosend) begin
SendCount[ram_width-1:0]=0;
SendCount[ram_width+1:ram_width] = SendCount[ram_width+1:ram_width] + 1;
rdaddress = wraddress_triggerpoint - triggerpoint;// - 1;
end
state=WRITE_EXT2;
end
end
WRITE_EXT2: begin
txStart = 0;
if(SendCount==0) begin
rden = 0;
state=READ;
end
else begin
if ( (rdaddress- wraddress_triggerpoint-64)>=0 && (rdaddress-wraddress_triggerpoint+64)<128 ) begin //update display // - triggerpoint ??
if (SendCount[ram_width+1:ram_width]==chanforscreen) screencolumndata[rdaddress - wraddress_triggerpoint - 64]=63-txData[7:2];//store most significant 6 bits
screenwren <= 1;
end
state=WRITE_EXT1;
end
end
WAIT_ADC1: begin
writeadc<=0;
getadcdata<=1;
//if (adcready) begin
state=WAIT_ADC2;
//end
end
WAIT_ADC2: begin
if (adcvalid) begin
adctestout<=adcdata;
writeadc<=1;
getadcdata<=0;
if (writesamp>=(nsamp-1)) begin
writesamp<=0;
writebyte<=0;
thecounterbit=thecounter[clockbitstowait];
state<=WRITE_BYTE1;
end
else begin
writesamp=writesamp+1;
state=WAIT_ADC1;
end
end
end
WRITE_BYTE1: begin
writeadc<=0;
if(!txBusy && (thecounter[clockbitstowait]!=thecounterbit)) begin
if (writebyte) txData=adcramdata[11:8];
else txData=adcramdata[7:0];
txStart=1;
state=WRITE_BYTE2;
end
end
WRITE_BYTE2: begin
txStart=0;
if (writebyte) writesamp=writesamp+1;
writebyte = ~writebyte;
if (writesamp>(nsamp-1)) state=READ;
else state=WRITE_BYTE1;
end
//just writng out some data bytes
WRITE1: begin
if (!txBusy) begin
txData = data[ioCount];
txStart = 1;
state = WRITE2;
end
end
WRITE2: begin
txStart = 0;
if (ioCount != LENMAX) begin
ioCount = ioCount + 1;
state = WRITE1;
end else begin
ioCount = 0;
state = READ;
end
end
//I2C
I2CWAIT: begin
if (~i2c_busy) begin
i2c_addr = 7'b0100000;// for all 3 pins (last 3 digits) to GND
//i2c_addr = 7'b0100111;// for all 3 pins (last 3 digits) to VCC
i2c_rw = 0;
i2c_datawr = i2cdata1;
//i2c_datard =
state=I2CSEND1;
end
end
I2CSEND1: begin
i2c_ena = 1;
if (i2c_busy) begin
i2c_datawr = i2cdata2;
state=I2CSEND2;
end
end
I2CSEND2: begin
if (~i2c_busy) begin
//i2c_ackerror
state=READ;
end
end
endcase
end
//update display
reg [5:0] columndata;
reg [2:0] row;
reg [6:0] column;
reg [3:0] b;
always @(posedge clk) begin
if (screenaddr>1000) screenreset<=1;
screenaddr = screenaddr + 1; // reset the screen at the beginning of the day - just once
row=7-screenaddr[9:7];
column=screenaddr[6:0];
//if (column>44) screencolumndata[column] = 64'h0000ff;//test hack
//else screencolumndata[column] = 64'h000022;//test hack
columndata = screencolumndata[column];//the current column, 64 bits, of screen data
//now draw this row of this column
if (columndata>=8*(row+1)) screendata = 8'hff; // e.g. if row=1, then if data is 16 and up, all on
else if (columndata>=8*row) begin // e.g. if row=1, then if data is 8-15, set the appropriate bits, e.g. 5 -> 00011111
screendata=8'h00; b=0;
while (b<8) begin
if (columndata[2:0]>b) screendata[b]=1;//7-b?
b=b+1;
end
end
else screendata = 8'h00; // e.g. if d=1, then if data is also not >7 (7 and down), all off
//screendata = 8'h00;//temporarily turn off histo
//if (column>44) screendata = 8'hff;//test hack
//draws the board ID
if (row==7 & trigDebug) begin
if (myid>=screenaddr[6:1] && !screenaddr[0]) screendata = 8'hfa;
end
if (row==6 & !trigDebug) begin
if (myid>=screenaddr[6:1] && !screenaddr[0]) screendata = 8'hfa;
end
end
endmodule