Project

General

Profile

PxCAPTANHardware.cc

C++ implementation on how to access Ryan or Purdue firmware - Lorenzo Uplegger, 01/12/2015 12:10 PM

 
1
#include "PxDAQHardware/include/PxCAPTANHardware.h"
2
#include "PxDAQHardware/include/PxFSSRFirmware.h"
3
#include "PxUtilities/PxDataTypes/include/PxDataTypes.h"
4
#include "PxUtilities/PxMacroUtilities/include/PxMacro.h"
5

    
6
#include <cstdlib>
7
#include <iostream>
8
#include <sstream>
9

    
10
using namespace std;
11
using namespace PxSuite;
12

    
13
//========================================================================================================================
14
PxCAPTANHardware::PxCAPTANHardware (string firmware, unsigned int firmwareVersion, string captanIPAddress, unsigned int captanIPPort, unsigned int captanReadSocket, unsigned int captanWriteSocket) :
15
        PxNetworkDevice             (captanIPAddress,captanIPPort),
16
        theFirmware_                (0),
17
        captanBaseIP_               ("192.168.133"),
18
        captanIPAddress_            (captanIPAddress),
19
        captanIPPort_               (captanIPPort)
20
        //        string16_                   (string(2,' ')),
21
        //        string32_                   (string(4,' ')),
22
        //        string64_                   (string(8,' '))
23
{
24
    destinationComputer_.ipAddress     = PxNetworkDevice::getFullIPAddress(captanBaseIP_);
25
    destinationComputer_.interfaceName = PxNetworkDevice::getInterfaceName(destinationComputer_.ipAddress);
26
    destinationComputer_.macAddress    = PxNetworkDevice::getMacAddress   (destinationComputer_.interfaceName);
27
    captanWriteSocketDescriptor_       = PxNetworkDevice::initSocket(captanWriteSocket);
28
    //captanReadSocketDescriptor_        = PxNetworkDevice::initSocket(captanReadSocket);
29

    
30
    if(firmware == "FSSR")
31
        theFirmware_ = new PxFSSRFirmware(firmwareVersion);
32
    //RYAN'S ONLY setupCaptanReadSocket ();
33
}
34

    
35
//========================================================================================================================
36
PxCAPTANHardware::PxCAPTANHardware (string captanIPAddress, unsigned int captanIPPort, unsigned int captanFEDReadSocket, unsigned int captanFEDWriteSocket, unsigned int captanSLinkReadSocket, unsigned int captanSLinkWriteSocket) :
37
        PxNetworkDevice             (captanIPAddress,captanIPPort),
38
        theFirmware_                (0),
39
        captanBaseIP_               ("192.168.133"),
40
        captanIPAddress_            (captanIPAddress),
41
        captanIPPort_               (captanIPPort)
42
        //        string16_                   (string(2,' ')),
43
        //        string32_                   (string(4,' ')),
44
        //        string64_                   (string(8,' '))
45
{
46
    destinationComputer_.ipAddress     = PxNetworkDevice::getFullIPAddress(captanBaseIP_);
47
    destinationComputer_.interfaceName = PxNetworkDevice::getInterfaceName(destinationComputer_.ipAddress);
48
    destinationComputer_.macAddress    = PxNetworkDevice::getMacAddress   (destinationComputer_.interfaceName);
49

    
50
    captanWriteSocketDescriptor_    = PxNetworkDevice::initSocket(captanFEDWriteSocket);
51
    //captanFEDReadSocketDescriptor_     = PxNetworkDevice::initSocket(captanFEDReadSocket);
52
    //captanSLinkReadSocketDescriptor_   = PxNetworkDevice::initSocket(captanSLinkReadSocket);
53
    //captanSLinkWriteSocketDescriptor_  = PxNetworkDevice::initSocket(captanSLinkWriteSocket);
54

    
55
    if(true)// || firmware == "FSSR")
56
        theFirmware_ = new PxFSSRFirmware(0);//firmwareVersion);
57
    //RYAN'S ONLY setupCaptanFEDReadSocket ();
58
    cout << __COUT_HDR__ << "Done initializing FED Hardware!" << endl;
59
}
60

    
61
//========================================================================================================================
62
PxCAPTANHardware::~PxCAPTANHardware(void)
63
{
64
    cout << __COUT_HDR__ << endl;
65
    close(captanWriteSocketDescriptor_);
66
    //close(captanReadSocketDescriptor_);
67

    
68
    //close(captanFEDWriteSocketDescriptor_);
69
    //close(captanFEDReadSocketDescriptor_);
70
    //close(captanSLinkWriteSocketDescriptor_);
71
    //close(captanSLinkReadSocketDescriptor_);
72
}
73

    
74
//========================================================================================================================
75
//Private constructor that I don't want to be called
76
PxCAPTANHardware::PxCAPTANHardware(void) :
77
        PxNetworkDevice("",0)
