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