// // test accessing local bus on HTR // #define HTR_SLOT 13 // default HTR VME slot #define SLB_SITE 3 // default HTR SLB site #define BUS_NAME "caen:0" #include "VMEDevice.hh" #include "VMEAddressTable.hh" #include "hcal/BusAdapterManager.hh" #include "hcalHTR.hh" #include "log4cplus/logger.h" #include "log4cplus/loglevel.h" #include "log4cplus/configurator.h" #include "unpack_print.hh" #include #include #include using namespace log4cplus; int main(int argc, char** argv) { char buff[80]; int htr_slot = HTR_SLOT; int slb_site = SLB_SITE; char *bname = BUS_NAME; HAL::VMEDevice* vme_dev; char slb_base[12]; static char *usage = "usage: cfgFPGA file_name"; if( argc != 2) { puts( usage); abort(); } // setup a logger log4cplus::BasicConfigurator config; config.configure(); Logger mylog = log4cplus::Logger::getInstance("mylog"); // log everything mylog.setLogLevel( log4cplus::TRACE_LOG_LEVEL); printf("HTR VME Slot (1-21) [%d] ?", htr_slot); fgets( buff, 79, stdin); if( isdigit( *buff)) htr_slot = strtol( buff, NULL, 0); // instantiate HTR with slot# and logger hcalHTR* htr = new hcalHTR::hcalHTR( htr_slot, mylog); try { // load address table cout << "Setup HTR VME" << endl; htr->setupVME( bname, "HTR_r4_series1.dat"); cout << "Get VMEDevice for Altera chip on HTR" << endl; vme_dev = htr->getAltera(); } catch(std::exception& e){ printf("HTR initialization %s\n", e.what()); abort(); } cout << "HTR initialization successful" << endl; try { // check ID unsigned long work = 0; vme_dev->read( "IDRegister", &work); if( work == 0xedcafe) cout << "Read HTR ID= 0x" << hex << work << " (OK)" << endl; else { cout << "Read HTR ID= 0x" << hex << work << " (WRONG)" << endl; abort(); } sprintf( slb_base, "SLB%d_BASE", slb_site); FILE *f; int ln = 0; char line[120]; unsigned buf,crc[24] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},tmp[24]; unsigned long data,wd_cnt = 0,chksum; // open input file with Altera code f = fopen(argv[1],"r"); if (!f) { printf("cfgFPGA: Cannot open file %s\n",argv[1]); abort(); } //reprogram FPGA vme_dev->write( slb_base, 3, HAL::HAL_NO_VERIFY, 0x10c); vme_dev->write( slb_base, 1, HAL::HAL_NO_VERIFY, 0x10c); //wait until init_b high do{ vme_dev->read( slb_base, &data, 0x10c); }while(!(data & 0x4)); // loop over record in the file and read them in the array "line" while ( fgets(line, 120, f) != NULL) { char c1[3],c2[5]; unsigned long data[200]; int i,j,byte_count,address,address_bias,rec_type,total; ln++; if(ln%0x1000 == 0)printf("current line: 0x%x\n",ln); // check that the record has more than 9 characters if ( strlen(line) < 9 ) { printf("programChip: Error while reading " "line %d: it has less than 9 characters\n",ln); abort(); } // number of bytes in the record (first two hex numbers) strncpy(c1,line+1,2); c1[2] = NULL; sscanf(c1,"%x",&byte_count); // address (hex numbers 3-6) strncpy(c2,line+3,4); c2[4] = NULL; sscanf(c2,"%x",&address); // record type (hex numbers 7-8) strncpy(c1,line+7,2); c1[2] = NULL; sscanf(c1,"%x",&rec_type); // calculate checksum total = byte_count+address/256+address%256+rec_type; // loop over the rest of the characters in the record and // convert them to int for (i=0; i<(byte_count+1);i++) { strncpy(c1,line+2*i+9,2); c1[2] = NULL; data[i] = strtol(c1,NULL,16); total = total + data[i]; } // check the checksum for the record if (total%256 != 0) { printf("programChip: Error while reading line " "%d - a checksum error is found\n",ln); abort(); } //byte count must be even if(byte_count%2 == 1)byte_count++; // only data records are written if (rec_type == 0) { for (i=0; iwrite( slb_base, data[i], HAL::HAL_NO_VERIFY, 0); //calculate CRC tmp[0] = data[i]%2+crc[21]+crc[20]+crc[19]+crc[18]+crc[17]+crc[16]; tmp[1] = (data[i]>>1)%2+crc[22]+crc[16]; tmp[2] = (data[i]>>2)%2+crc[23]+crc[17]; tmp[3] = (data[i]>>3)%2+crc[21]+crc[20]+crc[19]+crc[17]+crc[16]; tmp[4] = (data[i]>>4)%2+crc[22]+crc[19]+crc[16]; tmp[5] = (data[i]>>5)%2+crc[23]+crc[21]+crc[19]+crc[18]+crc[16]; tmp[6] = (data[i]>>6)%2+crc[22]+crc[21]+crc[18]+crc[16]; tmp[7] = (data[i]>>7)%2+crc[23]+crc[22]+crc[21]+crc[20]+crc[18]+crc[16]; tmp[8] = crc[23]+crc[22]+crc[21]+crc[19]+crc[17]+crc[0]; tmp[9] = crc[23]+crc[22]+crc[20]+crc[18]+crc[1]; tmp[10] = crc[23]+crc[20]+crc[18]+crc[17]+crc[16]+crc[2]; tmp[11] = crc[20]+crc[16]+crc[3]; tmp[12] = crc[21]+crc[17]+crc[4]; tmp[13] = crc[22]+crc[18]+crc[5]; tmp[14] = crc[23]+crc[21]+crc[20]+crc[18]+crc[17]+crc[16]+crc[6]; tmp[15] = crc[22]+crc[21]+crc[19]+crc[18]+crc[17]+crc[7]; tmp[16] = crc[23]+crc[22]+crc[20]+crc[19]+crc[18]+crc[8]; tmp[17] = crc[23]+crc[18]+crc[17]+crc[16]+crc[9]; tmp[18] = crc[21]+crc[20]+crc[16]+crc[10]; tmp[19] = crc[22]+crc[21]+crc[17]+crc[11]; tmp[20] = crc[23]+crc[22]+crc[18]+crc[12]; tmp[21] = crc[23]+crc[19]+crc[13]; tmp[22] = crc[20]+crc[14]; tmp[23] = crc[20]+crc[19]+crc[18]+crc[17]+crc[16]+crc[15]; for(j=0;j<24;j++) crc[j] = tmp[j]%2; wd_cnt++; if(wd_cnt > 0x33da0){ chksum = 0; for(j=0;j<24;j++) chksum = (chksum << 1) + crc[23-j]; vme_dev->read( slb_base, &data[i], 0x108); total = data[i] & 0xff; vme_dev->read( slb_base, &data[i], 0x104); total = (total << 8) + (data[i] & 0xff); vme_dev->read( slb_base, &data[i], 0x100); total = (total << 8) + (data[i] & 0xff); printf("wd_cnt = 0x%05x rd_back = 0x%06x chksum = 0x%06x\n",wd_cnt,total,chksum); } } // loop over bytes in the line } // rec_type equal to 0 } // loop over lines // close mcs file fclose(f); // check done signal vme_dev->read( slb_base, &data, 0x10c); if(data & 0x8) printf("Done is high\n"); // reset local mode bit vme_dev->write( slb_base, 0, HAL::HAL_NO_VERIFY, 0x10c); } catch(std::exception& e){ printf("Read from HTR %s\n", e.what()); abort(); } }