00001 #include <iostream>
00002 #include <fstream>
00003 #include <sstream>
00004 #include "pit_comm.h"
00005 #include "pit_comm_exception.h"
00006
00007
00008 using namespace std;
00009
00010 #define MAX_PHYSMEM (1024*1024*8)
00011
00012 pit_comm::pit_comm(void)
00013 {
00014 logCom = &(log4cpp::Category::getInstance("pitLog.comm"));
00015 logCom->info("(pit_comm::pit_comm()): Starting communication layer");
00016
00017
00018 rorcStatus = R_NOT_OPEN_ERR;
00019 DDLLinkStatus = FAILURE;
00020 controlFPGAAnswers = FAILURE;
00021
00022
00023 const string serviceCommunicationStatusName("COMMUNICATION_STATUS");
00024 serviceCommunicationStatus = new DimService(serviceCommunicationStatusName.c_str(), controlFPGAAnswers);
00025
00026 rorc = get_channel(RORC_REVISION, RORC_SERIAL, RORC_CHANNEL);
00027
00028
00029 if(rorc->status == R_OPEN) {
00030 rorcStatus = R_OPEN;
00031 logCom->info("(pit_comm::PITCheckCommStatus()): RORC handler valid");
00032 PITCheckCommStatus();
00033 }
00034 else {
00035 rorcStatus = rorc->err;
00036 logCom->error("(pit_comm::pit_comm()): Could not open rorc device : minor=%i channel=%i err=%i", rorc->minor, rorc->channel, rorc->err);
00037 }
00038
00039 }
00040
00041
00042 pit_comm::~pit_comm(void)
00043 {
00044
00045 if(rorc->status == R_OPEN) {
00046 PITrorcReset();
00047 release_channel(rorc);
00048 }
00049 }
00050
00051
00052
00053
00054
00055 bool pit_comm::PITCheckCommStatus()
00056 {
00057
00058
00059 if (rorcStatus == R_OPEN) {
00060 PITrorcReset();
00061 logCom->info("(pit_comm::PITCheckCommStatus()): Checking communication to PIT hardware");
00062
00063 try{
00064 int output;
00065
00066 output = ddl_statusReadout(rorc, PIT_GET_VERSION);
00067
00068 if (output< 0) {
00069 PITDecodeRORCError(output, "(PITCheckCommStatus) : ");
00070 }
00071
00072 DDLLinkStatus = SUCCESS;
00073 controlFPGAAnswers = true;
00074 serviceCommunicationStatus->updateService();
00075
00076 logCom->info("(pit_comm::PITCheckCommStatus()): Communication OK, RORC-SIU link OK, control FPGA answered with version number: %i", output);
00077 return true;
00078 }
00079 catch(...){
00080 DDLLinkStatus = FAILURE;
00081 controlFPGAAnswers = FAILURE;
00082 serviceCommunicationStatus->updateService();
00083 logCom->error("(pit_comm::PITCheckCommStatus()): Communication to PIT failed. Check hardware and optical links" );
00084 return false;
00085 }
00086 }
00087 else {
00088 DDLLinkStatus = FAILURE;
00089 controlFPGAAnswers = FAILURE;
00090 serviceCommunicationStatus->updateService();
00091 logCom->error("(pit_comm::PITCheckCommStatus()): Can not access SIU and RORC, invalid RORC handler");
00092 return false;
00093 }
00094
00095
00096 }
00097
00098
00099
00100
00101 int pit_comm::PITrorcReset(){
00102
00103 if (rorcStatus == R_OPEN) {
00104 rorc_pci_dev_t * device = &rorc->rorc;
00105
00106
00107 rorcReset(device, RORC_RESET_SIU | RORC_RESET_RORC);
00108
00109 this->logCom->info("(pit_comm::PITrorcReset()): Resetting SIU and RORC");
00110 return 0;
00111 }
00112 else {
00113 this->logCom->error("(pit_comm::PITrorcReset()): Can not reset SIU and RORC, invalid RORC handler");
00114 return -1;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 }
00130
00131
00137 void pit_comm::PITDecodeRORCError(int rorcError, const char *extraMsg) {
00138
00139 string srcMsg(extraMsg);
00140 logCom->error(srcMsg);
00141
00142
00143 stringstream errorMsgStream;
00144 errorMsgStream << "(pit_comm::PITDecodeRORCError) Communication problem, RORC returned DDL error number "
00145 << rorcError << " : ";
00146
00147 string errorMsg(errorMsgStream.str());
00148
00149 switch ( rorcError ) {
00150
00151 case RORC_LINK_NOT_ON :
00152 DDLLinkStatus = FAILURE;
00153 controlFPGAAnswers = FAILURE;
00154 errorMsg+="RORC-SIU Link DOWN. Check pit hardware powering and clocking.";
00155 break;
00156
00157 case RORC_TIMEOUT :
00158 DDLLinkStatus = SUCCESS;
00159 controlFPGAAnswers = FAILURE;
00160 errorMsg+="RORC timeout error, could not TRANSMIT command before timeout.";
00161 break;
00162
00163 case RORC_TOO_MANY_REPLY :
00164 DDLLinkStatus = SUCCESS;
00165 controlFPGAAnswers = FAILURE;
00166 errorMsg+="too many replies error. Check control FPGA configuration.";
00167 break;
00168
00169 case RORC_NOT_ENOUGH_REPLY :
00170 DDLLinkStatus = SUCCESS;
00171 controlFPGAAnswers = FAILURE;
00172 errorMsg+="not enough replies in timeout interval. Check control FPGA configuration.";
00173 break;
00174
00175 default :
00176 DDLLinkStatus = FAILURE;
00177 controlFPGAAnswers = FAILURE;
00178 errorMsg+="unknown error code. Ask DDL-RORC-SIU maintainers.";
00179 }
00180
00181 logCom->error(errorMsg);
00182
00183
00184
00185 throw pit_comm_exception( errorMsg);
00186
00187 }
00188
00189
00190 void pit_comm::PITDecodeComError(int comError, const char * extraMsg){
00191 string errorMsg(extraMsg);
00192 switch(comError){
00193 case ERROR_PROTOCOL_DATALENGTH:
00194 errorMsg+="writeBlock: bad length comparison";
00195 break;
00196 case ERROR_BAD_REGISTER_WRITE:
00197 errorMsg+= "Bad register Write";
00198 break;
00199 case ERROR_PARITY_CHECK:
00200 errorMsg+= "parity error";
00201 break;
00202 case ERROR_GENERIC_BUS_STATUS:
00203 errorMsg+= "generic bus status error";
00204 break;
00205 default:
00206 stringstream str;
00207 str << "Unknow pit_comm error code: " << comError;
00208 errorMsg+=str.str();
00209 }
00210
00211 logCom->error(errorMsg);
00212 throw pit_comm_exception(errorMsg);
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 int pit_comm::PITGetRdA()
00226 {
00227 unsigned int rd_a_lsb;
00228 unsigned int rd_a_msb;
00229 int output;
00230
00231 output = ddl_statusReadout(rorc, PIT_GET_RDA_LSB);
00232 if (output < 0){
00233 PITDecodeRORCError(output, "(PITGetRdA) PIT_GET_RDA_LSB :" );
00234 return output;
00235 }
00236
00237 rd_a_lsb = output & 0x7ffff;
00238 output = ddl_statusReadout(rorc, PIT_GET_RDA_MSB);
00239
00240 if (output< 0){
00241 PITDecodeRORCError(output, "(PITGetRdA) PIT_GET_RDA_MSB :" );
00242 return output;
00243 }
00244
00245 rd_a_msb = output & 0x1ff;
00246 output = (rd_a_msb << 19) | rd_a_lsb;
00247
00248 return(output);
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 int pit_comm::PITGetWrA()
00262 {
00263 unsigned int wr_a_lsb;
00264 unsigned int wr_a_msb;
00265 int output;
00266
00267 output = ddl_statusReadout(rorc, PIT_GET_WRA_LSB);
00268 if (output< 0){
00269 PITDecodeRORCError(output, "(PITGetWrA) PIT_GET_WRA_LSB :");
00270 return output;
00271 }
00272
00273 wr_a_lsb = output & 0x7ffff;
00274 output = ddl_statusReadout(rorc, PIT_GET_WRA_MSB);
00275 if (output< 0){
00276 PITDecodeRORCError(output,"(PITGetWrA) PIT_GET_WRA_MSB :");
00277 return output;
00278 }
00279 wr_a_msb = output & 0x1ff;
00280 output = (wr_a_msb << 19) | wr_a_lsb;
00281
00282 return(output);
00283 }
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 int pit_comm::PITGetLen()
00296 {
00297 int output;
00298
00299 output = ddl_statusReadout(rorc, PIT_GET_LEN);
00300 if(output >= 0) output = output & 0x7ffff;
00301 else PITDecodeRORCError(output, "(PITGetLen):");
00302 return(output);
00303 }
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 int pit_comm::PITGetRdLen()
00316 {
00317 int output;
00318
00319 output = ddl_statusReadout(rorc, PIT_GET_RD_LEN);
00320 if(output >=0) output = output & 0x7ffff;
00321 else PITDecodeRORCError(output, "(PITGetRdLen)");
00322 return(output);
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 int pit_comm::PITGetWrLen()
00336 {
00337 int output;
00338
00339 output = ddl_statusReadout(rorc, PIT_GET_WR_LEN);
00340 if(output >= 0) output = output & 0x7ffff;
00341 else PITDecodeRORCError(output, "(PITGetWrLen) : ");
00342 return(output);
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 int pit_comm::PITGetControl0()
00356 {
00357 int output;
00358
00359 output = ddl_statusReadout(rorc, PIT_GET_CONTROL0);
00360 if(output >= 0) output = output & 0xff;
00361 else PITDecodeRORCError(output, "(PITGetControl0) : ");
00362 return(output);
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 int pit_comm::PITGetControl1()
00376 {
00377 int output;
00378
00379 output = ddl_statusReadout(rorc, PIT_GET_CONTROL1);
00380 if(output >=0) output = output & 0xff;
00381 else PITDecodeRORCError(output, "(PITGetControl1) : ");
00382 return(output);
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 int pit_comm::PITGetVersion()
00396 {
00397 int output;
00398
00399 output = ddl_statusReadout(rorc, PIT_GET_VERSION);
00400 if(output >= 0) output = output & 0x3ffff;
00401 else PITDecodeRORCError(output, "(in pit_comm::PITGetVersion): ");
00402 return(output);
00403 }
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 int pit_comm::PITGetBusStatus()
00416 {
00417 int output;
00418
00419 output = ddl_statusReadout(rorc, PIT_GET_BUS_STATUS);
00420 if(output >=0) output = output & 0xfff;
00421 else PITDecodeRORCError(output, "(PITGetBusStatus) : ");
00422 return(output);
00423 }
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438 int pit_comm::PITSetRdA(UInt28 address)
00439 {
00440 unsigned int argument;
00441 unsigned int addr_extract;
00442 int output;
00443 unsigned int rd_a_msb;
00444
00445
00446 addr_extract = (address >> 19) & 0x1ff;
00447 argument = addr_extract | (0x3ff << 9);
00448 output = ddl_sendCommand(rorc, argument);
00449
00450 if (output){
00451 PITDecodeRORCError(output, "(PITSetRdA) write rd [27:19] : ");
00452 return output;
00453 }
00454
00455
00456 output = ddl_statusReadout(rorc, PIT_GET_RDA_MSB);
00457
00458 if (output<0){
00459 PITDecodeRORCError(output, "(PITSetRdA) read rd [27:19] : ");
00460 return output;
00461 }
00462
00463 rd_a_msb = (output ) & 0x1ff;
00464 if(rd_a_msb == !addr_extract){
00465 output = ERROR_BAD_REGISTER_WRITE;
00466 this->PITDecodeComError(output, "(PITSetRdA) read write comparison :");
00467 return output;
00468 }
00469
00470
00471
00472 return(0);
00473 }
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488 int pit_comm::PITSetWrA(UInt28 address)
00489 {
00490 unsigned int argument;
00491 unsigned int addr_extract;
00492 int output;
00493 unsigned int wr_a_msb;
00494
00495
00496 addr_extract = (address >> 19) & 0x1ff;
00497 argument = addr_extract | (0x3fe << 9);
00498 output = ddl_sendCommand(rorc, argument);
00499 if (output< 0){
00500 PITDecodeRORCError(output, "(PITSetWrA) setting the wr a[27:19] : ");
00501 return output;
00502 }
00503
00504
00505
00506 output = ddl_statusReadout(rorc, PIT_GET_WRA_MSB);
00507 if (output< 0){
00508 PITDecodeRORCError(output, "(PITSetWrA) reading the wr a[27:19] : ");
00509 return output;
00510 }
00511
00512
00513
00514 wr_a_msb = (output ) & 0x1ff;
00515 if(wr_a_msb != addr_extract){
00516 output = ERROR_BAD_REGISTER_WRITE;
00517 PITDecodeComError(output, "(PITSetWrA)read write comparison : ");
00518 return output;
00519 }
00520
00521 return(0);
00522 }
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536 int pit_comm::PITSetLen(UInt19 length)
00537 {
00538 unsigned int argument;
00539 int output;
00540 unsigned int len;
00541
00542
00543 argument = ((length & 0x7fe00) >> 9) | (0x1fc << 10);
00544 output = ddl_sendCommand(rorc, argument);
00545 if (output < 0){
00546 PITDecodeRORCError(output,"(PITSetLen) setting len [18:9] : ");
00547 return output;
00548 }
00549
00550 argument = (length & 0x1ff) | (0x3fb << 9);
00551 output = ddl_sendCommand(rorc, argument);
00552 if (output<0){
00553 PITDecodeRORCError(output, "(PITSetLen) setting len [8:0] : ");
00554 return output;
00555 }
00556
00557
00558 output = ddl_statusReadout(rorc, PIT_GET_LEN);
00559 if (output<0){
00560 PITDecodeRORCError(output, "(PITSetLen) reading len[18:0] : ");
00561 return output;
00562 }
00563
00564 len = (output) & 0x7ffff;
00565 if(len != length){
00566 output = ERROR_BAD_REGISTER_WRITE;
00567 PITDecodeComError(output, "(PITSetLen) read write comparison : ");
00568 return output;
00569 }
00570
00571 return(0);
00572 }
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586 int pit_comm::PITSetControl0(UInt8 value)
00587 {
00588 unsigned int argument;
00589 unsigned int control0;
00590 int output;
00591
00592
00593 argument = value & 0xff;
00594 output = ddl_sendCommand(rorc, argument);
00595 if(output){
00596 PITDecodeRORCError(output, "(PITSetControl0) setting register : ");
00597 return output;
00598 }
00599
00600
00601 output = ddl_statusReadout(rorc, PIT_GET_CONTROL0);
00602
00603 if(output < 0) {
00604 PITDecodeRORCError(output, "(PITSetControl0) reading register : ");
00605 return output;
00606 }
00607 control0 = (output ) & 0xff;
00608 if(control0 != value) {
00609 output = ERROR_BAD_REGISTER_WRITE;
00610 PITDecodeComError(output, "(PITSetControl0) read write comparison : ");
00611 return output;
00612 }
00613
00614
00615 return(0);
00616 }
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630 int pit_comm::PITSetControl1(UInt8 value)
00631 {
00632 unsigned int argument;
00633 unsigned int control1;
00634 int output;
00635
00636
00637 argument = (value & 0xff) | 0x100;
00638 output = ddl_sendCommand(rorc, argument);
00639 if (output){
00640 PITDecodeRORCError(output, "(PITSetControl1) writting register : ");
00641 return output;
00642 }
00643
00644
00645 output = ddl_statusReadout(rorc, PIT_GET_CONTROL1);
00646 if(output < 0){
00647 PITDecodeRORCError(output, "(PITSetControl1) reading register : ");
00648 return output;
00649 }
00650
00651
00652
00653 control1 = (output ) & 0xff;
00654 if(control1 != value) {
00655 output = ERROR_BAD_REGISTER_WRITE;
00656 PITDecodeComError(output, "(PITSetControl1) read write comparison : ");
00657 return output;
00658 }
00659
00660 return(0);
00661 }
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682 int pit_comm::PITReadBlock(UInt28 address, UInt19 datalength, UInt32* data)
00683 {
00684 int rd_len;
00685 int output;
00686
00687
00688 output = PITSetLen(datalength);
00689
00690 if(output){
00691
00692 PITDecodeRORCError(output, "(pit_comm::PITReadBlock): Problem encountered while setting length register");
00693 return output;
00694 }
00695
00696 output = PITSetRdA(address);
00697 if(output){
00698 PITDecodeRORCError(output, "(pit_comm::PITReadBlock): Problem encountered while setting the base address register");
00699 return output;
00700 }
00701
00702 output = ddl_readBlock(rorc,0x7ffff & address, (u_long*)data, datalength);
00703 if(output<0){
00704 PITDecodeRORCError(output, "(pit_comm::PITReadBlock) Problem encountered while reading data");
00705 return output;
00706 }
00707
00708 rd_len = PITGetRdLen();
00709 if( rd_len != (int)datalength){
00710 output = ERROR_BAD_LENGTH_COMPARISON;
00711 PITDecodeComError(output, "(pit_comm::PITReadBlock) Actual block read length mismatches requested block length");
00712 return output;
00713 }
00714
00715 int statusReg = PITGetBusStatus();
00716
00717 if (extractFromUInt32(0,1,statusReg) ){
00718 output= ERROR_PARITY_CHECK;
00719 PITDecodeComError(output, "(pit_comm::PITReadBlock)");
00720 return output;
00721
00722 }
00723 if (statusReg){
00724 output= ERROR_GENERIC_BUS_STATUS;
00725 PITDecodeComError(output, "(pit_comm::PITReadBlock) ");
00726 return output;
00727 }
00728
00729
00730 return(0);
00731 }
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749 int pit_comm::PITWriteBlock(UInt28 address, UInt19 datalength, UInt32* data)
00750 {
00751 unsigned int wr_len;
00752 int output;
00753
00754
00755 output = PITSetWrA(address);
00756
00757
00758 if(output< 0) {
00759 PITDecodeRORCError(output, "(PITWriteBlock) setting address :");
00760 return output;
00761 }
00762
00763 output = ddl_writeBlock(rorc, (u_long*)data, datalength, address & 0x7ffff);
00764 if(output<0) {
00765 PITDecodeRORCError(output, "(PITWriteBlock) sending data :");
00766 return output;
00767 }
00768
00769 wr_len = PITGetWrLen();
00770 if(wr_len != datalength){
00771 output = ERROR_BAD_LENGTH_COMPARISON;
00772 PITDecodeComError(output, "(PITWriteBlock) length check : ");
00773 return output;
00774 }
00775
00776 return(0);
00777 }
00778
00779
00780
00781 int pit_comm::PITWriteWord(UInt28 address, const UInt32 & data){
00782 UInt32 dataOut = data;
00783
00784 logCom->log(log4cpp::Priority::DEBUG, "(pit_comm::PITWriteWord): writing to address 0x%x value 0x%x", address, data);
00785 return PITWriteBlock(address,1,&dataOut);
00786 }
00787
00788
00789 int pit_comm::PITReadWord(UInt28 address, UInt32 *data){
00790 int return_value = PITReadBlock(address, 1 ,data);
00791
00792 logCom->log(log4cpp::Priority::DEBUG, "(pit_comm::PITReadWord): read word from address 0x%x, value 0x%x", address, *data);
00793
00794 return return_value;
00795 }