return err.set(1, BString("Error: Deleting Control Info: ") + err.getString());
}
return err;
}
BError tmsSetTestData(TmsControl& tmsControl, BString testData){
BError err;
BArray<UInt32> data;
BFile file;
UInt32 channel;
PuChannel puChannel;
char fileName[1024] = "\0";
int n;
int nb;
n = sscanf(testData, "%u,%s", &channel, fileName);
if((n < 1) || (n > 2)){
err.set(ErrorConfig, "Error in setTestData spec");
return err;
}
// get Physical Channel number
if(err = tmsControl.getPuChannel(channel, puChannel)){
return err.set(1, BString("Error: Getting Physical Channel Number: ") + err.getString());
}
if(n == 2){
if(err = file.open(fileName, "r")){
return err.set(1, BString("Error: Opening file: ") + fileName);
}
data.resize(1024);
if((nb = file.read(&data[0], 1024 * sizeof(UInt32))) <= 0){
return err.set(1, BString("Error: Reading 1024 32bit data items from file: ") + fileName);
}
data.resize(nb / sizeof(UInt32));
if(err = tmsControl.setTestData(puChannel, 1, data)){
return err.set(1, err.getString());
}
}
else {
if(err = tmsControl.setTestData(puChannel, 0, data)){
return err.set(1, err.getString());
}
}
return err;
}
BError tmsSetPupeConfig(TmsControl& tmsControl, BString pupeConfigString){
BError err;
UInt32 channel;
PuChannel puChannel;
PupeConfig pupeConfig;
UInt32 n;
n = sscanf(pupeConfigString, "%u,%u,%x,%u", &channel, &pupeConfig.adcSysclkSync, &pupeConfig.internalTimingMask, &pupeConfig.disableBlr);
if(n != 4){
err.set(ErrorConfig, "Error in pupeConfigString spec");
return err;
}
// get Physical Channel number
if(err = tmsControl.getPuChannel(channel, puChannel)){
return err.set(1, BString("Error: Getting Physical Channel Number: ") + err.getString());
}
if(err = tmsControl.setPupeConfig(puChannel, pupeConfig)){
return err.set(1, err.getString());
}
return err;
}
Int32 getBitValue(UInt64 value, int startBit, int nBits, int s){
Int64 v;
UInt64 bm = (1ULL << nBits) - 1;
UInt64 sm = (1ULL << (nBits - 1));
v = (value >> startBit) & bm;
if(s){
v = -(v & sm) | v;
}
return v;
}
#define BIT(v,b) ((v >> b) & 1)
BError tmsCaptureDiagnostics(TmsControl& tmsControl, TmsProcess& tmsProcess, BString captureSpec, BString outFile){
BError err;
TestCaptureInfo captureInfo;
BArray<UInt64> data;
UInt32 cn = 0;
BString ct;
BFile file;
UInt32 i;
UInt32 channel;
PuChannel puChannel;
if(sscanf(captureSpec, "%u,%u,%u, %u,%u,%u,%u,%u,%u", &channel, &captureInfo.source, &captureInfo.clock,
&captureInfo.startTime, &captureInfo.postTriggerDelay, &captureInfo.triggerAnd, &captureInfo.triggerStore,
&captureInfo.triggerSourceData, &captureInfo.triggerMask) != 9){
err.set(ErrorConfig, "Error in CapSpec");
return err;
}
if(outFile == "")
outFile = "data.txt";
if(err = file.open(outFile, "w")){
return err.set(1, BString("Error: Opening file: ") + outFile);
}
// get Physical Channel number
if(err = tmsControl.getPuChannel(channel, puChannel)){
return err.set(1, BString("Error: Getting Physical Channel Number: ") + err.getString());
}
// Get next Cycle Number
if(err = tmsProcess.getCycleInfo(cn, ct)){
return err.set(1, BString("Error: Getting Cycle Number: ") + err.getString());
}
printf("CaptureTestData: Channel: %u:%u:%u CycleNumber: %u CycleType: %s\n", puChannel.moduleNum, puChannel.pupeNum, puChannel.pupeChan, cn, ct.retStr());
if(err = tmsControl.captureDiagnostics(puChannel, captureInfo, data)){
return err.set(1, BString("Error: Getting Data: ") + err.getString());
}
// Assumes source == 0
if(captureInfo.source == 0){
if (kst) {
if(captureInfo.triggerStore){
file.printf("FrefIn PllMsb PhaseTableMsb Lo1 Sigma Lo2 Gate Blr Mean1 Mean2 RfSelect1 RfSelect2 SelFilter SwitchState DdsFreq ");
file.printf("FRef HChange Injection CalStop CalStart CycleStop CycleStart SysClock\n");
}
else {
file.printf("FrefIn PllMsb PhaseTableMsb Lo1 Sigma Lo2 Gate Blr Mean1 Mean2 RfSelect1 RfSelect2 SelFilter SwitchState DdsFreq\n");
}
}
for(i = 0; i < data.size(); i++){
if(captureInfo.triggerStore){
file.printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
getBitValue(data[i], 63, 1, 0), // FREF
!getBitValue(data[i], 62, 1, 0), // PllMsb (FRef Reference)
!getBitValue(data[i], 61, 1, 0), // PhaseTableMsb (Local FREF)
getBitValue(data[i], 60, 1, 0), // LO1
// getBitValue(data[i], 57, 3, 1), // Sigma (3 bits)
getBitValue(data[i], 32, 12, 1), // Sigma (12 bits)
getBitValue(data[i], 56, 1, 0), // LO2
getBitValue(data[i], 49, 1, 0), // Gate
getBitValue(data[i], 48, 1, 0), // Blr
getBitValue(data[i], 50, 1, 0), // Mean1
getBitValue(data[i], 51, 1, 0), // Mean2
getBitValue(data[i], 52, 1, 0), // RfSelect1
getBitValue(data[i], 53, 1, 0), // RfSelect2
getBitValue(data[i], 54, 1, 0), // SelFilter
getBitValue(data[i], 44, 4, 0), // SwitchState
getBitValue(data[i], 8, 24, 1), // DDS_FREQ
getBitValue(data[i], 7, 1, 0), // FREF
getBitValue(data[i], 6, 1, 0), // HCHANGE event
getBitValue(data[i], 5, 1, 0), // INJECTION event
getBitValue(data[i], 4, 1, 0), // CAL_STOP event
getBitValue(data[i], 3, 1, 0), // CAL_START event
getBitValue(data[i], 2, 1, 0), // CYCLE_STOP event
getBitValue(data[i], 1, 1, 0), // CYCLE_START event
getBitValue(data[i], 0, 1, 0) // 10MHz System Clock
);
}
else {
file.printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
getBitValue(data[i], 63, 1, 0), // FREF
!getBitValue(data[i], 62, 1, 0), // PllMsb (FRef Reference)
!getBitValue(data[i], 61, 1, 0), // PhaseTableMsb (Local FREF)
getBitValue(data[i], 60, 1, 0), // LO1
// getBitValue(data[i], 57, 3, 1), // Sigma (3 bits)
getBitValue(data[i], 32, 12, 1), // Sigma (12 bits)
getBitValue(data[i], 56, 1, 0), // LO2
getBitValue(data[i], 49, 1, 0), // Gate
getBitValue(data[i], 48, 1, 0), // Blr
getBitValue(data[i], 50, 1, 0), // Mean1
getBitValue(data[i], 51, 1, 0), // Mean2
getBitValue(data[i], 52, 1, 0), // RfSelect1
getBitValue(data[i], 53, 1, 0), // RfSelect2
getBitValue(data[i], 54, 1, 0), // SelFilter
getBitValue(data[i], 44, 4, 0), // SwitchState
getBitValue(data[i], 0, 32, 1) // DDS_FREQ
);
}
}
}
else if(captureInfo.source == 1){
if (kst) {
file.printf("FrefIn PllMsb PhaseTableMsb Lo1 Sigma Lo2 MultOut1 MultOut2 F_Error\n");
}
for(i = 0; i < data.size(); i++){
Int32 timing = 0;
if(captureInfo.triggerStore){
timing = getBitValue(data[i], 0, 8, 0);
}
file.printf("%d %d %d %d %d %d %d %d %d\n",
getBitValue(data[i], 63, 1, 0), // FREF
!getBitValue(data[i], 62, 1, 0), // PllMsb (FRef Reference)
!getBitValue(data[i], 61, 1, 0), // PhaseTableMsb (Local FREF)
getBitValue(data[i], 60, 1, 0), // LO1
getBitValue(data[i], 57, 3, 1), // Sigma (3 bits)
getBitValue(data[i], 56, 1, 0), // LO2
getBitValue(data[i], 38, 14, 1), // mult_out1
getBitValue(data[i], 24, 14, 1), // mult_out2
getBitValue(data[i], 0, 24, 1) // f_error
);
}
}
else if(captureInfo.source == 2){
if (kst) {
file.printf("FrefIn PllMsb PhaseTableMsb Lo1 Sigma Lo2 Gate B0 Result0\n");
}
for(i = 0; i < data.size(); i++){
Int32 timing = 0;
if(captureInfo.triggerStore){
timing = getBitValue(data[i], 0, 8, 0);
}
file.printf("%d %d %d %d %d %d %d %d %d\n",
getBitValue(data[i], 63, 1, 0), // FREF
!getBitValue(data[i], 62, 1, 0), // PllMsb (FRef Reference)
!getBitValue(data[i], 61, 1, 0), // PhaseTableMsb (Local FREF)
getBitValue(data[i], 60, 1, 0), // LO1
getBitValue(data[i], 57, 3, 1), // Sigma (3 bits)
getBitValue(data[i], 56, 1, 0), // LO2
getBitValue(data[i], 55, 1, 0), // Gate
getBitValue(data[i], 32, 23, 1), // B0
getBitValue(data[i], 0, 32, 1) // Result0
);
}
}
else if(captureInfo.source == 3){
if (kst) {
file.printf("Sigma Gate Blr X0 E0 Y0\n");
}
for(i = 0; i < data.size(); i++){
Int32 timing = 0;
if(captureInfo.triggerStore){
timing = getBitValue(data[i], 0, 8, 0);
}
file.printf("%d %d %d %d %d %d %d\n",
getBitValue(data[i], 48, 14, 1), // Sigma
getBitValue(data[i], 62, 1, 0), // Gate
getBitValue(data[i], 63, 1, 0), // Blr
getBitValue(data[i], 0, 16, 1), // X0
getBitValue(data[i], 16, 16, 1), // E0
getBitValue(data[i], 32, 16, 1) // Y0
);
}
}
else {
err.set(1, BString("Source: ") + captureInfo.source + " not supported");
}
file.close();
return err;
}
BError tmsGetData(TmsProcess& tmsProcess, BString readDataSpec, BString outFile){
BError err;
DataInfo dataInfo;
Data data;
UInt32 cn = 0;
BString ct;
BFile file;
UInt32 i;
UInt32 t = 0;
if(sscanf(readDataSpec, "%u,%u,%u,%u,%u,%u,%u", &dataInfo.channel, &dataInfo.cyclePeriod, &dataInfo.startTime, &dataInfo.orbitNumber, &dataInfo.bunchNumber, &dataInfo.numValues, &dataInfo.function) != 7){
err.set(ErrorConfig, "Error in DataSpec");
return err;
}
if(outFile == "")
outFile = "data.txt";
if(err = file.open(outFile, "w")){
return err.set(1, BString("Error: Opening file: ") + outFile);
}
// Set data require and wait for data
if(err = tmsProcess.getCycleInfo(cn, ct)){
return err.set(1, BString("Error: Getting Cycle Number: ") + err.getString());
}
dataInfo.cycleNumber = cn;
dataInfo.argument = 0;
dataInfo.beyondPeriod = 0;
printf("GetData: CycleNumber: %u CycleType: %s\n", cn, ct.retStr());
if(err = tmsProcess.getData(dataInfo, data)){
return err.set(1, BString("Error: Getting Data: ") + err.getString());
}
if(data.dataValues.size()){
t = data.dataValues[0].time;
}
printf("GotData: Num: %d NumBunches: %d NumChannels: %d FirstTime: %dms\n", data.numValues, data.numBunches, data.numChannels, t);
if (kst) {
file.printf("sigma deltaX deltaY\n");
}
for(i = 0; i < data.numValues; i++){
file.printf("%d %d %d\n", data.dataValues[i].sigma, data.dataValues[i].deltaX, data.dataValues[i].deltaY);
}
file.close();
return err;
}
BError tmsPerf(TmsProcess& tmsProcess, int channel, int numSamples){
BError err;
UInt32 i;
UInt32 num = 10;
DataInfo dataInfo;
Data data;
double t1, t2, r;
BString ct;
// Set data require and wait for data
dataInfo.cycleNumber = 0;
dataInfo.channel = channel;
dataInfo.cyclePeriod = CyclePeriodEvent0;
dataInfo.startTime = 0;
dataInfo.orbitNumber = 0;
dataInfo.bunchNumber = 0;
dataInfo.function = 0;
dataInfo.argument = 0;
dataInfo.numValues = numSamples;
#ifdef ZAP
tmsProcess.getCycleNumber(dataInfo.cycleNumber);
dataInfo.cycleNumber--;
t1 = getTime();
for(i = 0; i < num; i++){
if(err = tmsProcess.getData(dataInfo, data)){
return err.set(1, BString("Error: Getting Data: ") + err.getString());
}
}
t2 = getTime();
r = (double(numSamples) * num * sizeof(DataValue)) / (t2 - t1);
printf("DataSize: %d DataRate: %f MBytes/sec\n", numSamples, r / (1024 * 1024));
#else
while(1){
tmsProcess.getCycleInfo(dataInfo.cycleNumber, ct);
dataInfo.cycleNumber--;
t1 = getTime();
for(i = 0; i < num; i++){
while(err = tmsProcess.getData(dataInfo, data)){
if(err.getErrorNo() == -EPIPE){
printf("Try to reconnect\n");
tmsProcess.disconnectService();
while(err = tmsProcess.connectService(tmsProcess.getServiceName())){
printf("Still Trying to reconnect\n");
sleep(1);
}
}
else {
return err;
}
}
}
t2 = getTime();
r = (double(numSamples) * num * sizeof(DataValue)) / (t2 - t1);
printf("DataSize: %d CallTime: %f s DataRate: %f MBytes/sec\n", numSamples, (t2 - t1) / num, r / (1024 * 1024));
}
#endif
return err;
}
BError tmsPerfPu(TmsControl& tmsControl, TmsProcess& tmsProcess, BString hostName, int channel, int numSamples){
BError err;
UInt32 i;
UInt32 num = 10;
DataInfo dataInfo;
Data data;
double t1, t2, r;
PuChannel chan;
PuProcess puProcess;
BString ct;
BUInt32 orbitNumber;
tmsControl.getPuChannel(channel, chan);
if(err = puProcess.connectService(BString("//") + hostName + "/puProcess-" + chan.moduleNum))
return err;
// Set data require and wait for data
dataInfo.cycleNumber = 0;
dataInfo.channel = 1;
dataInfo.cyclePeriod = CyclePeriodEvent0;
dataInfo.startTime = 0;
dataInfo.orbitNumber = 0;
dataInfo.bunchNumber = 0;
dataInfo.function = 0;
dataInfo.argument = 0;
dataInfo.numValues = numSamples;
while(1){
tmsProcess.getCycleInfo(dataInfo.cycleNumber, ct);
dataInfo.cycleNumber--;
t1 = getTime();
for(i = 0; i < num; i++){
if(err = puProcess.getData(chan, dataInfo, data, orbitNumber)){
return err.set(1, BString("Error: Getting Data: ") + err.getString());
}
}
t2 = getTime();
r = (double(numSamples) * num * sizeof(DataValue)) / (t2 - t1);
printf("DataSize: %d DataRate: %f MBytes/sec\n", numSamples, r / (1024 * 1024));
}
return err;
}
BError tmsEventsProcess(TmsProcess& tmsProcess, BString host){
BError err;
static BoapServer boapServer;
static TmsEventServer tmsEventServer(boapServer, "");
if(err = boapServer.init(host, BoapServer::THREADED)){
return err;
}
boapServer.run(1);
if(err = tmsProcess.addEventServer(tmsEventServer.name())){
return err;
}
pause();
return err;
}
void usage(void) {
cerr << "Usage:\ttmsControl [options] hostname\n";
cerr << " --help - Help on command line parameters\n";
cerr << " --debug 0x1122 - Set Debug mask\n";
cerr << " --version - Display Version numbers\n";