78
{}
79
//========================================================================================================================
80
int PxCAPTANHardware::init(void)
81
{
82
    return 0;
83
}
84

    
85
//========================================================================================================================
86
int PxCAPTANHardware::configureFEC(void)
87
{
88

    
89
    /*RYAN'S//////////////////////////////////////
90
    //Reset DCM and set the right frequency
91
       string value(7,'\0');
92
       value += 1;
93
       write((UInt64)0x200,value);
94
       usleep(100000);
95

96
       string value2(6,'\0');
97
       value2+=5;
98
       value2+=0x50;
99
       write((UInt64)0x100,value2);
100
       usleep(100000);
101
       string value3(6,'\0');
102
       value3+=25;
103
       value3+=0x52;
104
       write((UInt64)0x100,value3);
105
       usleep(100000);
106

107
       string value1(8,'\0');
108
       write((UInt64)0x200,value1);
109
       usleep(100000);
110

111
       //start();
112
       usleep(100000);
113
       cout << __COUT_HDR__ << "Done configuring!" << endl;
114

115
    string data;
116
       //Reset DCM and set the right frequency
117
       cout << __COUT_HDR__ << "Setting BCO DCM frequencies!!!" << endl;
118
       string bvalue(8,'\0');
119
       write((UInt64)0xc00,bvalue);
120
       usleep(100000);
121
       string bvalue1(7,'\0');
122
       bvalue1 += (char)1;
123
       write((UInt64)0xc00,bvalue1);
124
       usleep(100000);
125
       string bvalue2(6,'\0');
126
       bvalue2+=6;
127
       bvalue2+=0x50;
128
       write((UInt64)0xe00,bvalue2);
129
       usleep(100000);
130
       string bvalue3(6,'\0');
131
       bvalue3+=25;
132
       bvalue3+=0x52;
133
       write((UInt64)0xe00,bvalue3);
134
       usleep(100000);
135
       string bvalue4(8,'\0');
136
       write((UInt64)0xc00,bvalue4);
137
       usleep(100000);
138

139
       cout << __COUT_HDR__ << "FFR reset!!!" << endl;
140
       string value5(8,'\0');
141
       value5[0] = (char)4;
142
       write((UInt64)0x300,value5);//307 broadcast to all modules connected
143
       string value6(8,'\0');
144
       write((UInt64)0x300,value6);
145
    END RYAN*/
146

    
147
    write(theFirmware_->configureClocks());
148

    
149
    write(theFirmware_->resetDetector());
150
    return 0;
151
}
152

    
153
//========================================================================================================================
154
int PxCAPTANHardware::configureFED(void)
155
{
156
    write(theFirmware_->setDataDestination());
157
    return 0;
158
}
159

    
160
//========================================================================================================================
161
int PxCAPTANHardware::start(void)
162
{
163

    
164
    cout << __COUT_HDR__ << "Setting send data!!!" << endl;
165
    //setSendData();
166
    //There should be a PxFSSRROCDefinitions, you can call write(0x300, PxFSSRROCDefinitions::makeDACWriteCommand(chipId, "SendData", 1)) directly.
167
    //clrRejectHits();
168
    //Similar as above... the problemis that write is called more than one time in setSendData... what are exactly the things it does each time?
169
    //WriteChipRegisters();
170
    write(theFirmware_->resetBCO());
171
    write(theFirmware_->startStream());
172

    
173
    return 0;
174
}
175

    
176
//========================================================================================================================
177
int PxCAPTANHardware::stop(void)
178
{
179
    //write(theFirmware_->stopStream());
180
    return 0;
181
}
182

    
183
//========================================================================================================================
184
int PxCAPTANHardware::pause(void)
185
{
186
    //write(theFirmware_->stopStream());
187
    return 0;
188
}
189

    
190
//========================================================================================================================
191
int PxCAPTANHardware::resume(void)
192
{
193
    write(theFirmware_->resetBCO());
194
    write(theFirmware_->startStream());
195
    return 0;
196
}
197

    
198
//========================================================================================================================
199
int PxCAPTANHardware::globalReset(void)
200
{
201
    cout << __COUT_HDR__ << "Global reset issued!" << endl;
202
    //Reset DCM and set the right frequency
203
    string value(7,'\0');
204
    value += (char)1;
205
    write((UInt64)0x700,value);
206
    return 0;
207
}
208

    
209
//========================================================================================================================
210
int  PxCAPTANHardware::read(UInt64 address, string& value)
211
{
212
    string buffer;
213
    buffer += (char)0;
214
    buffer += (char)1;
215
    appendToString(address,buffer);
216

    
217
    cout << __COUT_HDR__ << "Sending message:-";
218
    for(unsigned int i=0; i<buffer.size(); i++)
219
        cout << hex << (UInt8)buffer[i] << "-";
220
    cout << dec << endl;
221

    
222
    PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer);
