#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Izberi ustrezni interface v meniju projektnega okna
// Options->CompilerDefines (dodaj /DSISVME ali /DWIENVME)
#ifdef USE_DAQ
# ifdef SISVME
# include "sisvme_dll.h"
# endif
# ifdef WIENVME
# include "wienvme_dll.h"
# endif
# ifdef WUSBVME
# include "wusbvme_dll.h"
# endif
#endif
#include "wusbvme_dll.h"
#include <formatio.h>
#include "daq_cvi.h"
#include "CAENV965_DEF.h"
#define USBVME_NAME "VM0120"
#define VERSION 1.0
#define TIMEOUT 3
/* VME modules */
#define CAEN_V792 0x340000 // IJS V792
#define CAEN_V792_1 0x530000 // FMF1 V792
#define CAEN_V792_2 0x630000 // FMF2 V792
#define CAEN_V965 0x350000 // IJS V965
int addr[3]={CAEN_V792,CAEN_V792_1,CAEN_V965 };
int nadc=2;
#define BUFF_L 2048
static int stackwrite[10000];
static int stackdata[10000],stackdump[27000];
/************************************************/
int weight_while(int num)
{
int i, tmp;
for ( i =0; i <num; i++ ) tmp = 0;
return 0;
}
#define WWHILE weight_while(0)
#define TRUE 1
#define FALSE 0
int timer_out;
//struct sigaction oact;
void timerast (int signumber)
{
timer_out = TRUE;
}
void tmlnk (int tout)
{
/*
timer_out = FALSE;
struct sigaction act;
struct itimerval tdelay;
act.sa_handler = timerast;
sigemptyset (&act.sa_mask);
act.sa_flags = 0;
tdelay.it_value.tv_sec = tout / 100;
tdelay.it_value.tv_usec = 10000 * (tout % 100);
tdelay.it_interval.tv_sec = 0;
tdelay.it_interval.tv_usec = 0;
if (sigaction (SIGALRM, &act, &oact) < 0)
{
perror ("sigaction(tmlnk)");
exit (EXIT_FAILURE);
}
if (setitimer (ITIMER_REAL, &tdelay, NULL) < 0)
{
perror ("setitimer(tmlnk)");
exit (EXIT_FAILURE);
}
*/
}
void tmulk ()
{
/*
struct itimerval tdelay;
tdelay.it_value.tv_sec = 0;
tdelay.it_value.tv_usec = 0;
tdelay.it_interval.tv_sec = 0;
tdelay.it_interval.tv_usec = 0;
if (setitimer (ITIMER_REAL, &tdelay, NULL) < 0)
{
perror ("setitimer(tmulk)");
exit (EXIT_FAILURE);
}
if (sigaction (SIGALRM, &oact, NULL) < 0)
{
perror ("sigaction(tmulk)");
exit (EXIT_FAILURE);
}
*/
}
int fexist( char * path){
int size=0;
return GetFileInfo(path,&size);
}
uint32_t fInit[MAXSTACKSIZE];
uint32_t fRun[MAXSTACKSIZE];
int init(){
if (WUSB_udev == NULL) return -1;
int retval = xxusb_register_write(WUSB_udev,1,0x0); // Stop DAQ mode
if (retval<0) return retval;
while (xxusb_usbfifo_read(WUSB_udev,(int *) stackdump,BUFF_L,100)>0);
int rate=1000;
int ii=80000000/rate-40; // 80 MHz
if (ii<72) ii=72;
// Set DGG channel A as a pulser, output on O1,
// with delay =500 x 12.5ns,
// and width = 500 x 12.5ns,
// not latching or inverting
// VME_DGG(udev,0,6,0,24000,6000,0,0);
VME_DGG(WUSB_udev,0,6,0,ii,40,0,0);
// Set DGG channel B to trigger on NIM1, output on O2,
// with delay =200 x 12.5ns,
// and width = 200 x 12.5ns,
// not latching or inverting
VME_DGG(WUSB_udev,1,1,1,0,10,0,1);
printf("CAEN V965 Pedestal set to %d\n", fPedestal
);
// INIT stackdata
int fPedestal=255;
WIENER_VMUSB_StackInit();
for (int i=0;i<nadc;i++){
WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_CRN , 0x0);
WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_GEO , i);
WIENER_VMUSB_StackReadA24D16(addr[i] + CAENV965_GEO);
for (int j=0;j<32;j++){
WIENER_VMUSB_StackWriteA24D16(addr[i] + CAENV965_THM + 0x02*j, fThreshold[j+i*32]); // threshold/kill for 32 channels
}
WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BS1, 0x80 ); // soft reset
WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BC1, 0x80 ); // soft reset
WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_PED, fPedestal ); // pedestal
WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BS2,0x5000);
WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BS2,0x4); // clear module
WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BC2,0x4);
}
WIENER_VMUSB_StackMarker(0xFAFC);
WIENER_VMUSB_StackGetUInt32(MAXSTACKSIZE, fInit);
// fInit->Print();
// READOUT stackdata
WIENER_VMUSB_StackInit();
WIENER_VMUSB_StackMarker(0xFFAB);
//fStack->ConditionalRead(addr[i] + CAENV965_SR1,0x1); // TRG wait : loop until bit 0 is on
// fStack->RepeatRead( CMD_A24, CMD_D32, addr[i] + CAENV965_OB,34,0); // repead read
//fStack->ConditionalRead(addr[i] + CAENV965_OB ,0x4000000) ; // loop until bit 26 is on, read data
for (int j=0;j<36;j++) WIENER_VMUSB_StackReadA24D32(addr[0] + CAENV965_OB);
//for (int j=0;j<36;j++) fStack->ReadA24D32(addr[1] + CAENV965_OB);
//for (int j=0;j<8;j++) fStack->ReadA24D32(addr[2] + CAENV965_OB);// 4 channels connected
WIENER_VMUSB_StackMarker(0xFAFB);
for (int k=0;k<nadc;k++){
WIENER_VMUSB_StackWriteA24D16(addr[k] + CAENV965_BS2,0x4); // clear module
WIENER_VMUSB_StackWriteA24D16(addr[k] + CAENV965_BC2,0x4);
}
//fStack->Print();
VME_LED_settings(WUSB_udev, 0,0,0,0); // Set Yellow LED to light with with USB out buffer not empty
VME_LED_settings(WUSB_udev, 1,1,0,0); // Set Red LED to light with NIM1
VME_LED_settings(WUSB_udev,2,0,0,0); // Set Green LED to light when stack is not empty
uint32_t vmereg;
VME_register_read(WUSB_udev,0x00,&vmereg);
printf("VMUSB Firmware ID -> 0x%08X\n",vmereg
);
VME_register_read(WUSB_udev,0x04,&vmereg);
printf("VMUSB Global Mode -> 0x%08X\n",vmereg
);
vmereg=(vmereg&0xF000)|0x0004;
VME_register_write(WUSB_udev,0x04,vmereg);
VME_register_write(WUSB_udev,0x08,0x00000080);
VME_register_write(WUSB_udev,0x28,0x0);
VME_register_write(WUSB_udev,0x2C,0x0);
VME_register_write(WUSB_udev,0x30,0x0);
VME_register_write(WUSB_udev,0x34,0x0);
VME_register_write(WUSB_udev,0x3C,0x000);
int nb = 0;
int ret= xxusb_stack_execute(WUSB_udev,(uint32_t *)fInit);
printf("Init::%d ret=%d\n",nb
,ret
);
if (ret
>0) for (int ki
=0;ki
<ret
/2;ki
++) printf ("stackdata=0x%08X\n",stackdata
[ki
]);
int nb0= 0; //fStack->Get(10000,&stackwrite[0]);
if (nb0>768) {
fprintf(stderr
,"nb0=%d > 768 error xxusb_stack_write\n", nb0
);
}
nb =xxusb_stack_write(WUSB_udev,0x2,(uint32_t *) fRun);
nb0=xxusb_stack_read(WUSB_udev,0x2,(uint32_t *) stackdata);
for (int li=0;li<fRun[0]+1;li++){
if (stackdata
[li
]!=fRun
[li
]) printf("%d %d init err %x %x\n",nb
,nb0
,fRun
[li
], stackdata
[li
]);
}
if (fMode==2) xxusb_register_write(WUSB_udev,1,0x1); // Start DAQ mode
return 0;
}
int connect(){
WIENER_VMUSB_VME_START(USBVME_NAME);
if (WUSB_udev
== NULL
) printf("daq::connect() not initialized");
return 0;
}
int disconnect(){
/* zakljuci */
WIENER_VMUSB_VME_STOP();
printf("daq::disconnect()\n");
return 0;
}
int clear(){
return 0;
}
int module_header(int recid,uint32_t *data,int len){
data[0] = recid;
data[1] = (len >0)? len : 0 ;
return data[1]+2;
}
int event(unsigned int *data, int maxn, int *ctr, int print){
int tout=200; /* 1/100 of a second */
const int lsize=sizeof(uint32_t);
if (WUSB_udev==NULL) return -222;
ctr[0]++;
ctr[1]++;
int count=0;
switch (fMode){
case 0:// normal calls
{
unsigned short clr= 0x4;
unsigned int status=0;
int32_t mdata;
for (int i=0;i<nadc;i++){
// wait for trg
tmlnk (tout);
do WIENER_VMUSB_VME_A24D16_R( addr[i] + CAENV965_SR1, &status); while ( (status&0x1)==0 && timer_out==0 );
tmulk();
// readout data
if (timer_out) return 0;
int len=0;
do {
WIENER_VMUSB_VME_A24D32_R(addr[i] + CAENV965_OB, &mdata);
mdata=data[count++];
len++;
} while ( (mdata & 0x4000000)==0 && timer_out==0) ; // bit 26 EOB or not valid datum
// clear
WIENER_VMUSB_VME_A24D16_W( addr[i] + CAENV965_BS2, &clr);
WIENER_VMUSB_VME_A24D16_W( addr[i] + CAENV965_BC2, &clr);
if (count+2<maxn) {
if (print
) printf("V965 %3d\n",len
);
count+=module_header(0x130+i,&data[count],len);
ctr[2]++;
ctr[3]+=len;
}
timer_out=0;
}
}
break;
case 1:// stack execute
{
for (int k=0;k<10000;k++) data[k]=fRun[k];
int ret=xxusb_stack_execute(WUSB_udev,(uint32_t *) data); //The first element of the array is the number of bytes.
if (ret< 0 ) {
printf ("xxusb_stack_execute error err=%d\n",ret
); \
count = 0;
} else count= ret/lsize;
}
break;
case 2:// stack load
{
int ret=xxusb_usbfifo_read(WUSB_udev,(int *) data,BUFF_L,100);
if (ret< 0 ) {
if (ret!=-110) {
printf ("xxusb_usbfifo_read error err=%d\n",ret
);
end();
init();
}
count = 0;
} else {
if (0 && print && ret>0) {
for (int i=0;i<100;i++) {
printf ("%4d fifodata=0x%08X\n",i
, data
[i
]);
if (data[i]==0xFAFB) break;
}
/*
0 fifodata=0x0000000D
1 fifodata=0x00000049
2 fifodata=0x0000FFAB
3 fifodata=0x00002000
4 fifodata=0x00000200
5 fifodata=0x00004141
6 fifodata=0x00000000
7 fifodata=0x00004057
8 fifodata=0x00000010
9 fifodata=0x00004052
10 fifodata=0x00000001
11 fifodata=0x0000405C
12 fifodata=0x00000011
13 fifodata=0x0000405D
14 fifodata=0x00000002
15 fifodata=0x0000405E
16 fifodata=0x00000012
17 fifodata=0x0000401C
18 fifodata=0x00000003
19 fifodata=0x0000402F
20 fifodata=0x00000013
21 fifodata=0x00004024
22 fifodata=0x00000004
23 fifodata=0x00004076
24 fifodata=0x00000014
25 fifodata=0x0000412F
26 fifodata=0x00000005
27 fifodata=0x0000404C
28 fifodata=0x00000015
29 fifodata=0x00004132
30 fifodata=0x00000006
31 fifodata=0x00004044
32 fifodata=0x00000016
33 fifodata=0x0000404A
34 fifodata=0x00000007
35 fifodata=0x0000409B
36 fifodata=0x00000017
37 fifodata=0x000040F1
38 fifodata=0x00000008
39 fifodata=0x00004087
40 fifodata=0x00000018
41 fifodata=0x00004173
42 fifodata=0x00000009
43 fifodata=0x0000404C
44 fifodata=0x00000019
45 fifodata=0x0000406C
46 fifodata=0x0000000A
47 fifodata=0x00004070
48 fifodata=0x0000001A
49 fifodata=0x0000406E
50 fifodata=0x0000000B
51 fifodata=0x00004014
52 fifodata=0x0000001B
53 fifodata=0x000040B7
54 fifodata=0x0000000C
55 fifodata=0x000040A9
56 fifodata=0x0000001C
57 fifodata=0x00004048
58 fifodata=0x0000000D
59 fifodata=0x00004118
60 fifodata=0x0000001D
61 fifodata=0x0000409D
62 fifodata=0x0000000E
63 fifodata=0x0000405B
64 fifodata=0x0000001E
65 fifodata=0x00004285
66 fifodata=0x0000000F
67 fifodata=0x00004159
68 fifodata=0x0000001F
69 fifodata=0x00000035
70 fifodata=0x00000400
71 fifodata=0x00000035
72 fifodata=0x00000600
73 fifodata=0x00000035
74 fifodata=0x0000FAFB
*/
}
if (print
) printf("------------------ret=%d data[0]=%d\n",ret
,(int)data
[0]);
count= ret/lsize;
ctr[2]+=data[0];
ctr[3]+=count;
}
}
break;
}
return count*lsize;
}
int end(){
if (WUSB_udev!=NULL) {
xxusb_register_write(WUSB_udev,1,0x0); // Stop DAQ mode
while (xxusb_usbfifo_read(WUSB_udev,(int *) stackdata,BUFF_L,30)>0);
}
return 0;
}
int daq_init(){
fMode = 2;
fPedestal=255;
for (int i=0;i<128;i++){
if (i<72) fThreshold[i]= 0;
else fThreshold[i]= 0x1<<8; // samo 4 kanali na zadnjem modulu so enablani
}
fThresholdEnable=0;
fStop=0;
//fInit=NULL;
//fStack=NULL;
connect();
return 0;
}
/* ------------------- CatchSig ----------------- */
int ctrlcflag=0;
void SigInt (int sig)
{
ctrlcflag = 1;
timer_out=1;
}
int read_pedestals(char *fpedname) {
FILE
*fped
=fopen(fpedname
,"r");
if(fped==NULL) return -1;
int j=0;
int ndim=400;
char line[ndim];
int val=0;
while (fgets(line
,ndim
,fped
)!=NULL
) {
fThreshold[j++]=val;
}
fThresholdEnable=1;
//fclose(fped);
return 0;
}
int main (int argc, char **argv){
// intercept routine
if (signal (SIGINT, SigInt) == SIG_ERR) {
}
// print welcome message
time_t t,told=0, tstart, tstop;
fprintf(stdout
,"#############################################\n");
fprintf(stdout
,"Program %s version %2.1f\n",argv
[0], VERSION
);
fprintf(stdout
,"Compiled on %s %s\n",__DATE__
, __TIME__
);
fprintf(stdout
,"#############################################\n");
int neve=-1;
char cfname[100]="test.dat";
char *fname=cfname;
char *fpedname=NULL;
#define BSIZE 10000
uint32_t data[10000];
daq_init();
if (argc>3) {
fpedname = argv[3];
read_pedestals(fpedname);
}
if (argc
>2) neve
= atoi(argv
[2]); // negative argument time ( in s )limited event loop
if (argc>1) {
fname = argv[1];
if (fexist(fname)==1){
fprintf(stdout
,"File %s already exist. Appending ....\n",fname
);
//fprintf(stdout,"Remove the file and restart !!!\n");
//exit(0);
}
}
FILE
*fp
=fopen(fname
,"a");
//gzFile fp=gzopen(fname,"a");
init();
clear();
int hdr[4]={2}; // recid od run 11 naprej
int i=0;
int ntotal=0;
int counters[30]={0,0,0,0,0, 0,0,0,0,0,0,0};
char names[10][20]={"TRG","CAEN V965"};
tstart=t;
tstop=tstart+360000;
if (neve<-1) {
tstop=tstart-neve;
neve=-1;
}
for (i=0;i!=neve && !ctrlcflag && t<tstop;i++){
if (t
!=told
) printf("%d in %2.2f min daq::event() %s\n",i
, (double)(t
-tstart
)/60.
, ctime(&t
));
int nb=event(data,BSIZE, counters,t!=told);
if (nb>0){
// zapis v datoteko
hdr[1]=nb+4*sizeof(int);
hdr[3]=i;
fwrite(hdr
, sizeof(int),4 , fp
);
//gzwrite(fp, hdr, sizeof(int)*4); //gzip
// recid=1 do runa 10. ntotal += fwrite(data, sizeof(int),nb, fp);
ntotal
+= fwrite(data
, 1,nb
, fp
);
//ntotal += gzwrite(fp, data, nb);
told=t;
} else i--;
}
end();
printf("Number of Events: %d\n",i
);
if (ctrlcflag
) printf("User Program termination CTRL-C\n");
if (t
>tstop
) printf("Timeout termination tstart# t>tstop: %d# %d >%d\n",(int)t
, (int)tstart
, (int) tstop
);
//gzclose(fp);
fprintf(stdout
,"%d bytes written to %s\nCounts:\n", (int) (ntotal
*sizeof(int)),fname
);
for (i
=0;i
<2;i
++) fprintf(stdout
,"%s\t%d\t%d\n",names
[i
],counters
[2*i
],counters
[2*i
+1]) ;
if (argc==1) {
fprintf(stdout
,"Usage: %s [filename] [number of events] [thresholdfile]\n negative number of events = acq time in seconds\n",argv
[0]);
}
disconnect();
return 0;
}