diff --git a/ClassDigitizer.cpp b/ClassDigitizer.cpp index 6f9958e..4ad4b32 100644 --- a/ClassDigitizer.cpp +++ b/ClassDigitizer.cpp @@ -11,6 +11,7 @@ Digitizer::Digitizer(int boardID, int portID, bool program, bool verbose){ Digitizer::~Digitizer(){ delete data; + delete settingFile; CloseDigitizer(); } @@ -35,6 +36,8 @@ void Digitizer::Initalization(){ channelMask = 0xFFFF; data = new Data(); + + isSettingFilledinMemeory = false; settingFileName = ""; settingFileExist = false; @@ -60,7 +63,8 @@ void Digitizer::PrintBoard(){ printf("Sampling rate : %.0f MHz = %.1f ns \n", 1000/ch2ns, ch2ns); printf("Number of Channels : %d = 0x%X\n", NChannel, channelMask); printf("SerialNumber :\e[1m\e[33m %d\e[0m\n", BoardInfo.SerialNumber); - printf("ADC bit is \e[33m%d\e[0m, %d = 0x%x\n", ADCbits, ADCFullSize, ADCFullSize); + printf("DPPType : %d (%s)\n", DPPType, DPPTypeStr.c_str()); + printf("ADC bit is \e[33m%d\e[0m, %d = 0x%X\n", ADCbits, ADCFullSize, ADCFullSize); printf("ROC FPGA Release is %s\n", BoardInfo.ROC_FirmwareRel); printf("AMC FPGA Release is %s\n", BoardInfo.AMC_FirmwareRel); } @@ -105,16 +109,13 @@ int Digitizer::OpenDigitizer(int boardID, int portID, bool program, bool verbose ADCbits = BoardInfo.ADC_NBits; ADCFullSize = (unsigned int)( pow(2, ADCbits) -1 ); - if( verbose) PrintBoard(); - sscanf(BoardInfo.AMC_FirmwareRel, "%d", &DPPType); - if (DPPType < 128 && verbose) printf("This digitizer does not have DPP-PHA firmware\n"); - //SetBoardID(BoardInfo.SerialNumber); } } ///====================== Check DPP firmware revision + sscanf(BoardInfo.AMC_FirmwareRel, "%d", &DPPType); data->DPPType = DPPType; switch (DPPType){ @@ -130,9 +131,14 @@ int Digitizer::OpenDigitizer(int boardID, int portID, bool program, bool verbose case V1730_DPP_ZLE_CODE: DPPTypeStr = "DPP-ZLE x730"; break; /// 0x8C case V1730_DPP_DAW_CODE: DPPTypeStr = "DPP-DAW x730"; break; /// 0x8D } - if (DPPType >= 0x80 && verbose) { - printf("\t==== This digitizer has a DPP firmware!\n"); - printf("\e[32m\t %s \e[0m", DPPTypeStr.c_str()); + if ( verbose ){ + PrintBoard(); + if (DPPType < 0x80 ) { + printf("This digitizer does not have DPP-PHA firmware\n"); + }else { + printf("\t==== This digitizer has a DPP firmware!\n"); + printf("\e[32m\t %s \e[0m", DPPTypeStr.c_str()); + } } ///======================= Check virtual probe @@ -347,55 +353,69 @@ void Digitizer::ReadData(){ } //=========================================================== -void Digitizer::WriteRegister(uint32_t address, uint32_t value, int ch ){ +void Digitizer::WriteRegister(uint32_t registerAddress, uint32_t value, int ch ){ if( !isConnected ) return; - if( address < 0x8000){ - ///printf("0x%x, 0x%x=%u, ch:%d\n", address, value, value, ch); - if( ch < 0 ) { - ret = CAEN_DGTZ_WriteRegister(handle, address + 0x7000, value); - for( int i = 0; i < NChannel; i++) SaveSettingEditByte(value, address + (i << 8)); - }else{ - ret = CAEN_DGTZ_WriteRegister(handle, address + (ch<<8), value); - SaveSettingEditByte( value, address + (ch<<8) ); - } - }else{ - ret = CAEN_DGTZ_WriteRegister(handle, address, value); - } - ErrorMsg("WriteRegister:" + std::to_string(address)); -} - -uint32_t Digitizer::ReadRegister(uint32_t address, int ch, string str ){ - if( !isConnected ) { - //TODO read from Setting file; - return 0; - }else{ - uint32_t * data = new uint32_t[NChannel]; - if( address < 0x8000) { - if( ch < 0 ) { - ret = CAEN_DGTZ_ReadRegister(handle, address + 0x7000, data); - }else{ - ret = CAEN_DGTZ_ReadRegister(handle, address + (ch << 8), data); + ///printf("0x%X, 0x%X=%u, ch:%d\n", registerAddress, value, value, ch); + + if( registerAddress == 0x8180){ + if( ch < 0 ){ + for( int i = 0; i < NChannel/2; i++){ + ret = CAEN_DGTZ_WriteRegister(handle, registerAddress + 4*i, value); + SaveSettingToFile(registerAddress, value, i); } }else{ - ret = CAEN_DGTZ_ReadRegister(handle, address, data); + ret = CAEN_DGTZ_WriteRegister(handle, registerAddress + 4*(ch/2), value); + SaveSettingToFile(registerAddress, value, ch); } - ErrorMsg("ReadRegister:" + std::to_string(address)); - if( str != "" ) printf("%s : 0x%08x \n", str.c_str(), data[0]); - return data[0]; + }else if( registerAddress < 0x8000){ + if( ch < 0 ) { + ret = CAEN_DGTZ_WriteRegister(handle, registerAddress + 0x7000, value); + for( int i = 0; i < NChannel; i++) SaveSettingToFile(value, registerAddress, i); + }else{ + ret = CAEN_DGTZ_WriteRegister(handle, registerAddress + (ch<<8), value); + SaveSettingToFile(registerAddress, value, ch); + } + }else{ + ret = CAEN_DGTZ_WriteRegister(handle, registerAddress, value); + SaveSettingToFile(registerAddress, value); } + ErrorMsg("WriteRegister:" + std::to_string(registerAddress)); +} + +uint32_t Digitizer::ReadRegister(uint32_t registerAddress, int ch, string str ){ + if( !isConnected ) return 0; + + uint32_t actualAddress = registerAddress ; + if( registerAddress == 0x8180 ){ + if( ch < 0 ) ch = 0; + actualAddress = registerAddress + 4*(ch/2); + } + + if( registerAddress < 0x8000) { + if( ch >= 0 ) actualAddress = registerAddress + (ch << 8); + } + + uint32_t data[1]; + ret = CAEN_DGTZ_ReadRegister(handle, actualAddress, data); + + SaveSettingToFile(registerAddress, data[0], ch); + ErrorMsg("ReadRegister:" + std::to_string(registerAddress)); + if( str != "" ) printf("%s : 0x%04X(0x%04X) is 0x%08X \n", str.c_str(), actualAddress, registerAddress, data[0]); + return data[0]; + } void Digitizer::SetChannelMask(uint32_t mask){ if( !isConnected ) return; channelMask = mask; ret |= CAEN_DGTZ_SetChannelEnableMask(handle, channelMask); - SaveSettingEditByteByRegister(Register::DPP::ChannelEnableMask); + SaveSettingToFile(Register::DPP::ChannelEnableMask, mask); ErrorMsg(__func__); } void Digitizer::SetRecordLength(unsigned int ns, int ch){ - WriteRegister( Register::DPP::RecordLength_G, ns / ch2ns / 8 , ch); - if( ch >= 0 ) WriteRegister( Register::DPP::RecordLength_G, ns / ch2ns / 8 , ch + int(pow(-1, ch))); + WriteRegister( Register::DPP::RecordLength_G, ns / ch2ns / 8 , ch); + if( ch >= 0 ) WriteRegister( Register::DPP::RecordLength_G, ns / ch2ns / 8 , ch + int(pow(-1, ch))); ErrorMsg(__func__); } @@ -516,15 +536,15 @@ unsigned int Digitizer::ReadBits(uint32_t address, unsigned int bitLength, unsig } void Digitizer::SetBits(uint32_t address, unsigned int bitValue, unsigned int bitLength, unsigned int bitSmallestPos, int ch){ - ///printf("address : 0x%x, value : 0x%x, len : %d, pos : %d, ch : %d \n", address, bitValue, bitLength, bitSmallestPos, ch); + ///printf("address : 0x%X, value : 0x%X, len : %d, pos : %d, ch : %d \n", address, bitValue, bitLength, bitSmallestPos, ch); uint32_t bit ; uint32_t bitmask = (uint(pow(2, bitLength)-1) << bitSmallestPos); int tempCh = ch; if (ch < 0 && address < 0x8000 ) tempCh = 0; /// take ch-0 bit = ReadRegister(address, tempCh); - ///printf("bit : 0x%x, bitmask : 0x%x \n", bit, bitmask); + ///printf("bit : 0x%X, bitmask : 0x%X \n", bit, bitmask); bit = (bit & ~bitmask) | (bitValue << bitSmallestPos); - ///printf("bit : 0x%x, ch : %d \n", bit, ch); + ///printf("bit : 0x%X, ch : %d \n", bit, ch); WriteRegister(address, bit, ch); if( ret != 0 ) ErrorMsg(__func__); } @@ -544,7 +564,7 @@ int Digitizer::GetChTemperature(int ch){ uint32_t Digitizer::PrintRegister(uint32_t address, std::string msg){ if( !isConnected ) return 0 ; printf("\e[33m----------------------------------------------------\n"); - printf("------------ %s = 0x%x \n", msg.c_str(), address); + printf("------------ %s = 0x%X \n", msg.c_str(), address); printf("----------------------------------------------------\e[0m\n"); uint32_t * value = new uint32_t[1]; @@ -553,7 +573,7 @@ uint32_t Digitizer::PrintRegister(uint32_t address, std::string msg){ printf(" %*s | | | | | | | | |\n", (int) msg.length(), ""); printf(" %*s", (int) msg.length(), ""); cout << " : 0b" << bitset<32>(value[0]) << endl; - printf(" %*s : 0x%x\n", (int) msg.length(), msg.c_str(), value[0]); + printf(" %*s : 0x%X\n", (int) msg.length(), msg.c_str(), value[0]); return value[0]; @@ -567,14 +587,14 @@ void Digitizer::PrintACQStatue(){ printf(" 32 28 24 20 16 12 8 4 0\n"); printf(" | | | | | | | | |\n"); cout <<" 0b" << bitset<32>(status) << endl; - printf(" Acq state (0x%1x): %s \n", (status >> 2) & 0x1, ((status >> 2) & 0x1) == 0? "stopped" : "running"); - printf(" Event Ready (0x%1x): %s \n", (status >> 3) & 0x1, ((status >> 3) & 0x1) == 0? "no event in buffer" : "event in buffer"); - printf(" Event Full (0x%1x): %s \n", (status >> 4) & 0x1, ((status >> 4) & 0x1) == 0? "not full" : "full"); - printf(" Clock source (0x%1x): %s \n", (status >> 5) & 0x1, ((status >> 5) & 0x1) == 0? "internal" : "external"); - printf(" Board ready (0x%1x): %s \n", (status >> 8) & 0x1, ((status >> 8) & 0x1) == 0? "not ready" : "ready"); - printf(" Ch shutDown (0x%1x): %s \n", (status >> 19) & 0x1, ((status >> 19) & 0x1) == 0? "Channels are on" : "channels are shutdown"); - printf(" TRG-IN 0x%1x \n", (status >> 16) & 0x1); - printf(" Ch temp state 0x%04x \n", (status >> 20) & 0xF); + printf(" Acq state (0x%1X): %s \n", (status >> 2) & 0x1, ((status >> 2) & 0x1) == 0? "stopped" : "running"); + printf(" Event Ready (0x%1X): %s \n", (status >> 3) & 0x1, ((status >> 3) & 0x1) == 0? "no event in buffer" : "event in buffer"); + printf(" Event Full (0x%1X): %s \n", (status >> 4) & 0x1, ((status >> 4) & 0x1) == 0? "not full" : "full"); + printf(" Clock source (0x%1X): %s \n", (status >> 5) & 0x1, ((status >> 5) & 0x1) == 0? "internal" : "external"); + printf(" Board ready (0x%1X): %s \n", (status >> 8) & 0x1, ((status >> 8) & 0x1) == 0? "not ready" : "ready"); + printf(" Ch shutDown (0x%1X): %s \n", (status >> 19) & 0x1, ((status >> 19) & 0x1) == 0? "Channels are on" : "channels are shutdown"); + printf(" TRG-IN 0x%1X \n", (status >> 16) & 0x1); + printf(" Ch temp state 0x%04X \n", (status >> 20) & 0xF); } @@ -631,20 +651,49 @@ int Digitizer::ProgramPHABoard(){ } //========================================== setting file IO -void Digitizer::OpenSettingBinary(string fileName){ - settingFile = NULL; - this->settingFileName = fileName; - settingFile = fopen(fileName.c_str(), "r+"); - if( settingFile == NULL ){ - printf("cannot open file %s. \n", fileName.c_str()); - CreateAndSaveSettingBinary(fileName); +unsigned short Digitizer::CalSettingIndex(uint32_t registerAddress, int ch){ + + unsigned short index; + uint32_t newAddress; + + if( registerAddress == 0x8180){ + ///if( ch%2 ==1 ) return 0xFFFF; + newAddress = registerAddress + 4*(ch/2); + index = (newAddress & 0x0FFF) / 4; + }else if( registerAddress < 0x8000){ + if( ch < 0 ) ch = 0; + newAddress = registerAddress + (ch << 8); + index = newAddress / 4; }else{ - settingFileExist = true; - printf("setting file already exist.\n"); + newAddress = registerAddress; + index = (newAddress & 0x0FFF) / 4; } + //printf("---------address : 0x%04X = %5d = index : %6.1f (%d)\n", newAddress, newAddress, (newAddress & 0x0FFF) / 4., index); + return index; } - -void Digitizer::CreateAndSaveSettingBinary(string fileName){ + +void Digitizer::FillSetting(uint32_t registerAddress, int ch){ + + unsigned short index = CalSettingIndex(registerAddress, ch); + if( index > SETTINGSIZE ) return; + //if( setting[index] != 0 ) printf("##############################\n"); + setting[index] = ReadRegister(registerAddress, ch); +} + +void Digitizer::SetSettingToMemory(uint32_t registerAddress, unsigned int value, int ch ){ + unsigned short index = CalSettingIndex(registerAddress, ch); + if( index > SETTINGSIZE ) return; + setting[index] = value; +} + +unsigned int Digitizer::GetSettingFromMemory(uint32_t registerAddress, int ch ){ + unsigned short index = CalSettingIndex(registerAddress, ch); + if( index > SETTINGSIZE ) return 0xFFFF; + return setting[index] ; +} + +void Digitizer::FillAllSettings(){ + /// for 1 digitizer, 16 channels, needs 0x10XX to 0x1FXX /// Board setting is 0x8000 --> 0x0000 /// Saved date YYYYMMDDHH --> 0x0004 @@ -661,13 +710,121 @@ void Digitizer::CreateAndSaveSettingBinary(string fileName){ /// ------------------------------------- /// for 1 channel is 0x2000 = 8192 byte should be enough for all setting for 1 board + + if( isSettingFilledinMemeory) return; + + for( int ch = 0; ch < NChannel; ch ++){ + + FillSetting(Register::DPP::RecordLength_G , ch); /// 0x1020; /// R/W + FillSetting(Register::DPP::InputDynamicRange , ch); /// 0x1028; /// R/W + FillSetting(Register::DPP::NumberEventsPerAggregate_G , ch); /// 0x1034; /// R/W + FillSetting(Register::DPP::PreTrigger , ch); /// 0x1038; /// R/W + FillSetting(Register::DPP::DPPAlgorithmControl , ch); /// 0x1080; /// R/W + FillSetting(Register::DPP::ChannelStatus_R , ch); /// 0x1088; /// R + FillSetting(Register::DPP::AMCFirmwareRevision_R , ch); /// 0x108C; /// R + FillSetting(Register::DPP::ChannelDCOffset , ch); /// 0x1098; /// R/W + FillSetting(Register::DPP::ChannelADCTemperature_R , ch); /// 0x10A8; /// R + FillSetting(Register::DPP::VetoWidth , ch); /// 0x10D4; /// R/W + + if( DPPType == V1730_DPP_PHA_CODE ){ + FillSetting(Register::DPP::PHA::ChannelStopAcquisition , ch); /// 0x1040; /// R/W not sure + FillSetting(Register::DPP::PHA::RCCR2SmoothingFactor , ch); /// 0x1054; /// R/W Trigger Filter smoothing, triggerSmoothingFactor + FillSetting(Register::DPP::PHA::InputRiseTime , ch); /// 0x1058; /// R/W OK + FillSetting(Register::DPP::PHA::TrapezoidRiseTime , ch); /// 0x105C; /// R/W OK + FillSetting(Register::DPP::PHA::TrapezoidFlatTop , ch); /// 0x1060; /// R/W OK + FillSetting(Register::DPP::PHA::PeakingTime , ch); /// 0x1064; /// R/W OK + FillSetting(Register::DPP::PHA::DecayTime , ch); /// 0x1068; /// R/W OK + FillSetting(Register::DPP::PHA::TriggerThreshold , ch); /// 0x106C; /// R/W OK + FillSetting(Register::DPP::PHA::RiseTimeValidationWindow, ch); /// 0x1070; /// R/W OK + FillSetting(Register::DPP::PHA::TriggerHoldOffWidth , ch); /// 0x1074; /// R/W OK + FillSetting(Register::DPP::PHA::PeakHoldOff , ch); /// 0x1078; /// R/W OK + FillSetting(Register::DPP::PHA::ShapedTriggerWidth , ch); /// 0x1084; /// R/W not sure + FillSetting(Register::DPP::PHA::DPPAlgorithmControl2_G , ch); /// 0x10A0; /// R/W OK + FillSetting(Register::DPP::PHA::FineGain , ch); /// 0x10C4; /// R/W OK + } + + if( DPPType == V1730_DPP_PSD_CODE ){ + FillSetting(Register::DPP::PSD::CFDSetting , ch); /// 0x103C; /// R/W + FillSetting(Register::DPP::PSD::ChargeZeroSuppressionThreshold, ch); /// 0x1044; /// R/W + FillSetting(Register::DPP::PSD::ShortGateWidth , ch); /// 0x1054; /// R/W + FillSetting(Register::DPP::PSD::LongGateWidth , ch); /// 0x1058; /// R/W + FillSetting(Register::DPP::PSD::GateOffset , ch); /// 0x105C; /// R/W + FillSetting(Register::DPP::PSD::TriggerThreshold , ch); /// 0x1060; /// R/W + FillSetting(Register::DPP::PSD::FixedBaseline , ch); /// 0x1064; /// R/W + FillSetting(Register::DPP::PSD::TriggerLatency , ch); /// 0x106C; /// R/W + FillSetting(Register::DPP::PSD::ShapedTriggerWidth , ch); /// 0x1070; /// R/W + FillSetting(Register::DPP::PSD::TriggerHoldOffWidth , ch); /// 0x1074; /// R/W + FillSetting(Register::DPP::PSD::ThresholdForPSDCut , ch); /// 0x1078; /// R/W + FillSetting(Register::DPP::PSD::PurGapThreshold , ch); /// 0x107C; /// R/W + FillSetting(Register::DPP::PSD::DPPAlgorithmControl2_G , ch); /// 0x1084; /// R/W + FillSetting(Register::DPP::PSD::EarlyBaselineFreeze , ch); /// 0x10D8; /// R/W + } + + FillSetting(Register::DPP::TriggerValidationMask_G, ch); /// 0x8180; /// R/W, 0x8180 + 4n + } + + FillSetting(Register::DPP::BoardConfiguration ); /// 0x8000; /// R/W + FillSetting(Register::DPP::AggregateOrganization ); /// 0x800C; /// R/W + FillSetting(Register::DPP::AcquisitionControl ); /// 0x8100; /// R/W + FillSetting(Register::DPP::AcquisitionStatus_R ); /// 0x8104; /// R + FillSetting(Register::DPP::GlobalTriggerMask ); /// 0x810C; /// R/W + FillSetting(Register::DPP::FrontPanelTRGOUTEnableMask ); /// 0x8110; /// R/W + FillSetting(Register::DPP::LVDSIOData ); /// 0x8118; /// R/W + FillSetting(Register::DPP::FrontPanelIOControl ); /// 0x811C; /// R/W + FillSetting(Register::DPP::ChannelEnableMask ); /// 0x8120; /// R/W + FillSetting(Register::DPP::ROCFPGAFirmwareRevision_R ); /// 0x8124; /// R + FillSetting(Register::DPP::EventStored_R ); /// 0x812C; /// R + FillSetting(Register::DPP::VoltageLevelModeConfig ); /// 0x8138; /// R/W + FillSetting(Register::DPP::BoardInfo_R ); /// 0x8140; /// R /// [0:7] 0x0E = 725, 0x0B = 730, [8:15] 0x01 = 640 kSample, 0x08 = 5.12 MSample, [16:23] channel number + FillSetting(Register::DPP::AnalogMonitorMode ); /// 0x8144; /// R/W + FillSetting(Register::DPP::EventSize_R ); /// 0x814C; /// R + FillSetting(Register::DPP::TimeBombDowncounter_R ); /// 0x8158; /// R + FillSetting(Register::DPP::FanSpeedControl ); /// 0x8168; /// R/W + FillSetting(Register::DPP::RunStartStopDelay ); /// 0x8170; /// R/W + FillSetting(Register::DPP::BoardFailureStatus_R ); /// 0x8178; /// R + FillSetting(Register::DPP::DisableExternalTrigger ); /// 0x817C; /// R/W + + FillSetting(Register::DPP::FrontPanelLVDSIONewFeatures ); /// 0x81A0; /// R/W + FillSetting(Register::DPP::BufferOccupancyGain ); /// 0x81B4; /// R/W + FillSetting(Register::DPP::ExtendedVetoDelay ); /// 0x81C4; /// R/W + FillSetting(Register::DPP::ReadoutControl ); /// 0xEF00; /// R/W + FillSetting(Register::DPP::ReadoutStatus_R ); /// 0xEF04; /// R + FillSetting(Register::DPP::BoardID ); /// 0xEF08; /// R/W /// Geo address on VME crate + FillSetting(Register::DPP::MCSTBaseAddressAndControl ); /// 0xEF0C; /// R/W + FillSetting(Register::DPP::RelocationAddress ); /// 0xEF10; /// R/W + FillSetting(Register::DPP::InterruptStatusID ); /// 0xEF14; /// R/W + FillSetting(Register::DPP::InterruptEventNumber ); /// 0xEF18; /// R/W + FillSetting(Register::DPP::MaxAggregatePerBlockTransfer); /// 0xEF1C; /// R/W + FillSetting(Register::DPP::Scratch ); /// 0xEF20; /// R/W + + isSettingFilledinMemeory = true; +} + +void Digitizer::PrintSettingFromMemory(){ + for( int i = 0; i < SETTINGSIZE; i++) printf("%4d | 0x%04X |0x%08X = %u \n", i, i*4, setting[i], setting[i]); +} + +void Digitizer::OpenSettingBinary(string fileName){ + settingFile = NULL; + this->settingFileName = fileName; + settingFile = fopen(fileName.c_str(), "r+"); + if( settingFile == NULL ){ + printf("cannot open file %s. \n", fileName.c_str()); + CreateAndSaveSettingToFile(fileName); + }else{ + settingFileExist = true; + printf("setting file already exist.\n"); + } +} + +void Digitizer::CreateAndSaveSettingToFile(string fileName){ + settingFileName = fileName; settingFile = fopen(settingFileName.c_str(), "w+"); - unsigned int haha[1] = {0x00000000}; /// 4 bytes - for(int i = 0; i < 8192/4 ; i++){ - fwrite(haha, sizeof(unsigned int), 1, settingFile); - } + if( isSettingFilledinMemeory == false) FillAllSettings(); + + fwrite(setting, SETTINGSIZE * sizeof(unsigned int), 1, settingFile); fseek(settingFile, 0L, SEEK_END); unsigned int inFileSize = ftell(settingFile); @@ -675,115 +832,57 @@ void Digitizer::CreateAndSaveSettingBinary(string fileName){ fclose (settingFile); settingFileExist = true; - ///Save board setting - SaveSettingEditByteByRegister(Register::DPP::BoardConfiguration); /// 0x8000 - SaveSettingEditByteByRegister(Register::DPP::AggregateOrganization); /// 0x800C - SaveSettingEditByteByRegister(Register::DPP::GlobalTriggerMask); /// 0x810C - SaveSettingEditByteByRegister(Register::DPP::ChannelEnableMask); /// 0x8120 - SaveSettingEditByteByRegister(Register::DPP::ROM_BoardVersion_R); /// 0xF030 - SaveSettingEditByteByRegister(Register::DPP::ROM_BoardFromFactor_R); /// 0xF034 - SaveSettingEditByteByRegister(Register::DPP::ROM_BoardIDByte1_R); /// 0xF038 - SaveSettingEditByteByRegister(Register::DPP::ROM_BoardIDByte0_R); /// 0xF03C - SaveSettingEditByteByRegister(Register::DPP::ROM_BoardSerialNumByte1_R); /// 0xF080 - SaveSettingEditByteByRegister(Register::DPP::ROM_BoardSerialNumByte0_R); /// 0xF084 - SaveSettingEditByteByRegister(Register::DPP::FrontPanelTRGOUTEnableMask); /// 0x8110 - SaveSettingEditByteByRegister(Register::DPP::FrontPanelIOControl); /// 0x811C - SaveSettingEditByteByRegister(Register::DPP::ROCFPGAFirmwareRevision_R); /// 0x8124 - SaveSettingEditByteByRegister(Register::DPP::BoardInfo_R); /// 0x8140 - SaveSettingEditByteByRegister(Register::DPP::DisableExternalTrigger); /// 0x817C - SaveSettingEditByteByRegister(Register::DPP::TriggerValidationMask_G); /// 0x8180 - SaveSettingEditByteByRegister(Register::DPP::MaxAggregatePerBlockTransfer); /// 0xEF1C - - - ///================ channel settings - for( int ch = 0; ch < NChannel ; ch++){ - - SaveSettingEditByteByRegister(Register::DPP::RecordLength_G, ch); - SaveSettingEditByteByRegister(Register::DPP::InputDynamicRange, ch); - SaveSettingEditByteByRegister(Register::DPP::PreTrigger, ch); - SaveSettingEditByteByRegister(Register::DPP::ChannelDCOffset, ch); - SaveSettingEditByteByRegister(Register::DPP::NumberEventsPerAggregate_G, ch); - SaveSettingEditByteByRegister(Register::DPP::DPPAlgorithmControl, ch); - SaveSettingEditByteByRegister(Register::DPP::AMCFirmwareRevision_R, ch); - SaveSettingEditByteByRegister(Register::DPP::VetoWidth, ch); - - if( DPPType == V1730_DPP_PHA_CODE ) { /// DPP-PHA - SaveSettingEditByteByRegister(Register::DPP::PHA::TriggerThreshold, ch); - SaveSettingEditByteByRegister(Register::DPP::PHA::RCCR2SmoothingFactor, ch); - SaveSettingEditByteByRegister(Register::DPP::PHA::RiseTimeValidationWindow, ch); - SaveSettingEditByteByRegister(Register::DPP::PHA::TriggerHoldOffWidth, ch); - SaveSettingEditByteByRegister(Register::DPP::PHA::InputRiseTime, ch); - SaveSettingEditByteByRegister(Register::DPP::PHA::ShapedTriggerWidth, ch); - - SaveSettingEditByteByRegister(Register::DPP::PHA::TrapezoidRiseTime, ch); - SaveSettingEditByteByRegister(Register::DPP::PHA::TrapezoidFlatTop, ch); - SaveSettingEditByteByRegister(Register::DPP::PHA::DecayTime, ch); - SaveSettingEditByteByRegister(Register::DPP::PHA::PeakingTime, ch); - SaveSettingEditByteByRegister(Register::DPP::PHA::PeakHoldOff, ch); +} - SaveSettingEditByteByRegister(Register::DPP::PHA::DPPAlgorithmControl2_G, ch); - SaveSettingEditByteByRegister(Register::DPP::PHA::FineGain, ch); - } - - if( DPPType == V1730_DPP_PSD_CODE ) { /// DPP-PSD - SaveSettingEditByteByRegister(Register::DPP::PSD::CFDSetting, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::ChargeZeroSuppressionThreshold, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::ShortGateWidth, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::LongGateWidth, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::GateOffset, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::TriggerThreshold, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::FixedBaseline, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::TriggerLatency, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::ShapedTriggerWidth, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::TriggerHoldOffWidth, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::ThresholdForPSDCut, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::PurGapThreshold, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::DPPAlgorithmControl2_G, ch); - SaveSettingEditByteByRegister(Register::DPP::PSD::EarlyBaselineFreeze, ch); - } - } + +void Digitizer::LoadSettingBinary(string fileName){ + settingFileName = fileName; + settingFile = fopen(settingFileName.c_str(), "r"); + + if( settingFile == NULL ) { + printf(" %s does not exist.\n", fileName.c_str()); + settingFileExist = false; + return; + + }else{ + settingFileExist = true; + size_t dummy = fread( setting, SETTINGSIZE * sizeof(unsigned int), 1, settingFile); + } } -unsigned long Digitizer::ReadSettingBinary(uint32_t filePos, int ch){ +unsigned int Digitizer::ReadSettingFromFile(uint32_t registerAddress, int ch){ if ( !settingFileExist ) return -1; - if( filePos >= 0x8000 ) { - filePos = filePos & 0x0FFF; - }else{ - if( ch >= 0 ) { - filePos = filePos + (ch << 8); - }else{ - return -1; - } - } + unsigned short index = CalSettingIndex(registerAddress, ch); settingFile = fopen (settingFileName.c_str(),"r"); - fseek( settingFile, filePos, SEEK_SET); - ///printf(" at pos %lu \n", ftell(settingFile)); + ///fseek( settingFile, address, SEEK_SET); + fseek( settingFile, index * 4, SEEK_SET); + ///printf(" at pos %lu Byte = index(%lu)\n", ftell(settingFile), ftell(settingFile)/4); unsigned int lala[1]; size_t dummy = fread( lala, sizeof(unsigned int), 1, settingFile); - ///printf(" data at pos %lu : %x \n", ftell(settingFile), lala[0]); + ///printf(" data at pos %lu(%lu) : %X = %d\n", ftell(settingFile) - sizeof(unsigned int), (ftell(settingFile) - sizeof(unsigned int))/4, lala[0], lala[0]); fclose (settingFile); return lala[0]; + } -void Digitizer::SaveSettingEditByte(unsigned int value, uint32_t filePos){ +void Digitizer::SaveSettingToFile(uint32_t registerAddress, unsigned int value, int ch){ if ( !settingFileExist ) return ; - if( filePos >= 0x8000 ) filePos = filePos & 0x0FFF; - ///printf(" file Pos : 0x%x \n", filePos); + unsigned short index = CalSettingIndex(registerAddress, ch); + setting[index] = value; + settingFile = fopen (settingFileName.c_str(),"r+"); - fseek( settingFile, filePos, SEEK_SET); + ///fseek( settingFile, address, SEEK_SET); + fseek( settingFile, index * 4, SEEK_SET); unsigned int jaja[1] = {value}; size_t dummy = fwrite( jaja, sizeof(unsigned int), 1, settingFile); - ///printf("fwrie ret : %d, %x = %d\n", (int)dummy, jaja[0], jaja[0]); + ///printf("fwrite ret : %d, 0x%0X, 0x%0X, %d, 0x%X = %d\n", (int)dummy, registerAddress, index*4, index, jaja[0], jaja[0]); fclose (settingFile); } -void Digitizer::SaveSettingEditByteByRegister(uint32_t registerAddress, int ch){ - SaveSettingEditByte( ReadRegister(registerAddress, ch), registerAddress); -} diff --git a/ClassDigitizer.h b/ClassDigitizer.h index f6d38c0..2d46a74 100644 --- a/ClassDigitizer.h +++ b/ClassDigitizer.h @@ -32,22 +32,25 @@ class Digitizer{ int CloseDigitizer(); ///=================Settings - void WriteRegister(uint32_t address, uint32_t value, int ch = -1); - uint32_t ReadRegister(uint32_t address, int ch = -1, string str = ""); + /// write value to digitizer, memory, and settingFile (if exist) + void WriteRegister(uint32_t registerAddress, uint32_t value, int ch = -1); + /// read value from digitizer and memory, and settingFile(if exist), + /// when ch == -1 for register < 0x8000, read ch = 0; + uint32_t ReadRegister(uint32_t registerAddress, int ch = -1, string str = ""); ///common for PHA and PSD digitizers - void SetChannelMask(uint32_t mask); - void SetRecordLength(unsigned int ns, int ch = -1); /// when ch == -1, mean set all channels - void SetInputDynamicRange(unsigned int TwoVol_0_or_halfVol_1, int ch = -1); - void SetPreTriggerSample(unsigned int nSample, int ch = -1 ); - void SetPreTriggerDuration(unsigned int ns, int ch = -1 ); - void SetDCOffset(float offsetPrecentage, int ch = -1); - void SetVetoWidth(uint32_t bit, int ch = -1); /// See manual - void SetTriggerPolarity(bool RiseingIsZero, int ch = -1); ///not used for DPP firmware + void SetChannelMask (uint32_t mask); + void SetRecordLength (unsigned int ns, int ch = -1); /// when ch == -1, mean set all channels + void SetInputDynamicRange (unsigned int TwoVol_0_or_halfVol_1, int ch = -1); + void SetPreTriggerSample (unsigned int nSample, int ch = -1 ); + void SetPreTriggerDuration (unsigned int ns, int ch = -1 ); + void SetDCOffset (float offsetPrecentage, int ch = -1); + void SetVetoWidth (uint32_t bit, int ch = -1); /// See manual + void SetTriggerPolarity (bool RiseingIsZero, int ch = -1); ///not used for DPP firmware - void SetEventAggregation(unsigned int numEvent, int ch = -1); - void SetAggregateOrganization(unsigned int bit); - void SetMaxAggregatePerBlockTransfer(unsigned int numEvent); + void SetEventAggregation (unsigned int numEvent, int ch = -1); + void SetAggregateOrganization (unsigned int bit); + void SetMaxAggregatePerBlockTransfer (unsigned int numEvent); void SetBits(uint32_t address, unsigned int bitValue, unsigned int bitLength, unsigned int bitSmallestPos, int ch = -1); void SetDPPAlgorithmControl(uint32_t bit, int ch = -1); @@ -71,21 +74,21 @@ class Digitizer{ bool GetConnectionStatus() {return isConnected;} int GetDPPType() {return DPPType;} std::string GetDPPTypeString() {return DPPTypeStr;} - CAEN_DGTZ_ConnectionType GetLinkType() {return LinkType;} int GetADCBits() {return BoardInfo.ADC_NBits;} std::string GetROCVersion() {return BoardInfo.ROC_FirmwareRel;} std::string GetAMCVersion() {return BoardInfo.AMC_FirmwareRel;} - + + CAEN_DGTZ_ConnectionType GetLinkType() {return LinkType;} + int GetChTemperature(int ch) ; - unsigned int GetRecordLengthSample(int ch) {return ReadRegister(Register::DPP::RecordLength_G, ch) * 8;} - unsigned int GetInputDynamicRange(int ch) {return ReadRegister(Register::DPP::InputDynamicRange, ch);} - unsigned int GetPreTriggerSample(int ch) {return ReadRegister(Register::DPP::PreTrigger, ch) * 4;} - float GetDCOffset(int ch) {return 100.0 - ReadRegister(Register::DPP::ChannelDCOffset, ch) * 100. / 0xFFFFF; } - unsigned int GetVetoWidth(int ch) {return ReadRegister(Register::DPP::VetoWidth, ch);} - - unsigned int GetEventAggregation(int ch = -1) {return ReadRegister(Register::DPP::NumberEventsPerAggregate_G, ch);} - unsigned int GetAggregateOrganization() {return ReadRegister(Register::DPP::AggregateOrganization);} + unsigned int GetRecordLengthSample(int ch) {return ReadRegister(Register::DPP::RecordLength_G, ch) * 8;} + unsigned int GetInputDynamicRange(int ch) {return ReadRegister(Register::DPP::InputDynamicRange, ch);} + unsigned int GetPreTriggerSample(int ch) {return ReadRegister(Register::DPP::PreTrigger, ch) * 4;} + float GetDCOffset(int ch) {return 100.0 - ReadRegister(Register::DPP::ChannelDCOffset, ch) * 100. / 0xFFFFF; } + unsigned int GetVetoWidth(int ch) {return ReadRegister(Register::DPP::VetoWidth, ch);} + unsigned int GetEventAggregation(int ch = -1) {return ReadRegister(Register::DPP::NumberEventsPerAggregate_G, ch);} + unsigned int GetAggregateOrganization() {return ReadRegister(Register::DPP::AggregateOrganization);} unsigned int GetMaxNumberOfAggregatePerBlockTransfer() {return ReadRegister(Register::DPP::MaxAggregatePerBlockTransfer);} unsigned int ReadBits(uint32_t address, unsigned int bitLength, unsigned int bitSmallestPos, int ch = -1 ); @@ -110,15 +113,22 @@ class Digitizer{ int ProgramPHABoard(); ///================ Setting - void OpenSettingBinary(string fileName); - //TODO void LoadSettingBinary(string fileName); - void CreateAndSaveSettingBinary(string fileName); - void SaveSettingEditByte(unsigned int value, uint32_t filePos); - void SaveSettingEditByteByRegister(uint32_t registerAddress, int ch = -1); - unsigned long ReadSettingBinary(uint32_t filePos, int ch = -1); /// read from setting binary + unsigned short CalSettingIndex (uint32_t registerAddress, int ch = -1); /// real address is CalSettingIndex() * 4 + void FillSetting (uint32_t registerAddress, int ch = -1); + void SetSettingToMemory (uint32_t registerAddress, unsigned int value, int ch = -1); + unsigned int GetSettingFromMemory (uint32_t registerAddress, int ch = -1); + void FillAllSettings (); + void PrintSettingFromMemory (); + + void OpenSettingBinary (string fileName); + void LoadSettingBinary (string fileName); /// load settign file to memory + void CreateAndSaveSettingToFile (string fileName); + void SaveSettingToFile (uint32_t registerAddress, unsigned int value, int ch = -1); + unsigned int ReadSettingFromFile (uint32_t registerAddress, int ch = -1); /// read from setting binary virtual int ProgramBoard(); + protected: Data * data; @@ -151,6 +161,9 @@ class Digitizer{ string settingFileName; FILE * settingFile; bool settingFileExist; + + unsigned int setting[SETTINGSIZE]; /// Setting, 4bytes x 2048 = 8192 bytes + bool isSettingFilledinMemeory; ///========== void ErrorMsg(string header = ""); diff --git a/RegisterAddress.h b/RegisterAddress.h index 4d8c225..29c63fb 100644 --- a/RegisterAddress.h +++ b/RegisterAddress.h @@ -119,7 +119,7 @@ namespace Register { const uint32_t RelocationAddress = 0xEF10; /// R/W const uint32_t InterruptStatusID = 0xEF14; /// R/W const uint32_t InterruptEventNumber = 0xEF18; /// R/W - const uint32_t MaxAggregatePerBlockTransfer = 0xEF1C; /// R/W + const uint32_t MaxAggregatePerBlockTransfer= 0xEF1C; /// R/W const uint32_t Scratch = 0xEF20; /// R/W const uint32_t SoftwareReset_W = 0xEF24; /// W const uint32_t SoftwareClear_W = 0xEF28; /// W diff --git a/macro.h b/macro.h index 07373e7..382db58 100644 --- a/macro.h +++ b/macro.h @@ -6,4 +6,6 @@ #define MaxRecordLength 0x3fff * 8 #define MaxSaveFileSize 1024 * 1024 * 1024 * 2 +#define SETTINGSIZE 2048 + #endif diff --git a/test.cpp b/test.cpp index 4116fdb..ad6bf4a 100644 --- a/test.cpp +++ b/test.cpp @@ -99,11 +99,41 @@ int main(int argc, char* argv[]){ unsigned int waveFormLength = 2000; unsigned int ch2ns = (unsigned int) dig[0]->GetCh2ns(); - dig[0]->WriteRegister(Register::DPP::RecordLength_G, waveFormLength/ch2ns); - dig[0]->WriteRegister(Register::DPP::DPPAlgorithmControl, 0xe30200e); - - remove("test.bin"); + //dig[0]->WriteRegister(Register::DPP::RecordLength_G, waveFormLength/ch2ns, 0); + //dig[0]->WriteRegister(Register::DPP::RecordLength_G, waveFormLength/ch2ns + 10, 3); + //dig[0]->WriteRegister(Register::DPP::DPPAlgorithmControl, 0xe30200e); + //dig[0]->WriteRegister(Register::DPP::TriggerValidationMask_G, 0x500, 1); + //dig[0]->WriteRegister(Register::DPP::TriggerValidationMask_G, 0x600, 3); + //dig[0]->WriteRegister(Register::DPP::Scratch, 0x12345678); + dig[0]->ReadRegister(Register::DPP::RecordLength_G, 0, "A0"); + dig[0]->ReadRegister(Register::DPP::RecordLength_G, 3, "A0"); + dig[0]->ReadRegister(Register::DPP::DPPAlgorithmControl, 0, "A1"); + dig[0]->ReadRegister(Register::DPP::DPPAlgorithmControl, -1, "A1"); + dig[0]->ReadRegister(Register::DPP::TriggerValidationMask_G, 1, "A3"); + dig[0]->ReadRegister(Register::DPP::TriggerValidationMask_G, 3, "A3"); + dig[0]->ReadRegister(Register::DPP::Scratch); + + printf(" ======== %u \n", dig[0]->GetSettingFromMemory(Register::DPP::RecordLength_G, 0)); + printf(" ======== %u \n", dig[0]->GetSettingFromMemory(Register::DPP::RecordLength_G, 3)); + //printf(" ======== %u \n", dig[0]->GetSettingFromMemory(Register::DPP::DPPAlgorithmControl, 0)); + //printf(" ======== %u \n", dig[0]->GetSettingFromMemory(Register::DPP::TriggerValidationMask_G, 1)); + //printf(" ======== %u \n", dig[0]->GetSettingFromMemory(Register::DPP::TriggerValidationMask_G, 2)); + //printf(" ======== %u \n", dig[0]->GetSettingFromMemory(Register::DPP::TriggerValidationMask_G, 3)); + //printf(" ======== %u \n", dig[0]->GetSettingFromMemory(Register::DPP::Scratch)); + + dig[0]->ReadSettingFromFile(Register::DPP::RecordLength_G, 0); + dig[0]->ReadSettingFromFile(Register::DPP::RecordLength_G, 3); + //dig[0]->ReadSettingFromFile(Register::DPP::DPPAlgorithmControl, 0); + //dig[0]->ReadSettingFromFile(Register::DPP::TriggerValidationMask_G, 1); + //dig[0]->ReadSettingFromFile(Register::DPP::TriggerValidationMask_G, 2); + //dig[0]->ReadSettingFromFile(Register::DPP::TriggerValidationMask_G, 3); + //dig[0]->ReadSettingFromFile(Register::DPP::Scratch); + + //dig[0]->PrintSettingFromMemory(); + + /** + ///============================ Get Data TApplication * app = new TApplication("app", &argc, argv); TCanvas * canvas = new TCanvas("c", "haha", 1200, 400); canvas->Divide(3, 1); @@ -116,11 +146,12 @@ int main(int argc, char* argv[]){ canvas->cd(1); h1->Draw("hist"); canvas->cd(2); h2->Draw(); canvas->cd(3); g1->Draw("AP"); - Data * data = dig[0]->GetData(); data->AllocateMemory(); + remove("test.bin"); + dig[0]->StartACQ(); std::vector haha ; @@ -230,7 +261,7 @@ int main(int argc, char* argv[]){ */ - + dig[0]->CloseDigitizer(); return 0; }