-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathusbd2xx.cpp
executable file
·858 lines (726 loc) · 31.3 KB
/
usbd2xx.cpp
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
/*
FOXSI USB GSE USB Package ----------------------------------------
PURPOSE : Library for the USB D2xx class
REQUIRED :
WRITTEN : Steven Christe (18-Dec-2009), based on code by L. Glesener
MODIFIED Oct-2010 by Lindsay
NOTES : This class is shared with the full GSE program. The commented out lines of
code are used by the GSE program, please do not delete!
TO DO : Read the to do in the .h file and under each of the function definitions below.
*/
#include <iostream>
#include "ftd2xx.h"
#include "usbd2xx.h"
#include <pthread.h>
#include "Application.h"
#include "gui.h"
using namespace std;
extern Gui *gui;
extern Application *app;
#define XSTRIPS 128
#define YSTRIPS 128
#define CHANNELS 1024
#define FRAME_SIZE_IN_SHORTINTS 784
#define FRAME_SIZE_IN_BYTES 1568
extern double detImage[XSTRIPS][YSTRIPS];
extern double detImagemask[XSTRIPS][YSTRIPS];
extern unsigned short int buffer0[FRAME_SIZE_IN_SHORTINTS];
// default constructor method
USB_d2xx::USB_d2xx()
{
nBytesFrame = 624;
nBytesFrame = FRAME_SIZE_IN_SHORTINTS;
frameData = new unsigned short int [FRAME_SIZE_IN_SHORTINTS];
ftStatus = FT_OK;
ftHandle = NULL;
loadDefaultSettings();
}
// construct with initializer
USB_d2xx::USB_d2xx( const int n)
{
nBytesFrame = n;
ftStatus = FT_OK;
frameData = new unsigned short int [n];
ftHandle = NULL;
loadDefaultSettings();
}
int USB_d2xx::open(void)
{
// Define variables:
char * pcBufLD[2]; // Pointer to cBufLD
char cBufLD[1][64]; // Holds the device serial numbers
// FILE * dataFile;
// char dataFileName[20];
unsigned int lostbytes = 0, framesread = 0;
int iNumDevs = 0; //the number of found devices (should only be 1)
char buffer[50]; // used to pass messages to console.
// Initialize pcBufLD. This points to the character string where the device list is stored.
pcBufLD[0] = cBufLD[0];
pcBufLD[1] = NULL;
// Initialize USB connection (with errors)
// first list the devices
ftStatus = FT_ListDevices(pcBufLD, &iNumDevs, FT_LIST_ALL | FT_OPEN_BY_SERIAL_NUMBER);
//check if status is okay, if not display an error message
if(ftStatus != FT_OK or iNumDevs < 1) {
app->printf_to_console("Error: FT_ListDevices(%d)\n", NULL, ftStatus);
return -1;
}
//if status okay then print out list of found devices
for(int i = 0; i < iNumDevs; i++) {
app->printf_to_console("Found Device %d.", NULL, i);
app->printf_to_console("Serial Number - %s\n", cBufLD[i], 0);
}
//status is okay; now open the device
ftStatus = FT_OpenEx(cBufLD, FT_OPEN_BY_SERIAL_NUMBER, &ftHandle);
if(ftStatus != FT_OK){
app->printf_to_console("Error FT_OpenEx(%d), device\n", NULL, ftStatus);
return -1;
}
// printf("Opened device %s\n", cBufLD);
//FIX this code below
//gui->consoleBuf->insert("Opened device %s\n", cBufLD);
//fthandle holds the pointer to the device
// Set data transfer rate to 9600
// ftStatus = FT_SetBaudRate(ftHandle, 9600);
// if(ftStatus != FT_OK){
// char buffer[50];
// sprintf(buffer, "Error FT_SetBaudRate(%d), cBufLD = %s\n", ftStatus, cBufLD[1]);
// gui->consoleBuf->insert(buffer);
// cout << buffer << endl;
// return -1;
// }
// Set read and write timeouts, in millisec. (for now, arbitrary value of 1 second)
ftStatus = FT_SetTimeouts(ftHandle, 1000, 1000);
if(ftStatus != FT_OK){
app->printf_to_console("Error FT_SetTimeouts(%d)\n", NULL, ftStatus);
return -1;
}
ftStatus = FT_Purge(ftHandle, 3); // Purge write and read buffers
if(ftStatus != FT_OK){
app->printf_to_console("Error FT_Purge(%d)\n", NULL, ftStatus);
return -1;
}
// set flow control. although this is not required for FT245R chip, it can help prevent errors on the TXE line
// Set RTS/CTS flow control
ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_RTS_CTS, 0x11, 0x13);
if (ftStatus == FT_OK) {
//cout << "SetFlowControl successful." << endl;
// FT_SetFlowControl OK
} else {
//cout << "SetFlowControl failed." << endl;
return -1;
}
// set buffer size for transfer in function.
ftStatus = FT_SetUSBParameters(ftHandle, 704, 0);
if(ftStatus != FT_OK){
app->printf_to_console("Error FT_SetUSBParameters(%d)\n", NULL, ftStatus);
return -1;
}
//device is now open and ready!
// default data directory, display in "Filename" buffer.
//if(gui->filenameInput->size()==0) gui->filenameInput->insert("/Users/foxsi/GSE/testdata/");
return 0;
}
int USB_d2xx::findSync(void)
{
//cout << "Starting sync\n";
DWORD nBytes = 2; // number of bytes to read
DWORD nBytesRead = 0; // actual number of bytes read
unsigned int dataWord = 0;
int i = 0; // keep track of bytes read before sync word is found
int iMax = 10*nBytesFrame; // maximum times to try
DWORD nBytesToRead = nBytesFrame;
ftStatus = FT_GetQueueStatus(ftHandle, &nBytesRead);
ftStatus = FT_SetTimeouts(ftHandle, 500, 500);
//if( nBytesRead < nBytesToRead ) return -1;
printf("\nfinding sync...\n");
while (1) {
if (i == iMax){
return -1;
}
i++;
ftStatus = FT_GetQueueStatus(ftHandle, &nBytesRead);
//cout << "Attempt " << i << " value " << dataWord << " nBytesRead " << nBytesRead << endl;
if (nBytesRead == 0 or ftStatus != FT_OK) {
//app->printf_to_console("Error FT_Read(%i).", NULL, ftStatus);
return -1;
}
ftStatus = FT_Read(ftHandle, &dataWord, nBytes, &nBytesRead);
//printf("%u\t", dataWord);
// if the sync word is found, read again to see if the sync word is repeated.
if (dataWord == 0xEB90){
ftStatus = FT_Read(ftHandle, &dataWord, nBytes, &nBytesRead);
if (dataWord == 0xEB90) {printf("found sync!\n"); break;}
}
}
return 1;
}
int USB_d2xx::readFrame(void)
{
//cout << "Starting frame read\n";
/* Read */
int nFrames = 1;
DWORD nBytesToRead = nFrames*nBytesFrame;
DWORD nBytesRead = 0; // actual number of bytes read.
ftStatus = FT_GetQueueStatus(ftHandle, &nBytesRead);
ftStatus = FT_SetTimeouts(ftHandle, 500, 500);
if( nBytesRead < nBytesToRead ){
return -1;
}
for(int i=0; i<nBytesToRead; i++){
// initialize the frameData
frameData[i] = 0;
}
if(ftStatus == FT_OK) {
ftStatus = FT_Read(ftHandle, frameData, nBytesToRead, &nBytesRead);
memcpy((void *) buffer0,(void *) frameData, FRAME_SIZE_IN_BYTES);
if((ftStatus) != FT_OK || nBytesRead != nBytesToRead){
app->printf_to_console("Error FT_Read(%d)\n", NULL, ftStatus);
return -1;
}
} else {
app->print_to_console("Could not get USB queue status.\n");
return -1;
}
for(int i=0; i<nBytesToRead; i++){
// initialize the frameData
//printf("%i\t", frameData[i]);
}
//printf("\n");
return nBytesRead;
}
void USB_d2xx::close(void)
{
if(ftHandle != NULL) {
FT_Close(ftHandle);
ftHandle = NULL;
//fclose(dataFile);
app->print_to_console("Closing connection.\n");
}
//gui->usbReadBut->deactivate();
//gui->usbCloseBut->deactivate();
}
void USB_d2xx::printFrame(void)
{
// Data are stored as 16-bit words (short int)
// unsigned short int data[2];
int nData = 98; // n bytes per frame.
int pixel_value = 0;
int ydata[YSTRIPS];
int xdata[XSTRIPS];
unsigned int xmask[XSTRIPS];
unsigned int ymask[YSTRIPS];
int good = 0;
// Loop through the 4 ASICS
for(int j=0; j<4; j++)
{
int index = j*nData; // used for counting words in the data packet
// temporary variables to hold values from the data stream
int chipBit, trigBit, seuBit, commonMode, pedestal;
int mask1, mask2, mask3, mask4;
cout << "\n ASIC " << j << endl << endl;
// Sync word again.
cout << "Sync word:\t\t" << hex << uppercase << frameData[ index ] << endl; index++;
cout << "Detector time:\t\t" << dec << frameData[ index ] << endl; index++;
cout << "Start bit:\t\t" << frameData[ index ] << endl; index++;
cout << "Chip data bit:\t\t" << frameData[ index ] << endl;
chipBit = frameData[ index ];
index++;
cout << "Analog Trigger bit: " << frameData[ index ] << endl;
trigBit = frameData[ index ];
index++;
cout << "SEU bit:\t\t" << frameData[ index ] << endl;
seuBit = frameData[ index ];
index++;
// channel mask displayed in hex
// First two channel mask bits are stored as one word each.
cout << "Channel mask:\t\t:" << hex << uppercase << frameData[ index ]; index++;
cout << frameData[ index ]; index++;
cout << frameData[ index ]; mask1 = frameData[ index ]; index++;
cout << frameData[ index ]; mask2 = frameData[ index ]; index++;
cout << frameData[ index ]; mask3 = frameData[ index ]; index++;
cout << frameData[ index ] << endl; mask4 = frameData[ index ]; index++;
cout << "Common mode:\t\t" << dec << frameData[ index ] << endl;
commonMode = frameData[ index ];
index++;
for(int i=0; i<64; i++){
cout << "Strip " << i << ":\t\t" << frameData[ index ] << endl; index++;
}
cout << endl << mask1 << endl << mask2 << endl << mask3 << endl << mask4 << endl << seuBit << endl << endl;
if((mask1 == 65535) && (mask2 == 65535) && (mask3 == 65535) && (mask4 == 65535) && (seuBit == 0)){
good++;
for(int i=0; i< XSTRIPS/2; i++){
pixel_value = (int)(frameData[ index ]);// - (unsigned short int)commonMode;
cout << "Strip " << i << " data:\t" << (pixel_value) << endl;
// Update the Histogram but limit values to 1024
// only update for the first two ASICs (p-side)
if (j == 0 || j == 1){gui->mainHistogramWindow->add_count(0, pixel_value < 1024 ? pixel_value : 1024);}
//// update for both p-side and n-side
//HistogramFunction[pixel_value < 1024 ? pixel_value : 1024]++;
// // Update the data but limit values to 1024
// if (j == 0 || j == 1) {
// xdata[i + j*XSTRIPS/2] = pixel_value < 1024 ? pixel_value : 1024; }
//// xmask[i + j*XSTRIPS/2] = getbits(frameData[j*nData+3+i], i, 1);
// if (j == 2 || j == 3){
// ydata[i + (j-2)*YSTRIPS/2] = pixel_value < 1024 ? pixel_value : 1024;
// }
if (j == 0) xdata[i] = 0;
if (j == 1) xdata[i+64] = 1;
if (j == 2) ydata[i] = 2;
if (j == 3) ydata[i+64] = 3;
index++;
}
}
cout << "Pedestal:\t\t" << frameData[index] << endl; index++;
frame[j] = * (asic_dataframe*) (&frameData[ j*nData ]);
cout << "start bit:" << frame[j].start_bit << endl;
cout << "chip bit:" << frame[j].chip_bit << endl;
cout << "trig bit:" << frame[j].trig_bit << endl;
cout << "seu bit:" << frame[j].seu_bit << endl;
cout << "pedestal_bit:" << frame[j].pedestal_bit << endl;
cout << "asic mask:" << frame[j].asic_mask << endl;
cout << "common mode:" << frame[j].common_mode << endl;
cout << "noise:" << frame[j].noise << endl;
/* Some leftover code
// first two ASICs are the p-side ASICs and give the energy measurements
// define these as the X strips
cout << "Pedestal value:\t" << frameData[j*nData+72] << endl;
data[0] = frameData[ j*nData+73] % 0x100;
data[1] = frameData[ j*nData+73] / 0x100;
printf("Stop bit\t%u\n", data[0]);
printf("\n\nSYNC WORD\t\t%X\n", data[1]);
//for( int k = 0; k<64; k++){
// cout << k << " " << ;
//update displays
//HistogramFunction[dataWord%1024/10]++;
//for(int j=0;j<XSTRIPS;j++)
//{
// detImage[i-8][j] = dataWord%1024/10;
//}
//sprintf(buffer, "%u", dataWord%1024);
//gui->consoleBuf->insert(buffer);
// cout << buffer << endl;
}
*/
}
//for (int i = 0; i<XSTRIPS; i++) {
// for (int j = 0; j<YSTRIPS; j++) {
// detImage[i][j] = xdata[i]*ydata[j];
// If the glitch button is not ON then multiply the image by the
// mask image to kill the bad pixel data
// if (gui->glitchBut->value() == 0) {
//detImage[i][j] *= xmask[i]*ymask[j];}
// detImage[i][j] *= xmask[i]*ymask[j];}
// if(i == 0 && j == 0) detImage[i][j]=10; // set scale of intensity plot
// }
//}
//cout << "good = " << good << endl << endl;
// if(good >= 3){
gui->mainHistogramWindow->redraw();
gui->mainImageWindow->redraw();
// }
}
void USB_d2xx::writeHeader(FILE *dataFile)
{
int i=0;
// If first event, write data file header
// Header: Nevents and hold time
fprintf( dataFile, "%u\n", (unsigned int)gui->nEvents->value());
fprintf( dataFile, "%u\n", (unsigned int)gui->setHoldTimeWindow_holdTime->value());
// Header: slow control values for all ASICs.
for(i=0; i<45; i++) fprintf( dataFile, "%u\t", asic0settings[i]);
fprintf( dataFile, "\n");
for(i=0; i<64; i++) fprintf( dataFile, "%u\t", disable0[i]);
fprintf( dataFile, "\n");
for(i=0; i<64; i++) fprintf( dataFile, "%u\t", test0[i]);
fprintf( dataFile, "\n\n");
for(i=0; i<45; i++) fprintf( dataFile, "%u\t", asic1settings[i]);
fprintf( dataFile, "\n");
for(i=0; i<64; i++) fprintf( dataFile, "%u\t", disable1[i]);
fprintf( dataFile, "\n");
for(i=0; i<64; i++) fprintf( dataFile, "%u\t", test1[i]);
fprintf( dataFile, "\n\n");
for(i=0; i<45; i++) fprintf( dataFile, "%u\t", asic2settings[i]);
fprintf( dataFile, "\n");
for(i=0; i<64; i++) fprintf( dataFile, "%u\t", disable2[i]);
fprintf( dataFile, "\n");
for(i=0; i<64; i++) fprintf( dataFile, "%u\t", test2[i]);
fprintf( dataFile, "\n\n");
for(i=0; i<45; i++) fprintf( dataFile, "%u\t", asic3settings[i]);
fprintf( dataFile, "\n");
for(i=0; i<64; i++) fprintf( dataFile, "%u\t", disable3[i]);
fprintf( dataFile, "\n");
for(i=0; i<64; i++) fprintf( dataFile, "%u\t", test3[i]);
fprintf( dataFile, "\n");
}
void USB_d2xx::writeFrame(FILE *dataFile)
{
// This function is very close to the last one except that data is printed to file instead of screen.
// Also identifiers like "start bit", "strip 1 data", etc are not written.
// Data are stored as 16-bit words (short int)
int nData = 97; // n bytes per frame.
int pixel_value = 0;
int ydata[YSTRIPS];
int xdata[XSTRIPS];
unsigned int xmask[XSTRIPS];
unsigned int ymask[YSTRIPS];
int good = 0;
int index = 0;
// Loop through the 4 ASICS
for(int j=0; j<4; j++)
{
// int index = j*nData; // used for counting words in the data packet
fprintf( dataFile, "\n\n" );
// for the first ASIC, we need to artificially write the sync word.
// for the other ASICs, it's already in the data stream.
if(j==0){
fprintf( dataFile, "eb90\t");
} else{
fprintf( dataFile, "%x\t", frameData[index]); index++;
}
fprintf( dataFile, "%u\t", frameData[index]); index++; //frame counter
fprintf( dataFile, "%x\t", frameData[index]); index++; //start
fprintf( dataFile, "%x\t", frameData[index]); index++; //chip
fprintf( dataFile, "%x\t", frameData[index]); index++; //trigger
fprintf( dataFile, "%x\t", frameData[index]); index++; //seu
// channel mask displayed in hex
// First two channel mask bits are stored as one word each.
fprintf( dataFile, "%x", frameData[index]); index++;
fprintf( dataFile, "%x", frameData[index]); index++;
fprintf( dataFile, "%x", frameData[index]); index++;
fprintf( dataFile, "%x", frameData[index]); index++;
fprintf( dataFile, "%x", frameData[index]); index++;
fprintf( dataFile, "%x\t", frameData[index]); index++;
fprintf( dataFile, "%u\t", frameData[index]); index++; //pedestal
// strip data
for(int i=0; i<64; i++){
fprintf( dataFile, "%u\t", frameData[index]); index++;
}
fprintf( dataFile, "%u\t", frameData[index]); index++; //common mode
// 10 extra words of readout information
fprintf( dataFile, "%u\t", frameData[index]); index++;
fprintf( dataFile, "%u\t", frameData[index]); index++;
fprintf( dataFile, "%u\t", frameData[index]); index++;
fprintf( dataFile, "%u\t", frameData[index]); index++;
fprintf( dataFile, "%u\t", frameData[index]); index++;
fprintf( dataFile, "%u\t", frameData[index]); index++;
fprintf( dataFile, "%u\t", frameData[index]); index++;
fprintf( dataFile, "%u\t", frameData[index]); index++;
fprintf( dataFile, "%u\t", frameData[index]); index++;
fprintf( dataFile, "%u\t", frameData[index]); index++;
}
}
// ADDED JULY 2011
// Set individual ASIC slow control parameters
void USB_d2xx::setConfig(void)
{
int n;
const int nV=45; // number of values in sendParamsWindow
char *cBufWrite;
//const int n = [1,867];
if (gui->newControlRegisters_check->value() == 0) {
n = 52;
} else {
n = 867;
}
cBufWrite = (char *)malloc(n * sizeof(char));
//char cBufWrite[n]; // write array
DWORD dwBytesWritten; // returns number of bytes written.
// load user input configuration settings from sendParamsWindow.
int value[nV];
int chan[64];
int test[64];
int asic = gui->sendParamsWindow_asic->value();
// initialize configuration arrays
for(int i=0; i<nV; i++) value[i] = 0;
for(int i=0; i<64; i++) chan[i] = 0;
for(int i=0; i<64; i++) test[i] = 0;
// initialize write buffer.
for(int i=0; i<n; i++) cBufWrite[i] = 0;
// fill arrays with values from sendParameters window.
// CHAN values are from the channel disable buttons.
for(int i=0; i<nV; i++) value[i] = gui->sendParamsWindow_value[i]->value();
for(int i=0; i<64; i++) chan[i] = gui->sendParamsWindow_chan[i]->value();
for(int i=0; i<64; i++) test[i] = gui->sendParamsWindow_test[i]->value();
if (gui->newControlRegisters_check->value() == 0)
{
// logic to assemble configuration settings into write array.
// Note the pattern is not the same for each register and some bits are intentionally unused!
cBufWrite[0] = value[0]*16 + value[1]*8 + value[2]*4 + value[3]*2 + value[4] + 32*asic;
cBufWrite[1] = value[5]*16 + value[6]*8 + value[7]*4 + value[8]*2 + value[9] + 32*asic;
cBufWrite[2] = value[10]*16 + value[11]*8 + value[12]*4 + value[13]*2 + value[14] + 32*asic;
cBufWrite[3] = value[15]*16 + value[16]*8 + value[17]*4 + value[18]*2 + value[19] + 32*asic;
cBufWrite[4] = value[20]*16 + value[21]*8 + value[22]*4 + value[23]*2 + getbits(value[24],4,1) + 32*asic; // 4 single bits + LSB of dummy digital delay.
cBufWrite[5] = getbits(value[24], 9, 5) + 32*asic; // 5 MSB of dummy digital delay.
cBufWrite[6] = getbits(value[25], 4, 5) + 32*asic; // 5 LSB of digital threshold.
cBufWrite[7] = getbits(value[25], 9, 5) + 32*asic; // 5 MSB of digital threshold.
cBufWrite[8] = chan[0]*16 + chan[1]*8 + chan[2]*4 + chan[3]*2 + chan[4] + 32*asic;
cBufWrite[9] = chan[5]*16 + chan[6]*8 + chan[7]*4 + chan[8]*2 + chan[9] + 32*asic;
cBufWrite[10] = chan[10]*16 + chan[11]*8 + chan[12]*4 + chan[13]*2 + chan[14] + 32*asic;
cBufWrite[11] = chan[15]*16 + chan[16]*8 + chan[17]*4 + chan[18]*2 + chan[19] + 32*asic;
cBufWrite[12] = chan[20]*16 + chan[21]*8 + chan[22]*4 + chan[23]*2 + chan[24] + 32*asic;
cBufWrite[13] = chan[25]*16 + chan[26]*8 + chan[27]*4 + chan[28]*2 + chan[29] + 32*asic;
cBufWrite[14] = chan[30]*16 + chan[31]*8 + chan[32]*4 + chan[33]*2 + chan[34] + 32*asic;
cBufWrite[15] = chan[35]*16 + chan[36]*8 + chan[37]*4 + chan[38]*2 + chan[39] + 32*asic;
cBufWrite[16] = chan[40]*16 + chan[41]*8 + chan[42]*4 + chan[43]*2 + chan[44] + 32*asic;
cBufWrite[17] = chan[45]*16 + chan[46]*8 + chan[47]*4 + chan[48]*2 + chan[49] + 32*asic;
cBufWrite[18] = chan[50]*16 + chan[51]*8 + chan[52]*4 + chan[53]*2 + chan[54] + 32*asic;
cBufWrite[19] = chan[55]*16 + chan[56]*8 + chan[57]*4 + chan[58]*2 + chan[59] + 32*asic;
cBufWrite[20] = chan[60]*8 + chan[61]*4 + chan[62]*2 + chan[63]*1 + 32*asic; // No MSB for the last disable register.
cBufWrite[21] = test[0]*16 + test[1]*8 + test[2]*4 + test[3]*2 + test[4] + 32*asic;
cBufWrite[22] = test[5]*16 + test[6]*8 + test[7]*4 + test[8]*2 + test[9] + 32*asic;
cBufWrite[23] = test[10]*16 + test[11]*8 + test[12]*4 + test[13]*2 + test[14] + 32*asic;
cBufWrite[24] = test[15]*16 + test[16]*8 + test[17]*4 + test[18]*2 + test[19] + 32*asic;
cBufWrite[25] = test[20]*16 + test[21]*8 + test[22]*4 + test[23]*2 + test[24] + 32*asic;
cBufWrite[26] = test[25]*16 + test[26]*8 + test[27]*4 + test[28]*2 + test[29] + 32*asic;
cBufWrite[27] = test[30]*16 + test[31]*8 + test[32]*4 + test[33]*2 + test[34] + 32*asic;
cBufWrite[28] = test[35]*16 + test[36]*8 + test[37]*4 + test[38]*2 + test[39] + 32*asic;
cBufWrite[29] = test[40]*16 + test[41]*8 + test[42]*4 + test[43]*2 + test[44] + 32*asic;
cBufWrite[30] = test[45]*16 + test[46]*8 + test[47]*4 + test[48]*2 + test[49] + 32*asic;
cBufWrite[31] = test[50]*16 + test[51]*8 + test[52]*4 + test[53]*2 + test[54] + 32*asic;
cBufWrite[32] = test[55]*16 + test[56]*8 + test[57]*4 + test[58]*2 + test[59] + 32*asic;
cBufWrite[33] = test[60]*8 + test[61]*4 + test[62]*2 + test[63]*1 + 32*asic; // No MSB for the last disable register.
cBufWrite[34] = value[26]*4 + value[27]*2 + value[28] + 32*asic;
cBufWrite[35] = getbits(value[29], 4, 5) + 32*asic; // digital threshold
cBufWrite[36] = getbits(value[30], 3, 4) + 32*asic;
cBufWrite[37] = getbits(value[31], 3, 4) + 32*asic;
cBufWrite[38] = getbits(value[32], 3, 4) + 32*asic;
cBufWrite[39] = getbits(value[33], 3, 4) + 32*asic;
cBufWrite[40] = getbits(value[34], 2, 3) + 32*asic;
cBufWrite[41] = getbits(value[35], 2, 3) + 32*asic;
cBufWrite[42] = getbits(value[36], 2, 3) + 32*asic;
cBufWrite[43] = getbits(value[37], 2, 3) + 32*asic;
cBufWrite[44] = getbits(value[38], 2, 3) + 32*asic;
cBufWrite[45] = getbits(value[39], 2, 3) + 32*asic;
cBufWrite[46] = getbits(value[40], 2, 3) + 32*asic;
cBufWrite[47] = getbits(value[41], 2, 3) + 32*asic;
cBufWrite[48] = getbits(value[42], 2, 3) + 32*asic;
cBufWrite[49] = getbits(value[43], 2, 3) + 32*asic;
cBufWrite[50] = getbits(value[44], 2, 3) + 32*asic;
cBufWrite[51] = 0;
}
else {
// logic to assemble configuration settings into write array.
for(int j=0; j<11; j++)
for(int i=0; i<3; i++) cBufWrite[3*j+i] = getbits(value[44-j], 2-i, 1);
for(int j=0; j<4; j++)
for(int i=0; i<4; i++) cBufWrite[4*j+i+33] = getbits(value[33-j], 3-i, 1);
for(int i=0; i<5; i++) cBufWrite[i+49] = getbits(value[29], 4-i, 1);
cBufWrite[54] = getbits(value[28], 0, 1);
cBufWrite[55] = getbits(value[27], 0, 1);
cBufWrite[56] = getbits(value[26], 0, 1);
for(int i=0; i<64; i++) cBufWrite[i+57] = test[63-i];
for(int i=0; i<256; i++) cBufWrite[i+121] = 0;
for(int i=0; i<64; i++) cBufWrite[i+377] = chan[63-i];
for(int i=0; i<10; i++) cBufWrite[i+441] = getbits(value[25], 9-i, 1);
for(int i=0; i<390; i++) cBufWrite[i+451] = 0;
for(int i=0; i<24; i++) cBufWrite[i+841] = getbits(value[23-i], 0, 1);
cBufWrite[865] = 0;
cBufWrite[866] = 0;
}
// Testing purposes
// for(int i=0; i<39; i++)
// printf("%d\n", cBufWrite[i]);
/* Write */
dwBytesWritten = 0;
if((ftStatus = FT_Write(ftHandle, cBufWrite, n, &dwBytesWritten)) != FT_OK) {
printf("Error FT_Write(%d)\n", ftStatus);
return;
}
cout << "Wrote " << dwBytesWritten << " bytes." << endl << endl;
free(cBufWrite);
}
// Set global configuration
void USB_d2xx::setGlobalConfig(int option)
{
char *cBufWrite;
//const int n = [1,867];
int n;
if (gui->newControlRegisters_check->value() == 0) {
n = 1;
} else {
n = 867;
}
cBufWrite = (char *)malloc(n * sizeof(char));
int value = 0;
DWORD dwBytesWritten; // returns number of bytes written.
// initialize write buffer.
for(int i=0; i<n; i++) cBufWrite[i] = 0;
switch (option) {
case 0:
value = gui->setHoldTimeWindow_holdTime->value();
break;
case 1:
value = gui->setTrigWindow_timeoutTime->value();
break;
case 2:
value = gui->setTrigWindow_useTimeout->value();
break;
case 3:
value = gui->setTrigWindow_delayTime->value();
break;
default:
value = 0;
break;
}
value = value + 128; // put '1' in MSB as flag that this is a global setting.
value = value + option*32; // put indicator in bits [6:5] to tell FPGA which kind of setting it is.
for(int i=0; i<n; i++) {cBufWrite[i] = value;}
/* Write */
dwBytesWritten = 0;
if((ftStatus = FT_Write(ftHandle, cBufWrite, 1, &dwBytesWritten)) != FT_OK) {
printf("Error FT_Write(%d)\n", ftStatus);
free(cBufWrite);
return;
}
cout << "Wrote " << dwBytesWritten << " bytes, value: " << dec << cBufWrite[0] << " value is " << value << endl << endl;
printf("Wrote %d bytes, value %d\n\n", dwBytesWritten, cBufWrite[0]);
free(cBufWrite);
}
/*
// Added August 2011
// Break acquisition loop.
void USB_d2xx::breakAcq(int data)
{
const int n = 1; // number bytes to send
char cBufWrite[n]; // write array
DWORD dwBytesWritten; // returns number of bytes written.
// initialize write buffer.
cBufWrite[0] = data;
// Write
dwBytesWritten = 0;
if((ftStatus = FT_Write(ftHandle, cBufWrite, n, &dwBytesWritten)) != FT_OK) {
printf("Error FT_Write(%d)\n", ftStatus);
return;
}
cout << "Wrote " << dwBytesWritten << " bytes, value: " << dec << cBufWrite[0] << endl << endl;
printf("Wrote %d bytes, value %d\n\n", dwBytesWritten, cBufWrite[0]);
}
*/
// Added August 2011
// Save configuration settings for the selected ASIC.
// save values for this ASIC.
void USB_d2xx::saveSettings()
{
int nVal = 45;
int nChan = 64;
int asic = gui->sendParamsWindow_asic->value();
switch (asic) {
case 0:
for(int i=0; i<nVal; i++) asic0settings[i] = gui->sendParamsWindow_value[i]->value();
for(int i=0; i<nChan; i++) disable0[i] = gui->sendParamsWindow_chan[i]->value();
for(int i=0; i<nChan; i++) test0[i] = gui->sendParamsWindow_test[i]->value();
break;
case 1:
for(int i=0; i<nVal; i++) asic1settings[i] = gui->sendParamsWindow_value[i]->value();
for(int i=0; i<nChan; i++) disable1[i] = gui->sendParamsWindow_chan[i]->value();
for(int i=0; i<nChan; i++) test1[i] = gui->sendParamsWindow_test[i]->value();
break;
case 2:
for(int i=0; i<nVal; i++) asic2settings[i] = gui->sendParamsWindow_value[i]->value();
for(int i=0; i<nChan; i++) disable2[i] = gui->sendParamsWindow_chan[i]->value();
for(int i=0; i<nChan; i++) test2[i] = gui->sendParamsWindow_test[i]->value();
break;
case 3:
for(int i=0; i<nVal; i++) asic3settings[i] = gui->sendParamsWindow_value[i]->value();
for(int i=0; i<nChan; i++) disable3[i] = gui->sendParamsWindow_chan[i]->value();
for(int i=0; i<nChan; i++) test3[i] = gui->sendParamsWindow_test[i]->value();
break;
default:
break;
}
// testing
// for(int i=0; i<nVal; i++)
// cout << " " << asic0settings[i] << " " << asic1settings[i] << " " << asic2settings[i] << " " << asic3settings[i] << endl;
}
// Added August 2011
// Restore configuration settings for the selected ASIC.
void USB_d2xx::restoreSettings()
{
int nVal = 45;
int nChan = 64;
int asic = gui->sendParamsWindow_asic->value();
switch (asic) {
case 0:
for(int i=0; i<nVal; i++) gui->sendParamsWindow_value[i]->value(asic0settings[i]);
for(int i=0; i<nChan; i++) gui->sendParamsWindow_chan[i]->value(disable0[i]);
for(int i=0; i<nChan; i++) gui->sendParamsWindow_test[i]->value(test0[i]);
break;
case 1:
for(int i=0; i<nVal; i++) gui->sendParamsWindow_value[i]->value(asic1settings[i]);
for(int i=0; i<nChan; i++) gui->sendParamsWindow_chan[i]->value(disable1[i]);
for(int i=0; i<nChan; i++) gui->sendParamsWindow_test[i]->value(test1[i]);
break;
case 2:
for(int i=0; i<nVal; i++) gui->sendParamsWindow_value[i]->value(asic2settings[i]);
for(int i=0; i<nChan; i++) gui->sendParamsWindow_chan[i]->value(disable2[i]);
for(int i=0; i<nChan; i++) gui->sendParamsWindow_test[i]->value(test2[i]);
break;
case 3:
for(int i=0; i<nVal; i++) gui->sendParamsWindow_value[i]->value(asic3settings[i]);
for(int i=0; i<nChan; i++) gui->sendParamsWindow_chan[i]->value(disable3[i]);
for(int i=0; i<nChan; i++) gui->sendParamsWindow_test[i]->value(test3[i]);
break;
default:
break;
}
}
// This function sets the default configuration settings. For now, these are hardcoded.
void USB_d2xx::loadDefaultSettings()
{
asic0settings[0] = 0; asic1settings[0] = 0; asic2settings[0] = 0; asic3settings[0] = 0;
asic0settings[1] = 0; asic1settings[1] = 0; asic2settings[1] = 0; asic3settings[1] = 0;
asic0settings[2] = 0; asic1settings[2] = 0; asic2settings[2] = 0; asic3settings[2] = 0;
asic0settings[3] = 1; asic1settings[3] = 1; asic2settings[3] = 1; asic3settings[3] = 1; // sbi_hp2
asic0settings[4] = 0; asic1settings[4] = 0; asic2settings[4] = 0; asic3settings[4] = 0;
asic0settings[5] = 0; asic1settings[5] = 0; asic2settings[5] = 0; asic3settings[5] = 0;
asic0settings[6] = 0; asic1settings[6] = 0; asic2settings[6] = 0; asic3settings[6] = 0;
asic0settings[7] = 1; asic1settings[7] = 1; asic2settings[7] = 1; asic3settings[7] = 1; // ro_all
asic0settings[8] = 0; asic1settings[8] = 0; asic2settings[8] = 0; asic3settings[8] = 0;
asic0settings[9] = 1; asic1settings[9] = 1; asic2settings[9] = 1; asic3settings[9] = 1; // preb_hp
asic0settings[10] = 0; asic1settings[10] = 0; asic2settings[10] = 0; asic3settings[10] = 0;
asic0settings[11] = 0; asic1settings[11] = 0; asic2settings[11] = 0; asic3settings[11] = 0;
asic0settings[12] = 1; asic1settings[12] = 1; asic2settings[12] = 0; asic3settings[12] = 0; // nside
asic0settings[13] = 0; asic1settings[13] = 0; asic2settings[13] = 0; asic3settings[13] = 0;
asic0settings[14] = 0; asic1settings[14] = 0; asic2settings[14] = 0; asic3settings[14] = 0;
asic0settings[15] = 0; asic1settings[15] = 0; asic2settings[15] = 0; asic3settings[15] = 0;
asic0settings[16] = 1; asic1settings[16] = 1; asic2settings[16] = 0; asic3settings[16] = 0; // negQ
asic0settings[17] = 0; asic1settings[17] = 0; asic2settings[17] = 0; asic3settings[17] = 0;
asic0settings[18] = 0; asic1settings[18] = 0; asic2settings[18] = 0; asic3settings[18] = 0;
asic0settings[19] = 0; asic1settings[19] = 0; asic2settings[19] = 0; asic3settings[19] = 0;
asic0settings[20] = 0; asic1settings[20] = 0; asic2settings[20] = 0; asic3settings[20] = 0;
asic0settings[21] = 0; asic1settings[21] = 0; asic2settings[21] = 0; asic3settings[21] = 0;
asic0settings[22] = 0; asic1settings[22] = 0; asic2settings[22] = 0; asic3settings[22] = 0;
asic0settings[23] = 0; asic1settings[23] = 0; asic2settings[23] = 0; asic3settings[23] = 0;
asic0settings[24] = 0; asic1settings[24] = 0; asic2settings[24] = 0; asic3settings[24] = 0;
asic0settings[25] = 0; asic1settings[25] = 0; asic2settings[25] = 0; asic3settings[25] = 0;
asic0settings[26] = 0; asic1settings[26] = 0; asic2settings[26] = 0; asic3settings[26] = 0;
asic0settings[27] = 0; asic1settings[27] = 0; asic2settings[27] = 0; asic3settings[27] = 0;
asic0settings[28] = 0; asic1settings[28] = 0; asic2settings[28] = 0; asic3settings[28] = 0;
asic0settings[29] = 31; asic1settings[29] = 31; asic2settings[29] = 15; asic3settings[29] = 15; // vthr
asic0settings[30] = 10; asic1settings[30] = 10; asic2settings[30] = 6; asic3settings[30] = 6; // ifp
asic0settings[31] = 0; asic1settings[31] = 0; asic2settings[31] = 9; asic3settings[31] = 9; // iramp
asic0settings[32] = 0; asic1settings[32] = 0; asic2settings[32] = 0; asic3settings[32] = 0;
asic0settings[33] = 0; asic1settings[33] = 0; asic2settings[33] = 0; asic3settings[33] = 0;
asic0settings[34] = 0; asic1settings[34] = 0; asic2settings[34] = 0; asic3settings[34] = 0;
asic0settings[35] = 7; asic1settings[35] = 7; asic2settings[35] = 7; asic3settings[35] = 7; // shabias
asic0settings[36] = 1; asic1settings[36] = 1; asic2settings[36] = 0; asic3settings[36] = 0; // ifss
asic0settings[37] = 0; asic1settings[37] = 0; asic2settings[37] = 0; asic3settings[37] = 0;
asic0settings[38] = 0; asic1settings[38] = 0; asic2settings[38] = 0; asic3settings[38] = 0;
asic0settings[39] = 0; asic1settings[39] = 0; asic2settings[39] = 0; asic3settings[39] = 0;
asic0settings[40] = 7; asic1settings[40] = 7; asic2settings[40] = 7; asic3settings[40] = 7; // prebias
asic0settings[41] = 0; asic1settings[41] = 0; asic2settings[41] = 0; asic3settings[41] = 0;
asic0settings[42] = 0; asic1settings[42] = 0; asic2settings[42] = 0; asic3settings[42] = 0;
asic0settings[43] = 5; asic1settings[43] = 5; asic2settings[43] = 2; asic3settings[43] = 1; // ioffset
asic0settings[44] = 0; asic1settings[44] = 0; asic2settings[44] = 0; asic3settings[44] = 0;
for(int i=0; i<64; i++){
disable0[i] = 1;
disable1[i] = 1;
disable2[i] = 0;
disable3[i] = 0;
test0[i] = 0;
test1[i] = 0;
test2[i] = 0;
test3[i] = 0;
}
}