223
    PxNetworkDevice::receive(captanReadSocketDescriptor_,value);
224

    
225
    cout << __COUT_HDR__ << "\tMessage received:-";
226
    for(unsigned int i=0; i<value.size(); i++)
227
        cout << hex << (UInt8)value[i] << "-";
228
    cout << dec << endl;
229

    
230
    return 0;
231
}
232

    
233
//========================================================================================================================
234
int  PxCAPTANHardware::read(UInt32 address, string& value)
235
{
236
    string buffer;
237
    //Extra stuff for single command to be removed in later firmwares
238
    buffer += (char)1;
239
    buffer += (char)4;
240
    buffer += (char)0;
241
    buffer += (char)4;
242
    appendToString((UInt32)0,buffer);
243
    buffer += (char)1;
244
    buffer += (char)4;//READ COMMAND
245
    buffer += (char)0;
246
    buffer += (char)4;
247
    appendToString(address,buffer);
248

    
249
    cout << __COUT_HDR__ << "Sending message:-";
250
    for(unsigned int i=0; i<buffer.size(); i++)
251
        cout << hex << (UInt8)buffer[i] << "-";
252
    cout << dec << endl;
253

    
254
    PxNetworkDevice::send   (captanWriteSocketDescriptor_,buffer);
255
    PxNetworkDevice::receive(captanWriteSocketDescriptor_,value);
256

    
257
    cout << __COUT_HDR__ <<"\tMessage received:-";
258
    for(unsigned int i=0; i<value.size(); i++)
259
        cout << hex << (UInt8)value[i] << "-";
260
    cout << dec << endl;
261

    
262
    return 0;
263
}
264

    
265
//========================================================================================================================
266
int  PxCAPTANHardware::read(UInt32 address, UInt32 data, string& value)
267
{
268
    string buffer;
269
    //Extra stuff for single command to be removed in later firmwares
270
    buffer += (char)1;
271
    buffer += (char)4;
272
    buffer += (char)0;
273
    buffer += (char)4;
274
    appendToString((UInt32)0,buffer);
275
    buffer += (char)1;
276
    buffer += (char)4;//READ COMMAND
277
    buffer += (char)0;
278
    buffer += (char)4;
279
    appendToString(address,buffer);
280
    appendToString(data,buffer);
281

    
282
    cout << __COUT_HDR__ << "Sending message:-";
283
    for(unsigned int i=0; i<buffer.size(); i++)
284
        cout << hex << (UInt8)buffer[i] << "-";
285
    cout << dec << endl;
286

    
287
    PxNetworkDevice::send   (captanWriteSocketDescriptor_,buffer);
288
    PxNetworkDevice::receive(captanWriteSocketDescriptor_,value);
289

    
290
    cout << __COUT_HDR__ <<"\tMessage received:-";
291
    for(unsigned int i=0; i<value.size(); i++)
292
        cout << hex << (UInt8)value[i] << "-";
293
    cout << dec << endl;
294

    
295
    return 0;
296
}
297

    
298
//========================================================================================================================
299
int  PxCAPTANHardware::listen()
300
{
301
    /*
302
    cout << __COUT_HDR__ << "Listening" << endl;
303
    string value;
304
    PxNetworkDevice::listen(captanReadSocketDescriptor_,value);
305
    cout << __COUT_HDR__ << __LINE__ <<"\tMessage received:-";
306
    for(UInt32 i=0; i<value.size(); i++)
307
        cout << hex << (UInt8)value[i] << "-";
308
    cout << dec << endl;
309
    */
310
    return 0;
311
}
312

    
313
//========================================================================================================================
314
int  PxCAPTANHardware::read(UInt64 address, vector<UInt64>& value)
315
{
316
    return 0;
317
}
318

    
319
//========================================================================================================================
320
int  PxCAPTANHardware::read(UInt32 address, vector<UInt32>& value)
321
{
322
    return 0;
323
}
324

    
325
//========================================================================================================================
326
int PxCAPTANHardware::write(UInt64 address, const string& data)
327
{
328
    cout << __COUT_HDR__ << "Address: " << address << " Data:-"<< data << "-" << endl;
329
    string buffer;
330
    buffer += 1;
331
    buffer += data.size()/8;
332
    appendToString(address,buffer);
333
    buffer += data;
334

    
335
    //cout << __COUT_HDR__ << "Write buffer:-";
336
    //for(unsigned int i=0; i<buffer.size(); i++)
337
    //    cout << hex << (UInt8)buffer[i] << "-";
338
    //cout << dec << endl;
339

    
340
    return PxNetworkDevice::send(captanWriteSocketDescriptor_, buffer);
341
}
342

    
343
//========================================================================================================================
344
int PxCAPTANHardware::write(UInt32 address, const string& data)
345
{
346
    cout << __COUT_HDR__ << "Address: " << address << " Data:-"<< data << "-" << endl;
347
    string buffer;
348
    buffer += (char)1;
349
    buffer += (char)1;//WRITE COMMAND
350
    buffer += (char)0;
351
    buffer += (char)8;
352
    appendToString(address,buffer);
353
    buffer += data;
354

    
355
    cout << __COUT_HDR__ << "Sending message:-";
356
    for(unsigned int i=0; i<buffer.size(); i++)
357
        cout << hex << (UInt8)buffer[i] << "-";
358
    cout << dec << endl;
359

    
360
    //cout << __COUT_HDR__ << "Write buffer:-";
361
    //for(unsigned int i=0; i<buffer.size(); i++)
362
    //    cout << hex << (UInt8)buffer[i] << "-";
363
    //cout << dec << endl;
364

    
365
    return PxNetworkDevice::send(captanWriteSocketDescriptor_, buffer);
366
}
367

    
368
//========================================================================================================================
369
int PxCAPTANHardware::write(UInt64 address, const char* data, int length)
370
{
371
    cout << __COUT_HDR__ << "Address: " << address << " Data:-"<< data << "-" << endl;
372
    string buffer;
373
    buffer += 1;
374
    buffer += length/8;
375
    appendToString(address,buffer);
376
    for(int i=0; i<length; i++)
377
        buffer += data[i];
378

    
379
    //cout << __COUT_HDR__ << "Write buffer:-";
380
    //for(unsigned int i=0; i<buffer.size(); i++)
381
    //    cout << hex << (UInt8)buffer[i] << "-";
382
    //cout << dec << endl;
383

    
384
    return PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer);
