Subversion Repositories f9daq

Rev

Rev 52 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <CAENVMElib.h>
  5. #include <CAENVMElib.h>
  6. #include <CAENVMEoslib.h>
  7. #include <CAENVMEtypes.h>
  8.  
  9.  
  10. int32_t udev;
  11.  
  12. int VMEerrors;
  13.  
  14. int VME_start (char* serial)
  15. {
  16.  
  17.   CVErrorCodes result = CAENVME_Init( cvV1718 , 0 , 0 , &udev);
  18.   if (result != cvSuccess) {
  19.     printf("CAEN1718 Init %s\n", CAENVME_DecodeError(result));
  20.     printf(" Check usb cable, usb udev permissions and restart!  Exiting .....\n");
  21.     exit(-1);
  22.   }
  23.   if (serial == NULL) serial = malloc(100*sizeof(char));
  24.   CAENVME_BoardFWRelease(udev, serial);
  25.  
  26.   return result;
  27. }
  28.  
  29.  
  30. int VME_read(long Handle, unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW)
  31. {
  32.  
  33.  CVErrorCodes result;
  34.  int ncount=0;
  35.  while (1){
  36.    result = CAENVME_ReadCycle(Handle, Address, Data, AM, DW );
  37.    if (result !=cvSuccess ) {
  38.      VMEerrors++;
  39.      printf("%d CAENVME_ReadCycle at 0x%0lX failed! data=0x%0X  AM= 0x%0X DW= 0x%0X  err=%s\n", ncount,Address, *((uint32_t *) Data),AM,DW, CAENVME_DecodeError(result));
  40.      ncount++;
  41.      if (ncount==10) exit(result);
  42.    } else break;
  43.  }
  44.  return result;
  45. }
  46.  
  47. int VME_BltRead(long Handle, unsigned long Address, void *Data, int size, CVAddressModifier AM, CVDataWidth DW)
  48. {
  49.   int count=0;
  50.   CVErrorCodes result;
  51.   int ncount=0;
  52.  
  53.   while (1){
  54.    
  55.     result = CAENVME_BLTReadCycle(Handle, Address, Data, size, AM, DW , &count);
  56.     if (result !=cvSuccess ) {
  57.        VMEerrors++;
  58.        printf("%d CAENVME_BLTReadCycle at 0x%0lX failed! data=0x%0X  AM= 0x%0X DW= 0x%0X  err=%s count=%d\n", ncount,Address, *((uint32_t *) Data),AM,DW, CAENVME_DecodeError(result),count);
  59.        ncount++;
  60.        if (ncount==10) exit(result);
  61.     } else break;
  62.   }  
  63.   return count;
  64. }
  65.  
  66. int VME_write(long Handle, unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW)
  67. {
  68.  
  69.  CVErrorCodes result;
  70.  int ncount=0;
  71.  while (1){
  72.  result = CAENVME_WriteCycle(Handle, Address, Data, AM, DW );
  73.  if (result !=cvSuccess ) {
  74.     VMEerrors++;
  75.     printf("CAENVME_WriteCycle at 0x%0lX failed! data=0x%0X  AM= 0x%0X DW= 0x%0X  err=%s\n", Address, *((uint32_t *) Data),AM,DW, CAENVME_DecodeError(result));
  76.     if (ncount==10)  exit(result);
  77.  } else break;
  78.  }
  79.  return result;
  80. }
  81.  
  82.  
  83.  
  84.  
  85. uint32_t *WStackAddrs = NULL;
  86. uint32_t *WStackBuffer;  
  87. int WStackNCycles;
  88. int WStackMaxCycles=100000;
  89. CVAddressModifier *WStackAMs;
  90. CVDataWidth *WStackDWs;
  91. CVErrorCodes *WStackECs;
  92.  
  93.  
  94. uint32_t *RStackAddrs =NULL;
  95. int RStackNCycles;
  96. int RStackMaxCycles=100000;
  97. CVAddressModifier *RStackAMs;
  98. CVDataWidth *RStackDWs;
  99. CVErrorCodes *RStackECs;
  100.  
  101.  
  102.  
  103. int VME_appendread(unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW){
  104.   if (RStackNCycles<RStackMaxCycles){
  105.     RStackAddrs[RStackNCycles]  = Address;
  106.     RStackAMs[RStackNCycles]    = AM;
  107.     RStackDWs[RStackNCycles]    = DW;
  108.     //int i=RStackNCycles;
  109.     //printf("AppendRead %d ADDR= 0x%0X AM= 0x%0X DW= 0x%0X \n", i,RStackAddrs[i],RStackAMs[i], RStackDWs[i]);
  110.     RStackNCycles++;
  111.   } else {
  112.     printf("Increase DataBuffer RStackMaxCycles =%d\n", RStackMaxCycles);
  113.   }
  114.   return RStackNCycles;
  115. }
  116.  
  117. int VME_appendwrite(unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW){
  118.   if (WStackNCycles<WStackMaxCycles){
  119.     WStackAddrs[WStackNCycles]  = Address;
  120.     WStackBuffer[WStackNCycles] = *((uint32_t *) Data);
  121.     WStackAMs[WStackNCycles]    = AM;
  122.     WStackDWs[WStackNCycles]    = DW;
  123.     WStackNCycles++;
  124.   } else {
  125.     printf("Increase DataBuffer WStackMaxCycles =%d\n", WStackMaxCycles);
  126.   }
  127.   return 0;
  128. }
  129.  
  130. int VME_MultiReadReset(){
  131.   RStackNCycles=0;
  132.   if (RStackAddrs!=NULL) return 0;
  133.   RStackAddrs = (uint32_t *) malloc( RStackMaxCycles*sizeof(uint32_t));
  134.  
  135.   RStackAMs  = (CVAddressModifier *) malloc(RStackMaxCycles*sizeof(CVAddressModifier));
  136.   RStackDWs = (CVDataWidth *) malloc(RStackMaxCycles*sizeof(CVDataWidth));
  137.   RStackECs = (CVErrorCodes *) malloc(RStackMaxCycles*sizeof(CVErrorCodes));
  138.  
  139.   return 0;
  140. }
  141.  
  142. int VME_MultiWriteReset(){
  143.   WStackNCycles=0;
  144.   if (WStackAddrs!=NULL) return 0;
  145.   WStackBuffer = (uint32_t *) malloc( WStackMaxCycles*sizeof(uint32_t));  
  146.   WStackAddrs = (uint32_t *) malloc( WStackMaxCycles*sizeof(uint32_t));  
  147.   WStackAMs  = (CVAddressModifier *) malloc(WStackMaxCycles*sizeof(CVAddressModifier));
  148.   WStackDWs = (CVDataWidth *) malloc(WStackMaxCycles*sizeof(CVDataWidth));
  149.   WStackECs = (CVErrorCodes *) malloc(WStackMaxCycles*sizeof(CVErrorCodes));
  150.  
  151.   return 0;
  152. }
  153.  
  154. int VME_MultiReadExecute(long Handle, uint32_t *  Data){
  155.   //printf("len=%d\n",RStackNCycles);
  156.   int i=0;
  157.   CVErrorCodes result =CAENVME_MultiRead(Handle, RStackAddrs,  Data,  RStackNCycles, RStackAMs, RStackDWs, RStackECs);
  158.  if (result !=cvSuccess ) {
  159.    VMEerrors++;
  160.    printf("CAENVME_MultiRead at 0x%0X failed!  err=%s RStackNCycles=%d\n", RStackAddrs[0], CAENVME_DecodeError(result), RStackNCycles);
  161.    for (i=0;i<RStackNCycles;i++){
  162.      if (RStackECs[i]!=cvSuccess ) printf("%d ADDR= 0x%0X AM= 0x%0X DW= 0x%0X  failed!  err=%s\n", i,RStackAddrs[i],RStackAMs[i], RStackDWs[i], CAENVME_DecodeError(RStackECs[i]) );
  163.    }
  164.    exit(result);
  165.  }
  166.  return  RStackNCycles;
  167. }
  168.  
  169. int VME_MultiWriteExecute(long Handle){
  170.  int i=0;
  171.  CVErrorCodes result =CAENVME_MultiWrite(Handle, WStackAddrs, WStackBuffer,  WStackNCycles, WStackAMs, WStackDWs, WStackECs);
  172.  if (result !=cvSuccess ) {
  173.    printf("CAENVME_MultiWrite at 0x%0X failed!  err=%s WStackNCycles=%d\n", WStackAddrs[0], CAENVME_DecodeError(result), WStackNCycles);
  174.                
  175.    for (i=0;i<WStackNCycles;i++){
  176.      if (WStackECs[i]!=cvSuccess )
  177.      printf("%d ADDR= 0x%0X AM= 0x%0X DW= 0x%0X  data= 0x%0X  failed!  err=%s\n", i,WStackAddrs[i],WStackAMs[i], WStackDWs[i],  WStackBuffer[i], CAENVME_DecodeError(WStackECs[i]) );
  178.    }
  179.    exit(result);
  180. }
  181.  return  WStackNCycles;
  182. }
  183.  
  184.  
  185.  
  186.  
  187.  
  188.