v1729.c

Go to the documentation of this file.
00001 /*********************************************************************
00002 
00003   Name:         v1792.c
00004   Created by:   Pierre-Andre Amaudruz
00005 
00006   Contents:     V1729 4channel /12 bit sampling ADC (1,2Gsps)
00007 
00008   $Log: v1729.c,v $
00009 *********************************************************************/
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include <unistd.h>
00013 #include <math.h>
00014 
00015 #include "v1729.h"
00016 
00017 int debug = 1;
00018 static float  Corr_vernier[4];
00019 static int    trig_rec, end_cell, ncol, get_ref=1;
00020 static WORD   post_trig;
00021 static float  pedestals[V1729_RAM_SIZE], ped[4][V1729_MAX_CHANNEL_SIZE+3];
00022 static float  pedestalsRms[V1729_RAM_SIZE];
00023 static int    ped_ok = 0;
00024 static int    lMINVER, lMAXVER;
00025 static int    vernier_ok = 0;
00026 
00027 /*****************************************************************/
00028 /*****************************************************************/
00029 void v1729_AcqStart(MVME_INTERFACE *mvme, DWORD base)
00030 {
00031   int cmode;
00032 
00033   mvme_get_dmode(mvme, &cmode);
00034   mvme_set_dmode(mvme, MVME_DMODE_D16);
00035   mvme_write_value(mvme, base+V1729_INTERRUPT_REG, 0);
00036   mvme_write_value(mvme, base+V1729_ACQ_START_W, 0);
00037   mvme_set_dmode(mvme, cmode);
00038 }
00039 
00040 /*****************************************************************/
00041 void v1729_Reset(MVME_INTERFACE *mvme, DWORD base)
00042 {
00043   int cmode;
00044 
00045   mvme_get_dmode(mvme, &cmode);
00046   mvme_set_dmode(mvme, MVME_DMODE_D16);
00047   mvme_write_value(mvme, base+V1729_RESET_W, 0);
00048   mvme_set_dmode(mvme, cmode);
00049 }
00050 
00051 /*****************************************************************/
00052 /**
00053 Sets all the necessary paramters for a given configuration.
00054 The configuration is provided by the mode argument.
00055 Add your own configuration in the case statement. Let me know
00056 your setting if you want to include it in the distribution.
00057 @param *mvme VME structure
00058 @param  base Module base address
00059 @param mode  Configuration mode number
00060 @param *nentry number of entries requested and returned.
00061 @return MVME_SUCCESS
00062 */
00063 int  v1729_Setup(MVME_INTERFACE *mvme, DWORD base, int mode)
00064 {
00065   int      cmode;
00066 
00067   mvme_get_dmode(mvme, &cmode);
00068   mvme_set_dmode(mvme, MVME_DMODE_D16);
00069 
00070   switch (mode) {
00071   case 0x1:
00072     printf("4 channels 1Gsps External Trigger(mode:%d)\n", mode);
00073     mvme_write_value(mvme, base+V1729_INTERRUPT_ENABLE, 0);
00074     v1729_PreTrigSet(mvme, base, 0x4000);
00075     v1729_PostTrigSet(mvme, base, 64);
00076     v1729_NColsSet(mvme, base, 64);
00077     v1729_TriggerTypeSet(mvme, base,
00078        V1729_EXTERNAL_TRIGGER | V1729_RISING_EDGE
00079        |V1729_INHIBIT_RANDOM | V1729_NORMAL_TRIGGER);
00080     //    v1729_TriggerChannelSelect(mvme, base, 0x3); // V1729_ALL_FOUR);
00081     v1729_ChannelSelect(mvme, base, V1729_ALL_FOUR);
00082     v1729_FrqSamplingSet(mvme, base, V1729_1GSPS);
00083     break;
00084   case 0x2:
00085     printf("4 channels 2Gsps External Trigger(mode:%d)\n", mode);
00086     mvme_write_value(mvme, base+V1729_INTERRUPT_ENABLE, 0);
00087     v1729_PreTrigSet(mvme, base, 0x4000);
00088     v1729_PostTrigSet(mvme, base, 64);
00089     v1729_NColsSet(mvme, base, 64);
00090     v1729_TriggerTypeSet(mvme, base,
00091        V1729_EXTERNAL_TRIGGER | V1729_RISING_EDGE
00092        |V1729_INHIBIT_RANDOM | V1729_NORMAL_TRIGGER);
00093     v1729_ChannelSelect(mvme, base, V1729_ALL_FOUR);
00094     v1729_FrqSamplingSet(mvme, base, V1729_2GSPS);
00095     break;
00096   case 0x3:
00097     printf("4 channels 1Gsps External Trigger(mode:%d)\n", mode);
00098     mvme_write_value(mvme, base+V1729_INTERRUPT_ENABLE, 0);
00099     v1729_PreTrigSet(mvme, base, 0x4000);
00100     v1729_PostTrigSet(mvme, base, 32);//64
00101     v1729_NColsSet(mvme, base, 128);
00102     v1729_TriggerTypeSet(mvme, base,
00103        V1729_EXTERNAL_TRIGGER | V1729_RISING_EDGE
00104        |V1729_INHIBIT_RANDOM | V1729_NORMAL_TRIGGER);
00105     //    v1729_TriggerChannelSelect(mvme, base, 0x3); // V1729_ALL_FOUR);
00106     v1729_ChannelSelect(mvme, base, V1729_ALL_FOUR);
00107     v1729_FrqSamplingSet(mvme, base, V1729_1GSPS);
00108     break;
00109   case 0x7:
00110     printf("4 channels 1Gsps Soft Trigger for Pedestals(mode:%d)\n", mode);
00111     v1729_PreTrigSet(mvme, base, 0x4000);
00112     v1729_PostTrigSet(mvme, base, 1 );
00113     v1729_NColsSet(mvme, base, 128);
00114     v1729_TriggerTypeSet(mvme, base,
00115        V1729_SOFT_TRIGGER | V1729_RISING_EDGE
00116        | V1729_INHIBIT_RANDOM | V1729_NORMAL_TRIGGER);
00117     v1729_ChannelSelect(mvme, base, V1729_ALL_FOUR);
00118     v1729_FrqSamplingSet(mvme, base, V1729_1GSPS);
00119     break;
00120   case 0x8:
00121     printf("4 channels 1Gsps Random Trigger for Vernier time extraction(mode:%d)\n", mode);
00122     v1729_PreTrigSet(mvme, base, 1);
00123     v1729_PostTrigSet(mvme, base, 1);
00124     v1729_NColsSet(mvme, base, 0);
00125     v1729_TriggerTypeSet(mvme, base, V1729_AUTHORIZE_RANDOM);
00126     v1729_ChannelSelect(mvme, base, V1729_ALL_FOUR);
00127     v1729_FrqSamplingSet(mvme, base, V1729_1GSPS);
00128     break;
00129   default:
00130     printf("Unknown setup mode\n");
00131     mvme_set_dmode(mvme, cmode);
00132     return -1;
00133   }
00134   mvme_set_dmode(mvme, cmode);
00135   return 0;
00136 }
00137 
00138 /*****************************************************************/
00139 int  v1729_Status(MVME_INTERFACE *mvme, DWORD base)
00140 {
00141   WORD value, value1;
00142   int      cmode, ncol;
00143 
00144   mvme_get_dmode(mvme, &cmode);
00145   mvme_set_dmode(mvme, MVME_DMODE_D16);
00146   printf("--V1729---------------------------------\n");
00147   value = mvme_read_value(mvme, base+V1729_VERSION_R);
00148   printf("Version                       : 0x%02x\n", value);
00149   value = mvme_read_value(mvme, base+V1729_FRQ_SAMPLING);
00150   printf("Frequency Sampling            : %d[Gsps]\n", value==1 ? 2 : 1);
00151   value = mvme_read_value(mvme, base+V1729_CHANMASK);
00152   printf("Channel Mask                  : 0x%01x\n", value&0xf);
00153   value = mvme_read_value(mvme, base+V1729_TRIGCHAN);
00154   printf("Trigger Channel source(enable): 0x%01x\n", value&0xF);
00155   value = mvme_read_value(mvme, base+V1729_N_COL);
00156   ncol = v1729_NColsGet(mvme, base);
00157   printf("Number of Columns             : %d\n", ncol); // value&0xFF);
00158   value = mvme_read_value(mvme, base+V1729_POSTTRIG_LSB);
00159   value1 = mvme_read_value(mvme, base+V1729_POSTTRIG_MSB);
00160   printf("PostTrigger delay             : %d\n", ((value1&0xFF)<<8)|(value&0xFF));
00161   value = mvme_read_value(mvme, base+V1729_PRETRIG_LSB);
00162   value1 = mvme_read_value(mvme, base+V1729_PRETRIG_MSB);
00163   printf("PreTrigger delay              : %d\n", ((value1&0xFF)<<8)|(value&0xFF));
00164   value = mvme_read_value(mvme, base+V1729_FAST_READ_MODES);
00165   printf("Fast read modes               : %d\n", value & 3);
00166   value = mvme_read_value(mvme, base+V1729_TRIGTYPE);
00167   value &= 0x1F;
00168   if ((value&0x3)==0) printf("Software Trigger, ");
00169   if ((value&0x3)==1) printf("Trigger on Discr (fixed by DAC), ");
00170   if ((value&0x3)==2) printf("External Trigger, ");
00171   if ((value&0x3)==3) printf("Software Trigger OR Trigger on Discr, ");
00172   if (((value>>2)&0x1)==0) printf("on Rising Edge.\n");
00173   if (((value>>2)&0x1)==1) printf("on Fallling Edge.\n");
00174   if (((value>>3)&0x1)==0) printf("Inhibit random internal Trigger, ");
00175   if (((value>>3)&0x1)==1) printf("Authorize random internal Trigger, ");
00176   if (((value>>4)&0x1)==0) printf("Normal Trigger.\n");
00177   if (((value>>4)&0x1)==1) printf("External Trigger without masking.\n");
00178   mvme_set_dmode(mvme, cmode);
00179   return V1729_SUCCESS;
00180 }
00181 
00182 /*****************************************************************/
00183 void v1729_DataRead(MVME_INTERFACE *mvme, DWORD base, WORD *pdest, int nch, int npt)
00184 {
00185   int cmode, i, rdlen;
00186 
00187   mvme_get_dmode(mvme, &cmode);
00188   mvme_set_dmode(mvme, MVME_DMODE_D16);
00189   if (nch>V1729_MAX_CHANNELS) nch = V1729_MAX_CHANNELS;
00190   if (npt>V1729_MAX_CHANNEL_SIZE) npt = V1729_MAX_CHANNEL_SIZE;
00191   rdlen =  nch * (npt+3);
00192 
00193 
00194   for (i=0 ; i < rdlen ; i++) {
00195     pdest[i] = (int) mvme_read_value(mvme, base+V1729_DATA_FIFO_R);
00196   }
00197 
00198 
00199   //  mvme_read(mvme, pdest, base+V1729_DATA_FIFO_R, rdlen*2);
00200 
00201   get_ref = 1;
00202   mvme_set_dmode(mvme, cmode);
00203 }
00204 
00205 /*****************************************************************/
00206 int v1729_isTrigger(MVME_INTERFACE *mvme, DWORD base)
00207 {
00208   int cmode, done;
00209 
00210   mvme_get_dmode(mvme, &cmode);
00211   mvme_set_dmode(mvme, MVME_DMODE_D16);
00212   done =  mvme_read_value(mvme, base+V1729_INTERRUPT_REG);
00213   mvme_set_dmode(mvme, cmode);
00214   return (done & 0xFF);
00215 }
00216 
00217 /*****************************************************************/
00218 void v1729_PreTrigSet(MVME_INTERFACE *mvme, DWORD base, int value)
00219 {
00220   int cmode;
00221 
00222   mvme_get_dmode(mvme, &cmode);
00223   mvme_set_dmode(mvme, MVME_DMODE_D16);
00224   mvme_write_value(mvme, base+V1729_PRETRIG_LSB, value&0xFF);
00225   mvme_write_value(mvme, base+V1729_PRETRIG_MSB, (value>>8)&0xFF);
00226   mvme_set_dmode(mvme, cmode);
00227 }
00228 
00229 /*****************************************************************/
00230 void v1729_PostTrigSet(MVME_INTERFACE *mvme, DWORD base, int value)
00231 {
00232   int cmode;
00233 
00234   mvme_get_dmode(mvme, &cmode);
00235   mvme_set_dmode(mvme, MVME_DMODE_D16);
00236   mvme_write_value(mvme, base+V1729_POSTTRIG_LSB, value&0xFF);
00237   mvme_write_value(mvme, base+V1729_POSTTRIG_MSB, (value>>8)&0xFF);
00238   mvme_set_dmode(mvme, cmode);
00239 }
00240 
00241 /*****************************************************************/
00242 void v1729_TriggerTypeSet(MVME_INTERFACE *mvme, DWORD base, int value)
00243 {
00244   int cmode;
00245 
00246   mvme_get_dmode(mvme, &cmode);
00247   mvme_set_dmode(mvme, MVME_DMODE_D16);
00248   mvme_write_value(mvme, base+V1729_TRIGTYPE, value);
00249   mvme_set_dmode(mvme, cmode);
00250 }
00251 
00252 /*****************************************************************/
00253 void v1729_NColsSet(MVME_INTERFACE *mvme, DWORD base, int value)
00254 {
00255   int cmode;
00256 
00257   mvme_get_dmode(mvme, &cmode);
00258   mvme_set_dmode(mvme, MVME_DMODE_D16);
00259   mvme_write_value(mvme, base+V1729_N_COL, value & 0xFF);
00260   mvme_set_dmode(mvme, cmode);
00261 }
00262 
00263 /*****************************************************************/
00264 int v1729_NColsGet(MVME_INTERFACE *mvme, DWORD base)
00265 {
00266   int cmode, value;
00267 
00268   mvme_get_dmode(mvme, &cmode);
00269   mvme_set_dmode(mvme, MVME_DMODE_D16);
00270   value = mvme_read_value(mvme, base+V1729_N_COL);
00271   mvme_set_dmode(mvme, cmode);
00272   return (value & 0xFF);
00273 }
00274 
00275 /*****************************************************************/
00276 void v1729_ChannelSelect(MVME_INTERFACE *mvme, DWORD base, int value)
00277 {
00278   int cmode;
00279 
00280   mvme_get_dmode(mvme, &cmode);
00281   mvme_set_dmode(mvme, MVME_DMODE_D16);
00282   mvme_write_value(mvme, base+V1729_TRIGCHAN, value&0xF);
00283   mvme_set_dmode(mvme, cmode);
00284 }
00285 
00286 /*****************************************************************/
00287 void v1729_SoftTrigger(MVME_INTERFACE *mvme, DWORD base)
00288 {
00289   int cmode;
00290 
00291   mvme_get_dmode(mvme, &cmode);
00292   mvme_set_dmode(mvme, MVME_DMODE_D16);
00293   mvme_write_value(mvme, base+V1729_SOFT_TRIGGER_W, 0x1);
00294   mvme_set_dmode(mvme, cmode);
00295 }
00296 
00297 /*****************************************************************/
00298 void v1729_FrqSamplingSet(MVME_INTERFACE *mvme, DWORD base, int value)
00299 {
00300   int cmode;
00301 
00302   mvme_get_dmode(mvme, &cmode);
00303   mvme_set_dmode(mvme, MVME_DMODE_D16);
00304   if (value < 0) value = 1;
00305   if (value > 2) value = 2;
00306   mvme_write_value(mvme, base+V1729_FRQ_SAMPLING, value);
00307   mvme_set_dmode(mvme, cmode);
00308 }
00309 
00310 /*****************************************************************/
00311 /**
00312 Does Time calibration of all 4 channels. Requires the module to be in
00313 random Vernier mode (setup 8). Trigger internally generated for the whole
00314 memory.
00315 
00316 ! Code not yet guarantee to be correct.
00317 */
00318 int v1729_TimeCalibrationRun(MVME_INTERFACE *mvme, DWORD base, int flag)
00319 {
00320   int      cmode;
00321   int vernier[4][16384];
00322   int i_bar=0;
00323   int h[5000];
00324   char bars[] = "|\\-/";
00325   int i, k=0, j, toggle, save[4];
00326   FILE *fH=NULL;
00327 
00328   mvme_get_dmode(mvme, &cmode);
00329   mvme_set_dmode(mvme, MVME_DMODE_D16);
00330 
00331   memset(vernier, 0, sizeof(vernier));
00332   memset(h, 0, sizeof(h));
00333 
00334   // Set Vernier calibration
00335   v1729_Setup(mvme, base, 8);
00336 
00337   // Start ACQ
00338   v1729_AcqStart(mvme, base);
00339 
00340   usleep(10000);
00341 
00342   if (flag) {
00343     fH = fopen("timecalibration.txt", "w");
00344     if (fH == NULL) {
00345       printf("File not open\n");
00346     }
00347     printf("\ntimecalibration.txt file opened\n");
00348   }
00349 
00350 
00351   // Wait for trigger
00352   while(!v1729_isTrigger(mvme, base)) {
00353 
00354      printf("...%c Waiting trigger\r", bars[i_bar++ % 4]);
00355      fflush(stdout);
00356 
00357      usleep(10);
00358   };
00359 
00360   for (i=0;i<16384;i++) {
00361     vernier[3][i] =  mvme_read_value(mvme, base+V1729_DATA_FIFO_R);
00362     vernier[2][i] =  mvme_read_value(mvme, base+V1729_DATA_FIFO_R);
00363     vernier[1][i] =  mvme_read_value(mvme, base+V1729_DATA_FIFO_R);
00364     vernier[0][i] =  mvme_read_value(mvme, base+V1729_DATA_FIFO_R);
00365   }
00366   /*
00367   k = 1;
00368   for (l=0;l<16384;l++) {
00369     if (vernier[k][i] < 5000)
00370       h[vernier[k][l]] += 1;
00371     else
00372       printf("vernier:%d\n", vernier[k][i]);
00373   }
00374   */
00375   // Vernier evaluation done
00376   vernier_ok = 1;
00377 
00378   // Debugging code below... to display only the edges of the
00379   // time window. I expect to have only 1 edge above 2000.
00380   // Looking at the whole array, I've seen multiple group!
00381 
00382   if (flag)
00383     for (i=0;i<5000;i++) {
00384       fprintf(fH, "Vernier[%i][%i] = %6d\n", k, i, vernier[k][i]);
00385     }
00386 
00387   toggle=1;
00388   j=save[0]=save[1]=save[2]=save[3]=0;
00389   for (i=2000 ; i<4000 ; i++) {
00390     if (toggle && (h[i] != 0)) {
00391       save[j++] = i;
00392       toggle = 0;
00393     }
00394     if (!toggle && h[i] == 0) {
00395       save[j++] = i;
00396       toggle = 1;
00397     }
00398 
00399     if (flag) 
00400       fprintf(fH, "%i - h[%i] = %6d\n", k, i, h[ i]);
00401   }
00402   if (flag) fprintf(fH, "index lMINVERnier:%d lMAXVERnier:%d %d %d DiffMaxMin:%d\n"
00403                     , save[0], save[1], save[2], save[3], save[1]-save[0]);
00404 
00405   lMINVER = save[0];
00406   lMAXVER = save[1];
00407 
00408   if (flag) {
00409     fclose(fH);
00410     printf("File timecalibration.txt closed\n");
00411   }
00412 
00413   mvme_set_dmode(mvme, cmode);
00414   return 0;
00415 }
00416 
00417 /*****************************************************************/
00418 /**
00419    Pedestal extraction. Requires to module to be in setup mode 7
00420    (soft  trigger).
00421    Show major improvment in the WF reconstruction, but still has
00422    20 bin periodic spikes.
00423 
00424    Code not yet guarantee to be fully correct...
00425 */
00426 int v1729_PedestalRun(MVME_INTERFACE *mvme, DWORD base, int loop, int flag)
00427 {
00428   int cmode;
00429   //  int i_bar=0;
00430   //  char bars[] = "|\\-/";
00431   WORD data[V1729_RAM_SIZE];
00432   int i, l;
00433   int xloop;
00434   int stuck_trig_rec = -1;
00435   FILE *fH=NULL;
00436 
00437   memset(pedestals, 0, sizeof(pedestals));
00438 
00439   mvme_get_dmode(mvme, &cmode);
00440   mvme_set_dmode(mvme, MVME_DMODE_D16);
00441 
00442   // Set Trigger type Soft trigger
00443   v1729_Setup(mvme, base, 7);
00444 
00445   // give it time to settle...
00446   sleep(2);
00447 
00448   if (flag) {
00449     fH = fopen("pedestals.txt", "w");
00450     if (fH == NULL) {
00451       printf("File not open\n");
00452     }
00453     printf("\npedestals.txt file opened\n");
00454   }
00455 
00456   for (xloop=0; xloop<2; xloop++) {
00457     // loop twice: first pass: accept all data, second pass:
00458     // reject data too far away from the mean
00459     int countWide = 0;
00460 
00461     static double sum0[V1729_RAM_SIZE];
00462     static double sum1[V1729_RAM_SIZE];
00463     static double sum2[V1729_RAM_SIZE];
00464 
00465     memset(sum0, 0, sizeof(sum0));
00466     memset(sum1, 0, sizeof(sum1));
00467     memset(sum2, 0, sizeof(sum2));
00468 
00469   for (l=0;l<loop;l++) {
00470     // Start ACQ
00471     v1729_AcqStart(mvme, base);
00472 
00473     usleep(20000);
00474 
00475     // Generate soft Trigger
00476     v1729_SoftTrigger(mvme, base);
00477 
00478     // Wait for trigger
00479     while(1) {
00480 
00481       int t = v1729_isTrigger(mvme, base);
00482       //printf("trigger %d\n", t);
00483       if (t == 255)
00484         break;
00485 
00486       usleep(10);
00487     };
00488 
00489     v1729_DataRead(mvme, base, data, 4, V1729_MAX_CHANNEL_SIZE);
00490 
00491     {
00492       int oldi;
00493       int post_trig = (mvme_read_value(mvme, base+V1729_POSTTRIG_LSB) & 0xFF)
00494         | ((mvme_read_value(mvme, base+V1729_POSTTRIG_MSB) & 0xFF) << 8);
00495       int trig_rec = mvme_read_value(mvme, base+V1729_TRIGREC_R) & 0xFF;
00496 
00497       int end_cell = 20*(post_trig - trig_rec) % 128;
00498 
00499       if (stuck_trig_rec >= 0)
00500         if (trig_rec == stuck_trig_rec)
00501           fprintf(stderr,"v1729 at 0x%x: Warning: TRIG_REC is stuck at 0x%x\n", base, trig_rec);
00502       stuck_trig_rec = trig_rec;
00503 
00504       if (flag) fprintf(fH, "Loop:%d posttrig %d, trigrec %d, endcell %d\n", l, post_trig, trig_rec, end_cell);
00505 
00506       for (oldi=0; oldi<V1729_RAM_SIZE; oldi++)
00507         {
00508           int newi = (2560 + oldi - end_cell) % 2560;
00509 
00510           //i = newi;
00511           i = oldi;
00512           newi = i;
00513           
00514           if (flag) fprintf(fH, "Loop:%d data[%i] = %d\n", l, i, data[i]);
00515           
00516           if ((xloop==0) || (fabs(data[i] - pedestals[newi]) < 2+5*pedestalsRms[newi]))
00517             {
00518               sum0[newi] += 1;
00519               sum1[newi] += data[i];
00520               sum2[newi] += data[i]*data[i];
00521             }
00522         }
00523     }
00524   }
00525 
00526   for (i=0;i<V1729_RAM_SIZE;i++) {
00527     double mean = sum1[i]/sum0[i];
00528     double var  = sum2[i]/sum0[i] - mean*mean;
00529     double rms  = 0;
00530 
00531     if (sum0[i] < 3) {
00532       printf("Pedestal shifted, memory address %5d\n", i);
00533       mean = 0;
00534       var  = 0;
00535     }
00536 
00537     if (var > 0)
00538       rms = sqrt(var);
00539     pedestals[i] = mean;
00540     pedestalsRms[i] = rms;
00541 
00542     if (rms > 20) {
00543       countWide ++;
00544       printf("Wide pedestals, memory address %5d, mean: %8.1f, rms: %6.1f\n", i, mean, rms);
00545     }
00546 
00547   }
00548 
00549   printf("Found %d wide pedestals\n", countWide);
00550 
00551   if (flag)
00552     for (i=0;i<V1729_RAM_SIZE;i++) {
00553       fprintf(fH, "Loop:%d Pedestals[%i] = %f, rms: %f\n", l, i, pedestals[i], pedestalsRms[i]);
00554     }
00555 
00556   //break;
00557   }
00558 
00559   l = i = 0;
00560   while (i<V1729_RAM_SIZE ) {
00561     ped[3][l] = pedestals[i++];
00562     ped[2][l] = pedestals[i++];
00563     ped[1][l] = pedestals[i++];
00564     ped[0][l] = pedestals[i++];
00565     l++;
00566   }
00567 
00568   // Pedestal evaluation done
00569   ped_ok = 1;
00570 
00571   if (flag)
00572     for (i=0;i<2563;i++) {
00573       fprintf(fH, "Ped0,1,2,3[%i] = %f %f %f %f\n", i, ped[0][i], ped[1][i], ped[2][i], ped[3][i]);
00574     }
00575   if (flag) {
00576     fclose(fH);
00577     printf("File pedestals.txt closed\n");
00578   }
00579 
00580   mvme_set_dmode(mvme, cmode);
00581   return loop;
00582 }
00583 
00584 /*****************************************************************/
00585 /**
00586     Re-order given channel from srce to dest
00587     len is for now frozen to V1729_MAX_CHANNEL_SIZE
00588     Does pedestal subtraction if enabled (ped_ok)
00589     Doesn't correct for timing.
00590 
00591     NOTE:
00592     destination cell index[k] computed as 2560+j+end_cell instead
00593     of manual formula [2] page 13 (2560+j-end_cell).
00594 */
00595 int v1729_OrderData(MVME_INTERFACE *mvme, DWORD base, WORD *srce, int *dest, int nch, int chan, int npt)
00596 {
00597   int i, j, k;
00598   WORD value, value1;
00599   int cmode, temp, rdlen;
00600 
00601   if (get_ref) {
00602     // Set dmode to D16
00603     mvme_get_dmode(mvme, &cmode);
00604     mvme_set_dmode(mvme, MVME_DMODE_D16);
00605     
00606     value = mvme_read_value(mvme, base+V1729_POSTTRIG_LSB);
00607     value1 = mvme_read_value(mvme, base+V1729_POSTTRIG_MSB);
00608     post_trig = ((value1 & 0xFF)<<8) | (value & 0xFF);
00609     ncol = v1729_NColsGet(mvme, base);
00610     Corr_vernier[3] = (float) (srce[4] - lMINVER) / (float) (lMAXVER - lMINVER);
00611     Corr_vernier[2] = (float) (srce[5] - lMINVER) / (float) (lMAXVER - lMINVER);
00612     Corr_vernier[1] = (float) (srce[6] - lMINVER) / (float) (lMAXVER - lMINVER);
00613     Corr_vernier[0] = (float) (srce[7] - lMINVER) / (float) (lMAXVER - lMINVER);
00614     trig_rec = mvme_read_value(mvme, base+V1729_TRIGREC_R);
00615     trig_rec &= 0xFF;
00616     //end_cell = (20 * ((post_trig + trig_rec) % ncol));
00617     end_cell = (20 * ((post_trig + trig_rec) % 2560));
00618     get_ref = 0;
00619 
00620     //printf("posttrig %d, trigrec %d, endcell %d\n", post_trig, trig_rec, end_cell);
00621   }
00622 
00623   if (nch>V1729_MAX_CHANNELS) nch = V1729_MAX_CHANNELS;
00624   if (npt>V1729_MAX_CHANNEL_SIZE) npt = V1729_MAX_CHANNEL_SIZE;
00625   rdlen = nch * (npt+3);
00626 
00627   if (nch == 4) {    // 0-1-2/3-4-5/6-7-8 start=9>2, 10>1, 11>0
00628     for (i=12+3-chan, j=0; i < rdlen; i+=nch, j++) {
00629       temp =  ((int) srce[i] - pedestals[i]);
00630       k =  (20*ncol + j + end_cell) % (20*ncol);
00631       dest[k] = temp;
00632     }
00633   } else if (nch == 3) { // 0-1-2/3-4-5/6-7-8 start=9>2, 10>1, 11>0
00634     for (i=9+2-chan, j=0; i < rdlen; i+=nch, j++) {
00635       temp =  ((int) srce[i] - pedestals[i]);
00636       k =  ((20*ncol) + j + end_cell) % (20*ncol);
00637       dest[k] = temp;
00638     }
00639   } else if (nch == 2) { // 0-1/2-3/4-5  start=6>1, 7>0
00640     for (i=6+1-chan, j=0; i < rdlen; i+=nch, j++) {
00641       temp =  ((int) srce[i] - pedestals[i]);
00642       k =  ((20*ncol) + j + end_cell) % (20*ncol);
00643       dest[k] = temp;
00644     }
00645   } else if (nch == 1) { // 0/1/2        start=3>0
00646     for (i=3-chan, j=0; i < rdlen; i+=nch, j++) {
00647       //temp =  ((int) srce[i] - (ped_ok ? (int) ped[chan][3+j] : 0));
00648       temp =  ((int) srce[i] - pedestals[i]);
00649       k =  ((20*ncol) + j + end_cell) % (20*ncol);
00650       dest[k] = temp;
00651     }
00652   }
00653   
00654   /*
00655     if (debug) {
00656     for (i=0;i<V1729_MAX_CHANNEL_SIZE;i++) {
00657     printf("dest[%i] = %d\n", i, dest[i]);
00658     }
00659     }
00660     printf("First sample   %d %d %d %d\n", srce[0], srce[1], srce[2], srce[3]);
00661     printf("Vernier        %d %d %d %d\n", srce[4], srce[5], srce[6], srce[7]);
00662     printf("Reset Baseline %d %d %d %d\n", srce[8], srce[9], srce[10], srce[11]);
00663     printf("Value:%d Value1:%d\n", value, value1);
00664     printf("Post trig:%d - Trig rec:%d(C:%d) - End_cell:%d(%d)\n"
00665     , post_trig, trig_rec, 128-trig_rec, end_cell, end_cell/20);
00666     printf("Cvernier       %f %f %f %f\n", Corr_vernier[0], Corr_vernier[1]
00667     , Corr_vernier[2], Corr_vernier[3]);
00668   */
00669   
00670   mvme_set_dmode(mvme, cmode);
00671   return 0;
00672 }
00673 
00674 /********************************************************************/
00675 /********************************************************************/
00676 /*-PAA- For test purpose only */
00677 #ifdef MAIN_ENABLE
00678 int main () {
00679 
00680   MVME_INTERFACE *myvme;
00681   DWORD V1729_BASE = 0x600000;
00682   char bars[] = "|\\-/";
00683   int i_bar, wheel = 0, status, csr, i, j, len;
00684   WORD    data[20000];
00685   WORD    odata[3000];
00686 
00687   // Test under vmic
00688   status = mvme_open(&myvme, 0);
00689 
00690   // Set am to A24 non-privileged Data
00691   mvme_set_am(myvme, MVME_AM_A24_ND);
00692 
00693   // Set dmode to D16
00694   mvme_set_dmode(myvme, MVME_DMODE_D16);
00695 
00696   // Reset board
00697   v1729_Reset(myvme, V1729_BASE);
00698 
00699   // Get Firmware revision
00700   csr = mvme_read_value(myvme, V1729_BASE+V1729_VERSION_R);
00701   printf("Firmware revision: 0x%x\n", csr);
00702 
00703   // Collect Pedestals
00704   v1729_PedestalRun(myvme, V1729_BASE, 10);
00705 
00706   // Do vernier calibration
00707   v1729_TimeCalibrationRun(myvme, V1729_BASE);
00708 
00709   // Set mode 1
00710   v1729_Setup(myvme, V1729_BASE, 1);
00711 
00712   // Print Current status
00713   v1729_Status(myvme, V1729_BASE);
00714 
00715   // Start acquisition
00716   v1729_AcqStart(myvme, V1729_BASE);
00717 
00718   printf("ACQ started\n");
00719   // Wait for trigger
00720  while(!v1729_isTrigger(myvme, V1729_BASE)) {
00721    if (wheel) {
00722      printf("...%c Waiting trigger\r", bars[i_bar++ % 4]);
00723      fflush(stdout);
00724    };
00725  };
00726  printf("Going to read event (trig_rec:%ld)\n"
00727   , (0xFF & mvme_read_value(myvme, V1729_BASE+V1729_TRIGREC_R)));
00728 
00729  v1729_DataRead(myvme, V1729_BASE, data, 4, V1729_MAX_CHANNEL_SIZE);
00730  i = 0;
00731  printf("First Sample  = %d-%d-%d-%d\n", data[i], data[i+1], data[i+2], data[i+3]);
00732  i = 4;
00733  printf("Vernier       = %d-%d-%d-%d\n", data[i], data[i+1], data[i+2], data[i+3]);
00734  i = 8;
00735  printf("Reset Baseline= %d-%d-%d-%d\n", data[i], data[i+1], data[i+2], data[i+3]);
00736 
00737  for (i=12 , j=0; i<V1729_RAM_SIZE ; i+=4, j++) {
00738    printf("Data[%5i...] (%6d) = %4d-%4d-%4d-%4d\n", i, j, data[i], data[i+1], data[i+2], data[i+3]);
00739  }
00740 
00741  v1729_OrderData(myvme, V1729_BASE, data, odata, 4, 0, V1729_MAX_CHANNEL_SIZE);
00742 
00743  /*
00744  for (i=0 ; i<len; i+=4) {
00745    printf("%6d - Data[%5i...] = %4d-%4d-%4d-%4d\n", len, i, odata[i], odata[i+1], odata[i+2], odata[i+3]);
00746  }
00747  */
00748  // exit:
00749  status = mvme_close(myvme);
00750  return 0;
00751 }
00752 
00753 #endif
00754 

Midas DOC Version 2.0.2 ---- PSI Stefan Ritt ----
Contributions: Pierre-Andre Amaudruz - Sergio Ballestrero - Suzannah Daviel - Doxygen - Peter Green - Qing Gu - Greg Hackman - Gertjan Hofman - Paul Knowles - Exaos Lee - Rudi Meier - Glenn Moloney - Dave Morris - John M O'Donnell - Konstantin Olchanski - Renee Poutissou - Tamsen Schurman - Andreas Suter - Jan M.Wouters - Piotr Adam Zolnierczuk