385
}
386

    
387
//========================================================================================================================
388
int PxCAPTANHardware::write(UInt32 address, const char* data, int length)
389
{
390
    cout << __COUT_HDR__ << "Address: " << address << " Data:-"<< data << "-" << endl;
391
    string buffer;
392
    buffer += (char)1;
393
    buffer += (char)1;//WRITE COMMAND
394
    buffer += (char)0;
395
    buffer += (char)8;
396
    appendToString(address,buffer);
397
    for(int i=0; i<length; i++)
398
        buffer += data[i];
399

    
400
    //cout << __COUT_HDR__ << "Write buffer:-";
401
    //for(unsigned int i=0; i<buffer.size(); i++)
402
    //    cout << hex << (UInt8)buffer[i] << "-";
403
    //cout << dec << endl;
404

    
405
    return PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer);
406
}
407

    
408
//========================================================================================================================
409
int PxCAPTANHardware::write(UInt64 address, UInt64 data)
410
{
411
    string buffer;
412
    buffer += (char)1;
413
    buffer += (char)1;
414
    appendToString(address,buffer);
415
    appendToString(data,buffer);
416

    
417
    //cout << __COUT_HDR__ << "Write buffer:-";
418
    //for(unsigned int i=0; i<buffer.size(); i++)
419
    //    cout << hex << (UInt8)buffer[i] << "-";
420
    //cout << dec << endl;
421

    
422
    return PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer);
