/* This code module checks the data received from an AIDA FEE64 module */ int GoodItems = 0; int Items[9] = {0,0,0,0,0,0,0,0,0}; char MessageBuffer[132]; #include #include #include void checkFEE64DATAbuffer(char* p, int l) { int len; int module, range, channel; unsigned int chan; unsigned int * eventbuffer; unsigned int data; unsigned short adc; unsigned short icode; unsigned int ifield; unsigned int timestamp; int i; int count; int samples; static unsigned long long timestampf=0; static unsigned long long SYNC100=0; static unsigned long long WRTimeF=0; static unsigned long long LastSync = 0LL; static unsigned long long LastTimeStamp = 0LL; static unsigned long long CurrentTimeStamp = 0LL; int Code; int Info; unsigned long long Time; static int LinkState = 0; static unsigned int WRTime0019=0; static unsigned int WRTime2847=0; static unsigned int WRTime4863=0; unsigned int SYNC_STEP = 0x40000; unsigned int STEPINC; int timeL; int timeH; extern char MessageBuffer[]; extern int Items[]; extern int GoodItems; int trace = 0; int trace_data = 0 ; int trace_sync = 1 ; int trace_adc = 0; int trace_pause = 1; int trace_resume = 1; int trace_wr = 0; int trace_disc = 0; int trace_wave = 0; len = l/4; eventbuffer = (unsigned int *)p; if (trace_data) { printf("buffer len=%d\n",len); printf(">"); for (i = 0; i < 32; i++) { printf("0x%08lx ", eventbuffer[i]); } printf("\n"); } len = l/4; /* len in units of int */ eventbuffer = (unsigned int *)p; if(trace) printf("Block received length %d\n", len ); while (len >= 2) { data = *eventbuffer++; len--; timestamp = *eventbuffer++; len--; Code = -1; Info = 0; if (data == 0xffffffff) break; /* end of buffer */ Items[8]++; switch ((data >> 30 ) & 3) { case 3: /* ADC data word */ Items[0]++; range = (data >> 28) & 1; module = (data >> 22) & 63; channel = (data >> 16) & 63; adc = (unsigned short) (data & 0x0000ffff); timeL = timestamp; timeH = SYNC100 >> 28; if (timeL >= 0 && timeL <= 0xa0) { timestampf = ((unsigned long long) (timeH + 1) << 28) + (unsigned long long) timeL; } else { timestampf = ((unsigned long long) timeH << 28) + (unsigned long long) timeL; } if (trace_adc) printf("adc data: 0x%04x range=%d module=%d channel=%d: H=0x%08lx L=0x%08lx: TS=0x%012llx\n", adc, range, module, channel, data, timestamp, timestampf); Code = 16; Time = timestampf; break; case 2: /* information data word */ Items[7]++; icode = (data >> 20) & 0x0f; /* information code */ ifield = data & 0x000fffff; /* information field */ module = (data >> 24) & 0x3f; switch (icode) { case 2: /* PAUSE timestamp */ Items[2]++; timestampf = ((unsigned long long) ifield << 28) | (unsigned long long) timestamp; if (trace_pause ) printf("received PAUSE: m=%d, ifield=0x%05x: H=0x%08lx L=0x%08lx: TS=0x%012llx\n", module, ifield, data, timestamp, timestampf); Code = 2; Time = timestampf; LinkState = 1; break; case 3: /* RESUME timestamp */ Items[3]++; timestampf = ((unsigned long long) ifield << 28) | (unsigned long long) timestamp; if (trace_resume) printf("received RESUME: m=%d, ifield=0x%05x: H=0x%08lx L=0x%08lx: TS=0x%012llx\n", module, ifield, data, timestamp, timestampf); Code = 3; Time = timestampf; if ((timestampf & 0xfffffffffffc0000ull) == (SYNC100 & 0xfffffffffffc0000ull)) { LinkState = 2; } else { LinkState = 1; } break; case 4: /* SYNC100 timestamp / WR timestamp L */ Items[4]++; timestampf = ((unsigned long long) ifield << 28) | (unsigned long long) timestamp; WRTime0019 = timestamp; WRTime2847 = (ifield << 28); if (trace_sync) printf("received SYNC100 TS: m=%d, ifield=0x%05x: H=0x%08lx L=0x%08lx: TS=0x%012llx\n", module, ifield, data, timestamp, timestampf); Code = 4; Time = timestampf; STEPINC = timestampf - SYNC100; if (LinkState != 2) { printf("received SYNC100 TS -> GOING: 0x%012llx\n",timestampf); LinkState = 2; } else { if (STEPINC != SYNC_STEP && STEPINC != 0) { printf("received SYNC100 TS with unexpected value: 0x%x 0x%012llx 0x%012llx\n",STEPINC,timestampf,SYNC100); } } SYNC100 = timestampf; break; case 5: /* WR timestamp H */ Items[5]++; timestampf = ((unsigned long long) ifield << 28) | (unsigned long long) timestamp; WRTime0019 = timestamp; WRTime4863 = (ifield << 28); WRTimeF = ((unsigned long long) WRTime4863 << 48) | ((unsigned long long) WRTime2847 << 28) | (unsigned long long) WRTime0019; if (trace_wr) printf("received WR High TS: m=%d, ifield=0x%05x: H=0x%08lx L=0x%08lx: WR=0x%012llx\n", module, ifield, data, timestamp, WRTimeF); Code = 5; Time = WRTimeF; break; case 6: /* FEE64 discriminator data */ Items[6]++; timeL = timestamp; timeH = SYNC100 >> 28; if (timeL >= 0 && timeL <= 0xa0) { timestampf = ((unsigned long long) (timeH + 1) << 28) + (unsigned long long) timeL; } else { timestampf = ((unsigned long long) timeH << 28) + (unsigned long long) timeL; } if (trace_disc) printf("received AIDA Disc: m=%d, 0x%05x: H=0x%08lx L=0x%08lx: TS=0x%012llx\n", module, ifield, data, timestamp, timestampf); Code = 6; Time = timestampf; break; default: /* undefined code */ printf("received undefined information code: %d\n", icode); break; } break; case 1: /* wave form data */ Items[1]++; timeL = timestamp; timeH = SYNC100 >> 28; if (timeL >= 0 && timeL <= 0xa0) { timestampf = ((unsigned long long) (timeH + 1) << 28) + (unsigned long long) timeL; } else { timestampf = ((unsigned long long) timeH << 28) + (unsigned long long) timeL; } module = (data >> 22) & 63; channel = (data >> 16) & 63; samples = (data >> 18) & 0x00003fff; /* in units of 4 samples (64 bits) */ while (samples > 0) {samples--; eventbuffer++; len--; eventbuffer++; len--;} if (trace_wave) printf("received waveform: module=%d channel=%d samples=%d H=0x%08lx L=0x%08lx: TS=0x%012llx\n", module, channel, samples*4, data, timestamp, timestampf); Code = 32; Time = timestampf; break; case 0: /* bad format */ default: printf("bad event data (%d %d) - (0x%08lx 0x%08lx)\n", l/4, len, data, timestamp); len = 0; break; } if (LinkState == 2 && Code != -1) { if (Time < LastTimeStamp) { printf("%s",MessageBuffer); printf("bad timestamp %d 0x%08x 0x%08x 0x%012llx 0x%012llx 0x%012llx\n", Code, data, timestamp, SYNC100, Time, LastTimeStamp); LastTimeStamp = Time; } else { sprintf(MessageBuffer,"good timestamp %d 0x%08x 0x%08x 0x%012llx 0x%012llx 0x%012llx\n", Code, data, timestamp, SYNC100, Time, LastTimeStamp); GoodItems++; LastTimeStamp = Time; } } else { if (!(LinkState == 1 && Code == 2)) { printf("ignored timestamp %d 0x%08x 0x%08x 0x%012llx 0x%012llx 0x%012llx\n", Code, data, timestamp, SYNC100, Time, LastTimeStamp); } else { GoodItems++; } } } /* end of while */ }