//#define USE_DAQ
//#define USE_MIKRO
#define USE_EASIROC
#define USE_UDPCONTROL
#ifdef USE_EASIROC
#include "easiroc.h"
#define erIpAddr "192.168.10.16"
#define erDAQmode 3
#endif
#ifdef USE_UDPCONTROL
#include "SiTCP.h"
#include "sender.h"
#define IpAddr "192.168.10.16"
#define tcpport 24
#define udpport 4660
int HVDAC = 0;
int MADC = 0;
int MUX = 0;
double HV = 0;
double rd_data = 0;
float DACdata[63];
float HVC_1 = 483.183;
float HVC_2 = 780;
// HVDAC =HVC_1 * HV + HVC_2; convert HV to DAC bit
float ADC2HV = 0.00208;
// rd_data = ADC2HV * rd_data; convert ADC bit to HV
float ADC2uA = 0.034;
// rd_data = ADC2uA * rd_data; convert ADC bit to HVcurrent
float ADC2V = 0.0000685;
//rd_data = ADC2V * rd_data; convert ADC bit to inputDAC Voltage
float ADC2K = 4500;
// rd_data = ADC2K * rd_data/65535/2.4; convert ADC bit to TEMP(K)
#endif
// Izberi ustrezni interface v meniju projektnega okna
// Options->CompilerDefines (dodaj /DSISVME ali /DWIENVME)
#ifdef USE_DAQ
# define USE_CAMAC
# include "camac.h"
# ifdef SISVME
# include "sisWIENVME_VME_dll.h"
# endif
# ifdef WIENVME
//# include "wienWIENVME_VME_dll.h"
# include "wienvme_dll.h"
# endif
# include "CAENV965.h"
#endif
#ifdef USE_MIKRO
# include "MIKRO.h"
#endif
#include <userint.h>
#include <ansi_c.h>
#include <utility.h>
#include <analysis.h>
#include "l2d_ui.h"
#ifdef USE_DAQ
# define VADC_ADDR1 0x55550000
# define VADC1 1
//# define VADC_ADDR2 0x550000
# define VADC2 2
//# define NTDCJ1 17
//# define NTDCJ2 18
//# define NGL 23
# define NPIO 23
#define USE_CAEN_TDC
//CAEN V1290A TDC
#define TDC_ADDR 0x44440000
#define NCH_TDC 32
#define EVMAX 4 //how many of the multihit to save
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
static int tdcmap[32]={ 0, 1,2, 3,4, 5,6, 7,8,9,10,11,12,13,14,15,
// 16 19 18 19 20 21 22 23 24 25 26 27 28 29 30 31
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
#endif
#ifdef USE_CAEN_TDC
// CAEN V1290 TDC
int V1290_writeOC(int *write_buffer);
int V1290_readOC(int *write_buffer);
void V1290_init();
#endif
// Pomozne funkcije
int max_from(int* array, int ifrom, int ito);
#ifdef USE_MIKRO
# define MIKRO_COM 5
# define MIKRO_X 1
#define USE_MIKRO_Y
# define MIKRO_Y 2
#define USE_MIKRO_Z
# define MIKRO_Z 3
#endif
#define MAXCH_TDC 65536
#define MAXCH_QDC 0x1000
#define MAX_THREADS 10
#define IWAIT 200
# define NCH 32
static int p1h, pID, rID, tfID;
static int ph_tdc, ph_adc;
static int dtdc[NCH][2][MAXCH_TDC];
static int dadc[NCH][2][MAXCH_QDC];
static int daq_on;
static int poolHandle = 0;
static int ntics,dummy;
// <- NTDCJ1 ->|<- NTDCJ2 ->
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
//static int tdcmap[NCH]= { 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7 ,99,99,99,99};
//static int adcmap[NCH*2]={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, 99,99,99,99,
//99,99,99,9999,99,99,9999,99,99,9999,99,99,99,99, 99,99,99,99};
static int adcmap[32]={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
//static int adcmap[16]={99,99,99,99,99,99,99,99, 0, 1, 2, 3, 4, 5, 6, 7};
//static int adcmap[16]={ 0, 1, 2, 3, 4, 5,99, 7, 6,99,99,99,99,99,99,99};
//static int adcmap[16]={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15};
/************************************************************/
void wait_loop(unsigned long iloop)
{
int i;
for (i=0;i<iloop;i++);
return;
}
int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
QueueUserEvent (9000, p1h, P1_TIMER);
return (0);
}
int update_plots (void)
{
int irange, ch;
GetCtrlVal (p1h, P1_PLCH, &ch);
if (ph_tdc>0) DeleteGraphPlot (p1h, P1_TDC, ph_tdc, VAL_DELAYED_DRAW);
GetCtrlVal (p1h, P1_TDCHL, &irange);
ph_tdc = PlotY (p1h, P1_TDC, &dtdc[ch][irange], MAXCH_TDC, VAL_INTEGER,
VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
if (ph_adc>0) DeleteGraphPlot (p1h, P1_ADC, ph_adc, VAL_DELAYED_DRAW);
GetCtrlVal (p1h, P1_ADCHL, &irange);
ph_adc = PlotY (p1h, P1_ADC, &dadc[ch][irange], MAXCH_QDC, VAL_INTEGER,
VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE);
return (0);
}
int CVICALLBACK daq_run(void *functionData)
{
int i,j;
int ndat,dtype,ch,rg,adc,cres;
unsigned long a,b,ec1,ec2;
unsigned long data[100], data2[100];
unsigned long aa[NCH][4];
unsigned long step_minutes, end_time_s, cur_time_s;
int start_hours, start_minutes, start_seconds;
unsigned short tdcneve;
#ifdef USE_DAQ
unsigned long tdc_ev_c[NCH_TDC];
#endif
int abstime, num_hits, tdcch;
int tdcbuff;
int dsave,status,fmax,fcount,fev;
int popupret;
char dfile[MAX_PATHNAME_LEN],dfile0[MAX_PATHNAME_LEN];
int supr0,tdcmin,fseed,esave;
float frac;
double fracg;
int dum;
unsigned short dum16;
unsigned long dum32;
FILE *fp;
#define RUNREC_ID 1
#define ENDREC_ID 2
#define POSREC_ID 3
#define EVTREC_ID 4
typedef struct {
unsigned long id,len;
unsigned long nev,nch,ped,xy;
long nx,x0,dx,ny,y0,dy;
} RUNREC;
RUNREC runrec;
typedef struct {
unsigned long id,len;
} ENDREC;
ENDREC endrec;
typedef struct {
unsigned long id,len;
long ix,x,xset,iy,y,yset;
} POSREC;
POSREC posrec;
typedef struct {
unsigned long id,len;
unsigned long nev;
unsigned short data[NCH*2];
} EVTREC;
EVTREC evtrec;
runrec.id = RUNREC_ID;
runrec.len = sizeof(runrec);
runrec.fver = 0x10000;
runrec.nch = NCH;
GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);//runrec.xy = 1;
endrec.id = ENDREC_ID;
endrec.len = sizeof(endrec);
posrec.id = POSREC_ID;
posrec.len = sizeof(posrec);
evtrec.id = EVTREC_ID;
evtrec.len = sizeof(evtrec);
/*
printf("<<<<<<<<<<<runrec.len %d\n",runrec.len);
printf("<<<<<<<<<<<endrec.len %d\n",endrec.len);
printf("<<<<<<<<<<<posrec.len %d\n",posrec.len);
printf("<<<<<<<<<<<evtrec.len %d\n",evtrec.len);
printf("<<<<<<<<<<<siye evtrec.len %d\n",sizeof(evtrec.len));
printf("<<<<<<<<<<<siye evtrec.id %d\n",sizeof(evtrec.id));
printf("<<<<<<<<<<<siye evtrec.nev %d\n",sizeof(evtrec.nev));
printf("<<<<<<<<<<<siye evtrec.data %d\n",sizeof(evtrec.data)); */
cres = 0;
GetCtrlVal (p1h, P1_NEVE, &runrec.nev);
GetCtrlVal (p1h, P1_PEDESTAL, &runrec.ped);
GetCtrlVal (p1h, P1_NX, &runrec.nx);
GetCtrlVal (p1h, P1_XSTEP, &runrec.dx);
GetCtrlVal (p1h, P1_XMIN, &runrec.x0);
GetCtrlVal (p1h, P1_NY, &runrec.ny);
GetCtrlVal (p1h, P1_YSTEP, &runrec.dy);
GetCtrlVal (p1h, P1_YMIN, &runrec.y0);
GetCtrlVal (p1h, P1_NMIN, &step_minutes);
GetSystemTime(&start_hours, &start_minutes, &start_seconds);
//cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds;
end_time_s = cur_time_s + step_minutes*60;
printf("START:%2d-%2d-%2d (cur_time = %u s, end_time = %u s)\n", start_hours
, start_minutes
, start_seconds
, cur_time_s
, end_time_s
);
GetCtrlVal (p1h, P1_DSAVE, &dsave);
if (dsave) {
GetCtrlVal (p1h, P1_DFILE, dfile0);
fev=0;
fcount=1;
GetCtrlVal (p1h, P1_NEWF, &fmax);
fmax*=1000000;//fmax in Mega Bytes
}
GetCtrlVal (p1h, P1_SUPR, &supr0);
if (supr0) {
GetCtrlVal (p1h, P1_TDCMIN, &tdcmin);
GetCtrlVal (p1h, P1_FRAC, &frac);
}
#ifdef USE_DAQ
#ifdef VADC_ADDR1
V965_map (VADC1, VADC_ADDR1, 1);
V965_init (VADC1, runrec.ped);
for(i=0;i<2*16;i++) {
dum16=0;
WIENVME_VME_A24D16_W(VADC_ADDR1 + 0x1080 + i*2, &dum16);
//printf("writing threshold to addr = 0x%X\n", 0x1080 + i*2);
}
#endif
#ifdef VADC_ADDR2
V965_map (VADC2, VADC_ADDR2, 1);
V965_init (VADC2, runrec.ped);
for(i=0;i<2*16;i++) {
dum16=0;
WIENVME_VME_A24D16_W(VADC_ADDR2 + 0x1080 + i*2, &dum16);
//printf("writing threshold to addr = 0x%X\n", 0x1080 + i*2);
}
#endif
#ifdef USE_CAEN_TDC
dum=0; WIENVME_VME_A24D16_W(TDC_ADDR+0x1016, &dum); Delay(0.01);
//pair(00)/trailing(01)/leading(10)/both(11) edge
dum=0x2200; V1290_writeOC(&dum);
GetCtrlVal (p1h, P1_TDC_EDGE, &dum);
V1290_writeOC(&dum);
#endif
#ifdef USE_CAMAC
BZ(&cres);
CAMAC_cccz(&cres);
CAMAC_cccc(&cres);
CREM_I(&cres);
//CSSA_R(NGL,0,25,&cres);
#ifdef NPIO
//CAEN C 219 Progammable I/O
CSSA_R(NPIO, 0, 9, &dum);//init
//dum=6; //(OUTPUT, POSITIVE, NORMAL, TRANSPARENT)
dum=2; //(OUTPUT, NEGATIVE, NORMAL, TRANSPARENT)
CSSA_W(NPIO, 0, 17, &dum);
Delay(0.01);
#endif
Delay(0.01);
#ifdef NTDCJ1
CSSA_R(NTDCJ1,0,9,&cres);
printf("CSSA_R(NTDCJ1,0,9,&cres)=0x%0x\n", cres
);
CSSA_R(NTDCJ1,0,26,&cres);
CSSA_R(NTDCJ1,1,26,&cres);
printf("CSSA_R(NTDCJ1,0,26,&cres)=0x%0x\n", cres
);
#endif
#ifdef NTDCJ2
CSSA_R(NTDCJ2,0,9,&cres);
printf("CSSA_R(NTDCJ2,0,9,&cres)=0x%0x\n", cres
);
CSSA_R(NTDCJ2,0,26,&cres);
printf("CSSA_R(NTDCJ2,0,26,&cres)=0x%0x\n", cres
);
#endif
#endif
#endif
if (dsave) {
sprintf(dfile
,"%s_file%02d.dat",dfile0
,fcount
);
/*
// checks if file already exists and prompts user for action
if( fopen(dfile, "r") != NULL ) {
popupret = GenericMessagePopup("Warrning!", "File already exists!",
"New file", "Overwrite", "Cancel", 0, 1, 0,
VAL_GENERIC_POPUP_BTN1, VAL_GENERIC_POPUP_BTN1, VAL_GENERIC_POPUP_BTN3);
//printf("GenericMessagePopup return val = %d\n", popupret);
switch( popupret ) {
case 1: // "New file"
printf("last char = %c\n", dfile[strlen(dfile0)-1]);
break;
case 2: // "Overwrite"
break;
default: // "Cancel"
daq_on=0;
SetCtrlVal (p1h, P1_DAQ, daq_on);
return 0;
}
}
*/
fp
= fopen (dfile
, "wb");
status
= fwrite (&runrec
, 1, runrec.
len, fp
);
}
if (supr0) {
fseed
= runrec.
time & 0x7fffffff;
Uniform (1, fseed, &fracg);
}
for (posrec.iy=0;posrec.iy<runrec.ny;posrec.iy++) {
posrec.yset=runrec.y0+posrec.iy*runrec.dy;
#ifdef USE_MIKRO_Y
// printf("MIKRO_MoveTo (2, y);%d\n",y);
MIKRO_MoveTo (MIKRO_Y, posrec.yset);
// printf("->MIKRO_MoveTo (2, y);%d\n",y);
#endif
SetCtrlVal (p1h, P1_Y, posrec.yset);
SetCtrlVal (p1h, P1_IY, posrec.iy);
for (posrec.ix=0;posrec.ix<runrec.nx;posrec.ix++) {
posrec.xset=runrec.x0+posrec.ix*runrec.dx;
#ifdef USE_MIKRO
// printf("MIKRO_MoveTo (1, x);%d\n",x);
MIKRO_MoveTo (MIKRO_X, posrec.xset);
// printf("->MIKRO_MoveTo (1, x);%d\n",x);
#endif
SetCtrlVal (p1h, P1_X, posrec.xset);
SetCtrlVal (p1h, P1_IX, posrec.ix);
if (dsave) {
/*if (fmax && (ftell(fp) > fmax)) {
fcount+=1;
sprintf(dfile,"%s_file%02d.dat",dfile0,fcount);
fclose(fp);
fp = fopen (dfile, "wb");
} */
status
= fwrite (&posrec
, 1, posrec.
len, fp
);
}
// clear the plots
for (j=0;j<NCH;j++) {
for (i=0;i<MAXCH_TDC;i++){
dtdc[j][0][i]=0;
dtdc[j][1][i]=0;
}
for (i=0;i<MAXCH_QDC;i++){
dadc[j][0][i]=0;
dadc[j][1][i]=0;
}
}
evtrec.nev=1;
do {
for (j=0;j<NCH;j++)
for (i=0;i<4;i++)
aa[j][i]=0;
// if((neve%1000)==0) printf("Events %ld\n",neve);
#ifdef USE_DAQ
ndat = 0;
# ifdef VADC_ADDR1
ndat=V965_clear(VADC1);
# endif
# ifdef VADC_ADDR2
ndat=V965_clear(VADC2);
# endif
# ifdef USE_CAMAC
wait_loop(IWAIT);
# ifdef NTDCJ1
CSSA_R(NTDCJ1,0,9,&cres);
# endif
# ifdef NTDCJ2
CSSA_R(NTDCJ2,0,9,&cres);
# endif
#ifdef USE_CAEN_TDC
dum=0; WIENVME_VME_A24D16_W(TDC_ADDR+0x1016, &dum); //Delay(0.01);
#endif
// Delay(0.001);
wait_loop(IWAIT);
//CSSA_R(NGL,0,10,&cres);
# ifdef NPIO
dum=0x0;
CSSA_W(NPIO, 0, 16, &dum);
//printf("PIO VETO OFF\n");
# endif
# endif
// Waiting for LAM
ntics=0;
//while( (!(V965_status(VADC1)&0x1)) && (ntics<2) && daq_on );
/*
//tdcbuff=1;
#ifdef USE_CAEN_TDC
//dum=0x0; PCIWIENVME_VME_read16(hHandle24, 1, TDC_ADDR+0x1002, &dum);//Status Word
do {
dum16=0x0; WIENVME_VME_A24D16_R(TDC_ADDR+0x1002, &dum16); Delay(0.01);
//printf("tdc [%d] dum16 = %d | dum & 0x1 = %d | daq_on = %d\n", evtrec.nev, dum16, dum16 & 0x1, daq_on);
//tdcbuff=dum16 & 0x1;//LAM = last bit
} while( (!(dum16 & 0x1)) && daq_on );
#endif
do {
//dum16=0x0; WIENVME_VME_A24D16_R(VADC_ADDR1+0x100E, &dum16); Delay(0.01);
//printf("adc [%d] dum16 = %d | dum & 0x1 = %d | VADC_ADDR1 = %x\n", evtrec.nev, dum16, dum16 & 0x1, VADC_ADDR1);
}while( (!(V965_status(VADC1)&0x1)) && daq_on );
*/
//kobayashi
int tdc_lam, adc_lam;
#ifdef USE_CAEN_TDC
tdc_lam = 0;
#else
tdc_lam = 1;
#endif
#ifdef VADC_ADDR1
adc_lam = 0;
#else
adc_lam = 1;
#endif
//printf(">>>> LAM: ");
#ifdef USE_CAEN_TDC
do {
dum16=0x0; WIENVME_VME_A24D16_R(TDC_ADDR+0x1002, &dum16); //Delay(0.01);
tdc_lam = dum16 & 0x1;
} while( (!tdc_lam) && daq_on );
//printf("tdc_lam = %d | ", tdc_lam);
#endif
#ifdef VADC_ADDR1
do {
adc_lam = V965_status(VADC1)&0x1;
} while( (!adc_lam) && daq_on );
//printf("adc_lam = %d", adc_lam);
#endif
//printf("\n");
# ifdef NPIO
dum=0x1;
CSSA_W(NPIO, 0, 16, &dum);
//printf("PIO VETO ON\n");
# endif
wait_loop(2000);
if (!daq_on) break;
if (ntics>=2) {
# ifdef VADC_ADDR1
ndat=V965_read (VADC1, &data[0]);
# endif
# ifdef VADC_ADDR2
ndat=V965_read (VADC2, &data2[0]);
# endif
if(step_minutes > 0) {
GetSystemTime(&start_hours, &start_minutes, &start_seconds);
//cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds;
if(cur_time_s >= end_time_s) {
end_time_s = cur_time_s + step_minutes*60;
printf("STEP (lam):%2d-%2d-%2d @ %d\n", start_hours
, start_minutes
, start_seconds
, posrec.
xset);
break;
}
}
continue;
}
for (i=0; i<NCH; i++) {
adc=0;
wait_loop(IWAIT);
# ifdef USE_CAMAC
# ifdef NTDCJ1
if(i<8) {
CSSA_R(NTDCJ1,tdcmap[i],0,&adc);
//printf("TDC=0x%04X\n",adc);
adc=adc&0xfff;
aa[i][0]=adc;
dtdc[i][0][adc]+=1;
}
# endif
# ifdef NTDCJ2
if(8<=i && i<16) {
CSSA_R(NTDCJ2,tdcmap[i],0,&adc);
//printf("TDC=0x%04X\n",adc);
adc=adc&0xfff;
//offset for channel mapping (0-9 = MCPPMT5, 0-7 = tdc, 8,9=adc only)
aa[i+2][0]=adc;
dtdc[i+2][0][adc]+=1;
}
# endif
# endif
}
// TDC
#ifdef USE_CAEN_TDC
int dbg_tdc_t = 0;
int dbg_tdc_f = 0;
FILE *fpout=NULL;
if(dbg_tdc_f
) fpout
=fopen("TDCout.txt", "at");
//number of events
//tdcneve=0; PCIWIENVME_VME_read16(hHandle24, 1, TDC_ADDR+0x1020, &tdcneve);
tdcneve=0; WIENVME_VME_A24D16_R(TDC_ADDR+0x1020, &tdcneve);
unsigned long triggneve=0; WIENVME_VME_A24D32_R(TDC_ADDR+0x101C, &triggneve);
if(dbg_tdc_t
) printf("\n [%d] tdcneve = %u | triggneve = %u\n", evtrec.
nev, tdcneve
, triggneve
);
if(dbg_tdc_f
) fprintf(fpout
, "\n [%d] tdcneve = %u | triggneve = %u\n", evtrec.
nev, tdcneve
, triggneve
);
int tdcchhit[32];
unsigned short l=0;
for (i=0;i<NCH_TDC;i++)
tdc_ev_c[i]=0;
while(l < tdcneve)
{
//dum=0x0; PCIWIENVME_VME_read32(hHandle24, 1, TDC_ADDR+0x0, &dum);//read
dum32=0x0; WIENVME_VME_A24D32_R(TDC_ADDR+0x0, &dum32);
if(dbg_tdc_t
) printf(">>>>>>>>>>>>> l = %d | %x\n", l
, dum32
);
if(dbg_tdc_f
) fprintf(fpout
, ">>>>>>>>>>>>> l = %d | %x\n", l
, dum32
);
switch(dum32 & 0xf8000000)
{
case 0x40000000://01000 = Global Header
for (int k=0;k<32;k++) tdcchhit[k]=0;
if(dbg_tdc_t
) printf(">>> GLOBAL HEADER | event count = %d\n", (dum32
& 0x7FFFFFF) >> 5);
if(dbg_tdc_f
) fprintf(fpout
, ">>> GLOBAL HEADER | event count = %d\n", (dum32
& 0x7FFFFFF) >> 5);
break;
case 0x8000000://00001 = TDC Header
if(dbg_tdc_t
) printf(">>> TDC HEADER | Event ID =%d | Bunch ID = %d\n", (dum32
& 0xFFFFFF) >> 12, (dum32
& 0xFFF));
if(dbg_tdc_f
) fprintf(fpout
, ">>> TDC HEADER | Event ID =%d | Bunch ID = %d\n", (dum32
& 0xFFFFFF) >> 12, (dum32
& 0xFFF));
break;
case 0x00000000://00000 = TDC Measurement
if(dbg_tdc_t
) printf(">>> TDC Measurement !!!!!!!! >>> CH = %d | edge = %d | ev = %d\n", (dum32
& 0x3FFFFFF) >> 21, (dum32
& 0x7FFFFFF) >> 26, dum32
& 0x1FFFFF);
if(dbg_tdc_f
) fprintf(fpout
, ">>> TDC Measurement !!!!!!!! >>> CH = %d | edge = %d | ev = %d\n", (dum32
& 0x3FFFFFF) >> 21, (dum32
& 0x7FFFFFF) >> 26, dum32
& 0x1FFFFF);
//TDC Channel = 0000 0x<...Channel...>x 20*x
tdcch = tdcmap[(dum32 & 0x3FFFFFF) >> 21];
//TDC Measurement = 0000 0xxx xxx<...TDC...>
unsigned long tdc_ev = dum32 & 0x1FFFFF;
int tdc_edge = (dum32 & 0x1) >> 26;
/*
adc = (int)(tdc_ev/4.0); // 21 bit -> 16 bit ???
int adcs = (int)(adc/16.0);
*/
/*
int tdc_ev_offset = 31000;
adc = tdc_ev - tdc_ev_offset;
int adcs = 0;
if(0 < adc) adcs = adc;
if(4095 < adc) adcs = 4095;
*/
if(dbg_tdc_t
) printf(">>> tdcch = %d | tdc_ev = %d | raw = %d\n", tdcch
, tdc_ev
, dum32
);
if(dbg_tdc_f
) fprintf(fpout
, ">>> tdcch = %d | tdc_ev = %d | raw = %d\n", tdcch
, tdc_ev
, dum32
);
if(l==0)
if(tdcchhit[tdcch]==0)
if(tdc_edge==0) {
if(tdcch < NCH_TDC) {
aa[tdcch][0]=tdc_ev;
if(tdcch < MAXCH_TDC) {
dtdc[tdcch][0][tdc_ev]+=1;
}
}
}
tdcchhit[tdcch]=1;
break;
case 0x18000000://00011 = TDC Trailer
if(dbg_tdc_t
) printf(">>> TDC Trailer | Event ID =%d\n", (dum32
& 0xFFFFFF) >> 12);
if(dbg_tdc_f
) fprintf(fpout
, ">>> TDC Trailer | Event ID =%d\n", (dum32
& 0xFFFFFF) >> 12);
break;
case 0x20000000://00100 = TDC Error
if(dbg_tdc_t
) printf(">>> TDC Error = %d\n", dum32
& 0x7FFF);
if(dbg_tdc_f
) fprintf(fpout
, ">>> TDC Error = %d\n", dum32
& 0x7FFF);
break;
case 0x88000000://10001 = Extended Trigger Time Tag
if(dbg_tdc_t
) printf(">>> Extended Trigger Time Tag \n");
if(dbg_tdc_f
) fprintf(fpout
, ">>> Extended Trigger Time Tag \n");
break;
case 0xC0000000://11000 = Filler
if(dbg_tdc_t
) printf(">>> Filler \n");
if(dbg_tdc_f
) fprintf(fpout
, ">>> Filler \n");
break;
case 0x80000000://10000 = The Trailer
num_hits=((dum32 & 0x1FFFFF) >> 5) - 2;
l++;
if(dbg_tdc_t
) printf(">>> The Trailer | status bits: %d%d%d | num_hits = %d\n", (dum32
>> 26) % 0x1, (dum32
>> 25) % 0x1, (dum32
>> 24) % 0x1, num_hits
);
if(dbg_tdc_f
) fprintf(fpout
, ">>> The Trailer | num_hits = %d\n", num_hits
);
break;
/*
case 0x00000000://00000 = TDC Measurement
//TDC Channel = 0000 0x<...Channel...>x 20*x
tdcch = tdcmap[(dum32 & 0x3FFFFFF) >> 21];
//TDC Measurement = 0000 0xxx xxx<...TDC...>
tdc_ev = dum32 & 0x1FFFFF;
if((tdcch < NCH_TDC)&&(tdc_ev < MAXCH_TDC)&&(tdc_ev_c[tdcch] < EVMAX))
{
dtdc[tdcch][tdc_ev_c[tdcch]][tdc_ev]++;
//atdc[tdcch][tdc_ev_c[tdcch]++] = tdc_ev;
}
//#define CH_TRG 0
//#define CH_SIPM 1
//#define CH_PMT 2
//for(i=1;i<NCH_TDC;i++)
// if(tdc_ev_c[CH_TRG] && tdc_ev_c[i])
// {
// abstime=atdc[i][tdc_ev_c[i]-1] - atdc[CH_TRG][tdc_ev_c[CH_TRG]-1] + 30000;
// if((0 <= abstime) && (abstime < MAXCH_TDC))
// dtdc2[i][tdc_ev_c[i]-1][abstime]++;
// }
break;
case 0x80000000://10000 = Global Trailer
//Word Count = bit 31...21< Word Count: 20 ... 5 > 4...0
num_hits=((dum32 & 0x1FFFFF) >> 5) - 2;
l++;
break;
case 0xC0000000://11000 = Filler
l=tdcneve;//exit while loop
break; */
default:
break;
}
}
#endif
# ifdef VADC_ADDR1
ndat=V965_read (VADC1, &data[0]);
//printf("<<<<<ndat %d\n",ndat);
for (i=0; i<ndat; i++) {
dtype=(data[i]>>25)&0x3;
if (dtype==0) {
ch=adcmap[(data[i]>>17)&0xf];
if (ch<NCH) {
rg=(data[i]>>16)&0x1;
adc=data[i]&0xfff;
aa[ch][rg+2]=adc;
dadc[ch][rg][adc]+=1;
//printf("<<<<<ch %d rg %d adc %d map %d\n",ch,rg,adc, (data[i]>>17)&0xf);
}
}
}
//printf("VADC_ADDR1 READ\n");
# endif
// printf("======================================\n");
# ifdef VADC_ADDR2
ndat=V965_read (VADC2, &data2[0]);
for (i=0; i<ndat; i++) {
dtype=(data2[i]>>25)&0x3;
if (dtype==0) {
ch=adcmap[NCH + ((data2[i]>>17)&0xf)];
//printf("ADCCH = %2d , adcmap[ADCCH] = %2d , ADC = %d\n", NCH + ((data2[i]>>17)&0xf), ch, data2[i]&0xfff);
if (ch<NCH) {
rg=(data2[i]>>16)&0x1;
adc=data2[i]&0xfff;
aa[ch][rg+2]=adc;
dadc[ch][rg][adc]+=1;
}
}
}
# endif
#else /* generate test data */
if (!daq_on) break;
for (i=0;i<NCH;i++) {
adc=100+evtrec.nev%200;
dtdc[i][1][adc]+=1;
dadc[i][1][adc]+=1;
aa[i][0]=adc;
aa[i][2]=adc;
adc=200+evtrec.nev%400;
dtdc[i][0][adc]+=1;
dadc[i][0][adc]+=1;
aa[i][1]=adc;
aa[i][3]=adc;
}
#endif
esave=dsave;
if (supr0 && esave) {
Uniform (1, -1, &fracg);
if (fracg > frac) {
esave=0;
for (i=0;i<NCH;i++) {
if (aa[i][0] >= tdcmin) esave=1;
}
}
}
if (esave) {
// time (&evtrec.time);
//GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);
for (i=0;i<NCH;i++) {
evtrec.data[i] = aa[i][0]; //TDC
//printf(">>> TDC Measurement !!!!!!!! >>> CH = %d | ev = %d\n", i, evtrec.data[i]);
//if(i==0)
// evtrec.data[i+NCH] = aa[i][2]; //ADC High Range = 2 , low = 3
//else
evtrec.data[i+NCH] = aa[i][2]; //ADC High Range = 2 , low = 3
// printf(">>> ADC Measurement !!!!!!!! >>> CH = %d | ev = %d \n", i, evtrec.data[i+NCH]);
//printf("<<<<<<<<<<<evtrec.len %d\n",evtrec.len);
}
status
= fwrite (&evtrec
, 1, evtrec.
len, fp
);
}
//if (!(evtrec.nev%1000))
SetCtrlVal (p1h, P1_CEVE, evtrec.nev);
if (dsave&&(!(evtrec.nev%1000))) {
if (fmax
&& (ftell(fp
) > fmax
)) {
status
= fwrite (&endrec
, 1, endrec.
len, fp
);
fcount+=1;
sprintf(dfile
,"%s_file%02d.dat",dfile0
,fcount
);
fp
= fopen (dfile
, "wb");
printf(" Opened file %s\n", dfile
);
}
}
if(step_minutes > 0) {
GetSystemTime(&start_hours, &start_minutes, &start_seconds);
//cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds;
if(cur_time_s >= end_time_s) {
end_time_s = cur_time_s + step_minutes*60;
printf("STEP (nev):%2d-%2d-%2d @ %d\n", start_hours
, start_minutes
, start_seconds
, posrec.
xset);
break;
}
}
} while (evtrec.nev++<runrec.nev);
if (!daq_on) break;
}
if (!daq_on) break;
}
if (dsave) {
status
= fwrite (&endrec
, 1, endrec.
len, fp
);
}
daq_on=0;
SetCtrlVal (p1h, P1_DAQ, daq_on);
SetCtrlVal (p1h, P1_CEVE, evtrec.nev);
return 0;
}
//=================================================================================
//=================================================================================
//=================================================================================
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
int i,j,status,refon;
long int xpos, ypos, zpos;
char dfile[300];
FILE *fp;
int dum;
unsigned short dum16;
typedef struct {
unsigned long id,len;
unsigned long nev,nch,ped,xy;
long nx,x0,dx,ny,y0,dy;
} RUNREC;
RUNREC runrec;
if (InitCVIRTE (hInstance, 0, 0) == 0)
return -1; /* out of memory */
SetSleepPolicy(VAL_SLEEP_MORE);
CmtNewThreadPool (MAX_THREADS, &poolHandle);
SetStdioWindowOptions (1000, 0, 0);
//SetStdioWindowSize (150, 600);
//SetStdioWindowPosition (825, 250);
SetStdioWindowSize (900, 600);
//SetStdioWindowPosition (20, 800);
SetStdioWindowPosition (20, 1100);
#ifdef USE_DAQ
WIENVME_VME_START(NULL);
#endif
#ifdef USE_CAEN_TDC
V1290_init();
#endif
#ifdef USE_MIKRO
MIKRO_Open (MIKRO_COM);
MIKRO_Init (MIKRO_X,0);
#ifdef USE_MIKRO_Y
MIKRO_Init (MIKRO_Y,0);
#endif
#ifdef USE_MIKRO_Z
MIKRO_Init (MIKRO_Z,0);
#endif
#endif
if ((p1h = LoadPanel (0, "l2d_ui.uir", P1)) < 0) return -1;
DisplayPanel (p1h);
SetCtrlAttribute (p1h, P1_PLCH, ATTR_MAX_VALUE, NCH-1);
GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);
SetCtrlVal(p1h, P1_ADCHL, runrec.xy-2);
QueueUserEvent (1000, p1h, P1_RESET);
do {
GetUserEvent (1, &pID, &rID);
switch (rID) {
case P1_TIMER:
ntics+=1;
GetCtrlVal (p1h, P1_REFON, &refon);
if (refon) update_plots();
break;
case P1_REFRESH:
update_plots();
break;
case P1_DAQ:
GetCtrlVal (p1h, P1_DAQ, &daq_on);
if (daq_on) {
CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID);
} else {
CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID,
OPT_TP_PROCESS_EVENTS_WHILE_WAITING);
CmtReleaseThreadPoolFunctionID (poolHandle, tfID);
# ifdef NPIO
dum=0x0;
CSSA_W(NPIO, 0, 16, &dum);
//printf("PIO VETO OFF\n");
# endif
}
break;
case P1_ZSET:
if (!daq_on) {
GetCtrlVal (p1h, P1_ZSET, &zpos);
#ifdef USE_MIKRO_Z
MIKRO_MoveTo (MIKRO_Z, zpos);
#endif
}
break;
case P1_REREAD:
if (!daq_on) {
status = FileSelectPopup ("", "*.dat", ".dat",
"Izberi datoteko s podatki",
VAL_LOAD_BUTTON, 0, 0, 1, 0, dfile);
if (status==1) {
fp
= fopen (dfile
, "rb");
status
= fread (&runrec
, 1, sizeof(runrec
), fp
);
if (runrec.id==1) {
SetCtrlVal (p1h, P1_NX, runrec.nx);
SetCtrlVal (p1h, P1_XSTEP, runrec.dx);
SetCtrlVal (p1h, P1_XMIN, runrec.x0);
SetCtrlVal (p1h, P1_NY, runrec.ny);
SetCtrlVal (p1h, P1_YSTEP, runrec.dy);
SetCtrlVal (p1h, P1_YMIN, runrec.y0);
SetCtrlVal (p1h, P1_NEVE, runrec.nev);
}
}
}
break;
case P1_MGET:
#ifdef USE_MIKRO
MIKRO_GetPosition(MIKRO_X,&xpos);
Delay(0.01);
SetCtrlVal (p1h, P1_X, xpos);
#ifdef USE_MIKRO_Y
MIKRO_GetPosition(MIKRO_Y,&ypos);
Delay(0.01);
SetCtrlVal (p1h, P1_Y, ypos);
#endif
#ifdef USE_MIKRO_Z
MIKRO_GetPosition(MIKRO_Z,&zpos);
Delay(0.01);
SetCtrlVal (p1h, P1_Z, zpos);
#endif
#endif
break;
case P1_HO:
if (!daq_on) {
SetWaitCursor (1);
#ifdef USE_MIKRO
MIKRO_ReferenceMove (MIKRO_X);
#ifdef USE_MIKRO_Y
MIKRO_ReferenceMove (MIKRO_Y);
#endif
#ifdef USE_MIKRO_Z
MIKRO_ReferenceMove (MIKRO_Z);
#endif
#endif
SetWaitCursor (0);
}
break;
case P1_RESET:
for (j=0;j<NCH;j++) {
for (i=0;i<MAXCH_TDC;i++){
dtdc[j][0][i]=0;
dtdc[j][1][i]=0;
}
for (i=0;i<MAXCH_QDC;i++){
dadc[j][0][i]=0;
dadc[j][1][i]=0;
}
}
update_plots();
break;
case P1_TDCLOG:
GetCtrlVal (p1h, P1_TDCLOG, &status);
SetCtrlAttribute (p1h, P1_TDC, ATTR_YMAP_MODE, status);
update_plots();
break;
case P1_ADCLOG:
GetCtrlVal (p1h, P1_ADCLOG, &status);
SetCtrlAttribute (p1h, P1_ADC, ATTR_YMAP_MODE, status);
update_plots();
break;
case P1_PIO_ON:
# ifdef NPIO
dum=0x1;
CSSA_W(NPIO, 0, 16, &dum);
# endif
break;
case P1_PIO_OFF:
# ifdef NPIO
dum=0x0;
CSSA_W(NPIO, 0, 16, &dum);
# endif
break;
case P1_TDCCLEAR:
#ifdef USE_CAEN_TDC
dum16=0x0; WIENVME_VME_A24D16_W(TDC_ADDR+0x1014, &dum16); Delay(0.1);
dum16=0x0; WIENVME_VME_A24D16_W(TDC_ADDR+0x1016, &dum16); Delay(0.1);
dum16=0x0; WIENVME_VME_A24D16_W(TDC_ADDR+0x1018, &dum16); Delay(0.1);
#endif
break;
case P1_TDCMIKRO:
#ifdef USE_CAEN_TDC
dum
=0x0000; WIENVME_VME_A24D16_R
(TDC_ADDR
+ 0x1030, &dum
); Delay
(0.01); printf("Micro handshake = %x\n", dum
);
#endif
break;
case P1_TDCINIT:
#ifdef USE_CAEN_TDC
//V1290_init() ;
////Maximum number of hits per event
printf("V1290_writeOC(0x3300)\n");
dum=0x3300; V1290_writeOC(&dum); Delay(0.1);
//dum=0x0001; V1290_writeOC(&dum); Delay(0.1);
printf("V1290_writeOC(0x1000)\n");
dum=0x1000; V1290_writeOC(&dum); Delay(0.1);
printf("V1290_writeOC(0x3400)\n");
dum=0x3400; V1290_writeOC(&dum); Delay(0.1);
dum=0x0;
if(V1290_readOC(&dum) == -1)
printf("V1290_readOC timeout!!!\n");
else {
Delay(0.1);
printf("TDC Maximum number of hits per event = %d\n", dum
);
}
#endif
break;
case P1_TDCTEST:
#ifdef USE_CAEN_TDC
/*
for (i=0x4000;i<0x4050;i+=4) {
dum=0;
WIENVME_VME_A24D16_R(TDC_ADDR+i, &dum);
printf("0x%04X -> 0x%04X\n",i,dum);
}
for (i=0x4080;i<0x4088;i+=4) {
dum=0;
WIENVME_VME_A24D16_R(TDC_ADDR+i, &dum);
printf("0x%04X -> 0x%04X\n",i,dum);
}
dum=0;i=0x1026;
WIENVME_VME_A24D16_R(TDC_ADDR+i, &dum);
printf("0x%04X -> 0x%04X\n",i,dum);
dum=0x0200; V1290_writeOC(&dum);
dum=0x0; V1290_readOC(&dum); printf("dum = %04X\n", dum&0xFFFF);
*/
dum=0x4500; V1290_writeOC(&dum);
dum
=0x0; V1290_readOC
(&dum
); printf("Ch Enable 1 = %04X\n", dum
&0xFFFF);
dum
=0x0; V1290_readOC
(&dum
); printf("Ch Enable 2 = %04X\n", dum
&0xFFFF);
dum=0x1600; V1290_writeOC(&dum);
dum
=0x0000; V1290_readOC
(&dum
); printf("Match window Width = %d\n", dum
&0xFFFF);
dum
=0x0000; V1290_readOC
(&dum
); printf("Window Offset = %d\n", dum
&0xFFFF);
dum
=0x0000; V1290_readOC
(&dum
); printf("Extra Search Window = %d\n", dum
&0xFFFF);
dum
=0x0000; V1290_readOC
(&dum
); printf("Reject Margin = %d\n", dum
&0xFFFF);
dum
=0x0000; V1290_readOC
(&dum
); printf("Trigger Subtraction = %d\n", dum
&0xFFFF);
dum=0x3400; V1290_writeOC(&dum);
dum
=0x0000; V1290_readOC
(&dum
); printf("TDC Maximum number of hits per event = %d\n", dum
);
/*
// test
dum16=0x0; WIENVME_VME_A24D16_R(TDC_ADDR+0x1026, &dum16); Delay(0.1);
printf("WIENVME_VME_A24D16_R(TDC_ADDR+0x1026, dum16) -> %d\n", dum16);
//dum=0x20; WIENVME_VME_A24D16_W(TDC_ADDR+0x1000, &dum); Delay(0.1);
dum=0x0; WIENVME_VME_A24D16_R(TDC_ADDR+0x1000, &dum); Delay(0.1);
printf("WIENVME_VME_A24D16_R(TDC_ADDR+0x1000, dum) -> %d\n", dum);
dum=0x0; WIENVME_VME_A24D16_R(TDC_ADDR+0x1002, &dum); Delay(0.1);
printf("WIENVME_VME_A24D16_R(TDC_ADDR+0x1002, dum) -> %d\n", dum);
int tdcneve=0; WIENVME_VME_A24D16_R(TDC_ADDR+0x1020, &tdcneve);
unsigned long triggneve=0; WIENVME_VME_A24D32_R(TDC_ADDR+0x101C, &triggneve);
printf("tdcneve = %d | triggneve = %u\n", tdcneve, triggneve);
dum=0x4500; V1290_writeOC(&dum);
dum=0x0; V1290_readOC(&dum); printf("Channel enable mask word 1 = %x\n", dum);
dum=0x0; V1290_readOC(&dum); printf("Channel enable mask word 2 = %x\n", dum);
//dum=0x4500; WIENVME_VME_A24D16_W(TDC_ADDR+0x102E, &dum); Delay(0.1);
//dum=0x0000; WIENVME_VME_A24D16_R(TDC_ADDR+0x102E, &dum); Delay(0.1); printf("Channel enable mask word 1 = %x\n", dum);
//dum=0x0000; WIENVME_VME_A24D16_R(TDC_ADDR+0x102E, &dum); Delay(0.1); printf("Channel enable mask word 2 = %x\n", dum);
*/
//dum16=0x0; WIENVME_VME_A24D16_R(VADC_ADDR1+0x1000, &dum16); Delay(0.1);
//printf("WIENVME_VME_A24D16_R(VADC_ADDR1+0x1000, dum16) -> %x\n", dum16);
/*
dum=0xC200; V1290_writeOC(&dum); printf("V1290_writeOC(&dum) -> %d\n", dum);
dum=0x0; V1290_readOC(&dum); printf("1. V1290_readOC(&dum) -> %d\n", dum);
dum=0x0; V1290_readOC(&dum); printf("2. V1290_readOC(&dum) -> %d\n", dum);
dum=0x0; V1290_readOC(&dum); printf("3. V1290_readOC(&dum) -> %d\n", dum);
dum=0x0; V1290_readOC(&dum); printf("4. V1290_readOC(&dum) -> %d\n", dum);
dum16=0xC200; WIENVME_VME_A24D16_W(TDC_ADDR+0xC200, &dum16); Delay(0.1);
printf("WIENVME_VME_A24D16_W(TDC_ADDR+0xC200, dum) -> %x\n", dum16);
dum16=0x0; WIENVME_VME_A24D16_R(TDC_ADDR+0xC200, &dum16); Delay(0.1);
printf("1. WIENVME_VME_A24D16_R(TDC_ADDR+0xC200, dum) -> %x\n", dum16);
dum16=0x0; WIENVME_VME_A24D16_R(TDC_ADDR+0xC200, &dum16); Delay(0.1);
printf("2. WIENVME_VME_A24D16_R(TDC_ADDR+0xC200, dum16) -> %x\n", dum16);
dum16=0x0; WIENVME_VME_A24D16_R(TDC_ADDR+0xC200, &dum16); Delay(0.1);
printf("3. WIENVME_VME_A24D16_R(TDC_ADDR+0xC200, dum16) -> %x\n", dum16);
dum16=0x0; WIENVME_VME_A24D16_R(TDC_ADDR+0xC200, &dum16); Delay(0.1);
printf("4. WIENVME_VME_A24D16_R(TDC_ADDR+0xC200, dum16) -> %x\n", dum16); */
#endif
break;
#ifdef USE_EASIROC
case P1_ESRC_INIT:
easiroc_Init(erIpAddr, erDAQmode);
break;
case P1_ESRC_F1:
easiroc_fTransmitSC();
break;
case P1_ESRC_F2:
easiroc_fTransmitReadSC();
break;
case P1_ESRC_F3:
easiroc_fAsicInitialize();
break;
case P1_ESRC_F4:
int transmitProbeCh = 0;
GetCtrlVal (p1h, P1_ESRC_PROBE_CH, &transmitProbeCh);
int transmitProbeType = 0;
GetCtrlVal (p1h, P1_ESRC_PROBE_TYPE, &transmitProbeType);
printf("transmitProbeCh = %d | transmitProbeType = %d\n", transmitProbeCh
, transmitProbeType
);
if(easiroc_fTransmitProbe(transmitProbeCh, transmitProbeType) == 0)
printf("\nTransmit Probe SUCESS\n");
break;
case P1_ESRC_DAQ:
char esrcDaqFile[256];
GetCtrlVal (p1h, P1_DFILE, esrcDaqFile);
int esrcDaqNeve = 0;
GetCtrlVal (p1h, P1_NEVE, &esrcDaqNeve);
if(easiroc_fDAQ(esrcDaqFile, esrcDaqNeve) == 0)
printf("\nEASIROC DAQ SUCESS\n");
break;
case P1_ESRC_CLOSE:
easiroc_Close();
break;
#endif
#ifdef USE_UDPCONTROL
case P1_ER_INIT:
SiTCPinit();
SiTCPSetIPPort(IpAddr, tcpport, udpport);
// SiTCPCreateTCPSock();
SiTCPCreateUDPSock();
SenderInit();
Senderudp_send(0x00000012,248); //Set ADC rate to 50Hz
Senderudp_send(0x0000001f,0);
break;
case P1_ER_CLOSE:
SiTCPclose();
SenderClose();
break;
case P1_ER_HV_CONTROL:
GetCtrlVal (p1h, P1_ER_HV_CONTROL_IN, &HV);
HVDAC = HVC_1 * HV + HVC_2; //change HV to DAC bit
printf("P1_ER_HV_CONTROL: HV = %lf | HVDAC = %d\n", HV
, HVDAC
);
Senderudp_send(0x00000010,HVDAC/256);//Set higher 8bit to FPGA reg
Senderudp_send(0x00000011,HVDAC%256);//lower 8bit
Senderudp_send(0x0000001e,1); //Start DAC control
break;
case P1_ER_HV_STATUS:
rd_data = Senderread_madc(3);//Read ADC data
// std::cout <<"MADC_data = "<< rd_data;
rd_data = ADC2HV * rd_data; //convert ADC bit to HV
printf("Bias voltage : %.2lfV\n",rd_data
);
rd_data = Senderread_madc(4);//Read ADC data
//std::cout <<"MADC_data = "<< rd_data;
rd_data = ADC2uA * rd_data; //convert ADC bit to HVcurrent
printf("Bias current : %.2lfuA\n",rd_data
);
break;
#endif
}
} while ((rID != P1_EXIT)||daq_on);
CmtDiscardThreadPool (poolHandle);
DiscardPanel (p1h);
#ifdef USE_MIKRO
MIKRO_Close ();
#endif
#ifdef USE_DAQ
WIENVME_VME_STOP();
#endif
return 0;
}
#ifdef USE_CAEN_TDC
/* ****************** CAEN V1290 TDC Write OPCODE****************** */
int V1290_writeOC(int *write_buffer)
{
//printf(">>> Writing %d\n", *write_buffer);
int wait_count = 0;
int read_buffer = 0;
do
{
// Micro Handshake Register
WIENVME_VME_A24D16_R(TDC_ADDR + 0x1030, &read_buffer); Delay(0.01);
//printf("V1290_writeOC wait_count = %d | read_buffer = %x\n", wait_count, read_buffer);
if(100 < wait_count++)
return -1;
} while (!(read_buffer & 0x1));
// Micro Register
WIENVME_VME_A24D16_W(TDC_ADDR + 0x102E, write_buffer);
return 0;
}
/* ****************** CAEN V1290 TDC Read OPCODE ****************** */
int V1290_readOC(int *return_buffer)
{
int wait_count = 0;
int read_buffer = 0;
do
{
// Micro Handshake Register
WIENVME_VME_A24D16_R(TDC_ADDR + 0x1030, &read_buffer); Delay(0.01);
//printf("V1290_readOC wait_count = %d | read_buffer = %x\n", wait_count, read_buffer);
if(100 < wait_count++)
return -1;
} while (!(read_buffer&0x2));
// Micro Register
WIENVME_VME_A24D16_R(TDC_ADDR + 0x102E, return_buffer);
//printf(">>> Returning %d\n", *return_buffer);
return 0;
}
/* ****************** ******************************************* */
void V1290_init()
{
int dum;
// intialization
dum=0x0; WIENVME_VME_A24D16_W(TDC_ADDR+0x1014, &dum); Delay(0.1);
dum=0x0; WIENVME_VME_A24D16_W(TDC_ADDR+0x1016, &dum); Delay(0.1);
dum=0x0; WIENVME_VME_A24D16_W(TDC_ADDR+0x1018, &dum); Delay(0.1);
//Trigger matching
dum=0x0000; V1290_writeOC(&dum);
// Constraints: tdc_window < tdc_offset <= 4095
// Largest window: 4095-margin;
// units: clock cycles = 25 ns
int tdc_margin = 0;
int tdc_offset = 40;
int tdc_window = tdc_offset - tdc_margin;
//set window width
dum=0x1000; V1290_writeOC(&dum);
dum=tdc_window; V1290_writeOC(&dum);
//set window offset
dum=0x1100; V1290_writeOC(&dum);
dum=-tdc_offset; V1290_writeOC(&dum);
//set extra search margin
dum=0x1200; V1290_writeOC(&dum);
dum=tdc_margin; V1290_writeOC(&dum);
//enable all channels
dum=0x4200; V1290_writeOC(&dum);
//disable all channels
//dum=0x4300; V1290_writeOC(&dum);
//enable first NCH_TDC channels
//for(i=0;i<NCH_TDC;i++)
//{
// dum=0x4000 + i;
// V1290_writeOC(&dum);
//}
//enable used channels
//for(i=0;i<32;i++)
// if(tdcmap[i]<32)
// {
// dum=0x4000 + i;
// V1290_writeOC(&dum);
// }
//enable(0x30xx)/disable(0x31xx) TDC Header and Trailer
dum=0x3100; V1290_writeOC(&dum);
/*
////Maximum number of hits per event (does not work for unknown reason, sets to 0!)
dum=0x3300; V1290_writeOC(&dum);
//dum=0x0001; V1290_writeOC(&dum);
dum=0x1000; V1290_writeOC(&dum);
*/
dum=0x3400; V1290_writeOC(&dum);
dum=0x0000; V1290_readOC(&dum);
printf("TDC Maximum number of hits per event = %d\n", dum
);
//pair(00)/trailing(01)/leading(10)/both(11) edge
dum=0x2200; V1290_writeOC(&dum);
dum=0x1; V1290_writeOC(&dum);
//enable(0x14xx)/disable(0x15xx) substraction of trigger time
dum=0x1400; V1290_writeOC(&dum);
}
#endif
/* ****** vrne (INT!) max vrednost iz arraya od elementa do elementa ****** */
int max_from(int* array, int ifrom, int ito)
{
int i;
int vmax;
vmax = array[ifrom];
for(i=ifrom+1; i<=ito; i++)
if(vmax < array[i])
vmax = array[i];
return vmax;
}