423
}
424

    
425
//========================================================================================================================
426
int PxCAPTANHardware::write(UInt32 address, UInt32 data)
427
{
428
    string buffer;
429
    buffer += (char)1;
430
    buffer += (char)1;
431
    buffer += (char)0;
432
    buffer += (char)8;
433
    appendToString(address,buffer);
434
    appendToString(data,buffer);
435

    
436
    cout << __COUT_HDR__ << hex << "Address: " << address << " Data: " << data << dec << endl;
437
    cout << __COUT_HDR__ << "Write buffer:-";
438
    for(unsigned int i=0; i<buffer.size(); i++)
439
        cout << hex << (UInt8)buffer[i] << "-";
440
    cout << dec << endl;
441

    
442
    return PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer);
443
}
444

    
445
//========================================================================================================================
446
int PxCAPTANHardware::startBurst(UInt64 address, UInt64 data)
447
{
448
    string buffer;
449
    buffer += (char)2;
450
    buffer += (char)1;
451
    appendToString(address,buffer);
452
    appendToString(data,buffer);
453

    
454
    //cout << __COUT_HDR__ << "Write buffer:-";
455
    //for(unsigned int i=0; i<buffer.size(); i++)
456
    //    cout << hex << (UInt8)buffer[i] << "-";
457
    //cout << dec << endl;
458

    
459
    return PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer);
460
}
461

    
462
//========================================================================================================================
463
int PxCAPTANHardware::stopBurst(UInt64 address, UInt64 data)
464
{
465
    string buffer;
466
    buffer += (char)3;
467
    buffer += (char)1;
468
    appendToString(address,buffer);
469
    appendToString(data,buffer);
470

    
471
    //cout << __COUT_HDR__ << __LINE__ <<"\tMessage:-";
472
    //for(UInt32 i=0; i<buffer.size(); i++)
473
    //    cout << hex << (UInt16)buffer[i] << "-";
474
    //cout << dec << endl;
475

    
476
    return PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer);
