Subversion Repositories f9daq

Rev

Rev 97 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5.  
  6. #include <CAENVMElib.h>
  7. #include <CAENVMEoslib.h>
  8. #include <CAENVMEtypes.h>
  9.  
  10.  
  11. #include "CAENV1718.h"
  12.  
  13. int32_t CAEN_udev;
  14.  
  15. int VMEerrors;
  16.  
  17. int CAEN_VME_start (char* serial)
  18. {
  19.  
  20.   CVErrorCodes result = CAENVME_Init( cvV1718 , 0 , 0 , &CAEN_udev);
  21.   if (result != cvSuccess) {
  22.     printf("CAEN1718 Init %s\n", CAENVME_DecodeError(result));
  23.     printf(" Check usb cable, usb udev permissions and restart!  Exiting .....\n");
  24.     return (-1);
  25.   }
  26.   if (serial == NULL) serial = malloc(100*sizeof(char));
  27.   CAENVME_BoardFWRelease(CAEN_udev, serial);
  28.  
  29.   return result;
  30. }
  31.  
  32.  
  33. short CAEN_VME_read(long Handle, unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW)
  34. {
  35.  
  36.  CVErrorCodes result;
  37.  int ncount=0;
  38.  while (1){
  39.    result = CAENVME_ReadCycle(Handle, Address, Data, AM, DW );
  40.    if (result !=cvSuccess ) {
  41.      VMEerrors++;
  42.      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));
  43.      ncount++;
  44.      if (ncount==10) return (result);
  45.    } else break;
  46.  }
  47.  return result;
  48. }
  49.  
  50. int CAEN_VME_BltRead(long Handle, unsigned long Address, void *Data, int size, CVAddressModifier AM, CVDataWidth DW)
  51. {
  52.   int count=0;
  53.   CVErrorCodes result;
  54.   int ncount=0;
  55.  
  56.   while (1){
  57.    
  58.     result = CAENVME_BLTReadCycle(Handle, Address, Data, size, AM, DW , &count);
  59.     if (result !=cvSuccess ) {
  60.        VMEerrors++;
  61.        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);
  62.        ncount++;
  63.        if (ncount==10) return (result);
  64.     } else break;
  65.   }  
  66.   return count;
  67. }
  68.  
  69. short CAEN_VME_write(long Handle, unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW)
  70. {
  71.  
  72.  CVErrorCodes result;
  73.  int ncount=0;
  74.  while (1){
  75.  result = CAENVME_WriteCycle(Handle, Address, Data, AM, DW );
  76.  if (result !=cvSuccess ) {
  77.     VMEerrors++;
  78.     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));
  79.     if (ncount==10)  return (result);
  80.  } else break;
  81.  }
  82.  return result;
  83. }
  84.  
  85.  
  86.  
  87.  
  88. uint32_t *WStackAddrs = NULL;
  89. uint32_t *WStackBuffer;  
  90. short WStackNCycles;
  91. short WStackMaxCycles=0xFFFF;
  92. CVAddressModifier *WStackAMs;
  93. CVDataWidth *WStackDWs;
  94. CVErrorCodes *WStackECs;
  95.  
  96.  
  97. uint32_t *RStackAddrs =NULL;
  98. short RStackNCycles;
  99. short RStackMaxCycles=0xFFFF;
  100. CVAddressModifier *RStackAMs;
  101. CVDataWidth *RStackDWs;
  102. CVErrorCodes *RStackECs;
  103.  
  104.  
  105.  
  106. short CAEN_VME_appendread(unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW){
  107.   if (RStackNCycles<RStackMaxCycles){
  108.     RStackAddrs[RStackNCycles]  = Address;
  109.     RStackAMs[RStackNCycles]    = AM;
  110.     RStackDWs[RStackNCycles]    = DW;
  111.     //int i=RStackNCycles;
  112.     //printf("AppendRead %d ADDR= 0x%0X AM= 0x%0X DW= 0x%0X \n", i,RStackAddrs[i],RStackAMs[i], RStackDWs[i]);
  113.     RStackNCycles++;
  114.   } else {
  115.     printf("Increase DataBuffer RStackMaxCycles =%d\n", RStackMaxCycles);
  116.   }
  117.   return RStackNCycles;
  118. }
  119.  
  120. short CAEN_VME_appendwrite(unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW){
  121.   if (WStackNCycles<WStackMaxCycles){
  122.     WStackAddrs[WStackNCycles]  = Address;
  123.     WStackBuffer[WStackNCycles] = *((uint32_t *) Data);
  124.     WStackAMs[WStackNCycles]    = AM;
  125.     WStackDWs[WStackNCycles]    = DW;
  126.     WStackNCycles++;
  127.   } else {
  128.     printf("Increase DataBuffer WStackMaxCycles =%d\n", WStackMaxCycles);
  129.   }
  130.   return 0;
  131. }
  132.  
  133. short CAEN_VME_MultiReadReset(){
  134.   RStackNCycles=0;
  135.   if (RStackAddrs!=NULL) return 0;
  136.   RStackAddrs = (uint32_t *) malloc( RStackMaxCycles*sizeof(uint32_t));
  137.  
  138.   RStackAMs  = (CVAddressModifier *) malloc(RStackMaxCycles*sizeof(CVAddressModifier));
  139.   RStackDWs = (CVDataWidth *) malloc(RStackMaxCycles*sizeof(CVDataWidth));
  140.   RStackECs = (CVErrorCodes *) malloc(RStackMaxCycles*sizeof(CVErrorCodes));
  141.  
  142.   return 0;
  143. }
  144.  
  145. short CAEN_VME_MultiWriteReset(){
  146.   WStackNCycles=0;
  147.   if (WStackAddrs!=NULL) return 0;
  148.   WStackBuffer = (uint32_t *) malloc( WStackMaxCycles*sizeof(uint32_t));  
  149.   WStackAddrs = (uint32_t *) malloc( WStackMaxCycles*sizeof(uint32_t));  
  150.   WStackAMs  = (CVAddressModifier *) malloc(WStackMaxCycles*sizeof(CVAddressModifier));
  151.   WStackDWs = (CVDataWidth *) malloc(WStackMaxCycles*sizeof(CVDataWidth));
  152.   WStackECs = (CVErrorCodes *) malloc(WStackMaxCycles*sizeof(CVErrorCodes));
  153.  
  154.   return 0;
  155. }
  156.  
  157. short CAEN_VME_MultiReadExecute(long Handle, uint32_t *  Data){
  158.   //printf("len=%d\n",RStackNCycles);
  159.   int i=0;
  160.   CVErrorCodes result =CAENVME_MultiRead(Handle, RStackAddrs,  Data,  RStackNCycles, RStackAMs, RStackDWs, RStackECs);
  161.  if (result !=cvSuccess ) {
  162.    VMEerrors++;
  163.    printf("CAENVME_MultiRead at 0x%0X failed!  err=%s RStackNCycles=%d\n", RStackAddrs[0], CAENVME_DecodeError(result), RStackNCycles);
  164.    for (i=0;i<RStackNCycles;i++){
  165.      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]) );
  166.    }
  167.    exit(result);
  168.  }
  169.  return  RStackNCycles;
  170. }
  171.  
  172. short CAEN_VME_MultiWriteExecute(long Handle){
  173.  int i=0;
  174.  CVErrorCodes result =CAENVME_MultiWrite(Handle, WStackAddrs, WStackBuffer,  WStackNCycles, WStackAMs, WStackDWs, WStackECs);
  175.  if (result !=cvSuccess ) {
  176.    printf("CAENVME_MultiWrite at 0x%0X failed!  err=%s WStackNCycles=%d\n", WStackAddrs[0], CAENVME_DecodeError(result), WStackNCycles);
  177.                
  178.    for (i=0;i<WStackNCycles;i++){
  179.      if (WStackECs[i]!=cvSuccess )
  180.      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]) );
  181.    }
  182.    exit(result);
  183. }
  184.  return  WStackNCycles;
  185. }
  186.  
  187.  
  188. short __stdcall CAEN_VME_R( uint16_t AddressModifier, uint16_t DataWidth,  uint32_t VME_Address, uint32_t *Data){
  189.          return CAEN_VME_read( CAEN_udev,   VME_Address, (void *)Data,  AddressModifier, DataWidth);
  190. }
  191.  
  192. short __stdcall CAEN_VME_W( uint16_t AddressModifier, uint16_t DataWidth,  uint32_t VME_Address, uint32_t Data){
  193.          return CAEN_VME_write( CAEN_udev,   VME_Address, (void *)Data,  AddressModifier, DataWidth);
  194. }
  195.  
  196. short __stdcall CAEN_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data){
  197.  
  198.   return CAEN_VME_appendwrite(  VME_Address, &Data ,AM , DW) ;
  199.  
  200. }
  201. short __stdcall CAEN_VME_MWRST( void ){
  202.  
  203.   return CAEN_VME_MultiWriteReset();
  204.  
  205. }
  206. short __stdcall CAEN_VME_MWEXEC( void ){
  207.  
  208.   return CAEN_VME_MultiWriteExecute(CAEN_udev);
  209.  
  210. }
  211.  
  212. short __stdcall CAEN_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data){
  213.  
  214.   return   CAEN_VME_appendread(  VME_Address, Data ,AM , DW) ;
  215.  
  216. }
  217. short __stdcall CAEN_VME_MRRST( void ){
  218.  
  219.   return CAEN_VME_MultiReadReset();
  220.  
  221. }
  222. short __stdcall CAEN_VME_MREXEC(  uint32_t *Data  ){
  223.  
  224.   return 0;
  225.  
  226. }
  227.  
  228.  
  229.  
  230.