caenv1190B.c

Go to the documentation of this file.
00001 /*********************************************************************
00002 
00003   Name:         v1190B.c
00004   Created by:   Pierre-Andre Amaudruz
00005 
00006   Contents:     V1190B 64ch. TDC
00007                 
00008   $Id: caenv1190B.c 2753 2005-10-07 14:55:31Z ritt $
00009 
00010 *********************************************************************/
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <stdio.h>
00014 #include <stdint.h>
00015 
00016 #include <mvmestd.h>
00017 #include "caenv1190B.h"
00018 
00019 /*****************************************************************/
00020 /*****************************************************************/
00021 int v1190_EventRead(DWORD *pbase, DWORD *pdest, int * nentry)
00022 {
00023   uint32_t hdata;
00024 
00025   *nentry = 0;
00026   if (v1190_DataReady((WORD *)pbase)) {
00027     do {
00028       hdata = *pbase;
00029     } while (!(hdata & 0x40000000));
00030     pdest[*nentry] = hdata;
00031     *nentry += 1;
00032     do {
00033       pdest[*nentry] = *pbase;
00034       *nentry += 1;
00035     } while (!(pdest[*nentry-1] & 0xc0000000)); 
00036 
00037     nentry--;
00038   }
00039   return *nentry;
00040 
00041 /*
00042   header = *pbase & 0xFF000000;
00043   
00044   switch (header) {
00045   case 0x40000000:  // Global Header 
00046     break;
00047   case 0x00000000:  // TDC Header
00048     break;
00049   case 0x10000000:  // Data
00050     break;
00051   case 0x20000000:  // Error
00052     break;
00053   case 0x80000000:  // Trailer
00054     break;
00055   case 0xc0000000:  // Filler
00056     break;
00057   }
00058   
00059   return *nentry;
00060 */
00061 }
00062 
00063 /*****************************************************************/
00064 int v1190_DataRead(DWORD *pbase, uint32_t *dest, int maxwords)
00065 {
00066   int i;
00067   for (i=0; i<maxwords; i++)
00068     {
00069       if (!v1190_DataReady((uint16_t *)pbase))
00070         break;
00071       *dest++ = *pbase;
00072     }
00073   return i;
00074 }
00075 
00076 /*****************************************************************/
00077 int v1190_GeoWrite(WORD *pbase, int geo)
00078 {
00079   pbase[V1190_GEO_REG_RW] = (geo & 0x1F);
00080   return (int) (pbase[V1190_GEO_REG_RW] & 0x1F); 
00081 }
00082 
00083 /*****************************************************************/
00084 void v1190_SoftReset(uint16_t *pbase)
00085 {
00086   pbase[V1190_MODULE_RESET_WO] = 0;
00087 }
00088 
00089 /*****************************************************************/
00090 void v1190_SoftClear(uint16_t *pbase)
00091 {
00092   pbase[V1190_SOFT_CLEAR_WO] = 0;
00093 }
00094 
00095 /*****************************************************************/
00096 void v1190_SoftTrigger(uint16_t *pbase)
00097 {
00098   pbase[V1190_SOFT_TRIGGER_WO] = 0;
00099 }
00100 
00101 /*****************************************************************/
00102 int  v1190_DataReady(uint16_t *pbase)
00103 {
00104   return (pbase[V1190_SR_RO] & V1190_DATA_READY);
00105 }
00106 
00107 /*****************************************************************/
00108 int  v1190_EvtStored(uint16_t *pbase)
00109 {
00110   return (pbase[V1190_EVT_STORED_RO]);
00111 }
00112 
00113 /*****************************************************************/
00114 int  v1190_EvtCounter(uint16_t *pbase)
00115 {
00116   return (pbase[V1190_EVT_CNT_RO]);
00117 }
00118 
00119 /*****************************************************************/
00120 int  v1190B_TdcIdList(uint16_t *pbase)
00121 {
00122   uint16_t  i, code;
00123   uint32_t value;
00124 
00125 //  v1190_MicroFlush(pbase);
00126   for (i=0; i<2 ; i++) {
00127     code   = 0x6000 | (i & 0x0F);
00128     value  = v1190_MicroWrite(pbase, code);
00129     value  = v1190_MicroRead(pbase);
00130     value = (v1190_MicroRead(pbase) << 16) | value;
00131     printf("Received :code: 0x%04x  0x%08x\n", code, value); 
00132   }
00133   return 1;
00134 }
00135 
00136 /*****************************************************************/
00137 int  v1190B_ResolutionRead(uint16_t *pbase)
00138 {
00139   uint16_t  i, code;
00140   uint32_t value;
00141 
00142 //  v1190_MicroFlush(pbase);
00143   for (i=0; i<2 ; i++) {
00144     code = V1190_RESOLUTION_RO | (i & 0x0F);
00145     value = v1190_MicroWrite(pbase, code);
00146     value = v1190_MicroRead(pbase);
00147 //    printf("Received :code: 0x%04x  0x%08x\n", code, value); 
00148   }
00149   return 1;
00150 }
00151 
00152 /*****************************************************************/
00153 int  v1190B_AcqModeRead(uint16_t *pbase)
00154 {
00155   uint16_t  code;
00156   uint32_t value;
00157 
00158 //  v1190_MicroFlush(pbase);
00159   code = V1190_ACQ_MODE_RO;
00160   value = v1190_MicroWrite(pbase, code);
00161   value = v1190_MicroRead(pbase);
00162 //  printf("Received :code: 0x%04x  0x%08x\n", code, value); 
00163   return 1;
00164 }
00165 
00166 /*****************************************************************/
00167 int  v1190B_TriggerMatchingSet(uint16_t *pbase)
00168 {
00169   uint16_t  code;
00170   uint32_t value;
00171 
00172 //  v1190_MicroFlush(pbase);
00173   code = V1190_TRIGGER_MATCH_WO;
00174   value = v1190_MicroWrite(pbase, code);
00175 //  printf("Received :code: 0x%04x  0x%08x\n", code, value); 
00176   
00177   return 1;
00178 }
00179 
00180 /*****************************************************************/
00181 int  v1190B_ContinuousSet(uint16_t *pbase)
00182 {
00183   uint16_t  code;
00184   uint32_t value;
00185 
00186 //  v1190_MicroFlush(pbase);
00187   code = V1190_CONTINUOUS_WO;
00188   value = v1190_MicroWrite(pbase, code);
00189 //  printf("Received :code: 0x%04x  0x%08x\n", code, value); 
00190   
00191   return 1;
00192 }
00193 
00194 /*****************************************************************/
00195 void v1190_SetEdgeDetection(uint16_t *pbase,int enableLeading,int enableTrailing)
00196 {
00197   int value = 0;
00198 
00199   if (enableLeading)
00200     value |= 2;
00201 
00202   if (enableTrailing)
00203     value |= 1;
00204 
00205   v1190_MicroFlush(pbase);
00206   v1190_MicroWrite(pbase, 0x2200);
00207   v1190_MicroWrite(pbase, value);
00208 }
00209 
00210 /*****************************************************************/
00211 int v1190_MicroWrite(uint16_t *pbase, uint16_t data)
00212 {
00213   int i;
00214   for (i=0; i<1000; i++)
00215   {
00216     uint16_t microHS = pbase[V1190_MICRO_HAND_RO];
00217     if (microHS & V1190_MICRO_WR_OK) {
00218       pbase[V1190_MICRO_RW] = data;
00219       return 0;
00220     }
00221     udelay(500);
00222   }
00223   
00224   printf("v1190_MicroWrite: Micro not ready for writing!\n");
00225   return -1;
00226 }
00227 
00228 /*****************************************************************/
00229 int v1190_MicroRead(const uint16_t *pbase)
00230 {
00231   int i;
00232   int reg=-1;
00233 
00234   for (i=100; i>0; i--) {
00235     uint16_t  microHS = pbase[V1190_MICRO_HAND_RO];
00236     if (microHS & V1190_MICRO_RD_OK) {
00237       reg = pbase[V1190_MICRO_RW];
00238 //      printf("i:%d microHS:%d %x\n", i, microHS, reg);
00239       return (reg);
00240     }
00241     udelay(500);
00242   };
00243   return -1;
00244 }
00245 
00246 /*****************************************************************/
00247 int v1190_MicroFlush(const uint16_t *pbase)
00248 {
00249   int count = 0;
00250   while (1)
00251     {
00252       int data = v1190_MicroRead(pbase);
00253       printf("microData[%d]: 0x%04x\n",count,data);
00254       if (data < 0)
00255         break;
00256       count++;
00257     }
00258   return count;
00259 }
00260 
00261 /*****************************************************************/
00262 int  v1190_Setup(uint16_t *pbase, int mode)
00263 {
00264   uint16_t value;
00265   int      status = -1;
00266   
00267 //  v1190_MicroFlush(pbase);
00268   switch (mode) {
00269   case 0x1:
00270     printf("Trigger Matching Setup (mode:%d)\n", mode);
00271     printf("Default setting + Width : 800ns, Offset : -400ns\n");
00272     printf("Time subtract, Leading Edge only\n");
00273     status = v1190_MicroWrite(pbase, 0x0000);
00274     if (status < 0)
00275       return status;
00276 #if 0
00277     value = v1190_MicroWrite(pbase, 0x1000); // width
00278     value = v1190_MicroWrite(pbase, 0x60);     // Width : 800ns
00279     value = v1190_MicroWrite(pbase, 0x1100); // offset
00280     value = v1190_MicroWrite(pbase, 0xfe8);  // offset: -400ns
00281     value = v1190_MicroWrite(pbase, 0x1500); // subtraction flag
00282     value = v1190_MicroWrite(pbase, 0x2100); // leading edge detection
00283 #endif
00284     break;
00285   case 0x2:
00286     value = v1190_MicroWrite(pbase, 0x0500); // default configuration
00287     break;
00288   default:
00289     printf("Unknown setup mode\n");
00290     return -1;
00291   }
00292   v1190_Status(pbase);
00293   return 0;
00294 }
00295 
00296 /*****************************************************************/
00297 int v1190_Status(uint16_t *pbase)
00298 {
00299   uint16_t  i, code, pair=0;
00300   uint32_t value;
00301   
00302   printf("\nAcquisition mode:\n");
00303   value = v1190_MicroWrite(pbase, 0x0200);
00304   value = v1190_MicroRead(pbase);
00305   printf("  mode: 0x%02x\n", value);  
00306 
00307   code = 0x1600;
00308   printf("\nTrigger Section [code:0x%04x]:\n", code);
00309   if ((value = v1190_MicroWrite(pbase, code)) < 0)
00310     return -value;
00311   value = v1190_MicroRead(pbase);
00312   printf("  Match Window width       : 0x%04x\n", value);  
00313   value = v1190_MicroRead(pbase);
00314   printf("  Window offset            : 0x%04x\n", value);  
00315   value = v1190_MicroRead(pbase);
00316   printf("  Extra Search Window Width: 0x%04x\n", value);  
00317   value = v1190_MicroRead(pbase);
00318   printf("  Reject Margin            : 0x%04x\n", value);  
00319   value = v1190_MicroRead(pbase);
00320   printf("  Trigger Time subtration  : %s\n",(value & 0x1) ? "y" : "n");  
00321 
00322   printf("\nEdge Detection & Resolution Section:\n");
00323   code = 0x2300;
00324   value = v1190_MicroWrite(pbase, code);
00325   pair = value = v1190_MicroRead(pbase);
00326   printf("  Edge Detection (1:T/2:L/3:TL)           : 0x%02x\n", (value&0x3));  
00327   code = 0x2600;
00328   value = v1190_MicroWrite(pbase, code);
00329   value = v1190_MicroRead(pbase);
00330   if (pair==0x3) {
00331     value = v1190_MicroRead(pbase);
00332     printf("  Leading Edge Resolution (see table)     : 0x%02x\n", (value&0x3));  
00333     printf("  Pulse Width Resolution (see table)      : 0x%02x\n", ((value>>8)&0xF));  
00334   } else {
00335     printf("  Resolution [ps] (0:800/1:200/2:100)     : 0x%02x\n", (value&0x3));  
00336   }
00337   code = 0x2900;
00338   value = v1190_MicroWrite(pbase, code);
00339   value = v1190_MicroRead(pbase);
00340   printf("  Dead Time between hit [~ns](5/10/30/100): 0x%02x\n", (value&0x3));  
00341   
00342   printf("\nReadout Section:\n");
00343   code = 0x3200;
00344   value = v1190_MicroWrite(pbase, code);
00345   value = v1190_MicroRead(pbase);
00346   printf("  Header/Trailer                            : %s\n",(value & 0x1) ? "y" : "n");  
00347   code = 0x3400;
00348   value = v1190_MicroWrite(pbase, code);
00349   value = v1190_MicroRead(pbase);
00350   printf("  Max #hits per event 2^n-1 (>128:no limit) : %d\n", value&0xF);  
00351   code = 0x3a00;
00352   value = v1190_MicroWrite(pbase, code);
00353   value = v1190_MicroRead(pbase);
00354   printf("  Internal TDC error type (see doc)         : 0x%04x\n", (value&0x7FF));  
00355   code = 0x3c00;
00356   value = v1190_MicroWrite(pbase, code);
00357   value = v1190_MicroRead(pbase);
00358   printf("  Effective size of readout Fifo 2^n-1      : 0x%04x\n", (value&0xF));  
00359   
00360   printf("\nChannel Enable Section:\n");
00361   code = 0x4500;
00362   value = v1190_MicroWrite(pbase, code);
00363   value = v1190_MicroRead(pbase);
00364   printf("  Read Enable Pattern [  0..15 ] : 0x%04x\n", value);  
00365   value = v1190_MicroRead(pbase);
00366   printf("  Read Enable Pattern [ 16..31 ] : 0x%04x\n", value);  
00367   value = v1190_MicroRead(pbase);
00368   printf("  Read Enable Pattern [ 32..47 ] : 0x%04x\n", value);  
00369   value = v1190_MicroRead(pbase);
00370   printf("  Read Enable Pattern [ 48..63 ] : 0x%04x\n", value);  
00371   code = 0x4700;
00372   value = v1190_MicroWrite(pbase, code);
00373   value = v1190_MicroRead(pbase);
00374   value = (v1190_MicroRead(pbase)<<16) | value;
00375   printf("  Read Enable Pattern 32 (0) : 0x%08x\n", value);  
00376   code = 0x4701;
00377   value = v1190_MicroWrite(pbase, code);
00378   value = v1190_MicroRead(pbase);
00379   value = (v1190_MicroRead(pbase)<<16) | value;
00380   printf("  Read Enable Pattern 32 (1) : 0x%08x\n", value);  
00381   printf("\nAdjust Section:\n");
00382   code = 0x5100;
00383   value = v1190_MicroWrite(pbase, code);
00384   value = v1190_MicroRead(pbase);
00385   printf("  Coarse Counter Offset: 0x%04x\n", (value&0x7FF));  
00386   value = v1190_MicroRead(pbase);
00387   printf("  Fine   Counter Offset: 0x%04x\n", (value&0x1F));  
00388   printf("\nMiscellaneous Section:\n");
00389   for (i=0; i<2 ; i++) {
00390     code = 0x6000 | (i & 0x0F);
00391     value = v1190_MicroWrite(pbase, code);
00392     value = v1190_MicroRead(pbase);
00393     value = (v1190_MicroRead(pbase) << 16) | value;
00394     printf("  TDC ID(%i)  0x%08x  [code:0x%04x]\n", i, value, code);  
00395   }
00396   return 0;
00397 }
00398 
00399 /*****************************************************************/
00400 #ifndef HAVE_UDELAY
00401 // this is the VMIC version of udelay()
00402 int udelay(int usec)
00403 {
00404   int i, j, k = 0;
00405   for (i=0; i<133; i++)
00406     for (j=0; j<usec; j++)
00407       k += (k+1)*j*k*(j+1);
00408   return k;
00409 }
00410 #endif
00411 
00412 

Midas DOC Version 3.0.0 ---- 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