477
}
478

    
479
//========================================================================================================================
480
int PxCAPTANHardware::write(UInt64 address, const vector<UInt64>& data)
481
{
482
    string aknowledgment;
483
        if(data.size() == 1)
484
        return write(address,data[0]);
485
    else if(data.size() > MaxBufferSize)
486
    {
487
        unsigned int firstData = 0;
488
        unsigned int lastData  = MaxBufferSize-1;
489
        while(lastData < data.size())
490
        {
491
            write(address, vector<UInt64>(data.begin()+firstData,data.begin()+lastData));
492
            //PxNetworkDevice::receive(captanWriteSocketDescriptor_,aknowledgment);
493
            usleep(50000);
494
            firstData = lastData+1;
495
            lastData += MaxBufferSize;
496
        }
497
        if(data.size()-firstData > 0)
498
            write(address, vector<UInt64>(data.begin()+firstData,data.begin()+data.size()-1));
499
    }
500

    
501
    //    string buffer;
502
    //    buffer += (char)1;
503
    //    buffer += (char)data.size();
504
    //    appendToString(address,buffer);
505
    //    for(vector<UInt64>::const_iterator it=data.begin(); it!=data.end(); it++)
506
    //            appendToString(*it,buffer);
507
    //    return PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer);
508
    for(vector<UInt64>::const_iterator it=data.begin(); it!=data.end(); it++)
509
    {
510
        write(address,*it);
511
        //PxNetworkDevice::receive(captanWriteSocketDescriptor_,aknowledgment);
512
        usleep(50000);
513
    }
514

    
515
    return 0;
516
}
517

    
518
//========================================================================================================================
519
int PxCAPTANHardware::write(UInt32 address, const vector<UInt32>& data)
520
{
521
    string aknowledgment;
522
    if(data.size() == 1)
523
        return write(address,data[0]);
524
    else if(data.size() > MaxBufferSize)
525
    {
526
        unsigned int firstData = 0;
527
        unsigned int lastData  = MaxBufferSize-1;
528
        while(lastData < data.size())
529
        {
530
            write(address, vector<UInt32>(data.begin()+firstData,data.begin()+lastData));
531
            //PxNetworkDevice::receive(captanWriteSocketDescriptor_,aknowledgment);
532
            usleep(50000);
533
            firstData = lastData+1;
534
            lastData += MaxBufferSize;
535
        }
536
        if(data.size()-firstData > 0)
537
            write(address, vector<UInt32>(data.begin()+firstData,data.begin()+data.size()-1));
538
    }
539

    
540
    //    string buffer;
541
    //    buffer += (char)1;
542
    //    buffer += (char)data.size();
543
    //    appendToString(address,buffer);
544
    //    for(vector<UInt32>::const_iterator it=data.begin(); it!=data.end(); it++)
545
    //            appendToString(*it,buffer);
546
    //    return PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer);
547
    for(vector<UInt32>::const_iterator it=data.begin(); it!=data.end(); it++)
548
    {
549
        write(address,*it);
550
        //PxNetworkDevice::receive(captanWriteSocketDescriptor_,aknowledgment);
551
        usleep(50000);
552
    }
553

    
554
    return 0;
555
}
556

    
557
//========================================================================================================================
558
int PxCAPTANHardware::write(const PxFirmwareSequence<UInt64>& sequence)
559
{
560
    for(PxFirmwareSequence<UInt64>::const_iterator it=sequence.begin(); it!=sequence.end(); it++)
561
    {
562
        write(it->first,it->second);
563
        usleep(25000);
564
    }
565
    return 0;
566
}
567

    
568
//========================================================================================================================
569
int PxCAPTANHardware::write(const PxFirmwareSequence<UInt32>& sequence)
570
{
571
    for(PxFirmwareSequence<UInt32>::const_iterator it=sequence.begin(); it!=sequence.end(); it++)
572
    {
573
        write(it->first,it->second);
574
        usleep(25000);
575
    }
576
    return 0;
577
}
578

    
579
//========================================================================================================================
580
int PxCAPTANHardware::write(const string& buffer)
581
{
582
    string aknowledgment;
583
    cout << __COUT_HDR__ << "Sending packet!" << endl;
584
    int len = buffer.length();
585
    cout << __PRETTY_FUNCTION__ << "Write buffer:\n0-";
586
    for(int i=0; i<len; i++){
587
      cout << hex << (unsigned char)buffer[i] << "-" << i+1 << "-";
588
    }
589
    cout << "|" << dec << endl;
590
    if(buffer.length() <= MaxBufferSize)
591
    {
592
        PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer);
593
        //PxNetworkDevice::receive(captanWriteSocketDescriptor_,aknowledgment);
594
        usleep(100000);
595
            return 1;
596
    }
597
    unsigned int firstData = 0;
598
    unsigned int lastData  = MaxBufferSize-1;
599
    while(lastData < buffer.length())
600
    {
601
        PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer.substr(firstData,lastData-firstData+1));
602
        //PxNetworkDevice::receive(captanWriteSocketDescriptor_,aknowledgment);
603
        usleep(50000);
604
        firstData = lastData+1;
605
        lastData += MaxBufferSize;
606
    }
607
    if(buffer.length()-firstData > 0)
608
        PxNetworkDevice::send(captanWriteSocketDescriptor_,buffer.substr(firstData,buffer.length()-firstData+1));
609
    return buffer.length();
610
}
611

    
612
//========================================================================================================================
613
int PxCAPTANHardware::write(vector<string>& buffer)
614
{
615
    for(vector<string>::const_iterator it=buffer.begin(); it!=buffer.end(); it++)
616
        write(*it);
617
    return 1;
618
}
619

    
620
//========================================================================================================================
621
int PxCAPTANHardware::setupCaptanReadSocket(void)
622
{
623
    //RYAN'S FIRMWARE ONLY
624
    string value = convertToString((UInt32)openSockets_[captanReadSocketDescriptor_]).substr(2,2) + destinationComputer_.macAddress;
625
    write((UInt64)0x1900, value);
626
    value = convertToString((UInt64)atoi(destinationComputer_.ipAddress.substr(destinationComputer_.ipAddress.find_last_of('.')+1,destinationComputer_.ipAddress.size()).data()));
627
    write((UInt64)0x1A00, value);
628

    
629
    //value = convertToString((UInt32)openSockets_[captanReadSocketDescriptor_]).substr(2,2) + convertToString((UInt32)openSockets_[captanReadSocketDescriptor_]).substr(2,2);
630
    //write(0x1B00, value);
631

    
632
    //string returnVal;
633
    //read(0x100,returnVal);
634

    
635
    //ping Captan ***********************
636
    if(PxNetworkDevice::ping(captanReadSocketDescriptor_) == -1)
637
        cout << __COUT_HDR__ << "]\tUnable to ping CAPTAN" << endl;
638

    
639
    return 0;
640
}
641

    
642
//========================================================================================================================
643
int PxCAPTANHardware::setupCaptanFEDReadSocket(void)
644
{
645
    cout << __COUT_HDR__ << "Setting up FED CAPTAN" << endl;
646
    string value = convertToString((UInt32)openSockets_[captanSLinkReadSocketDescriptor_]).substr(2,2) + convertToString((UInt32)openSockets_[captanFEDReadSocketDescriptor_]).substr(2,2);
647
    //write(0x1B00, value);
648
    if(PxNetworkDevice::ping(captanFEDReadSocketDescriptor_) == -1)
649
        cout << __COUT_HDR__ << "]\tUnable to ping CAPTAN" << endl;
650

    
651
    return 0;
652
}
653

    
654
//========================================================================================================================
655
//extracts <num_of_bits> bits starting at <start_bit> from string <src> into integer <dest>
656
//assumes src and dest are big enough (32 bits is max size extracted)
657
//start_bit index corresponds to configware indices.
658
void PxCAPTANHardware::helperExtractBits(Char* src, UInt64& dest, UInt32 start_bit, UInt32 num_of_bits)
659
{
660

    
661
    unsigned int i  = (start_bit%64)/8;
662
    unsigned int qw = start_bit/64;
663
    unsigned int b  = start_bit%8;
664
    unsigned int db = 0;
665

    
666
    dest = 0; //init to 0
667

    
668
    while(db < num_of_bits)
669
    {
670
        if(b == 8)
671
        {
672
            b = 0;
673
            ++i;
674

    
675
            if(i == 8)
676
            {
677
                i = 0;
678
                ++qw;
679
            }
680
        }
681

    
682
        dest |= ((src[qw*8+7-i] >> b) & 1) << db;
683

    
684
        ++b;
685
        ++db;
686
    }
687
}
688

    
689
//========================================================================================================================
690
string PxCAPTANHardware::convertToString(UInt16 value)
691
{
692
    string string16 = "00";// TODO verify if this is effectively a string of 0
693
    for(UInt32 i=0; i<2; i++)
694
        string16[1-i] = (value >> i*8) & 0xff;
695

    
696
    return string16;  //We want to pass the reference, if we just declare string16 as a string (not *) we pass the reference to something produced in a function... doesn't look good...
697
}
698

    
699
//=======================================================================================================================
700
string PxCAPTANHardware::convertToString(UInt32 value)
701
{
702
    string string32 = "0000";
703
    for(UInt32 i=0; i<4; i++)
704
        string32[3-i] = (value >> i*8) & 0xff;
705

    
706
    return string32;
707
}
708

    
709
//========================================================================================================================
710
string PxCAPTANHardware::convertToString(UInt64 value)
711
{
712
    string string64 = "00000000";
713
    for(UInt32 i=0; i<8; i++)
714
        string64[7-i] = (value >> i*8) & 0xff;
715

    
716
    return string64;
717
}
718

    
719
//========================================================================================================================
720
void PxCAPTANHardware::appendToString(UInt32 value, std::string& buffer)
721
{
722
    buffer.resize(buffer.size()+4,'\0');
723
    for(UInt32 i=0; i<4; i++)
724
        buffer[buffer.size()-1-i] = (char)((value >> i*8) & 0xff);
725
}
726

    
727
//========================================================================================================================
728
void PxCAPTANHardware::appendToString(UInt64 value, std::string& buffer)
729
{
730
    buffer.resize(buffer.size()+8,'\0');
731
    for(UInt32 i=0; i<8; i++)
732
        buffer[buffer.size()-1-i] = (char)((value >> i*8) & 0xff);
733
}
734

    
735
//========================================================================================================================
736
void PxCAPTANHardware::uploadDACsToFEC(unsigned int channel, const PxROCStream& rocStream)
737
{
738
    string aknowledgment;
739
    cout << __COUT_HDR__ << "Making DAC Sequence!!!" << endl;
740
    /*RYAN'S
741
    PxFirmwareSequence<UInt64> sequence;
742
    theFirmware_->makeDACSequence(sequence, channel, rocStream);
743
    write(sequence);
744
    usleep(1000);
745
    sequence.clear();
746
    theFirmware_->makeMaskSequence(sequence, channel, rocStream);
747
    */
748

    
749
    //Matthew
750
    //PxFirmwareSequence<UInt32> sequence;
751
    //string buffer;
752
    //theFirmware_->makeDACBuffer(buffer, channel, rocStream);
753
    //write(buffer);
754
    vector<string> buffer;
755
    theFirmware_->makeDACBuffer(buffer, channel, rocStream);
756
    write(buffer);
757

    
758
    string maskBuffer;
759
    theFirmware_->makeMaskBuffer(maskBuffer, channel, rocStream);
760
    write(maskBuffer);
761
    //PxNetworkDevice::receive(captanWriteSocketDescriptor_,aknowledgment);
762
    usleep(100000); //Wait longer because the mask takes sometime to be downloaded
763
}
764

    
765
//========================================================================================================================
766
void PxCAPTANHardware::uploadDACsToDetector(unsigned int channel)
767
{}